instrument(av1): SGR real per-tap A/B ground truth + LR gate stream pin
Browse files at this commitfb35afcca44ebec350a67532587324b690d55d40Parent: e33c3853 changed files +85 -0
- crates/ec-av1/src/stream.rs +14 -0
- scripts/instrument-aom-oracle.sh +30 -0
- scripts/lr-sgr-pin-harness.c +41 -0
crates/ec-av1/src/stream.rsmodified+14 -0
| @@ -4781,6 +4781,20 @@ mod tests { | |||
| 4781 | // frame must now decode Ok AND match an independent | 4781 | // frame must now decode Ok AND match an independent |
| 4782 | // decoder pixel-exact, not just refuse cleanly. | 4782 | // decoder pixel-exact, not just refuse cleanly. |
| 4783 | let reference = ffmpeg_decode_sequence(&stream, width, height, pics.len()); | 4783 | let reference = ffmpeg_decode_sequence(&stream, width, height, pics.len()); |
| 4784 | + let mismatched = pics | ||
| 4785 | + .iter() | ||
| 4786 | + .zip(&reference) | ||
| 4787 | + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); | ||
| 4788 | + // r7: pin a mismatching stream the same way the | ||
| 4789 | + // masked-compound gate does -- lets a follow-up round | ||
| 4790 | + // decode this exact seed's bytes directly instead of | ||
| 4791 | + // re-deriving them from a live 40-attempt sweep. | ||
| 4792 | + if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { | ||
| 4793 | + std::fs::write(&path, &stream).expect("writing pinned stream"); | ||
| 4794 | + eprintln!( | ||
| 4795 | + "EC_AV1_GATE_DUMP: wrote mismatching LR stream (seed {seed}) to {path}" | ||
| 4796 | + ); | ||
| 4797 | + } | ||
| 4784 | for (i, (pic, refpic)) in pics.iter().zip(reference.iter()).enumerate() { | 4798 | for (i, (pic, refpic)) in pics.iter().zip(reference.iter()).enumerate() { |
| 4785 | assert_eq!(pic.y, refpic.y, "{NAME}: luma mismatch (seed {seed} frame {i})"); | 4799 | assert_eq!(pic.y, refpic.y, "{NAME}: luma mismatch (seed {seed} frame {i})"); |
| 4786 | assert_eq!(pic.u, refpic.u, "{NAME}: U mismatch (seed {seed} frame {i})"); | 4800 | assert_eq!(pic.u, refpic.u, "{NAME}: U mismatch (seed {seed} frame {i})"); |
scripts/instrument-aom-oracle.shmodified+30 -0
| @@ -27,6 +27,15 @@ | |||
| 27 | # the key-frame path has no traced symbol at all between the partition | 27 | # the key-frame path has no traced symbol at all between the partition |
| 28 | # read and the first coefficient block, which is precisely the gap a | 28 | # read and the first coefficient block, which is precisely the gap a |
| 29 | # rect-strip desync hides in. | 29 | # rect-strip desync hides in. |
| 30 | +# Rung 6 (unconditional, no env gate) -- SGR per-tap ground truth | ||
| 31 | +# (lane-lr r7): `calculate_intermediate_result` in | ||
| 32 | +# av1/common/restoration.c is `static`, so a standalone harness cannot | ||
| 33 | +# call it to get real A[]/B[] intermediate arrays, only the final | ||
| 34 | +# flt0/flt1 via the public av1_selfguided_restoration_c. This rung | ||
| 35 | +# drops `static` so an external harness can call it directly with a | ||
| 36 | +# `dgd32`-style buffer (see scripts/lr-sgr-pin-harness.c) and diff | ||
| 37 | +# every A[k]/B[k] tap against a from-scratch recompute -- the missing | ||
| 38 | +# half of r6's 9-tap cross-check. No behaviour change (only linkage). | ||
| 30 | # | 39 | # |
| 31 | # Idempotent: re-running is a no-op. Rebuild afterwards with | 40 | # Idempotent: re-running is a no-op. Rebuild afterwards with |
| 32 | # ninja -C ~/.cache/aom-oracle/build aomdec | 41 | # ninja -C ~/.cache/aom-oracle/build aomdec |
| @@ -263,3 +272,24 @@ s = s.replace(old, new, 1) | |||
| 263 | open(path, "w").write(s) | 272 | open(path, "w").write(s) |
| 264 | print("intra mode-info instrumented") | 273 | print("intra mode-info instrumented") |
| 265 | PYI | 274 | PYI |
| 275 | + | ||
| 276 | +# --- rung 6: export calculate_intermediate_result for a direct harness call | ||
| 277 | +I="$SRC/av1/common/restoration.c" | ||
| 278 | +[ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; } | ||
| 279 | + | ||
| 280 | +python3 - "$I" <<'PYA' | ||
| 281 | +import sys | ||
| 282 | +path = sys.argv[1] | ||
| 283 | +s = open(path).read() | ||
| 284 | +if "/* EC_INSTRUMENTED_AB */" in s: | ||
| 285 | + print("calculate_intermediate_result already exported (no-op)") | ||
| 286 | + sys.exit(0) | ||
| 287 | + | ||
| 288 | +old = """static void calculate_intermediate_result(int32_t *dgd, int width, int height,""" | ||
| 289 | +new = """/* EC_INSTRUMENTED_AB */ | ||
| 290 | +void calculate_intermediate_result(int32_t *dgd, int width, int height,""" | ||
| 291 | +assert old in s, "calculate_intermediate_result signature moved" | ||
| 292 | +s = s.replace(old, new, 1) | ||
| 293 | +open(path, "w").write(s) | ||
| 294 | +print("calculate_intermediate_result exported") | ||
| 295 | +PYA | ||
scripts/lr-sgr-pin-harness.cmodified+41 -0
| @@ -11,6 +11,16 @@ extern int av1_selfguided_restoration_c(const uint8_t *dgd8, int width, | |||
| 11 | int32_t *flt0, int32_t *flt1, | 11 | int32_t *flt0, int32_t *flt1, |
| 12 | int flt_stride, int sgr_params_idx, | 12 | int flt_stride, int sgr_params_idx, |
| 13 | int bit_depth, int highbd); | 13 | int bit_depth, int highbd); |
| 14 | +// Exported by scripts/instrument-aom-oracle.sh's rung 6 (normally `static` | ||
| 15 | +// in restoration.c) so this harness can get real per-tap A[]/B[] instead of | ||
| 16 | +// only the combined flt0/flt1 -- the missing half of r6's 9-tap cross-check. | ||
| 17 | +extern void calculate_intermediate_result(int32_t *dgd, int width, int height, | ||
| 18 | + int dgd_stride, int bit_depth, | ||
| 19 | + int sgr_params_idx, int radius_idx, | ||
| 20 | + int pass, int32_t *A, int32_t *B); | ||
| 21 | + | ||
| 22 | +#define SGRPROJ_BORDER_VERT 3 | ||
| 23 | +#define SGRPROJ_BORDER_HORZ 3 | ||
| 14 | 24 | ||
| 15 | int main(void) { | 25 | int main(void) { |
| 16 | const int bw = 102, bh = 10; | 26 | const int bw = 102, bh = 10; |
| @@ -44,5 +54,36 @@ int main(void) { | |||
| 44 | int r2 = av1_selfguided_restoration_c(dgd8, w, h, bw, flt0, flt1, w, 6, 8, 0); | 54 | int r2 = av1_selfguided_restoration_c(dgd8, w, h, bw, flt0, flt1, w, 6, 8, 0); |
| 45 | printf("selfguided ret=%d flt0[1][6]=%d flt1[1][6]=%d\n", r2, flt0[1 * w + 6], | 55 | printf("selfguided ret=%d flt0[1][6]=%d flt1[1][6]=%d\n", r2, flt0[1 * w + 6], |
| 46 | flt1[1 * w + 6]); | 56 | flt1[1 * w + 6]); |
| 57 | + | ||
| 58 | + // --- rung 6: real per-tap A[]/B[] ground truth for the dense (r1) arm --- | ||
| 59 | + // Replicate av1_selfguided_restoration_c's own uint8->int32 border-extend | ||
| 60 | + // copy (that function's `dgd32` setup, restoration.c) so | ||
| 61 | + // calculate_intermediate_result sees exactly the same input it would from | ||
| 62 | + // the real call path above. | ||
| 63 | + const int dgd32_stride = w + 2 * SGRPROJ_BORDER_HORZ; | ||
| 64 | + static int32_t dgd32_buf[(64 + 6) * (4 + 6)]; | ||
| 65 | + int32_t *dgd32 = dgd32_buf + dgd32_stride * SGRPROJ_BORDER_VERT + SGRPROJ_BORDER_HORZ; | ||
| 66 | + for (int i = -SGRPROJ_BORDER_VERT; i < h + SGRPROJ_BORDER_VERT; ++i) | ||
| 67 | + for (int j = -SGRPROJ_BORDER_HORZ; j < w + SGRPROJ_BORDER_HORZ; ++j) | ||
| 68 | + dgd32[i * dgd32_stride + j] = dgd8[i * bw + j]; | ||
| 69 | + | ||
| 70 | + const int width_ext = w + 2 * SGRPROJ_BORDER_HORZ; | ||
| 71 | + const int buf_stride = ((width_ext + 3) & ~3) + 16; | ||
| 72 | + static int32_t A_[2 * 400 * 400], B_[2 * 400 * 400]; | ||
| 73 | + // radius_idx=1 (dense/r1 arm), pass=0 (every row, matches | ||
| 74 | + // selfguided_restoration_internal's own call). | ||
| 75 | + calculate_intermediate_result(dgd32, w, h, dgd32_stride, /*bit_depth=*/8, | ||
| 76 | + /*sgr_params_idx=*/6, /*radius_idx=*/1, | ||
| 77 | + /*pass=*/0, A_, B_); | ||
| 78 | + int32_t *A = A_ + SGRPROJ_BORDER_VERT * buf_stride + SGRPROJ_BORDER_HORZ; | ||
| 79 | + int32_t *B = B_ + SGRPROJ_BORDER_VERT * buf_stride + SGRPROJ_BORDER_HORZ; | ||
| 80 | + const int ti = 1, tj = 6; | ||
| 81 | + const char *names[9] = { "c", "u", "d", "l", "r", "ul", "ur", "dl", "dr" }; | ||
| 82 | + const int di[9] = { 0, -1, 1, 0, 0, -1, -1, 1, 1 }; | ||
| 83 | + const int dj[9] = { 0, 0, 0, -1, 1, -1, 1, -1, 1 }; | ||
| 84 | + for (int t = 0; t < 9; ++t) { | ||
| 85 | + int k = (ti + di[t]) * buf_stride + (tj + dj[t]); | ||
| 86 | + printf("real_ab %s = (%d,%d)\n", names[t], A[k], B[k]); | ||
| 87 | + } | ||
| 47 | return 0; | 88 | return 0; |
| 48 | } | 89 | } |