Files
marfrit 85feba4087 Cycle 4 (LPF wd=8) closure: M1=100%, R=0.34, M4=+4.1%, PASS
Fourth daedalus-fourier kernel — VP9 8-tap inner loop filter wd=8
h_8_8 variant. Width extension of cycle 2's wd=4; completes VP9
inner-edge LPF coverage. Full cycle Phase 1-7 + M4'''' in one
combined go (cycle compressed since incremental from cycle 2).

Phase 5 review explicitly skipped (incremental ~30-line shader
delta from cycle 2 + same geometry + cycle-2 RED-pattern checks
still apply). Flagged in docs/k4_lpf8_phase4_7.md per dev_process.md
"Skipping phases is a deliberate choice that should be flagged."

Phase 6 v1 first-light: M1'''' 100.0000% bit-exact (65536/65536)
first try. Shaderdb shows 231 inst, 4 hardware threads, 0 spills,
27 max-temps, 48 uniforms — compiler at the latency-hiding ceiling.

Performance:

  M3'''' NEON (single-core)  52.382 Medge/s
  M2'''' QPU isolation       17.847 Medge/s
  R''''                      0.341  → ORANGE band
  30fps floor margin         9.2x (isolation), 20.3x (mixed)

M4'''' concurrent matrix:

  NEON 4-core                37.823 Medge/s  <- baseline
  QPU only                   14.867 Medge/s
  MIXED NEON-3 + QPU         39.389 Medge/s  <- +4.1% PASS

Verdict: YELLOW-via-M4'''' PASS. Deploy wd=8 LPF on QPU alongside
cycle 2 wd=4. Combined VP9 inner-edge LPF coverage now complete.

Cross-cycle LPF comparison:

  | | wd=4 (k2) | wd=8 (k4) |
  | M3 NEON     | 48.3      | 52.4      |
  | M2 QPU iso  | 19.6      | 17.8      |
  | R iso       | 0.41      | 0.34      |
  | M4 delta    | +6.9%     | +4.1%     |
  | 30fps mixed | 7.2x      | 20.3x     |
  | Verdict     | GO QPU    | GO QPU    |

NEW finding (Phase 9 lesson): NEON gets faster per edge as filter
width grows (20.7 → 19.1 ns wd=4 → wd=8). The relative QPU loss
grows with width. wd=16 would probably flip negative based on the
trend line.

Deployment recipe with cycle 4:

  IDCT 8x8 (k1)     -> QPU   (R=0.92, +7% mixed)
  LPF wd=4 (k2)     -> QPU   (R=0.41, +7% mixed)
  LPF wd=8 (k4)     -> QPU   (R=0.34, +4% mixed)
  MC 8h (k3)        -> CPU   (R=0.067, -19% mixed)
  Entropy           -> CPU   (structural)

VP9 inner-edge LPF coverage complete. Project continues to higgs
deployment plumbing or further kernels per user direction.

New artifacts:
- src/v3d_lpf_h_8_8.comp                 — GLSL shader
- tests/vp9_lpf8_ref.c                   — standalone C ref
- tests/bench_neon_lpf8.c                — M1+M3 bench
- tests/bench_v3d_lpf8.c                 — M1+M2 bench
- tests/bench_concurrent_lpf8.c          — M4 pthread bench
- docs/k4_lpf8_phase1_3.md + phase4_7.md — combined cycle docs

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 12:56:25 +00:00

193 lines
7.4 KiB
C

/*
* Cycle 4 Phase 6 — QPU bench for VP9 wd=8 LPF.
* Mirrors bench_v3d_lpf.c (cycle 2); changes: calls the wd=8 ref
* + asserts dst_stride >= 6 (cycle 4 contract).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include <assert.h>
#include <time.h>
#include <getopt.h>
#include <vulkan/vulkan.h>
#include "v3d_runner.h"
extern void daedalus_vp9_loop_filter_h_8_8_ref(
uint8_t *dst, ptrdiff_t stride, int E, int I, int H);
#define EDGE_STRIDE 8
#define EDGE_BYTES 64
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_edge_pixels(uint8_t *buf) {
int a = (int)(xs() % 200) + 20;
int b = (int)(xs() % 200) + 20;
int n = (int)(xs() % 30);
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++) {
int base = (c < 4) ? a : b;
int noise = ((int)(xs() % (2*n + 1))) - n;
int v = base + noise;
buf[r*EDGE_STRIDE + c] = (uint8_t)(v < 0 ? 0 : v > 255 ? 255 : v);
}
}
static void gen_thresholds(int *E, int *I, int *H) {
*E = (int)(xs() % 81);
*I = (int)(xs() % 41);
*H = (int)(xs() % 11);
}
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_edges, blocks_per_row, dst_stride_u8, _pad; } push_consts;
int main(int argc, char **argv)
{
int n_edges = 65536, iters = 100, verify_only = 0;
uint64_t seed = 0;
const char *spv = "v3d_lpf_h_8_8.spv";
static struct option opts[] = {
{"edges", required_argument, 0, 'e'},
{"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, "e:i:s:S:V", opts, 0)) != -1;) {
switch (c) {
case 'e': n_edges = atoi(optarg); break;
case 'i': iters = atoi(optarg); break;
case 's': seed = strtoull(optarg, 0, 0); break;
case 'S': spv = optarg; break;
case 'V': verify_only = 1; break;
default: return 2;
}
}
xs_state = seed ? seed : 0xa57edbeef5717ULL;
v3d_runner *r = v3d_runner_create();
if (!r) return 1;
printf("=== v3d LPF h_8_8 bench ===\n");
printf(" device: %s\n n_edges: %d iters: %d\n",
v3d_runner_device_name(r), n_edges, iters);
size_t dst_bytes = (size_t) n_edges * EDGE_BYTES;
size_t meta_bytes = (size_t) n_edges * 4 * sizeof(uint32_t);
v3d_buffer buf_meta = {0}, buf_dst = {0};
v3d_runner_create_buffer(r, meta_bytes, &buf_meta);
v3d_runner_create_buffer(r, dst_bytes, &buf_dst);
uint8_t *master = malloc(dst_bytes);
uint8_t *expected = malloc(dst_bytes);
int *Es = malloc(n_edges*sizeof(int)), *Is = malloc(n_edges*sizeof(int)), *Hs = malloc(n_edges*sizeof(int));
for (int i = 0; i < n_edges; i++) {
gen_edge_pixels(master + (size_t)i * EDGE_BYTES);
gen_thresholds(&Es[i], &Is[i], &Hs[i]);
}
memcpy(expected, master, dst_bytes);
for (int i = 0; i < n_edges; i++)
daedalus_vp9_loop_filter_h_8_8_ref(expected + (size_t)i * EDGE_BYTES + 4,
EDGE_STRIDE, Es[i], Is[i], Hs[i]);
uint32_t dst_stride = EDGE_STRIDE;
assert(dst_stride >= 6 && "cycle 4 §4 contract: dst_stride_u8 >= 6 (flat8in 6-write)");
uint32_t *meta = buf_meta.mapped;
for (int i = 0; i < n_edges; i++) {
uint32_t mx = (uint32_t)((size_t)i * EDGE_BYTES + 4);
assert(mx >= 4);
meta[4*i + 0] = mx;
meta[4*i + 1] = (uint32_t) Es[i];
meta[4*i + 2] = (uint32_t) Is[i];
meta[4*i + 3] = (uint32_t) Hs[i];
}
memcpy(buf_dst.mapped, master, dst_bytes);
v3d_pipeline pipe = {0};
if (v3d_runner_create_pipeline(r, spv, 2, sizeof(push_consts), &pipe)) return 1;
v3d_buffer bufs[2] = { buf_meta, buf_dst };
v3d_runner_bind_buffers(r, &pipe, bufs, 2);
const uint32_t edges_per_wg = 32;
uint32_t gc = (uint32_t)((n_edges + edges_per_wg - 1) / edges_per_wg);
push_consts pc = { .n_edges = (uint32_t) n_edges, .dst_stride_u8 = dst_stride };
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);
/* M1'''' */
printf("\n=== M1'''': QPU vs C bit-exact ===\n");
memcpy(buf_dst.mapped, master, dst_bytes);
if (v3d_runner_submit_wait(r, cb)) return 1;
int mis = 0, bytediffs = 0;
for (int i = 0; i < n_edges; i++) {
const uint8_t *q = (uint8_t *) buf_dst.mapped + (size_t)i * EDGE_BYTES;
const uint8_t *e = expected + (size_t)i * EDGE_BYTES;
if (memcmp(q, e, EDGE_BYTES) != 0) {
int d = 0;
for (int j = 0; j < EDGE_BYTES; j++) if (q[j] != e[j]) d++;
bytediffs += d;
if (mis < 3) fprintf(stderr, "MISMATCH edge %d (E=%d I=%d H=%d): %d/64 bytes\n",
i, Es[i], Is[i], Hs[i], d);
mis++;
}
}
printf(" edges bit-exact: %d / %d (%.4f%%)\n",
n_edges - mis, n_edges, 100.0 * (n_edges - mis) / n_edges);
if (mis > 0) { fprintf(stderr, "REFUSING throughput on broken kernel.\n"); return 1; }
if (verify_only) return 0;
/* M2'''' */
printf("\n=== M2'''': QPU throughput ===\n");
for (int i = 0; i < 10; i++) { memcpy(buf_dst.mapped, master, dst_bytes); v3d_runner_submit_wait(r, cb); }
double t0 = now_seconds();
for (int i = 0; i < iters; i++) { memcpy(buf_dst.mapped, master, dst_bytes); v3d_runner_submit_wait(r, cb); }
double t1 = now_seconds();
double s0 = now_seconds();
for (int i = 0; i < iters; i++) memcpy(buf_dst.mapped, master, dst_bytes);
double s1 = now_seconds();
double ks = (t1 - t0) - (s1 - s0);
double total = (double) n_edges * iters;
double mes = total / ks / 1e6;
printf(" edges/dispatch: %d, iters: %d, total: %.0f\n", n_edges, iters, total);
printf(" elapsed (kernel)=%.6f s\n per-edge = %.1f ns\n per-dispatch = %.1f us\n",
ks, ks / total * 1e9, ks / iters * 1e6);
printf(" M2'''' = %.3f Medge/s\n", mes);
double M3 = 52.382; /* k4 phase 3 baseline */
double R = mes / M3;
printf("\n Cycle 4 NEON M3'''' = %.3f Medge/s\n", M3);
printf(" R'''' = M2''''/M3'''' = %.3f\n", R);
if (R >= 1.0) printf(" decision band = GREEN\n");
else if (R >= 0.5) printf(" decision band = YELLOW\n");
else if (R >= 0.1) printf(" decision band = ORANGE\n");
else printf(" decision band = RED\n");
double floor30 = 64530.0 * 30 / 1e6;
printf(" 30fps@1080p floor : %.3f Medge/s — %.1fx margin\n",
floor30, mes / floor30);
v3d_runner_destroy_pipeline(r, &pipe);
v3d_runner_destroy_buffer(r, &buf_dst);
v3d_runner_destroy_buffer(r, &buf_meta);
v3d_runner_destroy(r);
return 0;
}