Skip to content

Read the MLX cache after eviction has run, not before - #236

Merged
cboulay merged 1 commit into
devfrom
cboulay/fix-mlx-cache-test
Sep 4, 2026
Merged

Read the MLX cache after eviction has run, not before#236
cboulay merged 1 commit into
devfrom
cboulay/fix-mlx-cache-test

Conversation

@cboulay

@cboulay cboulay commented Sep 4, 2026

Copy link
Copy Markdown
Member

Independent of my three stacked PRs (#233, #234, #235) and the reason all three show red CI — this is what they're failing on.

The failure

test_mlx_cache_limit_actually_bounds_the_cache has failed on every CI run since it landed on 2026-08-24, always with the byte-identical assertion:

AssertionError: assert 35340292 <= ((32 * 1024) * 1024)
FAILED tests/unit/test_asarray.py::test_mlx_cache_limit_actually_bounds_the_cache
  • main, run 32881198675, 2026-08-25 — same bytes
  • slicer-hash, 2026-09-02
  • and now my branches

dev's last green run was 2026-08-09, two weeks before the test existed, which is why this hasn't been noticed. Locally it fails about one run in six.

Cause

MLX admits a freed buffer to the cache and trims down to the limit on the next allocation, not on the free. Sampling immediately after the churn loop catches a transient in which the last buffer — up to 17 MB for the sizes this test allocates — is still above the line.

Whether it does depends on the process's allocation history, which is why it's deterministic on CI (fixed test order) and intermittent locally.

Measured across six fresh processes, cache size after the churn:

result
before one further allocation 17.00 MB ×5, 34.30 MB ×1 (limit 32 MB)
after one further allocation 17.30 MB ×6

Fix

Allocate once more before reading the total — which is what any real workload does anyway. Eight consecutive local runs pass.

The limit itself was never in doubt and the test's premise is sound: the same churn with no limit at all leaves 501.61 MB cached. The test was asserting on a transient rather than on the settled state it meant to measure.

🤖 Generated with Claude Code

`test_mlx_cache_limit_actually_bounds_the_cache` has failed on CI every run
since it landed -- `main` on 2026-08-25 and every branch since, always with the
byte-identical `assert 35340292 <= 33554432`. Locally it fails about one run in
six.

MLX admits a freed buffer to the cache and trims down to the limit on the
*next* allocation, not on the free. Sampling immediately after the churn loop
therefore catches a transient in which the last buffer -- up to 17 MB for the
sizes this test allocates -- is still sitting above the line. Whether it does
depends on the allocation history of the process, which is why it is
deterministic on CI, where the test order is fixed, and intermittent locally.

Measured across six fresh processes, cache size after the churn:

    before one further allocation:  17.00 MB x5, 34.30 MB x1   (limit 32 MB)
    after one further allocation:   17.30 MB x6

So the fix is to allocate once more before reading the total, which is what
any real workload does anyway. Eight consecutive local runs pass.

The limit itself was never in doubt and the test's premise is sound: the same
churn with no limit leaves 501.61 MB cached.
@cboulay
cboulay merged commit 971e429 into dev Sep 4, 2026
14 checks passed
@cboulay
cboulay deleted the cboulay/fix-mlx-cache-test branch September 4, 2026 05:00
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