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

Clears GPU Cache when there are no more active sessions #22490

Merged
merged 6 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions js/web/lib/wasm/jsep/backend-webgpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,10 @@ export class WebGpuBackend {
this.sessionStatus = 'default';
}

onCreateSession(): void {
this.gpuDataManager.onCreateSession();
}

onReleaseSession(sessionId: number): void {
this.unregisterBuffers(sessionId);
if (this.capturedCommandList.has(sessionId)) {
Expand Down
24 changes: 24 additions & 0 deletions js/web/lib/wasm/jsep/webgpu/gpu-data-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@ export interface GpuDataManager {
*/
dispose(): void;

/**
* create session related data.
*/
onCreateSession(): void;

/**
* release session related data.
* @param sessionId - specify the session ID.
*/
onReleaseSession(sessionId: number): void;

sessionCount: number;
}

interface StorageCacheValue {
Expand Down Expand Up @@ -214,6 +221,7 @@ class GpuDataManagerImpl implements GpuDataManager {
this.freeUniformBuffers.set(key, []);
}
}
sessionCount = 0;
prathikr marked this conversation as resolved.
Show resolved Hide resolved

upload(id: GpuDataId, data: Uint8Array): void {
const srcArrayBuffer = data.buffer;
Expand Down Expand Up @@ -460,6 +468,11 @@ class GpuDataManagerImpl implements GpuDataManager {
this.capturedPendingBuffers = new Map();
}

onCreateSession() {
LOG_DEBUG('verbose', () => '[WebGPU] Incrementing session count');
prathikr marked this conversation as resolved.
Show resolved Hide resolved
this.sessionCount += 1;
}

onReleaseSession(sessionId: number) {
// release the captured pending buffers.
const pendingBuffers = this.capturedPendingBuffers.get(sessionId);
Expand All @@ -469,6 +482,17 @@ class GpuDataManagerImpl implements GpuDataManager {
});
this.capturedPendingBuffers.delete(sessionId);
}

LOG_DEBUG('verbose', () => '[WebGPU] Decrementing session count');
prathikr marked this conversation as resolved.
Show resolved Hide resolved
// release the storage cache if no active sessions.
this.sessionCount -= 1;
if (this.sessionCount === 0) {
LOG_DEBUG('verbose', () => '[WebGPU] Clearing cache');
prathikr marked this conversation as resolved.
Show resolved Hide resolved
this.storageCache.forEach((storage) => {
storage.gpuData.buffer.destroy();
});
this.storageCache = new Map();
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions js/web/lib/wasm/wasm-core-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ export const createSession = async (
checkLastError("Can't create a session.");
}

wasm.jsepOnCreateSession?.();

// clear current MLContext after session creation
if (wasm.currentContext) {
wasm.jsepRegisterMLContext!(sessionHandle, wasm.currentContext);
Expand Down
6 changes: 6 additions & 0 deletions js/web/lib/wasm/wasm-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ export declare namespace JSEP {
* @param sessionId - specify the session ID.
*/
jsepOnRunStart: (sessionId: number) => void;
/**
* [exported from pre-jsep.js] Create a session. This function will be called after _OrtCreateSession() is
* called.
* @returns
*/
jsepOnCreateSession: () => void;
/**
* [exported from pre-jsep.js] Release a session. This function will be called before _OrtReleaseSession() is
* called.
Expand Down
3 changes: 3 additions & 0 deletions onnxruntime/wasm/pre-jsep.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ Module['jsepInit'] = (name, params) => {
Module['jsepCreateDownloader'] = (gpuBuffer, size, type) => {
return backend['createDownloader'](gpuBuffer, size, type);
};
Module['jsepOnCreateSession'] = sessionId => {
backend['onCreateSession'](sessionId);
};
Module['jsepOnReleaseSession'] = sessionId => {
backend['onReleaseSession'](sessionId);
};
Expand Down
Loading