Cycle 3 (MC interpolation) closure: M1'''=100%, R'''=0.067 RED, M4=-19.5%

Third daedalus-fourier kernel — VP9 8-tap regular subpel filter,
horizontal direction, 8-wide output. Multiply-heavy by design to
stress V3D's no-DP4A deficit. Full cycle Phase 1-7 + M4'''.

Phase 5''' second-model review delivered cleanly — caught 1 RED
bug pre-implementation (src_off off-by-3 indexing convention) and
2 YELLOW gaps (assert MUST language, shaderdb filter-LUT gate).
Without the review, M1''' would have failed silently on first run
with cryptic "high-index source pixels wrong" symptoms.

Phase 6 v1 first-light: M1''' 100.0000% bit-exact (65536/65536
blocks across all 16 mx phases). Phase 5''' filter-LUT prediction
materialised exactly: 197 uniforms (gate was 144), 2 threads (down
from cycle-2's 4 due to register pressure).

Performance:

  M2''' = 1.413 Mblock/s     (707.9 ns/block)
  M3''' = 20.997 Mblock/s    (NEON baseline phase3)
  R'''  = 0.067              (RED band — structural mismatch)
  shaderdb: 488 inst, 2 threads, 197 uniforms, 25 max-temps, 0 spills

M4''' concurrent matrix (8s windows):

  NEON 1-core           14.479 Mblock/s
  NEON 4-core           15.248 Mblock/s   <- baseline (compute-bound,
                                              not bandwidth-saturated
                                              like cycles 1+2!)
  QPU only               1.380 Mblock/s
  MIXED NEON-3 + QPU    12.277 Mblock/s   <- -19.5% (FAIL gate)
  MIXED NEON-4 + QPU    12.158 Mblock/s   <- -20.3%

NEW cross-cycle finding (Phase 9 lesson 2): compute-bound CPU
workloads make the QPU-offload story collapse. Cycles 1+2 were
bandwidth-saturated (4-core scaling 0.56-0.82x of 1-core), so
freeing a CPU core via QPU offload added throughput. Cycle 3 MC
is compute-bound (4-core scaling 1.05x of 1-core — near-linear),
no free cycles to free. QPU contribution (0.45 Mblock/s in
contention) doesn't compensate for losing 1 NEON core delivering
~3.8 Mblock/s.

But 30fps@1080p floor: PASS in every config (1.4x to 15.7x
isolation margin). Per project_30fps_floor_is_fine.md, user-facing
test never fails — daily YouTube playback works fine on any CPU/QPU
split.

DEPLOYMENT RECIPE for higgs (cycle 3 confirmed split):

  IDCT (k1)  -> QPU   (R=0.92, +7% mixed, frees CPU core)
  LPF  (k2)  -> QPU   (R=0.41, +7% mixed, frees CPU core)
  MC   (k3)  -> CPU   (R=0.067, -19.5% mixed — stays on CPU)
  Entropy    -> CPU   (structurally serial)

Mixed-substrate deployment, not "QPU does everything". Realistic for
higgs: entropy + MC on 2-3 ARM cores; IDCT + LPF dispatched to QPU
concurrently; 1-2 ARM cores left for vscode etc.

New artifacts:
- src/v3d_mc_8h.comp               — GLSL kernel
- tests/vp9_mc_ref.c               — standalone C ref (REGULAR filter
                                     embedded; clean transcription)
- tests/bench_neon_mc.c            — M1'''_c + M3''' bench
- tests/bench_v3d_mc.c             — M1''' + M2''' bench with contract
                                     asserts + 30fps margin display
- tests/bench_concurrent_mc.c      — M4''' pthread bench
- external/ffmpeg-snapshot/libavcodec/aarch64/vp9mc_neon.S    (vendored)
- external/ffmpeg-snapshot/libavcodec/vp9_subpel_filters_table.c
                                     (hand-extracted; provides
                                      ff_vp9_subpel_filters symbol
                                      without dragging in full vp9dsp.c)
- docs/k3_mc_phase{1,2,3,4,5,7}.md — full cycle documentation

Memory updates: project_30fps_floor_is_fine.md (user's 30fps target
recalibration), MEMORY.md index updated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-18 12:51:43 +00:00
parent 36eca40ff2
commit 356e446a49
15 changed files with 2545 additions and 1 deletions
+286
View File
@@ -0,0 +1,286 @@
/*
* Cycle 3 M4''' — concurrent CPU(NEON MC) + QPU(V3D MC) throughput.
* Same pthread/barrier pattern as bench_concurrent{,_lpf}.c.
* License: BSD-2-Clause.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include <time.h>
#include <getopt.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
#include <vulkan/vulkan.h>
#include "v3d_runner.h"
extern void ff_vp9_put_regular8_h_neon(
uint8_t *dst, ptrdiff_t dst_stride,
const uint8_t *src, ptrdiff_t src_stride,
int h, int mx, int my);
#define SRC_W 16
#define DST_W 8
#define SRC_H 8
#define DST_H 8
#define SRC_BYTES (SRC_H * SRC_W)
#define DST_BYTES (DST_H * DST_W)
static inline uint64_t xs_step(uint64_t *s) {
uint64_t x = *s; x ^= x << 13; x ^= x >> 7; x ^= x << 17; return *s = x;
}
static uint64_t xs_init(uint64_t s) { return s ? s : 0xa57edbeef5717ULL; }
static double now_s(void) {
struct timespec t; clock_gettime(CLOCK_MONOTONIC_RAW, &t);
return t.tv_sec + t.tv_nsec * 1e-9;
}
static volatile int g_stop = 0;
static pthread_barrier_t g_start;
/* --- NEON worker ----------- */
#define NEON_BATCH 8192
typedef struct {
int worker_id, affinity_core;
uint64_t blocks_done;
double elapsed_s;
} neon_args;
static void *neon_worker(void *p)
{
neon_args *a = p;
cpu_set_t cs; CPU_ZERO(&cs); CPU_SET(a->affinity_core, &cs);
pthread_setaffinity_np(pthread_self(), sizeof(cs), &cs);
uint64_t s = xs_init((uint64_t) a->worker_id * 0xc01dbeefULL);
uint8_t *master = malloc((size_t) NEON_BATCH * SRC_BYTES);
uint8_t *work = malloc((size_t) NEON_BATCH * SRC_BYTES);
uint8_t *dsts = malloc((size_t) NEON_BATCH * DST_BYTES);
int *mxs = malloc(NEON_BATCH * sizeof(int));
for (int i = 0; i < NEON_BATCH; i++) {
for (int j = 0; j < SRC_BYTES; j++)
master[(size_t)i * SRC_BYTES + j] = (uint8_t)(xs_step(&s) & 0xff);
mxs[i] = (int)(xs_step(&s) & 15);
}
pthread_barrier_wait(&g_start);
double t0 = now_s();
uint64_t done = 0;
while (!g_stop) {
memcpy(work, master, (size_t) NEON_BATCH * SRC_BYTES);
for (int i = 0; i < NEON_BATCH; i++)
ff_vp9_put_regular8_h_neon(
dsts + (size_t)i * DST_BYTES, DST_W,
work + (size_t)i * SRC_BYTES + 3, SRC_W,
DST_H, mxs[i], 0);
done += NEON_BATCH;
}
a->elapsed_s = now_s() - t0;
a->blocks_done = done;
free(master); free(work); free(dsts); free(mxs);
return NULL;
}
/* --- QPU worker ----------- */
typedef struct {
int affinity_core, n_blocks;
uint64_t blocks_done;
double elapsed_s;
} qpu_args;
typedef struct {
uint32_t n_blocks, dst_stride_u8, src_stride_u8, _pad;
} push_consts;
static void *qpu_worker(void *p)
{
qpu_args *a = p;
cpu_set_t cs; CPU_ZERO(&cs); CPU_SET(a->affinity_core, &cs);
pthread_setaffinity_np(pthread_self(), sizeof(cs), &cs);
v3d_runner *r = v3d_runner_create();
if (!r) return NULL;
int n_blocks = a->n_blocks;
size_t meta_bytes = (size_t) n_blocks * 4 * sizeof(uint32_t);
size_t src_bytes = (size_t) n_blocks * SRC_BYTES;
size_t dst_bytes = (size_t) n_blocks * DST_BYTES;
v3d_buffer buf_meta = {0}, buf_dst = {0}, buf_src = {0};
v3d_runner_create_buffer(r, meta_bytes, &buf_meta);
v3d_runner_create_buffer(r, dst_bytes, &buf_dst);
v3d_runner_create_buffer(r, src_bytes, &buf_src);
uint64_t s = 0xfeedfacecafebabeULL;
uint8_t *master = malloc(src_bytes);
for (size_t i = 0; i < src_bytes; i++) master[i] = (uint8_t)(xs_step(&s) & 0xff);
memcpy(buf_src.mapped, master, src_bytes);
uint32_t *meta = buf_meta.mapped;
assert(DST_W >= 8); assert(SRC_W >= 15);
for (int i = 0; i < n_blocks; i++) {
meta[4*i + 0] = (uint32_t)((size_t)i * DST_BYTES); /* dst_off */
meta[4*i + 1] = (uint32_t)((size_t)i * SRC_BYTES); /* src_off (RAW, no +3) */
meta[4*i + 2] = (uint32_t)(xs_step(&s) & 15); /* mx */
meta[4*i + 3] = 0;
}
v3d_pipeline pipe = {0};
v3d_runner_create_pipeline(r, "v3d_mc_8h.spv", 3, sizeof(push_consts), &pipe);
v3d_buffer bufs[3] = { buf_meta, buf_dst, buf_src };
v3d_runner_bind_buffers(r, &pipe, bufs, 3);
const uint32_t bpw = 32;
uint32_t gc = (uint32_t)((n_blocks + bpw - 1) / bpw);
push_consts pc = { .n_blocks = (uint32_t) n_blocks,
.dst_stride_u8 = DST_W,
.src_stride_u8 = SRC_W };
VkCommandBuffer cb = v3d_runner_alloc_cmdbuf(r);
VkCommandBufferBeginInfo cbbi = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
vkBeginCommandBuffer(cb, &cbbi);
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipe.pipeline);
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
pipe.layout, 0, 1, &pipe.desc_set, 0, NULL);
vkCmdPushConstants(cb, pipe.layout, VK_SHADER_STAGE_COMPUTE_BIT,
0, sizeof(pc), &pc);
vkCmdDispatch(cb, gc, 1, 1);
vkEndCommandBuffer(cb);
for (int i = 0; i < 5; i++) v3d_runner_submit_wait(r, cb);
pthread_barrier_wait(&g_start);
double t0 = now_s();
uint64_t done = 0;
while (!g_stop) {
memset(buf_dst.mapped, 0, dst_bytes);
v3d_runner_submit_wait(r, cb);
done += n_blocks;
}
a->elapsed_s = now_s() - t0;
a->blocks_done = done;
free(master);
v3d_runner_destroy_pipeline(r, &pipe);
v3d_runner_destroy_buffer(r, &buf_src);
v3d_runner_destroy_buffer(r, &buf_dst);
v3d_runner_destroy_buffer(r, &buf_meta);
v3d_runner_destroy(r);
return NULL;
}
typedef struct { double duration_s; } timer_args;
static void *timer_thread(void *p) {
timer_args *a = p;
pthread_barrier_wait(&g_start);
double end = now_s() + a->duration_s;
while (now_s() < end) {
struct timespec ts = {0, 1000000}; nanosleep(&ts, NULL);
}
g_stop = 1;
return NULL;
}
enum mode { MODE_NEON, MODE_QPU, MODE_MIXED };
int main(int argc, char **argv)
{
enum mode mode = MODE_NEON;
int n_neon = 4, qpu_core = 3, qpu_n_blocks = 65536;
double duration = 8.0;
static struct option opts[] = {
{"mode", required_argument, 0, 'm'},
{"neon-threads", required_argument, 0, 'n'},
{"qpu-core", required_argument, 0, 'c'},
{"qpu-blocks", required_argument, 0, 'b'},
{"duration", required_argument, 0, 'd'},
{0,0,0,0}
};
for (int c; (c = getopt_long(argc, argv, "m:n:c:b:d:", opts, 0)) != -1;) {
switch (c) {
case 'm':
if (!strcmp(optarg, "neon-only")) mode = MODE_NEON;
else if (!strcmp(optarg, "qpu-only")) mode = MODE_QPU;
else if (!strcmp(optarg, "mixed")) mode = MODE_MIXED;
else { fprintf(stderr, "bad mode\n"); return 2; }
break;
case 'n': n_neon = atoi(optarg); break;
case 'c': qpu_core = atoi(optarg); break;
case 'b': qpu_n_blocks = atoi(optarg); break;
case 'd': duration = atof(optarg); break;
default: return 2;
}
}
int has_qpu = (mode == MODE_QPU || mode == MODE_MIXED);
int has_neon = (mode == MODE_NEON || mode == MODE_MIXED);
int n_workers = (has_neon ? n_neon : 0) + (has_qpu ? 1 : 0);
int barrier_count = n_workers + 1 + 1;
printf("=== M4''' concurrent MC bench ===\n");
printf(" mode: %s, neon: %d, qpu: core %d / %d blocks, %.1fs\n",
mode == MODE_NEON ? "neon-only" : mode == MODE_QPU ? "qpu-only" : "mixed",
has_neon ? n_neon : 0,
has_qpu ? qpu_core : -1,
has_qpu ? qpu_n_blocks : 0,
duration);
pthread_barrier_init(&g_start, NULL, barrier_count);
pthread_t timer_tid; timer_args ta = { .duration_s = duration };
pthread_create(&timer_tid, NULL, timer_thread, &ta);
pthread_t neon_tids[16] = {0};
neon_args n_args[16] = {0};
if (has_neon) {
for (int i = 0; i < n_neon; i++) {
n_args[i] = (neon_args){ .worker_id = i, .affinity_core = i };
pthread_create(&neon_tids[i], NULL, neon_worker, &n_args[i]);
}
}
pthread_t qpu_tid = 0;
qpu_args q_args = {0};
if (has_qpu) {
q_args = (qpu_args){ .affinity_core = qpu_core, .n_blocks = qpu_n_blocks };
pthread_create(&qpu_tid, NULL, qpu_worker, &q_args);
}
pthread_barrier_wait(&g_start);
pthread_join(timer_tid, NULL);
if (has_neon) for (int i = 0; i < n_neon; i++) pthread_join(neon_tids[i], NULL);
if (has_qpu) pthread_join(qpu_tid, NULL);
uint64_t total = 0; double max_e = 0;
if (has_neon) {
printf("NEON per-thread:\n");
for (int i = 0; i < n_neon; i++) {
double mbs = n_args[i].blocks_done / n_args[i].elapsed_s / 1e6;
printf(" core %d: %.3f Mblock/s\n", n_args[i].affinity_core, mbs);
total += n_args[i].blocks_done;
if (n_args[i].elapsed_s > max_e) max_e = n_args[i].elapsed_s;
}
}
if (has_qpu) {
double mbs = q_args.blocks_done / q_args.elapsed_s / 1e6;
printf("QPU (core %d): %.3f Mblock/s\n", q_args.affinity_core, mbs);
total += q_args.blocks_done;
if (q_args.elapsed_s > max_e) max_e = q_args.elapsed_s;
}
double total_mbs = total / max_e / 1e6;
printf("\n=== AGGREGATE ===\n");
printf(" Mblock/s : %.3f\n", total_mbs);
printf(" 30fps@1080p floor: 0.972 Mblock/s — %.1fx margin\n",
total_mbs / 0.972);
pthread_barrier_destroy(&g_start);
return 0;
}
+220
View File
@@ -0,0 +1,220 @@
/*
* Cycle 3 Phase 3 — NEON M3''' baseline for VP9 8-tap regular
* horizontal MC interpolation, 8×8 block.
*
* Reports:
* M1'''_c (correctness): C-ref ↔ NEON bit-exact rate, N random
* 8×8 blocks with random source pixels and
* random subpel phase mx ∈ [0, 15]
* M3''' (throughput): NEON sustained Mblock/s, single-thread,
* time-based
*
* License: LGPL-2.1+ (statically links FFmpeg NEON snapshot).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
extern void daedalus_vp9_put_regular_8h_ref(
uint8_t *dst, ptrdiff_t dst_stride,
const uint8_t *src, ptrdiff_t src_stride,
int h, int mx, int my);
extern void ff_vp9_put_regular8_h_neon(
uint8_t *dst, ptrdiff_t dst_stride,
const uint8_t *src, ptrdiff_t src_stride,
int h, int mx, int my);
/* RNG ------------------------------------------------------------ */
static uint64_t xs_state;
static inline uint64_t xs(void) {
uint64_t x = xs_state;
x ^= x << 13; x ^= x >> 7; x ^= x << 17;
return xs_state = x;
}
/* Block layout: each block gets its own 8×16 source buffer + 8×8 dst.
* - source buffer is 16 cols wide; the filter is called with
* src = block_src + 3, so it reads cols [src+0-3..src+8+4] =
* [0..14] of the 16-col buffer. col 15 is unused padding.
* - dst is 8 cols × 8 rows.
*/
#define SRC_W 16
#define SRC_H 8
#define DST_W 8
#define DST_H 8
#define SRC_BYTES (SRC_H * SRC_W) /* 128 */
#define DST_BYTES (DST_H * DST_W) /* 64 */
static void gen_src(uint8_t *buf)
{
for (int i = 0; i < SRC_BYTES; i++)
buf[i] = (uint8_t)(xs() & 0xff);
}
static double now_seconds(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
/* M1'''_c correctness gate -------------------------------------- */
static int correctness_check(uint64_t seed, int n_blocks)
{
xs_state = seed ? seed : 0xabcdef1234567890ULL;
int mismatches = 0;
uint8_t src[SRC_BYTES];
uint8_t dst_a[DST_BYTES], dst_b[DST_BYTES];
int mx_hist[16] = {0};
for (int i = 0; i < n_blocks; i++) {
gen_src(src);
int mx = (int)(xs() & 15);
mx_hist[mx]++;
memset(dst_a, 0, DST_BYTES);
memset(dst_b, 0, DST_BYTES);
daedalus_vp9_put_regular_8h_ref(dst_a, DST_W, src + 3, SRC_W, DST_H, mx, 0);
ff_vp9_put_regular8_h_neon (dst_b, DST_W, src + 3, SRC_W, DST_H, mx, 0);
if (memcmp(dst_a, dst_b, DST_BYTES) != 0) {
if (mismatches < 3) {
fprintf(stderr, "MISMATCH block %d mx=%d:\n", i, mx);
fprintf(stderr, " ref:");
for (int r = 0; r < 8; r++) {
fprintf(stderr, "\n r%d ", r);
for (int c = 0; c < 8; c++) fprintf(stderr, "%3u ", dst_a[r*8+c]);
}
fprintf(stderr, "\n neon:");
for (int r = 0; r < 8; r++) {
fprintf(stderr, "\n r%d ", r);
for (int c = 0; c < 8; c++) fprintf(stderr, "%3u ", dst_b[r*8+c]);
}
fprintf(stderr, "\n");
}
mismatches++;
}
}
printf("M1'''_c correctness: %d / %d blocks bit-exact (%.4f%%)\n",
n_blocks - mismatches, n_blocks,
100.0 * (n_blocks - mismatches) / n_blocks);
/* mx histogram — confirms all 16 phases get exercised. */
int min_mx = mx_hist[0], max_mx = mx_hist[0];
for (int i = 1; i < 16; i++) {
if (mx_hist[i] < min_mx) min_mx = mx_hist[i];
if (mx_hist[i] > max_mx) max_mx = mx_hist[i];
}
printf(" mx phase coverage: min=%d max=%d (16 phases sampled)\n",
min_mx, max_mx);
return mismatches;
}
/* M3''' throughput ---------------------------------------------- */
static void throughput_neon(uint64_t seed, int n_blocks, double duration_s)
{
xs_state = seed ? seed : 0xdeadbeef12345678ULL;
uint8_t *master_src = malloc((size_t) n_blocks * SRC_BYTES);
uint8_t *work_src = malloc((size_t) n_blocks * SRC_BYTES);
uint8_t *dsts = malloc((size_t) n_blocks * DST_BYTES);
int *mxs = malloc(n_blocks * sizeof(int));
if (!master_src || !work_src || !dsts || !mxs) { fprintf(stderr, "alloc fail\n"); exit(1); }
for (int i = 0; i < n_blocks; i++) {
gen_src(master_src + (size_t)i * SRC_BYTES);
mxs[i] = (int)(xs() & 15);
}
/* Warm. */
memcpy(work_src, master_src, (size_t) n_blocks * SRC_BYTES);
for (int i = 0; i < n_blocks; i++)
ff_vp9_put_regular8_h_neon(dsts + (size_t)i * DST_BYTES, DST_W,
work_src + (size_t)i * SRC_BYTES + 3, SRC_W,
DST_H, mxs[i], 0);
double t0 = now_seconds();
double t_end = t0 + duration_s;
uint64_t done = 0;
while (now_seconds() < t_end) {
memcpy(work_src, master_src, (size_t) n_blocks * SRC_BYTES);
for (int i = 0; i < n_blocks; i++)
ff_vp9_put_regular8_h_neon(dsts + (size_t)i * DST_BYTES, DST_W,
work_src + (size_t)i * SRC_BYTES + 3, SRC_W,
DST_H, mxs[i], 0);
done += n_blocks;
}
double elapsed = now_seconds() - t0;
/* setup-only subtraction */
int setup_iters = (int) (done / n_blocks);
double s0 = now_seconds();
for (int it = 0; it < setup_iters; it++)
memcpy(work_src, master_src, (size_t) n_blocks * SRC_BYTES);
double s1 = now_seconds();
double kernel_seconds = elapsed - (s1 - s0);
double mbps = done / kernel_seconds / 1e6;
printf("M3''' NEON throughput:\n");
printf(" blocks/batch: %d\n", n_blocks);
printf(" batches done: %d\n", setup_iters);
printf(" total blocks: %llu\n", (unsigned long long) done);
printf(" elapsed (kernel)=%.6f s\n", kernel_seconds);
printf(" elapsed (setup) =%.6f s\n", s1 - s0);
printf(" throughput = %.3f Mblock/s\n", mbps);
printf(" per-block = %.1f ns\n", kernel_seconds / done * 1e9);
/* 1080p: 32400 blocks/frame */
printf(" equiv 1080p = %.1f FPS (32400 blocks/frame)\n",
mbps * 1e6 / 32400.0);
free(master_src); free(work_src); free(dsts); free(mxs);
}
int main(int argc, char **argv)
{
int n_blocks = 65536;
double duration = 5.0;
uint64_t seed = 0;
int do_correctness = 1;
static struct option opts[] = {
{"blocks", required_argument, 0, 'b'},
{"duration", required_argument, 0, 'd'},
{"seed", required_argument, 0, 's'},
{"no-correctness", no_argument, 0, 'C'},
{0,0,0,0}
};
for (int c; (c = getopt_long(argc, argv, "b:d:s:C", opts, 0)) != -1;) {
switch (c) {
case 'b': n_blocks = atoi(optarg); break;
case 'd': duration = atof(optarg); break;
case 's': seed = strtoull(optarg, 0, 0); break;
case 'C': do_correctness = 0; break;
default: return 2;
}
}
if (do_correctness) {
printf("=== M1'''_c bit-exact (10000 random blocks) ===\n");
if (correctness_check(seed, 10000) != 0) {
fprintf(stderr, "REFUSING to measure throughput on a broken kernel.\n");
return 1;
}
printf("\n");
}
printf("=== M3''' NEON throughput ===\n");
throughput_neon(seed, n_blocks, duration);
return 0;
}
+303
View File
@@ -0,0 +1,303 @@
/*
* Cycle 3 Phase 6 — QPU bench for VP9 8-tap "regular" subpel filter,
* horizontal, 8-wide output on V3D 7.1.
*
* Reports:
* M1''' (correctness): QPU output vs C reference, N blocks across
* all 16 mx phases
* M2''' (throughput): QPU sustained Mblock/s
*
* Per k3_mc_phase4.md §5 (revised per phase5''' findings 4 + 6):
* - src_off is the RAW block base (no +3 shift)
* - assert(dst_stride_u8 >= 8 && src_stride_u8 >= 15)
*
* License: BSD-2-Clause.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <getopt.h>
#include <vulkan/vulkan.h>
#include "v3d_runner.h"
extern void daedalus_vp9_put_regular_8h_ref(
uint8_t *dst, ptrdiff_t dst_stride,
const uint8_t *src, ptrdiff_t src_stride,
int h, int mx, int my);
/* Per-block layout: src buffer 8 rows × 16 cols = 128 bytes. The
* C bench's src+3 convention: NEON/C ref is called with
* `src = block_base + 3, src_stride = 16`. The shader's src_off
* is the RAW block_base (no +3 shift), and the shader reads
* s[0..14] from src_off + row*stride. Together this means:
* shader's s[k] for k=0..14 = master_src[block_base + row*16 + k]
* C ref's `src[x+k-3]` for x=0..7, k=0..7 with `src = block_base+3`
* = master_src[block_base + row*16 + (x+k)]
* = master_src[block_base + row*16 + (0..14)]
* which is exactly what the shader reads. */
#define SRC_W 16
#define SRC_H 8
#define DST_W 8
#define DST_H 8
#define SRC_BYTES (SRC_H * SRC_W)
#define DST_BYTES (DST_H * DST_W)
static uint64_t xs_state;
static inline uint64_t xs(void) {
uint64_t x = xs_state;
x ^= x << 13; x ^= x >> 7; x ^= x << 17;
return xs_state = x;
}
static void gen_src(uint8_t *b) {
for (int i = 0; i < SRC_BYTES; i++) b[i] = (uint8_t)(xs() & 0xff);
}
static double now_seconds(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
typedef struct {
uint32_t n_blocks;
uint32_t dst_stride_u8;
uint32_t src_stride_u8;
uint32_t _pad;
} push_consts;
int main(int argc, char **argv)
{
int n_blocks = 65536;
int iters = 100;
uint64_t seed = 0;
int verify_only = 0;
const char *spv_path = "v3d_mc_8h.spv";
static struct option opts[] = {
{"blocks", required_argument, 0, 'b'},
{"iters", required_argument, 0, 'i'},
{"seed", required_argument, 0, 's'},
{"spv", required_argument, 0, 'S'},
{"verify-only", no_argument, 0, 'V'},
{0,0,0,0}
};
for (int c; (c = getopt_long(argc, argv, "b:i:s:S:V", opts, 0)) != -1;) {
switch (c) {
case 'b': n_blocks = atoi(optarg); break;
case 'i': iters = atoi(optarg); break;
case 's': seed = strtoull(optarg, 0, 0); break;
case 'S': spv_path = optarg; break;
case 'V': verify_only = 1; break;
default: return 2;
}
}
xs_state = seed ? seed : 0xabcdef1234567890ULL;
v3d_runner *r = v3d_runner_create();
if (!r) { fprintf(stderr, "v3d_runner_create failed\n"); return 1; }
printf("=== v3d MC 8h bench ===\n");
printf(" device: %s\n", v3d_runner_device_name(r));
printf(" n_blocks: %d iters: %d\n", n_blocks, iters);
/* Buffers: meta + dst + src, all blocks contiguous. */
size_t meta_bytes = (size_t) n_blocks * 4 * sizeof(uint32_t);
size_t src_bytes = (size_t) n_blocks * SRC_BYTES;
size_t dst_bytes = (size_t) n_blocks * DST_BYTES;
v3d_buffer buf_meta = {0}, buf_dst = {0}, buf_src = {0};
if (v3d_runner_create_buffer(r, meta_bytes, &buf_meta)) return 1;
if (v3d_runner_create_buffer(r, dst_bytes, &buf_dst)) return 1;
if (v3d_runner_create_buffer(r, src_bytes, &buf_src)) return 1;
uint8_t *master_src = malloc(src_bytes);
uint8_t *expected = malloc(dst_bytes);
int *mxs = malloc(n_blocks * sizeof(int));
if (!master_src || !expected || !mxs) { fprintf(stderr, "alloc\n"); return 1; }
for (int i = 0; i < n_blocks; i++) {
gen_src(master_src + (size_t)i * SRC_BYTES);
mxs[i] = (int)(xs() & 15);
}
/* Build C-ref expected. C ref takes `src + 3, src_stride = SRC_W`. */
memset(expected, 0, dst_bytes);
for (int i = 0; i < n_blocks; i++) {
daedalus_vp9_put_regular_8h_ref(
expected + (size_t)i * DST_BYTES, DST_W,
master_src + (size_t)i * SRC_BYTES + 3, SRC_W,
DST_H, mxs[i], 0);
}
/* Populate GPU buffers. Contracts (phase4 §5) enforced via asserts. */
uint32_t dst_stride_u8 = DST_W;
uint32_t src_stride_u8 = SRC_W;
assert(dst_stride_u8 >= 8 && "phase4 §5 contract 1");
assert(src_stride_u8 >= 15 && "phase4 §5 contract 2");
uint32_t *meta = (uint32_t *) buf_meta.mapped;
for (int i = 0; i < n_blocks; i++) {
/* src_off: RAW block base. NO +3 shift. (phase5''' finding 4) */
uint32_t src_off = (uint32_t)((size_t)i * SRC_BYTES);
uint32_t dst_off = (uint32_t)((size_t)i * DST_BYTES);
meta[4*i + 0] = dst_off;
meta[4*i + 1] = src_off;
meta[4*i + 2] = (uint32_t) mxs[i];
meta[4*i + 3] = 0;
}
memcpy(buf_src.mapped, master_src, src_bytes);
memset(buf_dst.mapped, 0, dst_bytes);
/* Pipeline. */
v3d_pipeline pipe = {0};
if (v3d_runner_create_pipeline(r, spv_path,
/*n_ssbos=*/3,
/*push_const_size=*/sizeof(push_consts),
&pipe)) return 1;
v3d_buffer bind_bufs[3] = { buf_meta, buf_dst, buf_src };
if (v3d_runner_bind_buffers(r, &pipe, bind_bufs, 3)) return 1;
const uint32_t blocks_per_wg = 32;
uint32_t group_count_x = (uint32_t)((n_blocks + blocks_per_wg - 1) / blocks_per_wg);
printf(" dispatch: %u WGs × 256 invocations = %u blocks (rounded up from %d)\n",
group_count_x, group_count_x * blocks_per_wg, n_blocks);
push_consts pc = {
.n_blocks = (uint32_t) n_blocks,
.dst_stride_u8 = dst_stride_u8,
.src_stride_u8 = src_stride_u8,
._pad = 0,
};
VkCommandBuffer cb = v3d_runner_alloc_cmdbuf(r);
VkCommandBufferBeginInfo cbbi = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
vkBeginCommandBuffer(cb, &cbbi);
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipe.pipeline);
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
pipe.layout, 0, 1, &pipe.desc_set, 0, NULL);
vkCmdPushConstants(cb, pipe.layout, VK_SHADER_STAGE_COMPUTE_BIT,
0, sizeof(pc), &pc);
vkCmdDispatch(cb, group_count_x, 1, 1);
vkEndCommandBuffer(cb);
/* --- M1''' bit-exact --- */
printf("\n=== M1''': QPU vs C reference bit-exact ===\n");
memset(buf_dst.mapped, 0, dst_bytes);
if (v3d_runner_submit_wait(r, cb)) return 1;
int mismatch_blocks = 0;
int total_byte_diffs = 0;
int prints = 0;
for (int i = 0; i < n_blocks; i++) {
const uint8_t *q = (uint8_t *) buf_dst.mapped + (size_t)i * DST_BYTES;
const uint8_t *e = expected + (size_t)i * DST_BYTES;
if (memcmp(q, e, DST_BYTES) != 0) {
int diffs = 0;
for (int j = 0; j < DST_BYTES; j++) if (q[j] != e[j]) diffs++;
total_byte_diffs += diffs;
if (prints < 3) {
fprintf(stderr, "MISMATCH block %d mx=%d: %d/64 bytes differ\n",
i, mxs[i], diffs);
fprintf(stderr, " ref:");
for (int r0 = 0; r0 < 8; r0++) {
fprintf(stderr, "\n r%d ", r0);
for (int c = 0; c < 8; c++) fprintf(stderr, "%3u ", e[r0*8+c]);
}
fprintf(stderr, "\n qpu:");
for (int r0 = 0; r0 < 8; r0++) {
fprintf(stderr, "\n r%d ", r0);
for (int c = 0; c < 8; c++) fprintf(stderr, "%3u ", q[r0*8+c]);
}
fprintf(stderr, "\n");
prints++;
}
mismatch_blocks++;
}
}
printf(" blocks bit-exact: %d / %d (%.4f%%)\n",
n_blocks - mismatch_blocks, n_blocks,
100.0 * (n_blocks - mismatch_blocks) / n_blocks);
printf(" total byte diffs: %d / %zu (%.4f%%)\n",
total_byte_diffs, (size_t) n_blocks * DST_BYTES,
100.0 * total_byte_diffs / ((double) n_blocks * DST_BYTES));
if (mismatch_blocks > 0) {
fprintf(stderr, "REFUSING to measure throughput on a broken kernel.\n");
v3d_runner_destroy_pipeline(r, &pipe);
v3d_runner_destroy_buffer(r, &buf_src);
v3d_runner_destroy_buffer(r, &buf_dst);
v3d_runner_destroy_buffer(r, &buf_meta);
v3d_runner_destroy(r);
return 1;
}
if (verify_only) {
v3d_runner_destroy_pipeline(r, &pipe);
v3d_runner_destroy_buffer(r, &buf_src);
v3d_runner_destroy_buffer(r, &buf_dst);
v3d_runner_destroy_buffer(r, &buf_meta);
v3d_runner_destroy(r);
return 0;
}
/* --- M2''' throughput --- */
printf("\n=== M2''': QPU throughput ===\n");
for (int i = 0; i < 10; i++) {
memset(buf_dst.mapped, 0, dst_bytes);
if (v3d_runner_submit_wait(r, cb)) return 1;
}
double t0 = now_seconds();
for (int i = 0; i < iters; i++) {
memset(buf_dst.mapped, 0, dst_bytes);
if (v3d_runner_submit_wait(r, cb)) return 1;
}
double t1 = now_seconds();
double s0 = now_seconds();
for (int i = 0; i < iters; i++) memset(buf_dst.mapped, 0, dst_bytes);
double s1 = now_seconds();
double kernel_seconds = (t1 - t0) - (s1 - s0);
double total_blocks = (double) n_blocks * iters;
double mbps = total_blocks / kernel_seconds / 1e6;
printf(" blocks/dispatch: %d\n", n_blocks);
printf(" iters: %d\n", iters);
printf(" total blocks: %.0f\n", total_blocks);
printf(" elapsed (kernel)=%.6f s\n", kernel_seconds);
printf(" elapsed (setup) =%.6f s\n", s1 - s0);
printf(" M2''' throughput = %.3f Mblock/s\n", mbps);
printf(" per-block = %.1f ns\n", kernel_seconds / total_blocks * 1e9);
printf(" per-dispatch = %.1f us\n", kernel_seconds / iters * 1e6);
double M3 = 20.997; /* from k3_mc_phase3.md */
double R = mbps / M3;
printf("\n Cycle 3 NEON M3''' = %.3f Mblock/s\n", M3);
printf(" R''' = M2'''/M3''' = %.3f\n", R);
if (R >= 1.0) printf(" decision band = GREEN: QPU beats NEON in isolation\n");
else if (R >= 0.5) printf(" decision band = YELLOW: M4''' decides\n");
else if (R >= 0.1) printf(" decision band = ORANGE: M4''' may still rescue\n");
else printf(" decision band = RED: structural mismatch\n");
/* 30fps@1080p floor check (per project_30fps_floor_is_fine.md) */
double mblocks_per_1080p = 32400.0 * 30.0 / 1e6;
printf("\n 30fps@1080p floor : %.3f Mblock/s (32400 blocks × 30 fps)\n",
mblocks_per_1080p);
printf(" isolation margin : %.1fx over 30fps floor\n",
mbps / mblocks_per_1080p);
v3d_runner_destroy_pipeline(r, &pipe);
v3d_runner_destroy_buffer(r, &buf_src);
v3d_runner_destroy_buffer(r, &buf_dst);
v3d_runner_destroy_buffer(r, &buf_meta);
v3d_runner_destroy(r);
free(master_src); free(expected); free(mxs);
return 0;
}
+72
View File
@@ -0,0 +1,72 @@
/*
* Standalone bit-exact C reference for VP9 8-tap "regular" subpel
* filter, horizontal direction, 8-pixel-wide output. Transcribed
* from FFmpeg's libavcodec/vp9dsp_template.c FILTER_8TAP macro
* (vendored at external/ffmpeg-snapshot/). 8-bit pixels only.
*
* Filter coefficients embedded inline (REGULAR filter only, all 16
* subpel phases). Same values as ff_vp9_subpel_filters[1][mx] in
* external/ffmpeg-snapshot/libavcodec/vp9_subpel_filters_table.c.
*
* License: LGPL-2.1-or-later.
*
* Spec source: VP9 specification §8.5.1 — subpel motion compensation.
*/
#include <stdint.h>
#include <stddef.h>
static const int16_t vp9_8tap_regular_filters[16][8] = {
{ 0, 0, 0, 128, 0, 0, 0, 0 },
{ 0, 1, -5, 126, 8, -3, 1, 0 },
{ -1, 3, -10, 122, 18, -6, 2, 0 },
{ -1, 4, -13, 118, 27, -9, 3, -1 },
{ -1, 4, -16, 112, 37, -11, 4, -1 },
{ -1, 5, -18, 105, 48, -14, 4, -1 },
{ -1, 5, -19, 97, 58, -16, 5, -1 },
{ -1, 6, -19, 88, 68, -18, 5, -1 },
{ -1, 6, -19, 78, 78, -19, 6, -1 },
{ -1, 5, -18, 68, 88, -19, 6, -1 },
{ -1, 5, -16, 58, 97, -19, 5, -1 },
{ -1, 4, -14, 48, 105, -18, 5, -1 },
{ -1, 4, -11, 37, 112, -16, 4, -1 },
{ -1, 3, -9, 27, 118, -13, 4, -1 },
{ 0, 2, -6, 18, 122, -10, 3, -1 },
{ 0, 1, -3, 8, 126, -5, 1, 0 },
};
static inline uint8_t clip_u8(int x)
{
return (uint8_t)(x > 255 ? 255 : x < 0 ? 0 : x);
}
/*
* 8x8 horizontal 8-tap "put" (non-averaging). Width hard-coded 8.
* `src` must point at the row-0 output-column-0 source pixel; valid
* source memory must extend src[r*src_stride + (-3..+11)] for r=0..h-1.
* `dst` is written at dst[r*dst_stride + 0..7] for r=0..h-1.
*
* Matches ff_vp9_put_regular8_h_neon byte-for-byte on 8-bit input.
*/
void daedalus_vp9_put_regular_8h_ref(uint8_t *dst, ptrdiff_t dst_stride,
const uint8_t *src, ptrdiff_t src_stride,
int h, int mx, int my)
{
(void) my; /* horizontal-only filter ignores y phase */
const int16_t *F = vp9_8tap_regular_filters[mx & 15];
for (int r = 0; r < h; r++) {
for (int x = 0; x < 8; x++) {
int sum = F[0] * (int) src[x - 3]
+ F[1] * (int) src[x - 2]
+ F[2] * (int) src[x - 1]
+ F[3] * (int) src[x + 0]
+ F[4] * (int) src[x + 1]
+ F[5] * (int) src[x + 2]
+ F[6] * (int) src[x + 3]
+ F[7] * (int) src[x + 4];
dst[x] = clip_u8((sum + 64) >> 7);
}
dst += dst_stride;
src += src_stride;
}
}