Skip to content

metal: dispatch/commit/sync/wait counters (gemma4-qat#181) - #3

Merged
jeethu merged 5 commits into
fork/86-affine-sym-0.32.2from
fork/181-counters
Sep 24, 2026
Merged

jeethu merged 5 commits into
fork/86-affine-sym-0.32.2from
fork/181-counters

Conversation

@jeethu

@jeethu jeethu commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Part of numen-tech/gemma4-qat#178 (Phase 0), child numen-tech/gemma4-qat#181.

Stacks on #2 (fork/86-affine-sym-0.32.2); retarget after #2 merges.

Adds mlx::core::metal::counters() / reset() (struct Counters { uint64_t dispatches, commits, syncs, waits; }, declared in mlx/backend/metal/metal.h):

  • dispatches: CommandEncoder::dispatch_threadgroups / dispatch_threads (every compute dispatch goes through these two).
  • commits: CommandEncoder::commit (the eval-time needs_commit split, gpu::finalize, and synchronize).
  • syncs: explicit stream synchronizations only (CommandEncoder::synchronize, i.e. mx.synchronize()).
  • waits: host blocking waits on GPU completion, one per wait call: an eval/item() on an array still in flight (event.cpp), a CPU stream waiting on a GPU event (fence.cpp, counted inside the CPU-stream lambda so it counts the spin, not the enqueue), and a sync's waitUntilCompleted (so every sync is also a wait). MLX skips the wait when the array's event has already signaled, so waits measures stalls actually taken, never blocking time. Internal hook: metal::count_host_wait() (device.h), called immediately before each blocking wait.

Relaxed std::atomic<uint64_t> increments, always compiled; no_metal.cpp returns zeros. No happens-before with the GPU: snapshot only after the measured eval has completed and with no other thread encoding.

Test: tests/metal_counters_tests.cpp (doctest; matmul+eval bumps dispatches/commits with 0 syncs and ≤1 wait, a re-eval of an already-evaluated array adds no wait, synchronize() bumps syncs, commits and waits, reset() zeroes). Full C++ suite: 280/280 pass (Release, -DMLX_METAL_JIT=ON). No Python binding (not needed by the consumer; the C++ test covers it).

Consumer: numen-tech/mlx-swift#4 (prism-181-counters: extern "C" shim + MLX.GPUCounters), then gemma4-qat's NumenDispatchTests dispatch-budget gate.

Review log: Codex round on gemma4-qat#181 (P2: host waits were not counted, and syncs conflated explicit synchronizations with implicit host waits) → aaefa5005 adds waits and narrows syncs. /simplify pass: no change (already minimal).

Adds mlx::core::metal::counters()/reset(): process-wide relaxed
std::atomic<uint64_t> counts of kernel dispatches (CommandEncoder
dispatch_threadgroups/dispatch_threads), command-buffer commits
(CommandEncoder::commit) and blocking syncs (CommandEncoder::synchronize,
waitUntilCompleted). Always compiled; no_metal.cpp returns zeros.
Host waits on an array's event (eval/item) are not counted as syncs.
Read only after the measured eval completes. doctest in
tests/metal_counters_tests.cpp.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T05:54:41.150186Z 9cb5069 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5447675087

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/metal_counters_tests.cpp Outdated
…emma4-qat#181, Codex P2)

Adds a fourth field `waits` to metal::Counters: host blocking waits on
GPU completion, counted once per wait call (not blocking time) at every
host-side wait in the Metal backend: EventImpl::wait (eval()/item() on an
array still in flight, CPU streams waiting on a GPU event), the
waitUntilCompleted in CommandEncoder::synchronize(), and the CPU-stream
spin on a fast fence. `commits` includes automatic buffer splits and is
not a host wait; `syncs` is documented as explicit stream
synchronizations. counters()/reset() are independent loads/stores and
only meaningful while no eval is in flight.

tests/metal_counters_tests.cpp checks the new field (eval waits at most
once, re-eval does not wait, synchronize adds exactly one wait).
Verified on Apple Silicon: full doctest suite 280/280.
@jeethu jeethu changed the title metal: dispatch/commit/sync counters (gemma4-qat#181) metal: dispatch/commit/sync/wait counters (gemma4-qat#181) Sep 24, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aaefa50058

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread mlx/backend/metal/device.cpp Outdated
Comment thread mlx/backend/metal/metal.h Outdated
Comment thread mlx/backend/metal/event.cpp Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e5d6ccdf0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread mlx/scheduler.h Outdated
Comment thread mlx/backend/metal/device.cpp Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1fee32b6e6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread mlx/backend/metal/metal.h Outdated
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