Skip to content

fix: Do not create a GPU stream on :cpu workers to synchronize them - #164

Merged
polvalente merged 2 commits into
elixir-nx:mainfrom
georgeguimaraes:perf/skip-gpu-sync-on-cpu-workers
Nov 27, 2026
Merged

polvalente merged 2 commits into
elixir-nx:mainfrom
georgeguimaraes:perf/skip-gpu-sync-on-cpu-workers

Conversation

@georgeguimaraes

@georgeguimaraes georgeguimaraes commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Since #155, synchronize_thread_streams() calls default_stream(gpu) on every worker thread before a compiled while or a runtime_call. On a :cpu worker, this call creates a GPU stream that the thread never uses. The function then commits an empty command buffer on that stream and waits for it.

The function now peeks the default GPU stream of the thread and syncs it only when it exists. An op that a thread puts on its default GPU stream creates this stream first, so the sync still covers all the GPU work of the thread. Thus the function waits only for the streams that the thread used, and it creates no stream. The CPU sync, which fixed #154, does not change.

The time per call on a :cpu worker also goes down a little. A compiled while with 1 iteration, median of 5 batches of 2000 calls on an M2 Pro:

Worker main This PR
:cpu 124 µs 102-108 µs
:gpu 436-456 µs 439-452 µs

Checks on this branch:

  • The hook-in-reduce test from Flaky test: hook in custom-fun reduce body sometimes sees wrong step values #154: 800 runs on :cpu, 400 runs on :gpu, 0 failures.
  • Full suite without "compiles a chain of 20,000 ops": 2 runs on :cpu passed (2739 of 2739). A 3rd run had 1 failure in "allocating a tensor increases active memory". That test passed when run alone and with its file.
  • Full suite with EMLX_TEST_DEFAULT_GPU=true: 12 failures. main fails the same 12 tests (triangular_solve, runtime_call graph split and ownership, quantized matmul).

…ime call

synchronize_thread_streams() called default_stream(gpu) on every worker thread. On a :cpu worker, this call created a GPU stream with no work on it. Each sync then committed an empty command buffer and waited for it.

The function now peeks the default GPU stream of the thread and syncs it only when it exists. An op that a thread puts on its default GPU stream creates this stream first, so the sync still covers all the GPU work of the thread.

A compiled while with 1 iteration on an M2 Pro: the :cpu worker went from 124 µs to 102-108 µs per call. The :gpu worker did not change, at about 440 µs per call.
@polvalente

Copy link
Copy Markdown
Member

@georgeguimaraes I don't thing there's measureable differences here. Could you provide a benchmarking script in which these changes matter? 20us difference could be explained by host os scheduling differences even.

@georgeguimaraes georgeguimaraes changed the title perf: Do not sync a GPU stream on :cpu workers before a while or runtime call fix: Do not create a GPU stream on :cpu workers to synchronize them Nov 21, 2026
@georgeguimaraes

Copy link
Copy Markdown
Member Author

@polvalente You are right that the time difference alone does not justify this change. The main point is that synchronize_thread_streams() creates a GPU stream on every :cpu worker that runs a while or a runtime_call, only to commit an empty command buffer and wait for it. With this change, the function waits only for the streams that the thread used, and it creates no stream. I updated the title and the description to say this. The timing is a side effect.

@polvalente
polvalente merged commit 3712397 into elixir-nx:main Nov 27, 2026
10 checks passed
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.

Flaky test: hook in custom-fun reduce body sometimes sees wrong step values

2 participants