Skip to content

Stream checkpoints from disk during layerwise observation/pruning - #26

Open
aquilarubra wants to merge 2 commits into
CerebrasResearch:mainfrom
aquilarubra:pr/disk-streaming
Open

Stream checkpoints from disk during layerwise observation/pruning#26
aquilarubra wants to merge 2 commits into
CerebrasResearch:mainfrom
aquilarubra:pr/disk-streaming

Conversation

@aquilarubra

Copy link
Copy Markdown

Summary

Depends on #25 (Qwen3.5-MoE architecture support) — stacked on top of it, so the diff includes that commit until it merges; only the last commit is new here.

REAP's layerwise-observer path was RAM-bounded only in appearance: the observer phase loaded the entire model onto CPU via device_map="cpu" before any block-wise logic ran, so peak RSS still scaled with total checkpoint size rather than one layer's size. This makes it possible to prune a checkpoint far larger than available RAM+VRAM.

What's in this PR

  • src/reap/disk_stream_util.py (new): reads/writes checkpoint tensors directly from safetensors shards, opening and closing each shard scoped to a single read (deliberately never caches file handles, since a held mmap handle keeps its touched pages resident in RSS).
  • src/reap/layerwise_prune.py: replaces the full-RAM device_map="cpu" load with accelerate.init_empty_weights() (meta tensors) plus real materialization of only the embedding table, for local checkpoint directories (Hub model ids keep the original load path).
  • src/reap/layerwise_observer.py: generalizes the fused_experts branch's router lookup (was hardcoded to moe.router) and adds a dense all-experts-all-tokens fallback for architectures whose experts need top-k routing args to call (Qwen3.5, unlike Llama4's simpler dense-callable convention). Adds dual-mask support (ReplayBatch.causal_mask) so hybrid linear/full-attention models get the correct mask per layer type instead of replaying whichever mask block 0 received. Extends _move_block()/_offload_current_block() to materialize/free a block's real tensors from disk on demand, instead of only supporting already-CPU-resident blocks.

Validation

Verified peak RSS stays flat (~2.2GB) whether the test model has 4 or 32 layers, that el_prune_disk_surgery.py's output matches an in-memory reference prune bit-for-bit, and end-to-end at real ~807GB/60-layer/512-experts-per-layer scale (60 blocks processed, RSS stayed in a ~2-10GB band throughout).

Registers Qwen3_5MoeForConditionalGeneration in MODEL_ATTRS and
generalizes get_moe() to walk model.model.language_model.layers, not
just model.model.layers, for this architecture's nested multimodal
wrapper. Adds a matching observer hook config (num_experts/top_k live
on .experts/.gate submodules rather than the MoE block itself here).

Generalizes the fused-expert pruning branch, which was hardcoded to
Llama4's moe.router attribute and nn.Linear-style router with
out_features, to also support Qwen3.5's .gate raw weight Parameter.
Extracts select_retained_experts() as a standalone function from
prune()'s inline body so the same selection logic can be reused
outside an in-memory model (disk-based surgery).

Signed-off-by: Fabrizio del Tin <devotedmystic@gmail.com>
REAP's layerwise-observer path was RAM-bounded only in appearance: the
observer phase loaded the entire model onto CPU via device_map="cpu"
before any block-wise logic ran, so peak RSS still scaled with total
checkpoint size rather than one layer's size. This makes it possible
to prune a checkpoint far larger than available RAM+VRAM.

- New src/reap/disk_stream_util.py: reads/writes checkpoint tensors
  directly from safetensors shards, opening and closing each shard
  scoped to a single read (deliberately never caches file handles,
  since a held mmap handle keeps its touched pages resident in RSS).
- layerwise_prune.py: replaces the full-RAM device_map="cpu" load with
  accelerate.init_empty_weights() (meta tensors) plus real
  materialization of only the embedding table, for local checkpoint
  directories (Hub model ids keep the original load path).
- layerwise_observer.py: generalizes the fused_experts branch's router
  lookup (was hardcoded to moe.router) and adds a dense
  all-experts-all-tokens fallback for architectures whose experts need
  top-k routing args to call (Qwen3.5, unlike Llama4's simpler dense
  callable convention). Adds dual-mask support (ReplayBatch.causal_mask)
  so hybrid linear/full-attention models get the correct mask per layer
  type instead of replaying whichever mask block 0 received. Extends
  _move_block()/_offload_current_block() to materialize/free a block's
  real tensors from disk on demand via the new streaming primitive,
  instead of only supporting already-CPU-resident blocks.

Signed-off-by: Fabrizio del Tin <devotedmystic@gmail.com>
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