Skip to content

perf(ckpt): fsync checkpoint files in paced chunks and keep them out of the page cache - #4892

Open
MarioSieg wants to merge 4 commits into
mainfrom
mario/ckpt-paced-fsync
Open

MarioSieg wants to merge 4 commits into
mainfrom
mario/ckpt-paced-fsync

Conversation

@MarioSieg

@MarioSieg MarioSieg commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Paced fsync: CheckpointWriter writes files as PacedFiles that flush and fsync every ckpt.write_sync_mib MiB (new field, default 64; None syncs once at the end of each file as DCP does). DCP otherwise leaves a whole rank file dirty until its end, and the final fsync stalls any file operation of the training loop on that filesystem, holding every rank at the next collective. In an async-save benchmark (132 GB per save, one node), the step where the write ended went from 24-30 s over the median step to under 0.05 s over.
  • Page cache drop: after each sync, the synced range is dropped with posix_fadvise(DONTNEED). Checkpoint pages count against the container's memory cgroup and otherwise pile up save after save (an NFS client even keeps pages of files another node deleted for keep_last). Once the cgroup is at its limit, every allocation reclaims synchronously and one slow rank stalls all. Measured: page cache added per save went from about 34 GiB per node to about 0, and the longest step on a node close to its limit went from 5.3-7.5 s to 1.5 s.
  • Staging up front: with ckpt.async_save, CheckpointManager.prepare_async_save allocates the pinned staging buffers right after the first optimizer step, while memory is plentiful, instead of inside the first save.
  • Background system metrics: system/ckpt_disk_* is read in one long-lived daemon thread (BackgroundSystemMetrics), so a step never blocks on a filesystem that is flushing; each step logs the latest finished read, and a failed read keeps the last values. Under a cgroup v2 memory limit, every node's first rank also reads its cgroup and warns once above 90% of the limit, and rank 0 logs system/memory_{used,limit,page_cache,shmem}_gib.
  • Default: on (write_sync_mib = 64). --ckpt.write-sync-mib None restores DCP's one fsync per file (the page cache is still dropped at file end).

Test plan

  • uvx ruff check / uvx ruff format on the changed files.
  • CPU (torch 2.14) script that runs the new classes from this branch:
    • PacedFile writes the same bytes, and calls fadvise exactly on (0,64K),(64K,64K),(128K,64K),(192K,rest) for a 200 KiB file at 64 KiB syncs (whole file once with None).
    • A 200 MiB file written through PacedFile has 0 of its 51,200 pages resident (mincore), compared with 51,200 for a plain write.
    • Single-process dcp.save/dcp.load round trip through CheckpointWriter (paced, None, and small max_file_bytes) loads identical tensors, and the .distcp files are byte-identical to stock FileSystemWriter's.
    • BackgroundSystemMetrics against a fake cgroup: metrics, warning near the limit, keeps last disk values after a failed read, stops memory metrics after a failed memory read, returns {} with nothing to read.
  • CheckpointConfig parses write_sync_mib (64 default, None, rejects 0), and --trainer.ckpt.write-sync-mib shows up in rl --help.
  • Not run: GPU or multi-node training (no GPUs here).

Note

Medium Risk
Touches checkpoint persistence, async-save memory timing, and training-step metrics; misconfiguration could affect resume correctness or mask I/O stalls, but changes are localized and default-on with an escape hatch (write_sync_mib = None).

Overview
Checkpoint I/O now writes DCP shards through PacedFile / PacedFileSystem, flushing and fsyncing every ckpt.write_sync_mib MiB (default 64, or once per file when set to None), then dropping each synced range from the page cache via posix_fadvise(DONTNEED). That applies to sync and async saves via CheckpointWriter and the new _storage_writer helper.

Async save staging gains prepare_async_save, called after the first optimizer step in RL and SFT so pinned host buffers are sized up front instead of on the first save under memory pressure. Async save setup is factored into _setup_async_save.

Observability: get_ckpt_disk_metrics on the hot path is replaced by BackgroundSystemMetrics (daemon thread on each node’s local rank 0). Rank 0 logs the latest completed disk stats; under cgroup v2 limits it also logs system/memory_* metrics and warns once above 90% usage.

Documentation in training.md is expanded for host memory, page cache behavior, and system metrics.

Reviewed by Cursor Bugbot for commit 5d60715. Bugbot is set up for automated code reviews on this repo. Configure here.

…of the page cache

- The checkpoint writer flushes and fsyncs each file every `ckpt.write_sync_mib`
  MiB (64 by default) and drops each synced range from the page cache with
  posix_fadvise(DONTNEED). The end of a write no longer waits for a whole rank
  file of dirty pages, and checkpoints stop accumulating in the page cache, which
  counts against the container's memory cgroup.
- With `ckpt.async_save`, the pinned staging buffers are allocated right after
  the first optimizer step instead of by the first save.
- Checkpoint disk metrics are read in one background thread, and the first rank
  of every node reads its cgroup v2 memory usage there (rank 0 logs
  `system/memory_*`, each node warns once near its limit). A failed disk read
  keeps the last values.
@MarioSieg
MarioSieg marked this pull request as ready for review December 3, 2026 19:45

This branch has not been deployed

No deployments
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.

1 participant