Repository navigation
perf(cuda): add MLXCEL_CACHE_LIMIT and disable periodic decode cache-clear on CUDA - #696
Merged
Merged
Conversation
…clear on CUDA The decode loops and batch scheduler cleared the MLX buffer cache every 256 generated tokens (Python mlx-lm parity). A GB10 matrix and a 16k-token soak show this is dead work on CUDA: it neither improves decode throughput (196.38 tok/s clear-on and clear-off, identical) nor bounds memory (16k peak is 1.24 GB with the clear off vs 1.29 GB on). CUDA decode graphs are currently disabled here (#688), so there is no graph cache for the clear to defeat (mlx#2358). - Add MLXCEL_CACHE_LIMIT: wire the previously-uncalled set_cache_limit bridge symbol through runtime init (resolve_cache_limit), the intended way to bound the CUDA buffer cache. parse_memory_size now also accepts single G/M suffixes. - Make the periodic-clear cadence backend-aware (memory::cache_clear_interval + should_clear_cache_at): default 0 (disabled) on CUDA, 256 on Metal/CPU, overridable via MLXCEL_CACHE_CLEAR_INTERVAL (0 disables). Applied at the 4 decode-loop sites and the 3 periodic scheduler sites; the 5 one-shot prefill/chunk trims are unchanged. - Document both env vars and commit the GB10 benchmark table justifying defaults. Throughput is unchanged by design: the deliverable is the operator knob plus removing a per-token no-op on CUDA, not a speedup.
…ackend-aware cadence Review of PR #696 found 3 periodic (token-gated) clear_memory_cache sites in the speculative and DFlash decode loops that use non-modulo-256 idioms and so were missed by the first pass: speculative/mod.rs (len is_multiple_of 256), dflash/round_loop.rs (emitted is_multiple_of 256), and dflash/round_loop_batched.rs (256-boundary crossing). None are feature-gated, so on CUDA they ignored the new default and MLXCEL_CACHE_CLEAR_INTERVAL. Route all three through memory::should_clear_cache_at / should_clear_cache_crossing (a new helper for the batched crossing form). The should_clear_cache_at guard also drops the clear at emitted == 0 that the old modulo check fired. Also switch should_clear_cache_at to is_multiple_of (clippy). All periodic decode-loop clears in mlxcel-core (4 in generate.rs + 3 here) now honor the cadence policy, alongside the 3 scheduler sites.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…clear on CUDA (lablup#696) ## Summary Adds `MLXCEL_CACHE_LIMIT` and makes the periodic decode-loop `clear_memory_cache` cadence backend-aware, defaulting to disabled on CUDA. Part of epic lablup#623. Closes lablup#627 ## Changes - **`MLXCEL_CACHE_LIMIT`**: wires the previously-uncalled `set_cache_limit` bridge symbol through runtime init (`resolve_cache_limit`). `parse_memory_size` now also accepts single `G`/`M` suffixes (e.g. `8G`) alongside `GB`/`MB`. - **Backend-aware clear cadence**: `memory::cache_clear_interval()` + `should_clear_cache_at()` / `should_clear_cache_crossing()`. Default `0` (disabled) on CUDA, `256` on Metal/CPU; `MLXCEL_CACHE_CLEAR_INTERVAL` overrides (`0` disables). Applied at **all periodic decode-loop clears**: 4 in `generate.rs`, 3 in the speculative/DFlash loops (`speculative/mod.rs`, `dflash/round_loop.rs`, `dflash/round_loop_batched.rs`), and 3 in `scheduler.rs`. The one-shot prefill/chunk trims are left unchanged. - Documents both env vars; commits the GB10 benchmark table under `docs/benchmark_results/`. ## Benchmark (GB10) 16k-token soak (qwen2.5-0.5b-bf16), full detail in the committed table: | cfg | decode tok/s | peak | |---|---|---| | clear on (256) | 196.38 | 1.29 GB | | clear off (0) | 196.38 | 1.24 GB | | off + `MLXCEL_CACHE_LIMIT=2G` | 196.10 | 1.27 GB | Disabling the periodic clear does not grow memory (peak off <= on) and does not change throughput. Likely because CUDA decode graphs are disabled here (lablup#688), so mlx#2358's graph-reuse penalty does not apply. **Honest null on throughput**: the deliverable is the operator knob plus removing a per-token no-op on CUDA, not a speedup. ## Tests - lablup#627 cadence/limit unit tests pass 6/6 (`memory::`/`generate::` `periodic_clear_*`, `parse_cache_clear_interval_falls_back_on_garbage`, `set_cache_limit_round_trip`). - `make release-cuda` builds clean. - Note: full `cargo test --features cuda -p mlxcel-core --lib` aborts on a pre-existing Metal-only test (`cache/turbo/sparse_v` `metal_kernel` path -> "No Metal back-end"), unrelated to this change. ## Review follow-up pr-reviewer flagged 3 periodic clears in the speculative/DFlash decode loops missed by the first pass (non-modulo-256 idioms: `len().is_multiple_of(256)`, `emitted.is_multiple_of(256)`, `/256` crossing). All converted in `248c36f` (also fixes a latent off-by-one that cleared at `emitted == 0`). `manual_is_multiple_of` clippy warning fixed. 7/7 cadence tests pass; clippy clean for changed files (one pre-existing unrelated `sanitize.rs` warning). ## Acceptance criteria (lablup#627) - [x] `MLXCEL_CACHE_LIMIT` functional from all entry points; documented. - [x] Cadence policy backend-aware with env override; values justified by committed benchmark table. - [x] No RSS/peak regression on the soak; decode tok/s at or above baseline on CUDA. - [x] `cargo test --features cuda` passes for the changed code (see test note).
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
Adds
MLXCEL_CACHE_LIMITand makes the periodic decode-loopclear_memory_cachecadence backend-aware, defaulting to disabled on CUDA. Part of epic #623.Closes #627
Changes
MLXCEL_CACHE_LIMIT: wires the previously-uncalledset_cache_limitbridge symbol through runtime init (resolve_cache_limit).parse_memory_sizenow also accepts singleG/Msuffixes (e.g.8G) alongsideGB/MB.memory::cache_clear_interval()+should_clear_cache_at(). Default0(disabled) on CUDA,256on Metal/CPU;MLXCEL_CACHE_CLEAR_INTERVALoverrides (0disables). Applied at the 4 decode-loop clear sites and the 3 periodic scheduler sites; the 5 one-shot prefill/chunk trims are left as-is.docs/benchmark_results/.Benchmark (GB10)
16k-token soak (qwen2.5-0.5b-bf16), full detail in the committed table:
MLXCEL_CACHE_LIMIT=2GDisabling the periodic clear does not grow memory (peak off <= on) and does not change throughput. Likely because CUDA decode graphs are disabled here (#688), so mlx#2358's graph-reuse penalty does not apply.
Honest null on throughput: the deliverable is the operator knob plus removing a per-token no-op on CUDA, not a speedup.
Tests
memory::/generate::periodic_clear_*,parse_cache_clear_interval_falls_back_on_garbage,set_cache_limit_round_trip).make release-cudabuilds clean.cargo test --features cuda -p mlxcel-core --libaborts on a pre-existing Metal-only test (cache/turbo/sparse_vmetal_kernelpath -> "No Metal back-end"), unrelated to this change.Acceptance criteria (#627)
MLXCEL_CACHE_LIMITfunctional from all entry points; documented.cargo test --features cudapasses for the changed code (see test note).