tools: --shard balances by file size, so the parallel stage waits for nobody (output byte-identical) #57

Open
github-bot wants to merge 2 commits from github-bot/gh-57 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-24 02:09 UTC · original
ubiquitarianism:old-pr/9779 → main · +115 −3 · 2 files · 2 commits · draft

Carried over from old PR 9779 by pes7 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9779, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
pes7: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

The parallel translation stage waits for its slowest shard, and the split ignores size. This balances the shards on input size; on a real 247-listing run the shards come out level and every output stays byte-identical.

--shard i/n took files[index::count] — round-robin over the name-sorted list, which balances nothing when shaders differ in size. shard_bucket() sorts by file size, largest first, and gives each file to the lightest bucket so far; a shard translates its own bucket. Every shard derives the same partition from the same directory listing, so the processes still do not talk to each other, make_game.py and the --shard i/n interface are unchanged, and each bucket is still handed out in name order.

Measured

Independent run by @nicolasabosch on 247 listings, four --shard i/4 runs plus --merge against one unsharded run:

  • 258 .spv files on both sides, all 258 byte-identical; merge has 247 entries;
  • heaviest of 4 shards by listing bytes: 266,003 with this change against an ideal of 265,966 (round-robin on the same list: 286,904).

And on the real 26,503-listing corpus by @exotiquee: round-robin is already within 2.6% of even at 8 shards and 6.8% at 16, so the honest expected gain there is 0-7%, growing with the shard count; the change levels the tail rather than rescaling the stage. That correction is in this description on purpose — the synthetic corpus I first measured on was skewed far more than the real one, and its 17-26% is not the claim.

A size that cannot be read stops the shard

Reported by @Zhenyettta: with a weight of 0 for one unreadable file, two shards can take the same shader while a third falls out of every bucket. Each shard derives the partition alone, so the one thing that must not happen is a shard placing a file by a number the others do not have. shard_bucket() now raises with the path and the advice to run the shard again.

Tests

tests/test_rdna2spirv_shard.py (5): 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 an unreadable size is refused. --merge and test_rdna2spirv_merge.py are unchanged. Independent runs also confirm resume: make_game.translate's plan includes translator_digest(), so parts from the old bucketing are never merged with new ones.

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: same shaders, translated by a different process.)
**@⁠Andretuta** on GitHub · 2026-11-24 02:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/57) `ubiquitarianism:old-pr/9779` → `main` · +115 −3 · 2 files · 2 commits · draft > Carried over from **old PR 9779** by `pes7` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9779`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `pes7`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): **The parallel translation stage waits for its slowest shard, and the split ignores size.** This balances the shards on input size; on a real 247-listing run the shards come out level and every output stays byte-identical. `--shard i/n` took `files[index::count]` — round-robin over the name-sorted list, which balances nothing when shaders differ in size. `shard_bucket()` sorts by file size, largest first, and gives each file to the lightest bucket so far; a shard translates its own bucket. Every shard derives the same partition from the same directory listing, so the processes still do not talk to each other, `make_game.py` and the `--shard i/n` interface are unchanged, and each bucket is still handed out in name order. ### Measured Independent run by `@nicolasabosch` on 247 listings, four `--shard i/4` runs plus `--merge` against one unsharded run: - **258 `.spv` files on both sides, all 258 byte-identical**; merge has 247 entries; - heaviest of 4 shards by listing bytes: **266,003** with this change against an ideal of 265,966 (round-robin on the same list: 286,904). And on the real 26,503-listing corpus by `@exotiquee`: round-robin is already within 2.6% of even at 8 shards and 6.8% at 16, so the honest expected gain there is **0-7%**, growing with the shard count; the change levels the tail rather than rescaling the stage. That correction is in this description on purpose — the synthetic corpus I first measured on was skewed far more than the real one, and its 17-26% is not the claim. ### A size that cannot be read stops the shard Reported by `@Zhenyettta`: with a weight of 0 for one unreadable file, two shards can take the same shader while a third falls out of every bucket. Each shard derives the partition alone, so the one thing that must not happen is a shard placing a file by a number the others do not have. `shard_bucket()` now raises with the path and the advice to run the shard again. ### Tests `tests/test_rdna2spirv_shard.py` (5): 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 an unreadable size is refused. `--merge` and `test_rdna2spirv_merge.py` are unchanged. Independent runs also confirm resume: `make_game.translate`'s plan includes `translator_digest()`, so parts from the old bucketing are never merged with new ones. cc `@ItsNotRexed` `@KoertLichtendonk` ### Checklist - [x] 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: same shaders, 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.
Reported by @Zhenyettta on #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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:42:38 +00:00
Author
Owner

@⁠wavehs on GitHub · 2026-11-24 02:38 UTC · original

macOS arm64 verification (Apple M3 Pro) — the size-balanced partition is deterministic, measurably better on this repo's own listings, and the tests bite

Head ff80152d2 (2 commits, branch base 1cc6e3c12: 1,250 commits behind main), merged onto main (9ec34cdff) as c6b46bfe4 cleanly. 2 files +115/−3. Host: macOS 27.2.1, Apple M3 Pro (arm64), CPython 3.15.2, llvm-mc 23.1.5, MoltenVK 1.4.0, KosmicKrisp (Mesa 26.3.0).

Because the branch predates a lot of main, I checked that the merge did not smuggle anything back: git diff ubi/main c6b46bfe4 -- tools/rdna2spirv.py is exactly the new shard_bucket, the two-line caller change and the help text — main's 1,250 later commits are intact in the merged tree.

Check Merged c6b46bfe4 main 9ec34cdff
tests/test_rdna2spirv_shard.py 5/5 OK file absent; the same file against main's rdna2spirv.py: 5 errors
scripts/run_semantic_tests.py tests/semantic 463 files, 6,075 tests, 2 failed 463 / 6,075 / 2
the two failing files test_exec_computed, test_image_atomics_guard the same two
run_gpu_tests.py all cases, MoltenVK 182/248 passed, 61 skipped 182/248, 61 skipped
run_gpu_tests.py all cases, KosmicKrisp 177/248 passed, 64 skipped 177/248, 64 skipped

The GPU rows are identical to main line for line on both drivers after normalising work paths, as expected for a partitioning change; the disk build from the previous merge is reused, the translator is this merge's.

Negative controls, two of them. (1) Main's rdna2spirv.py with the new test file: 5 errors, all AttributeError: module 'rdna2spirv' has no attribute 'shard_bucket'. (2) That only proves the name exists, so I substituted the old files[index::count] into the same assertions the test makes, on its own skewed corpus: the size-based span is 6000 at 2, 4 and 8 shards, the round-robin span is 6300/6150/6120, and the test's assertLess(span, round_robin) fails for the round-robin version at every shard count. So the test catches a regression to the old split, not just a missing function.

Measured on real listings, not the synthetic ones. The repo's 104 semantic fixture listings (247,985 bytes, largest 9.1% of the total — close to the PR's 11% case):

shards   size-based max    spread    round-robin max   spread    heavy-bucket vs round-robin
  2        123,994         1.00x       142,616          1.35x          0.87x
  4         62,127         1.00x        86,040          1.70x          0.72x
  8         31,021         1.00x        45,548          2.14x          0.68x
 12         22,484         1.10x        50,931          8.49x          0.44x

At 12 shards the old split gives one process the 22,484-byte listing alone and another 6,000 bytes, which is exactly the "everyone waits for the biggest shard" case the PR describes; the new one is within 10%. The PR's 1.18x/1.30x at 4/8 shards is the same direction and magnitude.

Wiring checked end to end: tools/rdna2spirv.py tests/semantic/fixtures/disasm --out … --shard i/4 --limit 40 reports 9, 10, 10 and 11 shaders. That is exactly shard_bucket's buckets over the same 40 files (18,572 / 18,449 / 18,270 / 18,393 bytes), where files[i::4] would be 10/10/10/10 — so the call site really uses the partition, not only the function. (Those fixtures then fail to translate for an unrelated, expected reason: they have no FSSM or shader container, so I am quoting the counts, not translations.)

The code is right for the way shards run. Every process re-lists the directory, sorts by (-size, path), and breaks load ties by bucket index, so the partitions agree without the processes talking; the returned bucket is sorted again, keeping "handed out in name order". min(range(count), …) per file is O(files × shards): 26,503 × 12 is nothing next to the translation. Raising OSError instead of a 0 weight is the right call given that a disagreement silently produces a corpus with a file twice and one missing — and the message names the file and says to retry. One limit worth knowing: the CLI cannot easily reach that path (a dangling .s symlink is dropped by the file discovery before the partition), so the unit test is the whole cover; that is fine, not a defect.

Two small notes, neither blocking:

  • --limit is still applied before the partition (files = files[:a.limit] then shard), so --limit N --shard i/n means "the first N listings, split", which is what it meant before. Worth keeping that sentence in mind if the flag's help is ever expanded.
  • The wall-time evidence is synthetic. Now that the partition targets the real corpus, one measured make_game.py run at 4 and 8 shards before/after (the 80-minute figure in docs/pipeline_check.md:450) would turn the 1.18x/1.30x into a number from the 26k corpus.

No game files. AI-assisted.

**@⁠wavehs** on GitHub · 2026-11-24 02:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/57#issuecomment-7120340869) **macOS arm64 verification (Apple M3 Pro) — the size-balanced partition is deterministic, measurably better on this repo's own listings, and the tests bite** Head `ff80152d2` (2 commits, branch base `1cc6e3c12`: 1,250 commits behind `main`), merged onto `main` (`9ec34cdff`) as `c6b46bfe4` cleanly. 2 files +115/−3. Host: macOS 27.2.1, Apple M3 Pro (arm64), CPython 3.15.2, llvm-mc 23.1.5, MoltenVK 1.4.0, KosmicKrisp (Mesa 26.3.0). Because the branch predates a lot of `main`, I checked that the merge did not smuggle anything back: `git diff ubi/main c6b46bfe4 -- tools/rdna2spirv.py` is **exactly** the new `shard_bucket`, the two-line caller change and the help text — main's 1,250 later commits are intact in the merged tree. | Check | Merged `c6b46bfe4` | `main` `9ec34cdff` | |---|---|---| | `tests/test_rdna2spirv_shard.py` | **5/5 OK** | file absent; the same file against main's `rdna2spirv.py`: **5 errors** | | `scripts/run_semantic_tests.py tests/semantic` | **463 files, 6,075 tests, 2 failed** | 463 / 6,075 / 2 | | the two failing files | `test_exec_computed`, `test_image_atomics_guard` | the same two | | `run_gpu_tests.py` all cases, MoltenVK | **182/248 passed, 61 skipped** | 182/248, 61 skipped | | `run_gpu_tests.py` all cases, KosmicKrisp | **177/248 passed, 64 skipped** | 177/248, 64 skipped | The GPU rows are identical to `main` line for line on both drivers after normalising work paths, as expected for a partitioning change; the disk build from the previous merge is reused, the translator is this merge's. **Negative controls, two of them.** (1) Main's `rdna2spirv.py` with the new test file: 5 errors, all `AttributeError: module 'rdna2spirv' has no attribute 'shard_bucket'`. (2) That only proves the name exists, so I substituted the old `files[index::count]` into the same assertions the test makes, on its own skewed corpus: the size-based span is 6000 at 2, 4 and 8 shards, the round-robin span is 6300/6150/6120, and the test's `assertLess(span, round_robin)` fails for the round-robin version at every shard count. So the test catches a regression to the old split, not just a missing function. **Measured on real listings, not the synthetic ones.** The repo's 104 semantic fixture listings (247,985 bytes, largest 9.1% of the total — close to the PR's 11% case): ``` shards size-based max spread round-robin max spread heavy-bucket vs round-robin 2 123,994 1.00x 142,616 1.35x 0.87x 4 62,127 1.00x 86,040 1.70x 0.72x 8 31,021 1.00x 45,548 2.14x 0.68x 12 22,484 1.10x 50,931 8.49x 0.44x ``` At 12 shards the old split gives one process the 22,484-byte listing alone and another 6,000 bytes, which is exactly the "everyone waits for the biggest shard" case the PR describes; the new one is within 10%. The PR's 1.18x/1.30x at 4/8 shards is the same direction and magnitude. **Wiring checked end to end:** `tools/rdna2spirv.py tests/semantic/fixtures/disasm --out … --shard i/4 --limit 40` reports 9, 10, 10 and 11 shaders. That is exactly `shard_bucket`'s buckets over the same 40 files (18,572 / 18,449 / 18,270 / 18,393 bytes), where `files[i::4]` would be 10/10/10/10 — so the call site really uses the partition, not only the function. (Those fixtures then fail to translate for an unrelated, expected reason: they have no FSSM or shader container, so I am quoting the counts, not translations.) **The code is right for the way shards run.** Every process re-lists the directory, sorts by `(-size, path)`, and breaks load ties by bucket index, so the partitions agree without the processes talking; the returned bucket is sorted again, keeping "handed out in name order". `min(range(count), …)` per file is O(files × shards): 26,503 × 12 is nothing next to the translation. Raising `OSError` instead of a 0 weight is the right call given that a disagreement silently produces a corpus with a file twice and one missing — and the message names the file and says to retry. One limit worth knowing: the CLI cannot easily reach that path (a dangling `.s` symlink is dropped by the file discovery before the partition), so the unit test is the whole cover; that is fine, not a defect. Two small notes, neither blocking: - `--limit` is still applied before the partition (`files = files[:a.limit]` then shard), so `--limit N --shard i/n` means "the first N listings, split", which is what it meant before. Worth keeping that sentence in mind if the flag's help is ever expanded. - The wall-time evidence is synthetic. Now that the partition targets the real corpus, one measured `make_game.py` run at 4 and 8 shards before/after (the 80-minute figure in `docs/pipeline_check.md:450`) would turn the 1.18x/1.30x into a number from the 26k corpus. No game files. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/57/head:github-bot/gh-57'
git switch 'github-bot/gh-57'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-57'
git switch 'github-bot/gh-57'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-57'
git switch 'github-bot/gh-57'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-57'
git switch 'main'
git merge --squash 'github-bot/gh-57'
git switch 'main'
git merge --ff-only 'github-bot/gh-57'
git switch 'main'
git merge 'github-bot/gh-57'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.