Skip to content

perf(cuda): add MLXCEL_CACHE_LIMIT and disable periodic decode cache-clear on CUDA - #696

Merged
inureyes merged 2 commits into
mainfrom
perf/627-cuda-cache-clear-policy
Jul 8, 2026
Merged

inureyes merged 2 commits into
mainfrom
perf/627-cuda-cache-clear-policy

Conversation

@inureyes

@inureyes inureyes commented Jul 8, 2026

Copy link
Copy Markdown
Member

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 #623.

Closes #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(). Default 0 (disabled) on CUDA, 256 on Metal/CPU; MLXCEL_CACHE_CLEAR_INTERVAL overrides (0 disables). 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.
  • 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 (#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

Acceptance criteria (#627)

  • MLXCEL_CACHE_LIMIT functional from all entry points; documented.
  • Cadence policy backend-aware with env override; values justified by committed benchmark table.
  • No RSS/peak regression on the soak; decode tok/s at or above baseline on CUDA.
  • cargo test --features cuda passes for the changed code (see test note).

…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.
@inureyes inureyes added type:performance Performance improvements priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Jul 8, 2026
…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.
@inureyes
inureyes merged commit 98d4c5d into main Jul 8, 2026
4 checks passed
@inureyes
inureyes deleted the perf/627-cuda-cache-clear-policy branch July 8, 2026 13:50
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added the status:done Completed label Sep 6, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cuda): allocator policy: wire set_cache_limit and re-evaluate periodic clear_memory_cache

1 participant