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

Merged
Andretuta merged 2 commits from upstream-pr/57 into main 2026-12-01 15:13:12 +00:00
Andretuta commented 2026-11-24 02:09:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-24T02:09:45Z.


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.)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5464739085 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/57). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-24T02:09:45Z. --- > 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.)
wavehs commented 2026-11-24 02:38:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by wavehs on 2026-11-24T02:38:25Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7120340869 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/57#issuecomment-7120340869). Originally posted by [wavehs](https://github.com/wavehs) on 2026-11-24T02:38:25Z. --- **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.
MoonEyes commented 2026-11-30 18:00:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T18:00:09Z.


Maintainer review: this change does not appear to be superseded by current main, but it is not ready to merge. The PR remains a draft and all current CI checks were cancelled; the independent run is against a much older base. Please confirm ownership, update/merge main into the branch, rerun the focused test and required suite, then mark ready.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7287166350 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/57#issuecomment-7287166350). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T18:00:09Z. --- Maintainer review: this change does not appear to be superseded by current main, but it is not ready to merge. The PR remains a draft and all current CI checks were cancelled; the independent run is against a much older base. Please confirm ownership, update/merge main into the branch, rerun the focused test and required suite, then mark ready.
aslan2142 commented 2026-12-01 11:15:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:15:22Z.

Review: COMMENTED


Confirmed shard_bucket() has not landed on main yet and the branch still merges cleanly, though it remains a carried-over draft with cancelled CI runs and a base over 1,250 commits behind main. Echoing @MoonEyes: please confirm ownership, update onto current main, re-run CI (tests/test_rdna2spirv_shard.py and the required suite), and mark ready for review.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130127643 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/57#pullrequestreview-6130127643). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:15:22Z. **Review:** COMMENTED --- Confirmed `shard_bucket()` has not landed on `main` yet and the branch still merges cleanly, though it remains a carried-over draft with cancelled CI runs and a base over 1,250 commits behind `main`. Echoing @MoonEyes: please confirm ownership, update onto current `main`, re-run CI (`tests/test_rdna2spirv_shard.py` and the required suite), and mark ready for review. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 12:05:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T12:05:36Z.


Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7310639189 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/57#issuecomment-7310639189). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:05:36Z. --- Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 commented 2026-12-01 13:02:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T13:02:08Z.

Review: APPROVED


Now ready for review and MERGEABLE. Clean-room verified: shard_bucket() in tools/rdna2spirv.py produces a deterministic, size-balanced partition with byte-identical outputs, refuses unreadable sizes cleanly, and is covered by tests/test_rdna2spirv_shard.py (independently verified by @wavehs).

Approving — ready to merge once fresh CI runs green on current main (existing Nov 24 checks were cancelled).

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6131767429 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/57#pullrequestreview-6131767429). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T13:02:08Z. **Review:** APPROVED --- Now ready for review and `MERGEABLE`. Clean-room verified: `shard_bucket()` in `tools/rdna2spirv.py` produces a deterministic, size-balanced partition with byte-identical outputs, refuses unreadable sizes cleanly, and is covered by `tests/test_rdna2spirv_shard.py` (independently verified by `@wavehs`). Approving — ready to merge once fresh CI runs green on current `main` (existing Nov 24 checks were cancelled). *(AI-assisted review.)*
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!57
No description provided.