Skip to content

TEST: run a stratified sample of translated mesh shaders natively and as --mesh-compute and compare (vk_ms_sweep, tools/ms_sweep.py) - #5072

Open
kapitche wants to merge 6 commits into
ItsNotRexed:mainfrom
kapitche:ms-sweep
Open

kapitche wants to merge 6 commits into
ItsNotRexed:mainfrom
kapitche:ms-sweep

Conversation

@kapitche

Copy link
Copy Markdown
Contributor

Item: TEST, claimed at #4816 (comment). Depends on: #4628 (this branch is on top of it, so the diff shows its commits too; only the last one is new). Related: #4408/#2124 (--mesh-compute runtime and checker), #4815 (one real drawable through ms_pass).

What. The 9,282 mesh shaders had been translated, validated and pipeline-checked but never run on real shaders. vk_ms_sweep runs one translated mesh shader on the runtime's bindless set 0 (zero heap, four workgroups) with a generated pixel shader: native as a VK_EXT_mesh_shader shader (counting MESH_PRIMITIVES_GENERATED) or compute as the --mesh-compute fallback (dispatch, then the mesh_compute_vs.py draw). tools/ms_sweep.py samples (fixed seed, strata: size tercile, params, reads buffers, clip distances), translates both ways, runs, and compares the native primitive count and image with the fallback's slots and image. It also reads NaN/Inf positions and out-of-range indices from the slots. Docs: a section in docs/shader_validation.md.

Results (RTX 4080 SUPER, Windows 11, 400 of 9,282, seed 20261123): 399 agree (native primitive count equals the fallback's, images agree), 1 device lost in both modes (a null pointer read from the zero heap). 10 shaders have a NaN/Inf position from zero input, and native agrees with the fallback on them. Pipeline creation is slow: native median 3.4 s, 15 over 30 s (up to ~60 s). Limit: any data in the heap is read as a wild address, so the heap stays zero and the geometry is 1 to 2 primitives per workgroup.

Tests. python -I tests/test_ms_sweep.py (19 tests: sampling, slot analysis, image comparison, classification; Python 3.12.16, Linux), no GPU or game data. Windows build with MSVC 14.51, run on the RTX 4080 SUPER. Negative control: the new files do not exist on main, so the test fails there with ModuleNotFoundError.

AI-assisted.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked 5f6bb485f on Windows 11 (Vulkan SDK 1.4.363):

  • pytest tests/test_ms_sweep.py -q -> 19 passed.
  • vk_ms_sweep builds and links; with no arguments it prints usage: vk_ms_sweep native|compute ms.spv ps.spv vs.spv|- out.raw width height groups slot-bytes max-prims (exit 2).
  • git diff --check clean.

The new tools/ms_sweep.py + vk_ms_sweep worker samples translated mesh shaders, draws each natively and again as --mesh-compute, and compares the two readbacks; the Python tests cover the stratified picker, the run/compare modes and the empty-sample cases. I did not run the sweep itself, which needs a translated mesh-shader corpus and an ms.spv/PS/VS set this host does not keep. No blocking issue from build + unit evidence.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The sample can contain fewer shaders than requested. At 5f6bb485, the CLI returns 18 for --n 19 with 60 candidates. The three groups each contain 20 candidates. The minimum of six per group leaves room for the nineteenth sample.

Run this synthetic check from the checkout with py -3.14 -I repro.py:

import json, subprocess, sys, tempfile
from pathlib import Path

with tempfile.TemporaryDirectory() as tmp:
    d = Path(tmp)
    names = [f"m{i:02}" for i in range(60)]
    (d / "index.json").write_text(json.dumps({k: {"stage": "ms"} for k in names}))
    for i, k in enumerate(names):
        (d / (k + ".spv")).write_bytes(bytes(i + 1))
    subprocess.run([sys.executable, "-I", "tools/ms_sweep.py", "sample",
                    "--spirv", tmp, "--out", str(d / "out"), "--n", "19"], check=True)

Actual: population 60 strata 3 sampled 18. Expected: 19 distinct shaders. The sampling step only reads the file sizes. Each group rounds its allocation down to six. The adjustment loop removes excess samples but does not assign the remaining slot. Assign the remainder to a group with capacity and use a stable tie order.

All 19 existing tests pass. AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A truncated native readback can produce a false agreement at 5f6bb485. I reproduced this on Windows with the following script. Run it from the checkout with py -3.14 -I repro.py.

import struct, sys, tempfile
from pathlib import Path
sys.path.insert(0, str(Path.cwd() / "tools"))
import ms_sweep as m

with tempfile.TemporaryDirectory() as tmp:
    n, c = Path(tmp) / "native.raw", Path(tmp) / "fallback.raw"
    n.write_bytes(b"")
    c.write_bytes(struct.pack("<4f", 1, 0, 0, 1) * 4096 + bytes(32))
    layout = {"slot_bytes": 8, "max_vertices": 0, "max_primitives": 0}
    print(m.classify({"code": 0, "prims": 0}, {"code": 0}, n, c, layout))

Actual: agree, empty, with cov_fallback=0 and diff_px=0. The fallback image has 4,096 pixels with alpha 1. The native file contains no pixels. The comparison loop takes its length from the native file, so it checks neither image.

Expected: report a short native output. Check the native image size against W * H * 16 before comparing pixels. A short-read regression can use the same input without a GPU.

AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Partial independent check (Windows 11, RTX 5090, Vulkan SDK 1.4.363, MSVC 19.44):

  • python -I tests/test_ms_sweep.py → 19/19, tests/test_ps_sweep.py → 13/13
  • vk_ms_sweep + vk_ps_sweep build clean; --help usage verified; glslangValidator.exe present in the SDK
  • The real sample → translate → run(native|compute) → compare sweep needs a translated-shader corpus — one is being produced here (~23k/26.5k done); I can run a stratified slice on the RTX 5090 (native VK_EXT_mesh_shader + --mesh-compute fallback) once it lands and report agreement counts

AI-assisted (Devin); no game data.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At 5f6bb48, two independent executions of the same cached mesh entry on Windows RTX 5070 (616.56) gave four native primitives across four workgroups and zero vertices/primitives in every lowered header. The 64×64 images matched byte for byte. Both translations validated and both captures had the expected lengths. This confirms a reproducible count mismatch for one entry with the zero heap. Its cause and corpus frequency remain open. AI-assisted.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up at 5f6bb48 on Windows RTX 5070, driver 616.56. Four workgroups emitting zero primitives gave native query 0. Four groups emitting one triangle each gave query 4 and 512 covered pixels. Direct compute-slot controls returned the expected (0,0) and (3,1) per group, with matching image coverage and full capture lengths. This rules out the query merely counting workgroups here. The two cached translated runs still differ, native 4 versus lowered 0 with clear images. The direct compute control bypasses translation, so the responsible path and whether it actually wrote the cleared headers remain unresolved. AI-assisted.

@rgvxsthi

Copy link
Copy Markdown
Contributor

Tested 5f6bb48 merged onto main 3faac55 on AMD Radeon RX 9070 (RDNA4, subgroup 32..64), Windows 11 Pro 26300, MSVC 19.44, RelWithDebInfo, using the translated corpus from a local game build (9,282 mesh shaders), seed 20261123, --n 400, --timeout 180: pass.

  • sample: 400 sampled; translate: 400/400 translated and spirv-val clean both plain and --mesh-compute.
  • run --mode native: 400/400 exit 0. run --mode compute: 400/400 exit 0.
  • compare: 400 agree (geometry), 10 of them with a NaN/Inf position. The fallback's slots: 2,362 vertices, 2,362 primitives, 40 bad positions, 0 bad indices, 0 over the maxima.
  • Compared with the RTX 4080 SUPER table in docs/shader_validation.md (assuming the same sample, since the seed and corpus size match): the shader that lost the device there in both modes runs on AMD. The extra 4 vertices and primitives over NVIDIA's 2,358 fit one more shader emitting one primitive per workgroup. Same 10 NaN/Inf shaders and 40 positions.
  • Time per shader, process start included (a pipeline-cache build was running alongside, so these are upper bounds): native median 1.3 s, p90 13 s, max 83 s, 18 over 30 s. Compute median 1.1 s, p90 11 s, max 46 s, 11 over 30 s. Nothing timed out at 180 s.

I didn't check @shdann's two findings (short sample, truncated readback). This run got the full 400, and every record exited 0.

AI-assisted (Claude Code).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 5f6bb48 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_ms_sweep.py -> Ran 19 tests, OK; python -I tests/test_ps_sweep.py -> Ran 13 tests, OK
  • vk_*_sweep.cpp parts untested (no compiler, no GPU).

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted CPU follow-up at 5f6bb48. The zero-heap trace predicts (1,0) before dummy allocation, then (1,1) per group. That explains how the native query can return four primitives with a blank frame. Across both emitted modules, 5,492 instructions before the epilogue barrier matched after ID and constant normalization. Both epilogues read and unpack the same workgroup allocation word. The lowered path assigns local invocation zero to write the two counts at slot offsets 0 and 4. Both modules validated for Vulkan 1.3. Static analysis cannot determine whether the observed (0,0) headers were written or remained at their initial values. Prefilling the headers with nonzero sentinels would distinguish those outcomes.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted sentinel follow-up at 5f6bb48, Windows RTX 5070 driver 616.56. In three compute-only dispatches, all four 4,112-byte slots ended with (0,0) in place of their distinct nonzero header sentinels. Skipping dispatch preserved all eight words. A synthetic compute writer replaced all eight with the expected per-slot values. No draw ran in the compute tests. The native rerun generated four primitives. This rules out untouched headers for this cached module and input. The readback establishes final values only; the responsible instruction and intermediate values remain unresolved.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Head: 5f6bb48 (matches refs/pull/5072/head). Base d0835dd, 5 commits behind current main 3faac55; the PR delta is 8 files. Windows 11, RTX 5070 Ti, VS 2022 build of this head.

  • git diff --check 3faac557b...HEAD: clean.
  • python -I -m pytest tests/test_ms_sweep.py tests/test_ps_sweep.py -q: 32 passed (19 ms + 13 ps).
  • Direct CI-style runs: python -I tests/test_ms_sweep.py -> Ran 19, OK; python -I tests/test_ps_sweep.py -> Ran 13, OK.
  • python -I tools/ms_sweep.py --help: exit 0.
  • tests/test_ci_registration.py: 8 passed.
  • vk_ms_sweep builds (cmake --build ... --target vk_ms_sweep: no work to do), and LEONIDA_VK_DEVICE works: with LEONIDA_VK_DEVICE='RTX 5070 Ti' it prints device: NVIDIA GeForce RTX 5070 Ti through vk_device_pick.h. ms_sweep.py run inherits the environment, so there is no device-selection blocker.

Not run: the sweep itself. It needs a translated corpus (--spirv with index.json + .spv; the PR used 9,282 mesh entries from game data) and, for translate, <work>/shaders with dxbc/index.json, .dxbc, disasm/*.s and optional fssm/*.fssm. Neither is present here; glslangValidator (Vulkan SDK 1.4.363.0) and the built worker are.

Scope/duplicate: this branch contains open PR #4628's head 8a2644f. Its three ps_sweep files are byte-identical to #4628; only the five ms_sweep files (plus CMake/docs lines) are new here. Please state the dependency, or rebase after #4628 lands, so this is not merged as an 8-file PR carrying unreviewed #4628 work (#4847 also builds on ps_sweep).

Merge-tree: clean against #2124, #1712 and #3777 (the shared runtime/CMakeLists.txt line merges without conflict).

Verdict: tests and tooling are sound; not merge-ready until the #4628 dependency is resolved, and the end-to-end sweep is unverified here.

…t native capture is a failure, not an agreement
@kapitche

Copy link
Copy Markdown
Contributor Author

@shdann both reports were real; fixed in 91d8d21c5.

  1. Short sample (--n 19, 60 candidates, 3 strata). Rounding each stratum's share down left 18 and nothing handed out the remainder. pick_sample now gives each missing slot to the stratum furthest below its proportional share that still has candidates, with the sorted stratum key breaking ties. Your script now prints population 60 strata 3 sampled 19 (7/6/6). New test test_the_sample_has_n_shaders_when_the_population_holds_n checks n = 18, 19, 20, 21, 59, 60 give n distinct shaders, that n above the population gives all 60, and that the allocation is repeatable.
  2. Truncated native readback. classify took the pixel count from the native file, so an empty native capture read as agree, empty. It now requires both captures to hold W*H*16 bytes and reports fails in one mode, native: short output (or fallback: short output). New test test_a_short_capture_is_a_failure_not_an_agreement covers an empty native file, one 4 bytes short, and a short fallback file.

Run: Linux, Python 3.12.16, python -I tests/test_ms_sweep.py: 21 tests OK (19 before). Negative control: with the previous tools/ms_sweep.py the two new tests fail and the other 19 pass. C++ untouched.

@kapitche

Copy link
Copy Markdown
Contributor Author

@kacper-serewis thanks for the sentinel and zero-heap follow-ups. I could not find the cause, and here is what I checked.

I do not have your cached entry (and a synthetic module cannot be made to take the same zero-heap path), so this is a read of the generator, not a reproduction. In tools/rdna2spirv.py both paths get their counts from the same place: s_sendmsg MSG_GS_ALLOC_REQ stores M0 into the workgroup word %ms_counts, and after the barrier both epilogues load it, unpack vertices from bits 0..11 and primitives from bits 12..23, and clamp each with UMin against the module's max. Native passes the result to OpSetMeshOutputsEXT; --mesh-compute lane 0 stores it at slot offsets 0 and 4. So a (0,0) header with a native query of 4 means the %ms_counts word read 0 in the lowered run, and the clamp cannot make it 0 unless the max is 0. Your sentinel run rules out untouched headers, which fits that. It leaves two candidates I cannot tell apart from here: the ALLOC_REQ store not executing, or executing with a different M0, in the compute dispatch for that entry (for example under a different EXEC mask). Telling them apart needs the raw word stored before the clamp; I have not added that.

The sweep already surfaces this as primitive count differs, so no change to the PR for it. If you can share the entry's index name (not content) and its ms metadata (max vertices, max primitives), I can check the clamp bound.

@thehollowedone

Copy link
Copy Markdown

Independent Windows CPU check of 91d8d21c, with GitHub base 3faac557. Windows x64, CPython 3.12.8.

  • Stock test_ms_sweep.py and test_ps_sweep.py passed all 34 tests.
  • With 60 synthetic candidates in three strata and --n 19, the previous head 5f6bb485 selected 18. The current head selected 19 unique entries (7/6/6). Repeating with the same seed produced byte-identical output.
  • The comparison CLI agreed on complete synthetic captures when the native image had a trailing byte. It reported native: short output for a truncated native image and fallback: short output for truncated fallback slots.

Synthetic CPU fixtures only. AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants