Repository navigation
feat: explicit activation term in memory estimate - #173
Merged
Merged
Conversation
TIER 2 of the epic #52 memory-estimation accuracy hardening (follow-up to closed #52; groundwork for the #122 block budget). The pre-load reserve beyond weights+KV was a single flat factor (1.20 x (weights+kv)) that tracked weight bytes — but activation memory scales with the workload (batch x context x hidden + MLP intermediate + vocab logits), so the flat factor under-reserved exactly in the serving regime the #122 batch budget cares about. The reserve is now two terms: (1) allocator overhead — the existing flat MLXCEL_HEADROOM_FACTOR (1.20) on weights+kv, modelling MLX's allocator/graph working set (the #55-calibrated 1.10..1.25 band); plus (2) a new activation term = mult x batch x min(ctx, prefill_chunk) x (hidden + intermediate) x 2 + last-token logits (batch x vocab x 2). Activations are FP16 regardless of weight/KV quant. Prefill is chunked (default 512 tokens), so the activation peak is bounded by the chunk, not the full context — the term does not blow up at long ctx. The activation is added on top of the allocator factor, so the total is never below the previous estimate. mult defaults to a conservative 2.0 (MLXCEL_ACTIVATION_MULT overrides; recalibrate via the #55 peak-memory FFI). MemoryEstimate gains an activation_bytes field; mlxcel inspect prints Activation and Allocator-overhead as separate lines. Real check: inspect qwen3-0.6b -n 8192 reports activation 8.3 MiB at batch 1 and 66.3 MiB at batch 8 (linear), bounded by the 512-token prefill chunk. Adds 6 activation unit tests.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
TIER 2 of the epic lablup#52 memory-estimation accuracy hardening (follow-up to closed lablup#52; groundwork for the lablup#122 block budget). The pre-load reserve beyond weights+KV was a single flat factor (1.20 x (weights+kv)) that tracked weight bytes — but activation memory scales with the workload (batch x context x hidden + MLP intermediate + vocab logits), so the flat factor under-reserved exactly in the serving regime the lablup#122 batch budget cares about. The reserve is now two terms: (1) allocator overhead — the existing flat MLXCEL_HEADROOM_FACTOR (1.20) on weights+kv, modelling MLX's allocator/graph working set (the lablup#55-calibrated 1.10..1.25 band); plus (2) a new activation term = mult x batch x min(ctx, prefill_chunk) x (hidden + intermediate) x 2 + last-token logits (batch x vocab x 2). Activations are FP16 regardless of weight/KV quant. Prefill is chunked (default 512 tokens), so the activation peak is bounded by the chunk, not the full context — the term does not blow up at long ctx. The activation is added on top of the allocator factor, so the total is never below the previous estimate. mult defaults to a conservative 2.0 (MLXCEL_ACTIVATION_MULT overrides; recalibrate via the lablup#55 peak-memory FFI). MemoryEstimate gains an activation_bytes field; mlxcel inspect prints Activation and Allocator-overhead as separate lines. Real check: inspect qwen3-0.6b -n 8192 reports activation 8.3 MiB at batch 1 and 66.3 MiB at batch 8 (linear), bounded by the 512-token prefill chunk. Adds 6 activation unit tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TIER 2 of the epic #52 memory-estimation accuracy hardening — groundwork for the #122 block budget. (Follow-up to epic #52, already merged; not a reopen.) The pre-load reserve beyond weights + KV was a single flat factor
1.20 × (weights + kv)that tracks weight bytes — but activation memory scales with the workload (batch × context × hidden + MLP intermediate + vocab logits), so the flat factor under-reserved exactly in the serving regime the #122 batch budget cares about.What it does
The reserve is now two terms:
MLXCEL_HEADROOM_FACTOR(1.20) onweights + kv, modelling MLX's allocator / graph working set (the feat: wrap MLX runtime memory APIs (active/peak/limit) in FFI #55-calibrated 1.10–1.25 band). Unchanged.mult × batch × min(ctx, prefill_chunk) × (hidden + intermediate) × 2+ the last-token logit bufferbatch × vocab × 2. Activations are FP16 regardless of weight/KV quant. Prefill is chunked (defaultprefill_chunk_size = 512), so the activation peak is bounded by the chunk — it does not blow up at long context.multdefaults to a conservative2.0(MLXCEL_ACTIVATION_MULToverrides; recalibrate via the feat: wrap MLX runtime memory APIs (active/peak/limit) in FFI #55 peak-memory FFI).The activation is added on top of the allocator factor, so the total is never below the previous estimate — strictly more conservative, now batch / context / vocab-aware.
Changes
memory_estimate.rs—compute_activation_bytes+activation_dims_from_path(hidden / intermediate / vocab,text_config-aware, withintermediate = 4 × hiddenandvocab = 0fallbacks);estimate_total_memorysplits the reserve into allocator overhead + activation; newMemoryEstimate.activation_bytes;mlxcel inspectprints Activation and Allocator ovhd as separate lines.Tests
6 new unit tests: the streaming + logit math, linear batch scaling, prefill-chunk capping (activation flat past the chunk; proportional below it), activation present in the full estimate, batch growth through
estimate_total_memory, and dims fallbacks.Validation (orchestrator-run, M1 Ultra, metal+accelerate)
Cold root
cargo clippy --testsclean;cargo fmt --checkclean; 3035 lib tests pass / 0 failed. Realmlxcel inspect qwen3-0.6b -n 8192: activation 8.3 MiB at batch 1, 66.3 MiB at batch 8 (linear), bounded by the 512-token prefill chunk; allocator overhead keeps the 1.20× safety.Scope
This completes #52's accuracy hardening (TIER 1 architecture-aware KV in #172 + TIER 2 here). Next: #122 sub-step (b) derives the opt-in block budget from
available − weights − activationand admits/evicts against it.