[PR #4411] [Engine] session-file saves: ping-pong blocks and a writer thread so the source copy overlaps the previous block write #5037

Open
opened 2026-11-07 16:32:13 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4411
Author: @sakuumomo
Created: 10/28/2026
Status: 🔄 Open

Base: main ← Head: pr/engine-save-overlap


📝 Commits (4)

  • 5b44337 core(session-file): a test hook for the write path - every full block reports its OS-write nanosecond span, plus the pipeline witness test (a block write inside a source-read window) that the serialized copy->write loop cannot pass
  • a4a39a3 core(session-file): pipelined FileSink - ping-pong 16 MiB blocks and a lazily started writer thread so a source copy overlaps the previous block's OS write; the file bytes, golden hashes, progress reports, fault latching, and publish semantics are unchanged
  • 36d1e8d core(session-file): account for the pipeline - the save preflight reserves the two 16 MiB write buffers, the peak test band moves to 32..34 MiB (RED against the old single buffer first), and the header documents that progress reports follow real movement from the caller's thread
  • 8d944e7 core(session-file): drain synchronously when no writer thread can start - std::thread construction is caught (system_error -> the un-pipelined serialized path), pinned by a deterministic tests-only "writer_thread" seam and a starved-writer test; the block_span header now names the calling thread contract

📊 Changes

4 files changed (+271 additions, -21 deletions)

View changed files

📝 include/strata/core/conversation_file.hpp (+8 -2)
📝 src/core/conversation_file.cpp (+130 -15)
📝 src/core/conversation_file_test.cpp (+131 -3)
📝 src/program/generate.cpp (+2 -1)

📄 Description

The problem

A busy-live session save crawls at ~110 MB/s while the storage beneath it measures 500+ MB/s (the owner's bcachefs pool on the desktop host). FileSink serializes: the producer copies one 16 MiB block out of the K/V sources, hands it to write_all, waits for the write, then copies the next block. The GPU-side copy and the disk write never overlap.

What this does

FileSink now ping-pongs two 16 MiB buffers with a lazily started writer thread: while the writer drains block N to the OS, the producer already copies block N+1 out of the sources. When both buffers are busy the producer waits — at most one block behind.

Deliberately unchanged:

  • the byte stream: blocks are written in the same fixed order, so hashes and file bytes are identical — the golden format tests pass untouched;
  • progress semantics: reports still come from the calling thread, follow real movement (monotonic, may lag ≤1 block, never lead);
  • fault latching and publish: a writer-side write_all failure merges into the existing f.error()/kind() channel after a clean join; the temp file is never published; Publisher untouched;
  • finish() (pad → truncate → sync → close) runs only after the writer has joined.

New pieces:

  • the save RAM preflight now reserves 2 × kBlock (the two write buffers);
  • if a writer thread cannot start (process/thread limits — std::system_error), FileSink falls back to synchronous draining: the exact old serialized path. The pipeline can never crash a save.

Tests (all CPU, no new dependencies)

  • pipeline witness: a tests-only block_span hook records each full block's OS-write span; the test asserts a write begins during a source read — causally impossible in the serialized loop (verified RED against it, GREEN against the pipeline). The hook sits beside the existing fault hook in SessionWriteOptions (same tests-only precedent).
  • starved writer: a "writer_thread" fault seam forces the fallback; the save still completes, byte-identical, with no overlap witnessed.
  • mid-pipeline write-fault test (existing fault injection) still passes unchanged.
  • CPU gate conversation_(file|split_failure|cache|memory)_test, session_save_reclaim_test: 5/5. ThreadSanitizer run of conversation_file_test: 0 warnings.

Independent of #4310: the pipelined code is the FileSink core as it stands on main; the chain here is these four commits on main alone.

Update (2026-10-30)

Rebased onto upstream v0.2.8 (870aedd9); content unchanged, branch tip 2a8d10b8 (still these four commits). Gates re-run on the rebuilt lineage: the CPU session test family 7/7 (conversation_append_test 1969 checks, conversation_file_test 263, session_watermark_test 21), the ThreadSanitizer run 0 warnings (1969 + 260 checks), and the owner's HIP-only (gfx1030) and CUDA+HIP hybrid (gfx1150) flavor builds green with installCheck. Commits re-signed (GPG ed25519) on 2026-10-30; trees unchanged from the validated work.

Update (2026-11-07)

Rebased onto upstream v0.2.12 (123bad4a, engine 0.1.58); content unchanged, branch tip 8d944e7f (still these four commits, zero conflicts). Gates re-run on the rebuilt lineage: CPU session family 7/7 (append 1969, file 263, watermark 31), ThreadSanitizer 1969 + 260 checks 0 warnings, and the owner's HIP-only (gfx1030) and CUDA+HIP hybrid (gfx1150) flavor builds green with installCheck on the redeployed train nixos @ c3e4cdc4. metal-link and title jobs are green on the new head; the PR is mergeable against current main. Commits re-signed (GPG ed25519) on 2026-11-07; trees unchanged from the validated work. The pre-rebase tip 2a8d10b8 stays fetchable as an ancestor of the train tag pin-f6f00908.

Numbers (live battery on the owner's desktop host, post-merge)

A 166k-token, 4.94 GB session file: the old build saved it in 42–54 s uncontended (160 s worst, running through a heavy prefill); this build saves it in 54.5 s. Storage probes on the same pool at the same moment: 265 MB/s direct, 475 MB/s buffered+fsync — so the save ends at 91 MB/s because the engine-side source chain (K/V streamed out of the live state) is the binding rate on this host, not the disk. What overlap buys here is that the producer never stalls on disk mid-stream (the writer thread drains blocks while the source keeps copying; a save whose source runs at 91 MB/s cannot stream faster than its source, and the live rows confirm that); the byte stream is unchanged, and the pipelined write path is the foundation for append-delta saves — saving only the dirty tail (a few MB per turn at this size), which is where this host's save time actually collapses.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4411 **Author:** [@sakuumomo](https://github.com/sakuumomo) **Created:** 10/28/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/engine-save-overlap` --- ### 📝 Commits (4) - [`5b44337`](https://github.com/Niko1221/Strata/commit/5b44337abcea6b4e136be9d4ea53226dda6f9e89) core(session-file): a test hook for the write path - every full block reports its OS-write nanosecond span, plus the pipeline witness test (a block write inside a source-read window) that the serialized copy->write loop cannot pass - [`a4a39a3`](https://github.com/Niko1221/Strata/commit/a4a39a3f5d276138c01e4c4b215d2c94d49ad7c3) core(session-file): pipelined FileSink - ping-pong 16 MiB blocks and a lazily started writer thread so a source copy overlaps the previous block's OS write; the file bytes, golden hashes, progress reports, fault latching, and publish semantics are unchanged - [`36d1e8d`](https://github.com/Niko1221/Strata/commit/36d1e8ddb112722047f42c3c1a322467a7d6d112) core(session-file): account for the pipeline - the save preflight reserves the two 16 MiB write buffers, the peak test band moves to 32..34 MiB (RED against the old single buffer first), and the header documents that progress reports follow real movement from the caller's thread - [`8d944e7`](https://github.com/Niko1221/Strata/commit/8d944e7fc47fd8b7bd2ad7889516cfb07f779aea) core(session-file): drain synchronously when no writer thread can start - std::thread construction is caught (system_error -> the un-pipelined serialized path), pinned by a deterministic tests-only "writer_thread" seam and a starved-writer test; the block_span header now names the calling thread contract ### 📊 Changes **4 files changed** (+271 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `include/strata/core/conversation_file.hpp` (+8 -2) 📝 `src/core/conversation_file.cpp` (+130 -15) 📝 `src/core/conversation_file_test.cpp` (+131 -3) 📝 `src/program/generate.cpp` (+2 -1) </details> ### 📄 Description ### The problem A busy-live session save crawls at ~110 MB/s while the storage beneath it measures 500+ MB/s (the owner's bcachefs pool on the desktop host). `FileSink` serializes: the producer copies one 16 MiB block out of the K/V sources, hands it to `write_all`, waits for the write, then copies the next block. The GPU-side copy and the disk write never overlap. ### What this does `FileSink` now ping-pongs two 16 MiB buffers with a lazily started writer thread: while the writer drains block N to the OS, the producer already copies block N+1 out of the sources. When both buffers are busy the producer waits — at most one block behind. Deliberately unchanged: - the **byte stream**: blocks are written in the same fixed order, so hashes and file bytes are identical — the golden format tests pass untouched; - progress semantics: reports still come from the calling thread, follow real movement (monotonic, may lag ≤1 block, never lead); - fault latching and publish: a writer-side `write_all` failure merges into the existing `f.error()`/`kind()` channel after a clean join; the temp file is never published; `Publisher` untouched; - `finish()` (pad → truncate → sync → close) runs only after the writer has joined. New pieces: - the save RAM preflight now reserves **2 × kBlock** (the two write buffers); - if a writer thread cannot start (process/thread limits — `std::system_error`), `FileSink` falls back to synchronous draining: the exact old serialized path. The pipeline can never crash a save. ### Tests (all CPU, no new dependencies) - **pipeline witness**: a tests-only `block_span` hook records each full block's OS-write span; the test asserts a write *begins during* a source read — causally impossible in the serialized loop (verified RED against it, GREEN against the pipeline). The hook sits beside the existing `fault` hook in `SessionWriteOptions` (same tests-only precedent). - **starved writer**: a `"writer_thread"` fault seam forces the fallback; the save still completes, byte-identical, with no overlap witnessed. - mid-pipeline write-fault test (existing fault injection) still passes unchanged. - CPU gate `conversation_(file|split_failure|cache|memory)_test`, `session_save_reclaim_test`: 5/5. ThreadSanitizer run of `conversation_file_test`: 0 warnings. Independent of #4310: the pipelined code is the `FileSink` core as it stands on `main`; the chain here is these four commits on `main` alone. ### Update (2026-10-30) Rebased onto upstream `v0.2.8` (`870aedd9`); content unchanged, branch tip `2a8d10b8` (still these four commits). Gates re-run on the rebuilt lineage: the CPU session test family 7/7 (`conversation_append_test` 1969 checks, `conversation_file_test` 263, `session_watermark_test` 21), the ThreadSanitizer run 0 warnings (1969 + 260 checks), and the owner's HIP-only (gfx1030) and CUDA+HIP hybrid (gfx1150) flavor builds green with installCheck. Commits re-signed (GPG ed25519) on 2026-10-30; trees unchanged from the validated work. ### Update (2026-11-07) Rebased onto upstream `v0.2.12` (`123bad4a`, engine 0.1.58); content unchanged, branch tip `8d944e7f` (still these four commits, zero conflicts). Gates re-run on the rebuilt lineage: CPU session family 7/7 (append 1969, file 263, watermark 31), ThreadSanitizer 1969 + 260 checks 0 warnings, and the owner's HIP-only (gfx1030) and CUDA+HIP hybrid (gfx1150) flavor builds green with installCheck on the redeployed train `nixos` @ `c3e4cdc4`. metal-link and title jobs are green on the new head; the PR is mergeable against current `main`. Commits re-signed (GPG ed25519) on 2026-11-07; trees unchanged from the validated work. The pre-rebase tip `2a8d10b8` stays fetchable as an ancestor of the train tag `pin-f6f00908`. ### Numbers (live battery on the owner's desktop host, post-merge) A 166k-token, 4.94 GB session file: the old build saved it in 42–54 s uncontended (160 s worst, running through a heavy prefill); this build saves it in 54.5 s. Storage probes on the same pool at the same moment: 265 MB/s direct, 475 MB/s buffered+fsync — so the save ends at 91 MB/s because the **engine-side source chain** (K/V streamed out of the live state) is the binding rate on this host, not the disk. What overlap buys here is that the producer never stalls on disk mid-stream (the writer thread drains blocks while the source keeps copying; a save whose source runs at 91 MB/s cannot stream faster than its source, and the live rows confirm that); the byte stream is unchanged, and the pipelined write path is the foundation for append-delta saves — saving only the dirty tail (a few MB per turn at this size), which is where this host's save time actually collapses. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
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.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5037
No description provided.