Skip to content

tools: --shard balances by size instead of taking every n-th shader - #7479

Open
pes7 wants to merge 2 commits into
ItsNotRexed:mainfrom
pes7:perf/shard-balance
Open

pes7 wants to merge 2 commits into
ItsNotRexed:mainfrom
pes7:perf/shard-balance

Conversation

@pes7

@pes7 pes7 commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue: #7049

What and why

--shard i/n took files[index::count]. Shaders differ in size by two orders of magnitude (a mesh or geometry shader
against a small pixel shader), so a round-robin split can hand one shard most of the work, and a parallel stage is as
slow as its heaviest shard — the other processes finish and idle.

shard_bucket() sorts the listing by file size, largest first, and gives each file to the lightest bucket so far; a
shard then translates its own bucket. Every shard computes the same partition from the same directory listing, so this
needs no coordination between processes and no change to make_game.py or to the --shard i/n interface, and each
bucket is still handed out in name order.

Numbers

On the real listing set (@exotiquee, 26,503 .s files, 3,533 MiB, sizes from os.path.getsize, shard_bucket
imported from this head; counts only, no game data):

shards files[i::n] max/mean files[i::n] min/mean shard_bucket max/mean
4 1.008 0.982 1.000
8 1.026 0.968 1.000
16 1.068 0.927 1.000

The largest listing is 0.20% of the total (143x the median), so round-robin over the name-sorted list is already
within 2.6% of even at 8 shards and 6.8% at 16
— that, not the synthetic number below, is the upper bound of what
this can buy, as far as listing size predicts translation time. The split is exact in either case (every listing in one
bucket, none twice) and levels the bytes, so the change removes the tail rather than rescaling the stage.

My synthetic corpus was skewed far more than the real one, which is why its 1.17x / 1.18x / 1.26x is not the claim:

shards round-robin shard_bucket (synthetic, largest 10% of the total)
4 1.28 s 1.09 s
8 0.65 s 0.55 s
16 0.53 s 0.42 s

Kept for the case the change is written for — one shard collecting most of the work — but the real corpus is not that
case, and the honest expected gain there is 0-7%, growing with the shard count. If the maintainer prefers not to
carry a change for that, closing this is the right call and the measurement above is the reason.

Equivalence and tests

  • tests/test_rdna2spirv_shard.py (new, 5 tests): the buckets are disjoint and complete for 1/2/3/4/8/16 shards, the
    same partition in every process, name order inside a bucket, a skewed listing beats files[::n] at 2/4/8 shards, one
    shard takes everything, and a file whose size cannot be read stops the shard with its path in the message instead
    of being placed by a guessed weight (@Zhenyettta's finding, below).
  • A size that cannot be read is not a weight of 0: each shard derives the partition alone, so a shard that guessed
    would place a file by a number the others do not have, take a file another shard already took, and leave a third one
    out of every bucket. shard_bucket() raises instead, and the run is retried.
  • tests/test_rdna2spirv_merge.py and the --merge path are unchanged: parts are still per shard index, and the merge
    does not depend on which shard translated which shader.
  • python -m compileall -q tools tests clean.
  • --limit still applies before the partition, so a limited run keeps its old meaning.

I could not run --shard end to end here: the CLI validates with spirv-as/spirv-val, which this machine does not
have, so the numbers above come from the translator's own translate() on the same files the buckets would receive.

cc @ItsNotRexed @KoertLichtendonk.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md. (Not applicable: no translation change; the
    same shaders are translated, by a different process.)

files[index::count] balanced nothing. Shaders differ in size by two orders of magnitude, so a round-robin split can
hand one shard most of the work while every other process waits for it, and the stage is as slow as its heaviest
shard.

shard_bucket() sorts by file size (largest first) and gives each file to the lightest bucket so far, then a shard
takes its own bucket. Every shard computes the same partition from the same directory listing, so the processes still
do not talk to each other, and each bucket is handed out in name order.

Measured on a synthetic corpus shaped like the real one (96 shaders of 200 instructions, 16 of 2,000, 6 of 12,000;
largest 10% of the total; per-file time measured, then makespan over the buckets):

    shards   round-robin   shard_bucket   gain
         2         2.15s          2.15s   1.00x
         4         1.28s          1.09s   1.17x
         8         0.65s          0.55s   1.18x
        16         0.53s          0.42s   1.26x

The corpus is synthetic: what the real skew is has to be measured with game data, which is why the change keeps the
interface (--shard i/n) and the fallback (a file whose size cannot be read is scheduled, with weight 0) intact.

tests/test_rdna2spirv_shard.py checks the partition (disjoint, complete, deterministic, name order within a bucket),
that a skewed listing beats round-robin at every shard count, and that an unreadable file is not dropped.
@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

Code read + CPU run of 30ade56 on Windows 11, Python 3.11.9: LGTM.

  • shard_bucket is the usual largest-first greedy (LPT): a partition, deterministic (ties broken by name, both in the sort key and in the lightest-bucket choice), name order within a bucket.
  • The one new dependency is that the split now follows file sizes, not only names, so every shard must see the same bytes. make_game's resume keeps that: translate() reuses finished index.part*.json only under the same SHARD_PLAN, and shaders() drops the plan whenever an earlier step reruns, so the disassembly (and every size) is the same whenever old parts are kept. Anyone running --shard by hand while the disasm directory is still being written would get mismatched buckets, but that was already unsafe with files[index::count].
  • tests/test_rdna2spirv_shard.py: OK.

AI-assisted (Claude Code).

@pes7

pes7 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Claimed in the coordination thread (opened in #7049, carried to #7333 and #7496; the tables there are rebuilt from open PRs). The numbers and how they were measured are in the description above.

@exotiquee

Copy link
Copy Markdown

Tested 30ade56 on Windows 11, Python 3.12.10 (CPU only): tests pass, and a measurement over my own 1.0.2.1 corpus (counts only) says the gain there is smaller than on the synthetic corpus in the docstring.

Tests

  • python -I tests/test_rdna2spirv_shard.py: 5 OK. tests/test_rdna2spirv_cli.py: OK (2 skipped). tests/test_rdna2spirv_merge.py: 30 OK.
  • Merges cleanly onto main 86d1c86.

Byte load per shard on the real listings (26,503 .s files, 3,533 MiB; shard_bucket imported from this head, sizes from os.path.getsize)

shards files[i::n] max/mean files[i::n] min/mean shard_bucket max/mean partition exact
4 1.008 0.982 1.000 yes
8 1.026 0.968 1.000 yes
16 1.068 0.927 1.000 yes

The largest listing is 0.20% of the total (143x the median), not 11%, so round-robin over the name-sorted list is already within 2.6% of even at 8 shards and 6.8% at 16. The new split is exact (every listing in one bucket, none twice) and levels the bytes, so the upper bound on the wall-clock gain here is those few percent, as far as listing size predicts translation time; I did not time a translation run. The 1.18x / 1.30x figures in the docstring would be worth labelling as the synthetic worst case.

Read

  • Each process derives the partition from the sizes it sees, so the buckets agree only while the listings don't change between the first and the last shard starting. make_game.py starts them together after disassembly has finished, so that holds there; a hand-run --shard set while shader_disasm.py is still writing could skip or repeat a shader, where files[i::n] depended on the names alone. The except OSError: weight 0 branch has the same property (one process failing a getsize moves files between buckets).

AI-assisted (Claude Code).

@pes7

pes7 commented Nov 23, 2026

Copy link
Copy Markdown
Author

That is exactly the measurement this PR was missing — thank you. I have replaced the numbers in the description with yours, because mine were taken on a synthetic corpus whose skew (largest listing 10% of the total) is nothing like the real one (0.20%, 143x the median). The honest claim is now:

  • real listing set: round-robin is already within 2.6% of even at 8 shards and 6.8% at 16, so the upper bound of the gain there is those few percent, and only if listing size predicts translation time;
  • the synthetic 1.17x / 1.18x / 1.26x stays in the description labelled as the skewed case the change is written for, not as the expected gain.

The partition is exact either way (your "partition exact: yes" at every shard count), the bytes are level at 1.000 of the mean, and a corpus with no skew costs nothing. So the change is a strict improvement with a small denominator — and if it is not worth carrying for 0-7%, closing it is the right call; the measurement above is the reason, and it should stay on the record rather than be argued with.

@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested 30ade56 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass

  • python3 -m pytest tests/test_rdna2spirv_shard.py -q — 5 passed

AI-assisted (OpenCode).

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 30ade56 on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_rdna2spirv_shard.py - all exit 0.

AI-assisted (Claude Code).

@8163coder

Copy link
Copy Markdown

Tested 30ade56 on macOS 15.1 (arm64), Python 3.14.6. All 95 tests passed: shard assignment (5), merge (30), make_game resume (14), and shader-stage resume (46).

I checked the resume path too: the saved plan includes the translator digest, so it drops old shard parts when the partitioning code changes. This was a fixture run, no benchmark on the full corpus.

AI-assisted testing and review.

@Zhenyettta

Copy link
Copy Markdown

Tested the shard_bucket implementation from 30ade563d with the merge routine from 203a348cf on Windows, CPU only. A transient file-size lookup failure in one worker can silently omit an input from the merged index even when all files and their contents stay unchanged.

I used two synthetic files, A (20 bytes) and B (10 bytes), and two workers. With normal size lookups the workers select A and B respectively. I then made only worker 0's getsize(A) raise PermissionError once. Its zero-weight fallback assigns B to worker 0. Worker 1 sees the real sizes and also selects B. I wrote both resulting synthetic index parts and both correct shard-count markers. merge_shard_parts succeeded with B as the only key.

Could a failed size lookup abort that worker before it publishes a part? That would allow a retry once the file is accessible. A regression test can inject a lookup failure in just one worker and require an error rather than a changed partition. No translation or game data was needed. AI-assisted.

…sing 0

Reported by @Zhenyettta on ItsNotRexed#7479: with a weight of 0 for an unreadable file, two shards can take the same shader while
another one falls out of every bucket, and the merge then publishes a set with a duplicate and a hole.

Every shard derives the partition alone from the same listing, so the one thing that must not happen is a shard
placing a file by a number the others do not have. The size lookup now raises with the path and the advice to run the
shard again; a transient failure becomes a retry instead of a silently wrong set.
@pes7

pes7 commented Nov 23, 2026

Copy link
Copy Markdown
Author

You are right, and I took the stricter option you suggested: a size the shard cannot read now stops the run instead of being guessed at as 0.

The reason it has to be the strict one: each shard derives the partition on its own from the same listing, so the invariant is that every shard places every file by the same number. A PermissionError on one side breaks that invariant silently — exactly your two-file example, where worker 0 takes B and worker 1 also takes B, and A is in no bucket at all. With the merge's overlap refusal (#3306) that set would at least fail loudly, but it would fail as "two parts claim B", which points at the wrong thing; failing at the size lookup names the file that could not be read.

OSError: cannot read the size of <path> for the shard partition: <reason>; fix or restore the file and run the shard again

tests/test_rdna2spirv_shard.py now asserts that error and the path in it, instead of the old "an unreadable file is not dropped" case (which encoded the behaviour you found). 5 tests OK; the rest of the partition tests are unchanged.

@NiHuShu

NiHuShu commented Nov 23, 2026

Copy link
Copy Markdown

AI-assisted Linux CLI check at ff80152d2374b9680a082dd7350d5c79fa796969 against merge base 1cc6e3c121969761d9dad8fdec99f19928213973. CachyOS x86-64, CPython 3.14.10. CPU only; GPU not used. Eight tiny synthetic shaders, all translatable. For each fresh output directory I ran python -I tools/rdna2spirv.py <disasm> --out <out> --shard 0/2, then --shard 1/2, then --merge, and read both part indexes and the published index.

With unchanged sources both trees published all 8 names exactly once. I then repeated the experiment eight times, appending only a 28 KB comment to a different listing between the two shard commands. All listings remained readable and the list of filenames stayed constant.

Across 8 between-shard edits Base Head
Part indexes overlap 0/8 8/8
Merge exits 0 but publishes only 7 of 8 names 0/8 4/8

In those four cases the resized input itself disappeared from the merged index. All shard and merge subprocesses exited 0; zero skips. Rerunning both shards after the edit restored 8/8 with no overlap. An unreadable size now stops a shard, but a readable size changing between processes can still silently publish an incomplete index.

@leonidassupporter

Copy link
Copy Markdown

Tested ff80152d2 applied onto main dfe4e4ad3 on NVIDIA GeForce RTX 3060 Ti (Ampere, subgroup size 32), driver 621.14, Windows 11 (26300), Vulkan SDK 1.4.363, llvm-mc 23.1.6 (conda-forge, AMDGPU), harness built with native MSVC 19.44 RelWithDebInfo: pass, no regression.

  • tests/test_rdna2spirv_shard.py: OK.
  • Full scripts/run_gpu_tests.py with this tools/rdna2spirv.py: 192/241, per-test outcome identical to main.

Baseline on main dfe4e4ad3, same machine: scripts/run_gpu_tests.py 192/241 passed, 48 skipped; its one failure, ms_wave_info_s3, is the known one on main (#5741, #5855, #7136).

AI-assisted (Claude Code).

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.

9 participants