-
Notifications
You must be signed in to change notification settings - Fork 4.1k
webgpu: Add session-level buffer pool for graph capture reuse #28761
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f1d977f
webgpu: Add session-level buffer pool for graph capture reuse
qjia7 9a11120
Address PR review: strict option parsing, non-const cache accessors
qjia7 3d56558
Document SessionBufferPool invariants and provider option
qjia7 c7f6e58
Move sessionBufferPoolGenerations description to provider_options.h
qjia7 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "core/providers/webgpu/session_buffer_pool.h" | ||
|
|
||
| #include "core/providers/webgpu/buffer_manager.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace webgpu { | ||
|
|
||
| namespace { | ||
| void ReleaseSlotBuffers(std::vector<std::pair<size_t, WGPUBuffer>>& entries) { | ||
| for (auto& entry : entries) { | ||
| if (entry.second) { | ||
| wgpuBufferRelease(entry.second); | ||
| } | ||
| } | ||
| entries.clear(); | ||
| } | ||
| } // namespace | ||
|
|
||
| SessionBufferPool::SessionBufferPool(size_t max_generations) | ||
| : max_generations_{max_generations} { | ||
| } | ||
|
|
||
| SessionBufferPool::~SessionBufferPool() { | ||
| Clear(); | ||
| } | ||
|
|
||
| void SessionBufferPool::Donate(BufferManager& retiring_mgr) { | ||
| // Safe to take ownership of the retiring manager's buffers because: | ||
| // 1. InferenceSession::session_mutex_ serializes Run and | ||
| // ReleaseCapturedGraph for the WebGPU EP, so no Run is in progress on | ||
| // this session while we donate. | ||
| // 2. OnRunEnd flushes the queue (and, in ValidationMode::Basic+, fences | ||
| // via PopErrorScope) before Run returns, so prior captured-graph work | ||
| // has been submitted. | ||
| // 3. All work goes through a single WebGPU device queue, which orders | ||
| // future submits after the prior submitted work, so a subsequent | ||
| // generator's first Submit using these buffer handles happens-after | ||
| // any in-flight read/write on the GPU timeline. | ||
| // Donating across a different queue or after a host-side readback on | ||
| // donated buffers would invalidate (3) and require an explicit fence here. | ||
| if (max_generations_ == 0) { | ||
| return; | ||
| } | ||
|
|
||
| Slot slot; | ||
| slot.storage = retiring_mgr.StorageCache().ExtractCachedBuffers(); | ||
| slot.uniform = retiring_mgr.UniformCache().ExtractCachedBuffers(); | ||
|
|
||
| // The caches are now empty either way; the retiring manager is about to be | ||
| // destroyed, so the early-exit only avoids pushing an empty slot. | ||
| if (slot.storage.empty() && slot.uniform.empty()) { | ||
| return; | ||
| } | ||
|
|
||
| // Evict the oldest slot if at capacity so the freshest buffers (which most | ||
| // accurately reflect the current per-generator shape distribution) are kept. | ||
| while (slots_.size() >= max_generations_) { | ||
| auto& victim = slots_.front(); | ||
| ReleaseSlotBuffers(victim.storage); | ||
| ReleaseSlotBuffers(victim.uniform); | ||
| slots_.erase(slots_.begin()); | ||
| } | ||
|
|
||
| slots_.emplace_back(std::move(slot)); | ||
| } | ||
|
|
||
| void SessionBufferPool::SeedInto(BufferManager& new_mgr) { | ||
| if (slots_.empty()) { | ||
| return; | ||
| } | ||
| Slot slot = std::move(slots_.back()); | ||
| slots_.pop_back(); | ||
| new_mgr.StorageCache().AbsorbCachedBuffers(std::move(slot.storage)); | ||
| new_mgr.UniformCache().AbsorbCachedBuffers(std::move(slot.uniform)); | ||
|
Check warning on line 77 in onnxruntime/core/providers/webgpu/session_buffer_pool.cc
|
||
| } | ||
|
|
||
| void SessionBufferPool::Clear() { | ||
| for (auto& slot : slots_) { | ||
| ReleaseSlotBuffers(slot.storage); | ||
| ReleaseSlotBuffers(slot.uniform); | ||
| } | ||
| slots_.clear(); | ||
| } | ||
|
|
||
| } // namespace webgpu | ||
| } // namespace onnxruntime | ||
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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "core/providers/webgpu/webgpu_external_header.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace webgpu { | ||
|
|
||
| class BufferManager; | ||
|
|
||
| // SessionBufferPool retains buffers from retired per-graph BufferManagers so | ||
| // that subsequent generators on the same session can reuse them instead of | ||
| // allocating from the device. Scoped per WebGpuExecutionProvider (per session) | ||
| // because intermediate buffer shapes are model-dependent. | ||
| // | ||
| // Thread safety: not internally synchronized. Donate / SeedInto / Clear must | ||
| // not be called concurrently. The WebGPU EP relies on the fact that | ||
| // InferenceSession serializes Run and ReleaseCapturedGraph under | ||
| // session_mutex_ for execution providers where ConcurrentRunSupported() is | ||
| // false, which serializes the only call sites (OnRunStart -> SeedInto and | ||
| // ReleaseCapturedGraph -> Donate). Clear runs from the EP destructor after | ||
| // all Run / ReleaseCapturedGraph calls have returned. | ||
| class SessionBufferPool { | ||
| public: | ||
| explicit SessionBufferPool(size_t max_generations); | ||
|
|
||
| ~SessionBufferPool(); | ||
|
|
||
| // Move freed buffers from a retiring per-graph BufferManager into the pool. | ||
| // When the pool is at capacity, the oldest slot is evicted (its buffers | ||
| // released to the device) so the freshest buffers are always retained. This | ||
| // lets the pool adapt when intermediate buffer shapes change between | ||
| // generators (for example when max_length differs). | ||
| void Donate(BufferManager& retiring_mgr); | ||
|
|
||
| // Pre-populate a newly created per-graph BufferManager with one slot worth of | ||
| // pooled buffers (LIFO). No-op if the pool is empty. The receiving manager | ||
| // may end up holding at most one donated slot's worth of buffer sizes that | ||
| // the new generator never requests; those sit in the receiving manager's | ||
| // caches until that manager is itself donated or destroyed. | ||
| void SeedInto(BufferManager& new_mgr); | ||
|
|
||
| // Release all pooled buffers. Called on session teardown. | ||
| void Clear(); | ||
|
|
||
| size_t Size() const { return slots_.size(); } | ||
| size_t Capacity() const { return max_generations_; } | ||
|
|
||
| private: | ||
| struct Slot { | ||
| std::vector<std::pair<size_t, WGPUBuffer>> storage; | ||
| std::vector<std::pair<size_t, WGPUBuffer>> uniform; | ||
| }; | ||
| std::vector<Slot> slots_; | ||
| size_t max_generations_; | ||
| }; | ||
|
|
||
| } // namespace webgpu | ||
| } // namespace onnxruntime |
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
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.