Skip to content

Synchronize CPU and Metal allocator statistics - #16

Merged
jjang-ai merged 2 commits into
qwen4-expfrom
fix/allocator-statistics-synchronization
Sep 30, 2026
Merged

jjang-ai merged 2 commits into
qwen4-expfrom
fix/allocator-statistics-synchronization

Conversation

@jjang-ai

@jjang-ai jjang-ai commented Sep 29, 2026 •

Copy link
Copy Markdown

Allocator statistics were read without synchronization while allocation/completion threads updated them under a mutex. Use relaxed atomic observations for active bytes, peak bytes and the memory limit in the CPU and Metal allocators. Keep existing writer mutexes and allocation thresholds unchanged. Protect Metal cache-size observations with its mutex, and use already-protected direct cache reads internally to avoid recursive locking.

Adds actual allocator regressions for tiny-buffer allocation/free versus statistics reads, and unchanged-value memory-limit writes/peak resets versus readers. Each statistic is an independent observation; this does not introduce an atomic multi-field snapshot.

SOURCE EVIDENCE: Changes are limited to mlx/backend/no_gpu/allocator.cpp, mlx/backend/metal/allocator.h, mlx/backend/metal/allocator.cpp and tests/allocator_tests.cpp, based on qwen4-exp after #15 at 795d1b3849b82aaaeccddaa7fce9e0bb76a4f847.

LIVE EVIDENCE: Independently rebuilt complete CPU and Metal libraries and the actual existing doctest target with manual Apple Clang ThreadSanitizer instrumentation. Both controls contain the same scheduler-counter fix from #15, isolating allocator changes. All four public changed-file blobs match the tested candidate.

Scope Control Allocator candidate
CPU statistics concurrency TSan active-counter race 1/1 passed
CPU limit/peak concurrency TSan memory-limit race 1/1 passed
CPU scheduler suite 5 passed, then existing allocator race abort 11/11 passed
Actual Metal statistics concurrency MetalAllocator malloc/get_active_memory race 1/1 passed
Actual Metal limit/peak concurrency MetalAllocator set/get_memory_limit race 1/1 passed

All candidate rows exited 0, with no TSan report and unchanged sources. Separate library builds avoid mixing old allocator objects with changed headers. Actual compiler commands verify instrumentation and backend selection; Metal builds exclude the no-GPU allocator. The two Metal candidate cases were run sequentially for the final proof.

DEVICE=cpu TSAN_OPTIONS=halt_on_error=1:exitcode=66 tests/tests --test-case="test concurrent allocator statistics"
DEVICE=cpu TSAN_OPTIONS=halt_on_error=1:exitcode=66 tests/tests --test-case="test concurrent allocator limit and peak observations"
DEVICE=cpu TSAN_OPTIONS=halt_on_error=1:exitcode=66 tests/tests --source-file="*scheduler_tests.cpp"

DEVICE=cpu selects the default stream, not the allocator implementation: the Metal runs link and exercise the actual Metal allocator with 4 KiB/8 KiB buffers. CPU candidate binary SHA256:c6cfd9973894fc45f3387f1907eef1a5ec17aa5f49c17bc89579037f47243d02; Metal candidate:d5ad3303d94ac39e5157ad99d5dc0379c088df7f85a90d6a91082a8ae3fef146.

This is a manual Clang sanitizer configuration; the upstream macOS USE_TSAN gate remains unchanged. Metal proof is limited to the two allocator cases, with no model/shader execution or full Metal-suite claim. No performance improvement or production crash root cause is claimed. Full CPU scheduler-suite proof includes #15; the allocator source diff remains independent.

The published branch includes merged #15. Compared with the complete tested candidate source, the only tracked-file difference is scheduler comment wrapping; all noncomment lines and all allocator/test blobs are identical. No new exact-head binary test is claimed for that comment-only integration difference.

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