Repository navigation
Synchronize streams before the allocator statistics baseline - #33
Merged
Merged
Conversation
"test concurrent allocator statistics" took get_active_memory() as its
baseline without waiting for the streams, and checked at its end that
the active memory came back to it. With Metal, a buffer of an earlier
case can still be live when the baseline is read: "test array buffer
size" evaluates a reshape of a 16-byte array, and Metal keeps the
inputs of each evaluated primitive until its command buffer's
completion handler runs, which can be after the evaluation returns.
The buffer is then freed during the case, and the final check sees 0
against 16. Run alone with Metal on macOS, the allocator tests failed
39 times in 50, each as:
tests/allocator_tests.cpp:190: ERROR: CHECK_EQ( get_active_memory(), initial_active ) is NOT correct!
values: CHECK_EQ( 0, 16 )
The case now synchronizes every CPU stream and this thread's default
GPU stream, then clears the cache and reads the baseline, the order
python/tests/test_memory.py uses for its active memory count. A GPU
stream made on another thread cannot be synchronized from this one,
so only the default one is. The final check is unchanged. With this,
the allocator tests passed 50 times in 50; without the GPU synchronize
they failed 36 times in 50.
This was referenced Nov 3, 2026
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.
What fails
With Metal,
test concurrent allocator statisticsfails in most runs of the allocator tests alone, and in some runs of the whole suite. Line numbers below are atmain=4a22e4fed.tests/allocator_tests.cpp:143-194) callsclear_cache()and reads its baseline,initial_active = get_active_memory()(:147), without waiting for any stream. At its end it checksCHECK_EQ(get_active_memory(), initial_active)(:190).test array buffer size(:72-88), evaluates a reshape of a 16-byte array on the default stream.gpu::evalkeeps the inputs of each primitive in a completion handler of the command buffer (mlx/backend/metal/eval.cpp:47-67).evalreturns when the stream's event signals (mlx/transforms.cpp:345-353,:386;array::wait,mlx/array.cpp:145-153), and that can be before the handler runs.:147and be freed before:190:Without Metal it passed in every run on record, each a single run: the whole Metal-build suite with
DEVICE=cpuatd6dab016b, whose test and allocator sources are the same asmain's (308 | 308 passed,mf-qmv-mbasecpu-macmetal.tests.log), and the CPU-only builds' whole suites atmain(macOS 280 of 280, Linux 264 of 264,bf-ar-base-{mac,arm,x86}.tests.log).How often
The allocator tests alone (
-sf=*allocator_tests.cpp), 50 runs in a row on macOS with Metal atmain: 39 failed, each as above (bf-as-red-macmetal.loop.log). Earlier runs of the same test source gave 21, 34 and 40 of 50, and 9 of 20. The whole Metal suite atmainfailed this one case in this run (308 | 307 passed | 1 failed).The fix
Only the test changes. Before it clears the cache and reads the baseline, the case now:
peek_default_stream, so no stream is made). This is the step that removes the failure (see the mutation check);get_streams(), as the CPU allocator'ssynchronize_cpu_streams()does (mlx/backend/no_gpu/allocator.cpp:26-32). This step is defensive: without a GPU,clear_cache()already synchronizes the CPU streams when its cache is not empty (no_gpu/allocator.cpp:162-172).CommandEncoder::synchronize(mlx/backend/metal/device.cpp:581-591) commits the stream's open command buffer and waits for it to complete. Upstream'stest_active_memory_count(python/tests/test_memory.py:87-99) uses the same order:mx.synchronize(),mx.clear_cache(), thenmx.get_active_memory().That this wait also covers the earlier command buffer's completion handler, and the release of what it holds (
eval.cpp:66-67), is observed, not documented. Apple documents that a queue runs its command buffers in order. It does not document that a later buffer'swaitUntilCompletedreturns only after an earlier buffer's completion handlers have run and been released. The evidence here is 0 failures in 500 runs (below).Why only the default GPU stream
A GPU stream made on another thread cannot be synchronized from this one:
metal::get_command_encoder(device.cpp:954-967) throws "There is no Stream(gpu, N) in current thread." Such a stream needs no synchronize here, for two reasons:test new stream in threads(tests/scheduler_tests.cpp:92) makes one and then callsclear_streams()(:98). That destroys the thread's command encoders, and~CommandEncodersynchronizes (device.cpp:326-332).:111), which the CPU loop covers. Under ctest, each discovered case also runs in its own process (doctest_discover_tests,tests/CMakeLists.txt:54), and the whole-binary test (:55) runs in file order.Not covered:
--order-by=rand, streams fromnew_thread_unsafe_stream, and non-default GPU streams made on the main thread. If an earlier case left buffers in one of these, the race could come back.CPU streams can be synchronized from any thread:
Scheduler::get_thread(mlx/scheduler.cpp:148-162) finds or makes the stream's thread.The final check (
:190, now:204) is unchanged.Verification
On macOS. Each Metal run held vmlx's Metal test lock.
main:190withCHECK_EQ( 0, 16 )bf-as-red-macmetal.loop.logbf-as-green-macmetal.loop.logbf-as-green500-macmetal.loop.logCHECK_EQ( 0, 16 )bf-as-mut-macmetal.loop.logmainbf-as-suitebase-macmetal.tests.logbf-as-suitefix-macmetal.tests.logbf-as-cpu2-mac.tests.log:197).Upstream
ml-explore/mlx
main(1ba7f969a) has notest concurrent allocator statistics: itstests/allocator_tests.cppends withtest clear cache synchronizes cpu streams. The case came with #16, so this is fork-only, and there is no upstream report.🤖 Generated with Claude Code