Skip to content

feat: explicit activation term in memory estimate - #173

Merged
inureyes merged 1 commit into
mainfrom
feat/52-activation-model
Jun 7, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/52-activation-model

Conversation

@inureyes

@inureyes inureyes commented Jun 7, 2026

Copy link
Copy Markdown
Member

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:

  1. Allocator overhead — the existing flat MLXCEL_HEADROOM_FACTOR (1.20) on weights + 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.
  2. Activation (new) — mult × batch × min(ctx, prefill_chunk) × (hidden + intermediate) × 2 + the last-token logit buffer batch × vocab × 2. Activations are FP16 regardless of weight/KV quant. Prefill is chunked (default prefill_chunk_size = 512), so the activation peak is bounded by the chunk — it does not blow up at long context. mult defaults to a conservative 2.0 (MLXCEL_ACTIVATION_MULT overrides; 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, with intermediate = 4 × hidden and vocab = 0 fallbacks); estimate_total_memory splits the reserve into allocator overhead + activation; new MemoryEstimate.activation_bytes; mlxcel inspect prints 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 --tests clean; cargo fmt --check clean; 3035 lib tests pass / 0 failed. Real mlxcel 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 − activation and admits/evicts against it.

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.
@inureyes inureyes added the type:enhancement New features, capabilities, or significant additions label Jun 7, 2026
@inureyes
inureyes merged commit f2979c4 into main Jun 7, 2026
4 checks passed
@inureyes
inureyes deleted the feat/52-activation-model branch June 7, 2026 11:31
@inureyes inureyes self-assigned this Jun 10, 2026
@inureyes inureyes added status:done Completed priority:medium Medium priority labels Jun 10, 2026
@inureyes inureyes added this to the 0.2 milestone Jun 21, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant