-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Open
qjia7
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
qjia7:webgpu-session-buffer-pool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,74 @@ | ||
| // 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) { | ||
| if (max_generations_ == 0) { | ||
| return; | ||
| } | ||
|
|
||
| Slot slot; | ||
| slot.storage = retiring_mgr.StorageCache().ExtractCachedBuffers(); | ||
| slot.uniform = retiring_mgr.UniformCache().ExtractCachedBuffers(); | ||
|
|
||
| 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 62 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,54 @@ | ||
| // 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. | ||
| 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. | ||
| 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.