Skip to content
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

[js/webgpu] Destroy staging buffers aggressively during weights uploading #22726

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions js/web/lib/wasm/jsep/webgpu/gpu-data-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ class GpuDataManagerImpl implements GpuDataManager {
// GPU Data ID => GPU Data ( storage buffer )
private storageCache: Map<GpuDataId, StorageCacheValue>;

// pending buffers for uploading ( data is unmapped )
private buffersForUploadingPending: GPUBuffer[];
// pending buffers for computing
private buffersPending: GPUBuffer[];

Expand All @@ -212,7 +210,6 @@ class GpuDataManagerImpl implements GpuDataManager {
this.storageCache = new Map();
this.freeBuffers = new Map();
this.freeUniformBuffers = new Map();
this.buffersForUploadingPending = [];
this.buffersPending = [];
this.capturedPendingBuffers = new Map();

Expand Down Expand Up @@ -252,13 +249,12 @@ class GpuDataManagerImpl implements GpuDataManager {
gpuBufferForUploading.unmap();

// GPU copy
const commandEncoder = this.backend.getCommandEncoder();
this.backend.endComputePass();
const commandEncoder = this.backend.device.createCommandEncoder();
commandEncoder.copyBufferToBuffer(gpuBufferForUploading, 0, gpuDataCache.gpuData.buffer, 0, size);
this.backend.device.queue.submit([commandEncoder.finish()]);
gpuBufferForUploading.destroy();

LOG_DEBUG('verbose', () => `[WebGPU] GpuDataManager.upload(id=${id})`);

this.buffersForUploadingPending.push(gpuBufferForUploading);
}

memcpy(sourceId: GpuDataId, destinationId: GpuDataId): void {
Expand Down Expand Up @@ -395,12 +391,6 @@ class GpuDataManagerImpl implements GpuDataManager {
}

refreshPendingBuffers(): void {
for (const buffer of this.buffersForUploadingPending) {
// upload buffer is only useful in the session creation time. So we don't need to reuse them in session running.
buffer.destroy();
}
this.buffersForUploadingPending = [];

if (this.buffersPending.length === 0) {
return;
}
Expand Down
Loading