From bed54d12dc5a8cf8e6a514b2a49d3592d6cecb65 Mon Sep 17 00:00:00 2001 From: Waozi Date: Sun, 22 Nov 2026 04:31:12 -0300 Subject: [PATCH] Bound AV1 deblock engine validation and align package pins --- tests/av1_deblock.sh | 68 +++++++++++++++++++++++----------- tests/av1_deblock_oracle.py | 74 +++++++++++++++++++++++-------------- ziran.lock | 25 +++++++------ ziran.toml | 4 +- 4 files changed, 108 insertions(+), 63 deletions(-) diff --git a/tests/av1_deblock.sh b/tests/av1_deblock.sh index f3b8a5c..ef22b61 100644 --- a/tests/av1_deblock.sh +++ b/tests/av1_deblock.sh @@ -3,33 +3,59 @@ set -eu . "$(dirname -- "$0")/common.sh" out="$root/build/scratch/av1-deblock" aom=${AV1_AOM_SOURCE:-$("$ziran" pkg path libaom)} +rm -f "$out/receipt.json" python3 tests/av1_deblock_oracle.py --root "$root" --out "$out" --aom "$aom" module=av1_deblock_vectors -"$ziran" ir --root "$out" --module-path "image_codec=$foundation/src" --module-path "std=$toolchain/std" -o "$out/ir" "$out/$module.zi" -for form in source saved; do - input="$out/$module.zi" - set -- --root "$out" --module-path "image_codec=$foundation/src" --module-path "std=$toolchain/std" - if test "$form" = saved; then input="$out/ir/$module.zir"; set -- --root "$out/ir" --module-path "$out/ir"; fi - "$ziran" build --target=c --exe --entry "$module:main" "$@" -o "$out/$form" "$input" - for tier in 1 2 3 4; do - printf 'AV1 deblock %s SIMD tier %s\n' "$form" "$tier" - ZIRAN_SIMD_TIER=$tier "$out/$form/$module" +check_native() { + "$ziran" ir --root "$out" --module-path "image_codec=$foundation/src" --module-path "std=$toolchain/std" -o "$out/ir" "$out/$module.zi" + for form in source saved; do + input="$out/$module.zi" + set -- --root "$out" --module-path "image_codec=$foundation/src" --module-path "std=$toolchain/std" + if test "$form" = saved; then input="$out/ir/$module.zir"; set -- --root "$out/ir" --module-path "$out/ir"; fi + "$ziran" build --target=c --exe --entry "$module:main" "$@" -o "$out/$form" "$input" + for tier in 1 2 3 4; do + printf 'AV1 deblock %s SIMD tier %s\n' "$form" "$tier" + ZIRAN_SIMD_TIER=$tier "$out/$form/$module" + done + "$ziran" build --target=go --pkg main --entry "$module:main" "$@" -o "$out/$form-go" "$input" + printf 'package main\nfunc main() { if Av1DeblockVectors_Main() != 0 { panic("AV1 deblock") } }\n' > "$out/$form-go/main.go" + (cd "$out/$form-go" && GO111MODULE=off go build -o run . && ./run) done - "$ziran" bundle --entry "$module:main" "$@" -o "$out/$form.zx" "$input" - test "$("$bin/zi2zx" run --vm "$out/$form.zx")" = 0 - "$ziran" build --target=go --pkg main --entry "$module:main" "$@" -o "$out/$form-go" "$input" - printf 'package main\nfunc main() { if Av1DeblockVectors_Main() != 0 { panic("AV1 deblock") } }\n' > "$out/$form-go/main.go" - (cd "$out/$form-go" && GO111MODULE=off go build -o run . && ./run) + CFLAGS='-O0 -g -fsanitize=address,undefined -fno-omit-frame-pointer' LDFLAGS='-fsanitize=address,undefined' \ + "$ziran" build --target=c --exe --entry "$module:main" --root "$out" \ + --module-path "image_codec=$foundation/src" --module-path "std=$toolchain/std" -o "$out/sanitized" "$out/$module.zi" + ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=halt_on_error=1 "$out/sanitized/$module" +} +check_engine() { + shard="$out/$1" + source="$shard/$module.zi" + saved="$shard/ir/$module.zir" + set -- --root "$shard" --module-path "image_codec=$foundation/src" --module-path "std=$toolchain/std" + printf 'AV1 deblock %s source engine\n' "${shard##*/}" + "$ziran" bundle --entry "$module:main" "$@" -o "$shard/source.zx" "$source" + result=$("$bin/zi2zx" run --backend engine "$shard/source.zx") + if test "$result" != 0; then printf '%s source engine returned %s\n' "$shard" "$result" >&2; return 1; fi + printf 'AV1 deblock %s saved engine\n' "${shard##*/}" + "$ziran" ir "$@" -o "$shard/ir" "$source" + "$ziran" bundle --entry "$module:main" --root "$shard/ir" --module-path "$shard/ir" -o "$shard/saved.zx" "$saved" + result=$("$bin/zi2zx" run --backend engine "$shard/saved.zx") + if test "$result" != 0; then printf '%s saved engine returned %s\n' "$shard" "$result" >&2; return 1; fi +} +shards=$(python3 -c 'import json,sys; print(*(s["directory"] for s in json.load(open(sys.argv[1]))["engine_shards"]))' "$out/oracle.json") +check_native & +pids=$! +for shard in $shards; do + check_engine "$shard" & + pids="$pids $!" done -CFLAGS='-O0 -g -fsanitize=address,undefined -fno-omit-frame-pointer' LDFLAGS='-fsanitize=address,undefined' \ - "$ziran" build --target=c --exe --entry "$module:main" --root "$out" \ - --module-path "image_codec=$foundation/src" --module-path "std=$toolchain/std" -o "$out/sanitized" "$out/$module.zi" -ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=halt_on_error=1 "$out/sanitized/$module" +status=0 +for pid in $pids; do wait "$pid" || status=1; done +test "$status" = 0 python3 - "$out" <<'PY' import json,pathlib,sys out=pathlib.Path(sys.argv[1]);receipt=json.loads((out/'oracle.json').read_text()) -receipt.update(command='build-queue -- sh tests/av1_deblock.sh',exit_code=0, - forms=['source','saved IR'],targets=['C','Go','zi2zx run --vm'],c_simd_tiers=[1,2,3,4],sanitizers=['ASan','UBSan'],sanitizer_optimization='-O0') +receipt.update(command='sh tests/av1_deblock.sh',exit_code=0, + forms=['source','saved IR'],targets=['C','Go','zi2zx run --backend engine'],c_simd_tiers=[1,2,3,4],sanitizers=['ASan','UBSan'],sanitizer_optimization='-O0') (out/'receipt.json').write_text(json.dumps(receipt,indent=2)+'\n') PY -printf '%s\n' 'AV1 deblock: pinned line and four-line segment vectors, source/saved C/Go/VM + ASan/UBSan passed' +printf '%s\n' 'AV1 deblock: pinned line and four-line segment vectors, source/saved C/Go/engine + ASan/UBSan passed' diff --git a/tests/av1_deblock_oracle.py b/tests/av1_deblock_oracle.py index ed1239f..823941b 100644 --- a/tests/av1_deblock_oracle.py +++ b/tests/av1_deblock_oracle.py @@ -122,6 +122,21 @@ def reference(source,length,blimit,limit,threshold,depth): return output,[bool(enabled),hev,flat,wide] +def write_vectors(root, out, groups): + out.mkdir(parents=True, exist_ok=True) + module = os.path.relpath(root/'src/filter/deblock.zi', out) + bounds = os.path.relpath(root/'tests/av1_deblock.zi', out) + lines = ['#import, file "'+module+'";', '#import, file "'+bounds+'";', 'using Codec :: #import "image_codec/api"', + '#program_export', 'main :: () -> s32 {', ' if DeblockBoundaries() != 0 { return 99 }', + ' if DeblockLinesBoundaries() != 0 { return 98 }'] + for name, code, _ in groups: + lines.append(f' if {name}() != 0 {{ return {code} }}') + lines += [' return 0', '}'] + for name, _, body in groups: + lines += [f'{name} :: () -> s32 {{', *body, ' return 0', '}'] + (out/'av1_deblock_vectors.zi').write_text('\n'.join(lines)+'\n') + + def main(): parser = argparse.ArgumentParser() for name in ('root','out','aom'): @@ -197,40 +212,43 @@ def main(): raise SystemExit(f'native/spec four-line deblock disagreement at {i}') if mixed == 0: raise SystemExit('four-line deblock segments never mix line decisions') - module = os.path.relpath(a.root/'src/filter/deblock.zi',a.out) - bounds = os.path.relpath(a.root/'tests/av1_deblock.zi',a.out) - lines = ['#import, file "'+module+'";','#import, file "'+bounds+'";','using Codec :: #import "image_codec/api"', - '#program_export','main :: () -> s32 {',' if DeblockBoundaries() != 0 { return 99 }', - ' if DeblockLinesBoundaries() != 0 { return 98 }'] - groups = (len(cases)+15)//16 - for group in range(groups): - lines.append(f' if Group{group}() != 0 {{ return {group+1} }}') - line_groups = (len(segments)+15)//16 - for group in range(line_groups): - lines.append(f' if Lines{group}() != 0 {{ return {groups+group+1} }}') - lines += [' return 0','}'] - for group in range(groups): - lines.append(f'Group{group} :: () -> s32 {{') + edge_groups = [] + for group in range((len(cases)+15)//16): + body = [] for c in cases[group*16:(group+1)*16]: checks = ' || '.join('result.'+name+' != '+str(flag).lower() for name,flag in zip(('filter_enabled','high_variance','flat','wide'),c['flags'])) - lines += [' {',' input: [16]u16 = .['+','.join(map(str,c['source']))+']', - f' result := FilterDeblockEdge(input[:], {c["length"]}, {c["blimit"]}, {c["limit"]}, {c["threshold"]}, {c["depth"]})', - ' expected: [16]u16 = .['+','.join(map(str,c['expected']))+']', - f' if result.error.code != DecodeOK || {checks} {{ return 1 }}', - ' for j: 0..15 { if result.samples[j] != expected[j] { return 2 } }',' }'] - lines += [' return 0','}'] - for group in range(line_groups): - lines.append(f'Lines{group} :: () -> s32 {{') + body += [' {',' input: [16]u16 = .['+','.join(map(str,c['source']))+']', + f' result := FilterDeblockEdge(input[:], {c["length"]}, {c["blimit"]}, {c["limit"]}, {c["threshold"]}, {c["depth"]})', + ' expected: [16]u16 = .['+','.join(map(str,c['expected']))+']', + f' if result.error.code != DecodeOK || {checks} {{ return 1 }}', + ' for j: 0..15 { if result.samples[j] != expected[j] { return 2 } }',' }'] + edge_groups.append((f'Group{group}', group+1, body)) + line_groups = [] + for group in range((len(segments)+15)//16): + body = [] for c in segments[group*16:(group+1)*16]: - lines += [' {',' lines: [64]u16 = .['+','.join(map(str,sum(c['lines'],[])))+']', - ' expected: [64]u16 = .['+','.join(map(str,c['expected']))+']', - f' if DeblockLinesCase(lines[:], expected[:], {c["length"]}, {c["blimit"]}, {c["limit"]}, {c["threshold"]}, {c["depth"]}) != 0 {{ return 1 }}', - ' }'] - lines += [' return 0','}'] - (a.out/'av1_deblock_vectors.zi').write_text('\n'.join(lines)+'\n') + body += [' {',' lines: [64]u16 = .['+','.join(map(str,sum(c['lines'],[])))+']', + ' expected: [64]u16 = .['+','.join(map(str,c['expected']))+']', + f' if DeblockLinesCase(lines[:], expected[:], {c["length"]}, {c["blimit"]}, {c["limit"]}, {c["threshold"]}, {c["depth"]}) != 0 {{ return 1 }}', + ' }'] + line_groups.append((f'Lines{group}', len(edge_groups)+group+1, body)) + write_vectors(a.root, a.out, edge_groups + line_groups) + + # Engine lowering must fit its allocation budget. Keep each bundle to at + # most 768 edges and 256 segments, using the same bodies as the full test. + shards = [] + edge_chunk, line_chunk = 48, 16 + starts = zip(range(0, len(edge_groups), edge_chunk), range(0, len(line_groups), line_chunk), strict=True) + for shard, (edge, line) in enumerate(starts): + directory = f'engine-{shard}' + groups = edge_groups[edge:edge+edge_chunk] + line_groups[line:line+line_chunk] + write_vectors(a.root, a.out/directory, groups) + shards.append(dict(directory=directory, vectors=min(16*edge_chunk, len(cases)-16*edge), + line_segments=min(16*line_chunk, len(segments)-16*line))) (a.out/'oracle.json').write_text(json.dumps(dict(revision=PIN,library_sha256=digest(library),host_sha256=hashlib.sha256(HOST.encode()).hexdigest(), lines_host_sha256=hashlib.sha256(LINES_HOST.encode()).hexdigest(), vectors=len(cases),line_segments=len(segments),mixed_decision_segments=mixed,depths=[8,10,12],filter_lengths=[4,6,8,14],native_orientations=['horizontal','vertical'],coverage=counts, + engine_shards=shards, scope='standalone edge masks/narrow/wide math, one line and four-line segments; no frame boundary/threshold scheduling or encoded-frame conformance'),indent=2)+'\n') print(f'Pinned deblock oracle: {len(cases)} vectors, {len(segments)} four-line segments ({mixed} with mixed line decisions), ' f'horizontal/vertical parity, {counts["changed_samples"]} changed samples') diff --git a/ziran.lock b/ziran.lock index 8231788..afd92a8 100644 --- a/ziran.lock +++ b/ziran.lock @@ -1,6 +1,15 @@ { "format": 1, "packages": [ + { + "commit": "3017f0d1cc56975aa29c600a1f55a62e1193cf7b", + "checked": {"profile": "default", "sha256": "977dd8832c2c660bbc40ff20d704557939cccf6818c29b3d9402d737590d869d", "toolchain": "pb6607dd27b15499f"}, + "dependencies": {}, + "id": "p115fee450ee07367", + "name": "image-codec", + "ref": "3017f0d1cc56975aa29c600a1f55a62e1193cf7b", + "url": "https://git.waozi.xyz/ziranlang/image-codec.git" + }, { "commit": "b141d9c45db236335351c8a4b2a14b3ade27c937", "dependencies": {}, @@ -18,24 +27,16 @@ "ref": "54706fc6bc0cdecab7e9593974a4039cc038fca7", "source": true, "url": "https://github.com/videolan/dav1d.git" - }, - { - "commit": "360553150917f352e49e1beb78917370792ccaef", - "dependencies": {}, - "id": "pfe0d72921e7aaac1", - "name": "image-codec", - "ref": "360553150917f352e49e1beb78917370792ccaef", - "url": "https://git.waozi.xyz/ziranlang/image-codec.git" } ], "root": { - "dependencies": {"dav1d": "pb24767b9eb34d961", "image_codec": "pfe0d72921e7aaac1", "libaom": "p74ab3cb6eb3b86fa"}, - "dependency_refs": {"dav1d": "54706fc6bc0cdecab7e9593974a4039cc038fca7", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937"}, + "dependencies": {"dav1d": "pb24767b9eb34d961", "image_codec": "p115fee450ee07367", "libaom": "p74ab3cb6eb3b86fa"}, + "dependency_refs": {"dav1d": "54706fc6bc0cdecab7e9593974a4039cc038fca7", "image_codec": "3017f0d1cc56975aa29c600a1f55a62e1193cf7b", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937"}, "name": "av1" }, "toolchain": { - "commit": "9f42cf010fc48bec9fa8aa87d928fe032f2c719f", - "ref": "9f42cf010fc48bec9fa8aa87d928fe032f2c719f", + "commit": "5db3d3c39425d5dfe4bfe4a84e6f6e2e5a810ad0", + "ref": "5db3d3c39425d5dfe4bfe4a84e6f6e2e5a810ad0", "url": "https://git.waozi.xyz/ziranlang/ziran.git" } } diff --git a/ziran.toml b/ziran.toml index e10d5ac..dfbc5bf 100644 --- a/ziran.toml +++ b/ziran.toml @@ -4,14 +4,14 @@ module_roots = ["src"] [toolchain] git = "https://git.waozi.xyz/ziranlang/ziran.git" -ref = "9f42cf010fc48bec9fa8aa87d928fe032f2c719f" +ref = "5db3d3c39425d5dfe4bfe4a84e6f6e2e5a810ad0" [exports] decoder = "src/decoder.zi" [dependencies.image_codec] git = "https://git.waozi.xyz/ziranlang/image-codec.git" -ref = "360553150917f352e49e1beb78917370792ccaef" +ref = "3017f0d1cc56975aa29c600a1f55a62e1193cf7b" # Test oracle only; no product module links it. [dependencies.libaom]