Skip to content

[WebGPU] Use Queue.WriteBuffer for small CPU-to-GPU uploads - #33551

Merged
Baiju Meswani (baijumeswani) merged 5 commits into
mainfrom
aciddelgado/webgpu_queue_uploads
Oct 28, 2026
Merged

Baiju Meswani (baijumeswani) merged 5 commits into
mainfrom
aciddelgado/webgpu_queue_uploads

Conversation

@aciddelgado

@aciddelgado aciddelgado commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Automatically use Queue.WriteBuffer for eligible CPU-to-GPU uploads up to and including 1 MiB.

  • Flush deferred readers before queue writes to preserve read-before-overwrite ordering.
  • Preserve mapped-buffer handling and staging for larger, unaligned, and capture/replay uploads.
  • Retain existing completion, validation, device-loss, and failure handling.
  • Add focused upload tests and document ordering, source-lifetime guarantees, and the size threshold.

Motivation and Context

Profiling Qwen/DFlash2 on A100 identified substantial host overhead from allocating, mapping, copying, and submitting a fresh staging buffer for each upload. Queue writes let Dawn reuse staging allocations and batch pending transfers with subsequent queue work, without adding CPU completion waits.

The conservative 1 MiB cap is the largest consistently beneficial size measured on A100/Vulkan. Larger transfers can regress, so they retain the existing staging path rather than extrapolating the small-upload improvement.

Validation: On Linux/A100/Vulkan, 80 native tests passed with one unrelated build-mode skip, and 100 GenAI tests passed. Both 512-token baseline and DFlash2 generation runs preserved exact tokens and speculative counters. Formatting and lint checks passed.

Update:

Rebased onto latest main, including merged PR #33491, and reran A100/Vulkan benchmarks to measure the incremental benefit of Queue.WriteBuffer.

Results:

  • Baseline: 3.3% faster (short), 6.8% faster (long)
  • DFlash2 width 3: 2.5% faster (short), 3.1% faster (long)

All generated tokens and speculative counters matched. Native and plugin tests passed. Existing batched uploads remain unaffected, and larger transfers retain the staging path.

This confirms that the optimization provides additional performance benefits beyond #33491.

aciddelgado and others added 2 commits October 22, 2026 22:02
Use queue-ordered WriteBuffer uploads after flushing recorded readers. Preserve mapped, unaligned, and capture/replay fallbacks, including mapped zero-byte unmapping. Add native correctness, lifetime, ordering, validation, and concurrency coverage; leave the option off by default.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove environment gating and use Queue.WriteBuffer automatically for eligible uploads through 1 MiB. Preserve mapped and zero-byte handling, ordering/error checks, and staging for larger, unaligned, and capture/replay transfers.

Simplify opt-in tests, verify queue/staging routing at size and state boundaries, and document the conservative A100-backed cap and outstanding Metal/D3D12 validation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@qjia7

Copy link
Copy Markdown
Contributor

aciddelgado Could #33491 help on your case?

@aciddelgado

Copy link
Copy Markdown
Contributor Author

aciddelgado Could #33491 help on your case?

yes, this looks like a related optimization.

aciddelgado and others added 3 commits October 23, 2026 19:22
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Main advanced during the A100 benchmarks only by changing a macOS workflow timeout. Runtime sources, dependencies and build inputs are identical to the measured main revision.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Cover alternating batched and single uploads with recorded readers. Document controlled A100/Vulkan microbenchmarks and five paired Qwen baseline/width-3 generation runs after batched input uploads, including path/driver submission counters, correctness results, limitations and merge recommendation. Preserve the 1 MiB queue-write limit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@aciddelgado
aciddelgado marked this pull request as ready for review October 23, 2026 20:48
Copilot AI balanced review requested due to automatic review settings October 23, 2026 20:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The default upload-path change needs maintainer confirmation of current-head Windows/D3D12 and macOS/Metal validation alongside the reported Vulkan results.

No open issues 🎉

What changed in this PR

Optimizes WebGPU single CPU-to-GPU uploads with Queue.WriteBuffer for eligible transfers up to 1 MiB, complementing the existing batched-upload optimization from #33491.

Changes:

  • Flushes recorded work before queue writes while preserving mapped-buffer and staging fallbacks.
  • Adds coverage for upload boundaries, source lifetime, ordering, validation, and mixed batch/single transfers.
  • Documents transfer guarantees, the size threshold, and incremental A100/Vulkan benchmark results.
File Description
onnxruntime/​test/​providers/​webgpu/​webgpu_context_test.cc Adds queue-upload correctness, fallback, and validation tests.
onnxruntime/​test/​providers/​webgpu/​data_transfer_test.cc Tests ordering across alternating batched and single uploads.
onnxruntime/​core/​providers/​webgpu/​buffer_manager.cc Adds the bounded queue-write path and zero-byte handling.
docs/​WebGPU_Queue_Uploads.md Documents behavior, performance evidence, and remaining platform validation.

Review effort: Balanced

@baijumeswani
Baiju Meswani (baijumeswani) merged commit 49481a7 into main Oct 28, 2026
99 of 103 checks passed
@baijumeswani
Baiju Meswani (baijumeswani) deleted the aciddelgado/webgpu_queue_uploads branch October 28, 2026 06:06

@qjia7 Jiajia Qin (qjia7) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, just find I have a pending comment and forgot to send.

@@ -0,0 +1,327 @@
# Native WebGPU queue uploads

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we remove this standalone document from the PR? Intermediate revisions, machine-local reproduction paths, and merge recommendations will become stale rather than serve as maintained WebGPU documentation. Others LGTM, thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just seeing this sorry, will address in separate PR.

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.

4 participants