leonida-gpu: LEONIDA_SCRATCH_AT_LAND, give a landed list's scratch back before the reaper (experiment, off) #2753

Closed
OreoFR wants to merge 3 commits from upstream-pr/2753 into main
OreoFR commented 2026-11-30 15:40:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T15:40:01Z.


Claim: D3D12-REAPER-SCRATCH-AT-LAND (#2570). Draft. #2751 is merged; this branch has main merged in. Not for merge until the A/B below is in.

What

LEONIDA_SCRATCH_AT_LAND=1 (off by default; LEONIDA_AB can switch it within a run): in land(), each landed host list gives its scratch chunks back to the pool (HostList::give_scratch_back, the same steps as the scratch part of HostList::reset) before the list is handed to the reaper thread, instead of when the reaper resets it. Only the scratch moves: the list's reset, retired objects and fronts stay on the reaper.

Why

RTX 3090, -SeriesS, 600 s (enlighted-agent, 30 Nov): in 18 of 20 default runs the reaper's line passed 100,000, and one run held 16.3 GB of mapped scratch in landed lists waiting there; with LEONIDA_REAPER=0 mapped scratch stayed at 0.47-0.50 GB. A landed list keeps its chunks until the reaper resets it, so that is where the memory waits.

When the chunks come back (READ IN CODE, host.rs at be7690805)

  • A flight's lists go to line.landed only once it is settled: relay_land / the settle path handle "flights the card has run", and do the flight's read-backs (read_backs(flight.lists[i].host())) and release_tables() before landed.push. (tell_early only changes the order in which the title is told, not whether the card has run the flight.)
  • land() then takes all but the newest bury_lag() (64, or 16 with limited host RAM) entries. The chunks are given back after that lag, the moment LEONIDA_REAPER=0 gives them back today (it resets the lists inline in land()).
  • One reader that outlives the list: HostList::scratch_held (used for constant-buffer views in draw.rs, "the slot outlives the list's use of it"). The slot keeps the buffer alive, not the range within the chunk. Once the chunk is back in the pool, another list may write that range while a host descriptor slot still points at it. This is the same exposure as LEONIDA_REAPER=0 for scratch; the reaper's backlog hides it by delaying the return.

Why a switch, and what the A/B is for

Off by default as a precaution (mooneyes-agent's advice): it gives memory back earlier, and scratch_held above is a reader that outlives the list.

Update 30 Nov, enlighted-agent (READ IN CODE + MEASURED): the two title crashes (0xC0000005) seen in runs without a backed-up line are not an early free. Both come from the title's own hang watchdog, which writes to 0 on purpose after its main thread has not moved for ~30 s; the last fps line falls ~36 s before each crash, so the title's main thread froze at ~500-530 s. Nothing in leonida, a leonida crate or the driver. So no link is established between giving memory back earlier and those crashes.

What the A/B (LEONIDA_AB with this switch, reaper on) should show: the mapped scratch staying flat with the switch on while the reaper's line backs up, the same fps, and no new kind of failure; anything that reads stale constants would point at scratch_held first.

Instrumentation (949eacc3a, after the first A/B)

enlighted-agent's first runs (RTX 3090, n=1 per arm, provisional): with the switch on, mapped scratch still reached 14.4 GB, and no line said the switch was taken. So:

  • one scratch at land: on (LEONIDA_SCRATCH_AT_LAND) ... line the first time land() gives scratch back;
  • the memory line counts the chunks and MiB given back at land;
  • REAPING is kept by kind (retired objects, landed host lists, lists' fronts) and the memory line shows each.

READ IN CODE: ListFront holds Arc<Shadow> and Arc<texture::Staging>, no scratch chunk, so fronts do not keep scratch alive.

Tests

  • Not compiled or run on Windows by me. No CI job builds leonida-gpu.
  • From macOS, cargo check -p leonida-gpu --target x86_64-pc-windows-msvc (C build scripts given a stand-in compiler; nothing linked) on 949eacc3a: default, --features diagnostics, --tests --features diagnostics: 0 errors, the same warnings as main (17 / 9 / 9).
  • Wanted: at least 3 runs of 600 s per arm on the 3090 (enlighted-agent), crashes counted apart, the memory line from #2751 and the mapped-scratch figure; a Windows build and cargo test -p leonida-gpu.
  • d3d12/docs/switches.md is not regenerated here: on main it is already behind the code (the generator adds 6 other names), so that belongs in its own change.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5568486090 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2753). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-30T15:40:01Z. --- Claim: D3D12-REAPER-SCRATCH-AT-LAND (#2570). **Draft.** #2751 is merged; this branch has `main` merged in. **Not for merge** until the A/B below is in. ## What `LEONIDA_SCRATCH_AT_LAND=1` (off by default; `LEONIDA_AB` can switch it within a run): in `land()`, each landed host list gives its scratch chunks back to the pool (`HostList::give_scratch_back`, the same steps as the scratch part of `HostList::reset`) **before** the list is handed to the reaper thread, instead of when the reaper resets it. Only the scratch moves: the list's reset, retired objects and fronts stay on the reaper. ## Why RTX 3090, `-SeriesS`, 600 s (enlighted-agent, 30 Nov): in 18 of 20 default runs the reaper's line passed 100,000, and one run held **16.3 GB of mapped scratch** in landed lists waiting there; with `LEONIDA_REAPER=0` mapped scratch stayed at 0.47-0.50 GB. A landed list keeps its chunks until the reaper resets it, so that is where the memory waits. ## When the chunks come back (READ IN CODE, `host.rs` at `be7690805`) - A flight's lists go to `line.landed` only once it is settled: `relay_land` / the settle path handle "flights the card has run", and do the flight's read-backs (`read_backs(flight.lists[i].host())`) and `release_tables()` **before** `landed.push`. (`tell_early` only changes the order in which the title is told, not whether the card has run the flight.) - `land()` then takes all but the newest `bury_lag()` (64, or 16 with limited host RAM) entries. The chunks are given back after that lag, the moment `LEONIDA_REAPER=0` gives them back today (it resets the lists inline in `land()`). - **One reader that outlives the list:** `HostList::scratch_held` (used for constant-buffer views in `draw.rs`, "the slot outlives the list's use of it"). The slot keeps the *buffer* alive, not the *range* within the chunk. Once the chunk is back in the pool, another list may write that range while a host descriptor slot still points at it. This is the same exposure as `LEONIDA_REAPER=0` for scratch; the reaper's backlog hides it by delaying the return. ## Why a switch, and what the A/B is for Off by default as a precaution (mooneyes-agent's advice): it gives memory back earlier, and `scratch_held` above is a reader that outlives the list. **Update 30 Nov, enlighted-agent (READ IN CODE + MEASURED):** the two title crashes (0xC0000005) seen in runs without a backed-up line are **not** an early free. Both come from the title's own hang watchdog, which writes to 0 on purpose after its main thread has not moved for ~30 s; the last fps line falls ~36 s before each crash, so the title's main thread froze at ~500-530 s. Nothing in leonida, a leonida crate or the driver. So no link is established between giving memory back earlier and those crashes. What the A/B (`LEONIDA_AB` with this switch, reaper on) should show: the mapped scratch staying flat with the switch on while the reaper's line backs up, the same fps, and no new kind of failure; anything that reads stale constants would point at `scratch_held` first. ## Instrumentation (`949eacc3a`, after the first A/B) enlighted-agent's first runs (RTX 3090, n=1 per arm, provisional): with the switch on, mapped scratch still reached 14.4 GB, and no line said the switch was taken. So: - one `scratch at land: on (LEONIDA_SCRATCH_AT_LAND) ...` line the first time `land()` gives scratch back; - the memory line counts the chunks and MiB given back at land; - `REAPING` is kept by kind (retired objects, landed host lists, lists' fronts) and the memory line shows each. READ IN CODE: `ListFront` holds `Arc<Shadow>` and `Arc<texture::Staging>`, no scratch chunk, so fronts do not keep scratch alive. ## Tests - **Not compiled or run on Windows by me.** No CI job builds `leonida-gpu`. - From macOS, `cargo check -p leonida-gpu --target x86_64-pc-windows-msvc` (C build scripts given a stand-in compiler; nothing linked) on `949eacc3a`: default, `--features diagnostics`, `--tests --features diagnostics`: 0 errors, the same warnings as `main` (17 / 9 / 9). - Wanted: at least 3 runs of 600 s per arm on the 3090 (enlighted-agent), crashes counted apart, the memory line from #2751 and the mapped-scratch figure; a Windows build and `cargo test -p leonida-gpu`. - `d3d12/docs/switches.md` is not regenerated here: on `main` it is already behind the code (the generator adds 6 other names), so that belongs in its own change. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
OreoFR commented 2026-11-30 17:50:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T17:50:48Z.


Do not merge (A/B result, provisional). enlighted-agent, RTX 3090, -SeriesS, 600 s, n=1 per arm, exe 14772321f:

arm reaper line at end mapped scratch outcome
default backs up grows slows down late
SCRATCH_AT_LAND=1 backs up (2 runs of 2) grows slows down
FRONTS_LEAN=0 2,331 0.49 → 0.52 GB, flat 17.6 fps, no drop
FRONTS_LEAN=0 + SCRATCH_AT_LAND=1 459,013 9.7 → 15.4 GB device lost at ~505 s (E_OUTOFMEMORY closing a host list)

So, on n=1, this switch does not help and seems to make things worse, while turning FRONTS_LEAN off removes the backlog. Assumed cause for the worsening: give_chunks frees chunks beyond SPARE_CARD's 256 MiB on the landing thread, which slows landing. The lead is now the fronts: in the port this came from they had a dropping thread of their own (comment above fronts_lean in host.rs), while here they share the reaper with the lists. This stays a draft for the instrumentation in 949eacc3a (reaper line by kind) until that is confirmed.

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7286933157 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2753#issuecomment-7286933157). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-30T17:50:48Z. --- **Do not merge (A/B result, provisional).** enlighted-agent, RTX 3090, `-SeriesS`, 600 s, n=1 per arm, exe `14772321f`: | arm | reaper line at end | mapped scratch | outcome | | --- | --- | --- | --- | | default | backs up | grows | slows down late | | `SCRATCH_AT_LAND=1` | backs up (2 runs of 2) | grows | slows down | | `FRONTS_LEAN=0` | 2,331 | 0.49 → 0.52 GB, flat | 17.6 fps, no drop | | `FRONTS_LEAN=0` + `SCRATCH_AT_LAND=1` | 459,013 | 9.7 → 15.4 GB | device lost at ~505 s (`E_OUTOFMEMORY` closing a host list) | So, on n=1, this switch does not help and seems to make things worse, while turning `FRONTS_LEAN` off removes the backlog. Assumed cause for the worsening: `give_chunks` frees chunks beyond `SPARE_CARD`'s 256 MiB on the landing thread, which slows landing. The lead is now the fronts: in the port this came from they had a dropping thread of their own (comment above `fronts_lean` in `host.rs`), while here they share the reaper with the lists. This stays a draft for the instrumentation in `949eacc3a` (reaper line by kind) until that is confirmed. AI-assisted: Claude Code
OreoFR commented 2026-11-30 18:25:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T18:25:31Z.


Final A/B (enlighted-agent, RTX 3090, -SeriesS, 600 s, 2 runs per arm, exe 14772321f), correcting the provisional n=1 table above:

arm reaper line backs up
default 2 of 2
SCRATCH_AT_LAND=1 2 of 2
FRONTS_LEAN=0 1 of 2
FRONTS_LEAN=0 + SCRATCH_AT_LAND=1 2 of 2
LEONIDA_REAPER=0 (on main e1fe82f63) 0 of 4

So this switch does not prevent the backlog, and FRONTS_LEAN=0 is not the remedy either; only LEONIDA_REAPER=0 prevented it every time. Three device losses (~485-505 s, E_OUTOFMEMORY closing a host list) were all in FRONTS_LEAN=0 arms of this exe; no FRONTS_LEAN=0 control on main has been run, so their cause is open. This draft stays not-for-merge. A real fix (let go inline beyond a backlog threshold, or LEONIDA_REAPER=0 by default on large cards) would be its own change.

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7287829899 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2753#issuecomment-7287829899). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-30T18:25:31Z. --- **Final A/B (enlighted-agent, RTX 3090, `-SeriesS`, 600 s, 2 runs per arm, exe `14772321f`), correcting the provisional n=1 table above:** | arm | reaper line backs up | | --- | --- | | default | 2 of 2 | | `SCRATCH_AT_LAND=1` | 2 of 2 | | `FRONTS_LEAN=0` | 1 of 2 | | `FRONTS_LEAN=0` + `SCRATCH_AT_LAND=1` | 2 of 2 | | `LEONIDA_REAPER=0` (on `main` `e1fe82f63`) | 0 of 4 | So this switch does not prevent the backlog, and `FRONTS_LEAN=0` is not the remedy either; only `LEONIDA_REAPER=0` prevented it every time. Three device losses (~485-505 s, `E_OUTOFMEMORY` closing a host list) were all in `FRONTS_LEAN=0` arms of this exe; no `FRONTS_LEAN=0` control on `main` has been run, so their cause is open. **This draft stays not-for-merge.** A real fix (let go inline beyond a backlog threshold, or `LEONIDA_REAPER=0` by default on large cards) would be its own change. AI-assisted: Claude Code
KelboLive commented 2026-12-01 05:55:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T05:55:34Z.

Review: COMMENTED


The flight fence is sound, and that is the part that decides this: the relay only lands a list after it waited for the card's fence and after each flight's read_backs / release_tables (host.rs:6744-6760), and land() only drains entries older than bury_lag (:4870-4874) — the same point LEONIDA_REAPER=0 already returns scratch inline (recycle → reset, :8481-8487). So the list's own GPU reader is done when the chunks are given back, and a second flight or the submitting thread cannot touch them.

The remaining reader is the one you name, and it needs to be the headline: scratch_held (:9273-9276, draw.rs:5905-5911) hands a chunk range to a host descriptor slot, and the slot keeps the buffer alive, not the range — once the range is pooled, another list can write it while the slot still names it, and no fence in land() covers that. One correction to the "same as reaper=0" argument: recycle drops the list's _state/_uploads before reset returns the chunks, whereas this switch returns chunks while the landed list (and any held slot it still owns) waits for the reaper — so the window is at least as wide, possibly wider. That is the thing the A/B should look at first (stale constants), not just fps.

Bookkeeping is there (A/B pick, once-only line, chunk/byte counters, REAPING split by kind), but two things contradict or lag:

  • The switch row is missing from d3d12/docs/switches.md (d3d12_switches.py --check flags LEONIDA_SCRATCH_AT_LAND); the body defers docs, so this is expected for a draft — flagging it so it is not forgotten at merge.
  • The code comment says the reason for off-by-default is "two of four ended in a crash of the title", while the body's update attributes those to the title's own watchdog. Correct the comment to the scratch_held exposure plus the pending A/B, or the next reader will inherit a false cause.

Coverage: forcing the early return unconditional leaves all 648 tests green — nothing touches land() or the switch, so the A/B (three 600 s runs per arm, scratch flat, no new failure kind) is the only guard. That is acceptable for a draft; it is not enough to merge before the A/B.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6125889635 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2753#pullrequestreview-6125889635). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T05:55:34Z. **Review:** COMMENTED --- The flight fence is sound, and that is the part that decides this: the relay only lands a list after it waited for the card's fence and after each flight's `read_backs` / `release_tables` (`host.rs:6744-6760`), and `land()` only drains entries older than `bury_lag` (`:4870-4874`) — the same point `LEONIDA_REAPER=0` already returns scratch inline (`recycle` → `reset`, `:8481-8487`). So the list's own GPU reader is done when the chunks are given back, and a second flight or the submitting thread cannot touch them. The remaining reader is the one you name, and it needs to be the headline: `scratch_held` (`:9273-9276`, `draw.rs:5905-5911`) hands a chunk range to a host descriptor slot, and the slot keeps the **buffer** alive, not the range — once the range is pooled, another list can write it while the slot still names it, and no fence in `land()` covers that. One correction to the "same as reaper=0" argument: `recycle` drops the list's `_state`/`_uploads` **before** `reset` returns the chunks, whereas this switch returns chunks while the landed list (and any held slot it still owns) waits for the reaper — so the window is at least as wide, possibly wider. That is the thing the A/B should look at first (stale constants), not just fps. Bookkeeping is there (A/B pick, once-only line, chunk/byte counters, `REAPING` split by kind), but two things contradict or lag: - The switch row is missing from `d3d12/docs/switches.md` (`d3d12_switches.py --check` flags `LEONIDA_SCRATCH_AT_LAND`); the body defers docs, so this is expected for a draft — flagging it so it is not forgotten at merge. - The code comment says the reason for off-by-default is "two of four ended in a crash of the title", while the body's update attributes those to the title's own watchdog. Correct the comment to the `scratch_held` exposure plus the pending A/B, or the next reader will inherit a false cause. Coverage: forcing the early return unconditional leaves all 648 tests green — nothing touches `land()` or the switch, so the A/B (three 600 s runs per arm, scratch flat, no new failure kind) is the only guard. That is acceptable for a draft; it is not enough to merge before the A/B.
aslan2142 commented 2026-12-01 11:11:35 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Thanks for running the A/B experiment and documenting the negative result clearly (LEONIDA_SCRATCH_AT_LAND=1 did not prevent the reaper backlog and hit E_OUTOFMEMORY device loss in the FRONTS_LEAN=0 arm). Keeping this unmerged per your "Do not merge" note—feel free to close if the experiment is concluded.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130076788 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2753#pullrequestreview-6130076788). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:11:35Z. **Review:** COMMENTED --- Thanks for running the A/B experiment and documenting the negative result clearly (`LEONIDA_SCRATCH_AT_LAND=1` did not prevent the reaper backlog and hit `E_OUTOFMEMORY` device loss in the `FRONTS_LEAN=0` arm). Keeping this unmerged per your "Do not merge" note—feel free to close if the experiment is concluded. *(AI-assisted review.)*
merzleckeier commented 2026-12-01 12:28:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T12:28:54Z.


Independent offscreen check of 949eacc3a merged onto c60ce461a. I linked the MSVC leonida-gpu test binary with diagnostics and added a synthetic test that reads back an actual GPU copy from list scratch. After the synchronous submission completed, the fixture called HostList::give_scratch_back() directly. On AMD Raphael (RADV 26.3.1) and RTX 4070 SUPER (NVIDIA 620.73.4) under Wine, the copied word matched in all four runs.

With LEONIDA_SCRATCH_POOL=1, it returned one 256 KiB chunk and the list’s retained scratch dropped to zero. With LEONIDA_SCRATCH_POOL=0, it returned zero and the list kept its 4 MiB chunk. A second call returned zero in both settings. Scope is the completed list’s own scratch. Reaper backlog and the descriptor-held range discussed above remain outside this fixture.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7311192847 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2753#issuecomment-7311192847). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-01T12:28:54Z. --- Independent offscreen check of `949eacc3a` merged onto `c60ce461a`. I linked the MSVC `leonida-gpu` test binary with diagnostics and added a synthetic test that reads back an actual GPU copy from list scratch. After the synchronous submission completed, the fixture called `HostList::give_scratch_back()` directly. On AMD Raphael (RADV 26.3.1) and RTX 4070 SUPER (NVIDIA 620.73.4) under Wine, the copied word matched in all four runs. With `LEONIDA_SCRATCH_POOL=1`, it returned one 256 KiB chunk and the list’s retained scratch dropped to zero. With `LEONIDA_SCRATCH_POOL=0`, it returned zero and the list kept its 4 MiB chunk. A second call returned zero in both settings. Scope is the completed list’s own scratch. Reaper backlog and the descriptor-held range discussed above remain outside this fixture. AI-assisted with OpenAI Codex.
MoonEyes commented 2026-12-02 11:50:41 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T11:50:41Z.


Closing: this pull request no longer applies to current main (merge conflict in d3d12/crates/leonida-gpu/src/census.rs, d3d12/crates/leonida-gpu/src/host.rs), and open pull requests that have fallen out of step with main are being closed so the list stays workable. If you still want this change, rebase it on current main and reopen it (or open a new pull request); it will be looked at again.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7344484203 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2753#issuecomment-7344484203). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T11:50:41Z. --- Closing: this pull request no longer applies to current `main` (merge conflict in `d3d12/crates/leonida-gpu/src/census.rs`, `d3d12/crates/leonida-gpu/src/host.rs`), and open pull requests that have fallen out of step with `main` are being closed so the list stays workable. If you still want this change, rebase it on current `main` and reopen it (or open a new pull request); it will be looked at again. AI-assisted (Claude Code).

Pull request closed

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!2753
No description provided.