Repository navigation
metal: dispatch/commit/sync/wait counters (gemma4-qat#181) #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5447675
metal: dispatch/commit/sync counters (numen-tech/gemma4-qat#181)
jeethu aaefa50
metal: host-wait counter; syncs = explicit stream synchronizations (g…
jeethu 2e5d6cc
metal: counters skip teardown and CPU-only waits, count GPU throttle …
jeethu 1fee32b
metal: count throttle waits only on GPU completion; skip eval error-r…
jeethu 9cb5069
metal: condense counter comments (gemma4-qat#181, Codex P1)
jeethu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright © 2026 Apple Inc. | ||
|
|
||
| #include <thread> | ||
|
|
||
| #include "doctest/doctest.h" | ||
|
|
||
| #include "mlx/backend/metal/metal.h" | ||
| #include "mlx/mlx.h" | ||
|
|
||
| using namespace mlx::core; | ||
|
|
||
| TEST_CASE("test metal counters count dispatches, commits, syncs and waits") { | ||
| // Use the GPU explicitly: DEVICE=cpu makes the CPU the default device. | ||
| Device gpu = Device::gpu; | ||
| auto a = random::normal({64, 64}, float32, 0.0, 1.0, std::nullopt, gpu); | ||
| auto b = random::normal({64, 64}, float32, 0.0, 1.0, std::nullopt, gpu); | ||
| eval(a, b); | ||
|
|
||
| metal::reset(); | ||
| auto z = metal::counters(); | ||
| CHECK_EQ(z.dispatches, 0); | ||
| CHECK_EQ(z.commits, 0); | ||
| CHECK_EQ(z.syncs, 0); | ||
| CHECK_EQ(z.waits, 0); | ||
|
|
||
| auto c = matmul(a, b, gpu); | ||
| eval(c); | ||
| auto after_eval = metal::counters(); | ||
| CHECK_GE(after_eval.dispatches, 1); | ||
| CHECK_GE(after_eval.commits, 1); | ||
| CHECK_EQ(after_eval.syncs, 0); | ||
| // eval() blocks on c's completion event unless the GPU already signaled it | ||
| // before the host checked, so at most one wait. | ||
| CHECK_LE(after_eval.waits, 1); | ||
|
|
||
| // Re-evaluating (or reading) an evaluated array does not wait again. | ||
| eval(c); | ||
| CHECK_EQ(metal::counters().waits, after_eval.waits); | ||
|
|
||
| synchronize(default_stream(gpu)); | ||
| auto after_sync = metal::counters(); | ||
| CHECK_EQ(after_sync.syncs, 1); | ||
| CHECK_EQ(after_sync.waits, after_eval.waits + 1); | ||
| CHECK_GE(after_sync.commits, after_eval.commits + 1); | ||
| CHECK_EQ(after_sync.dispatches, after_eval.dispatches); | ||
|
|
||
| metal::reset(); | ||
| auto r = metal::counters(); | ||
| CHECK_EQ(r.dispatches, 0); | ||
| CHECK_EQ(r.commits, 0); | ||
| CHECK_EQ(r.syncs, 0); | ||
| CHECK_EQ(r.waits, 0); | ||
| } | ||
|
|
||
| TEST_CASE("test metal counters skip CPU-only work") { | ||
| Device cpu = Device::cpu; | ||
| auto a = random::normal({64, 64}, float32, 0.0, 1.0, std::nullopt, cpu); | ||
| auto b = random::normal({64, 64}, float32, 0.0, 1.0, std::nullopt, cpu); | ||
| eval(a, b); | ||
|
|
||
| metal::reset(); | ||
| auto c = matmul(a, b, cpu); | ||
| eval(c); | ||
| synchronize(default_stream(cpu)); | ||
| auto r = metal::counters(); | ||
| CHECK_EQ(r.dispatches, 0); | ||
| CHECK_EQ(r.commits, 0); | ||
| CHECK_EQ(r.syncs, 0); | ||
| CHECK_EQ(r.waits, 0); | ||
| } | ||
|
|
||
| TEST_CASE("test metal counters skip encoder teardown") { | ||
| // A new thread gets its own GPU stream, so clear_streams() there does not | ||
| // touch the streams of the main thread. | ||
| std::thread t([]() { | ||
| Device gpu = Device::gpu; | ||
| auto a = random::normal({64, 64}, float32, 0.0, 1.0, std::nullopt, gpu); | ||
| eval(a); | ||
| metal::reset(); | ||
| clear_streams(); | ||
| }); | ||
| t.join(); | ||
|
|
||
| auto r = metal::counters(); | ||
| CHECK_EQ(r.syncs, 0); | ||
| CHECK_EQ(r.waits, 0); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.