From fa8487ea3a3a224050c816457b111c0769e0e9bf Mon Sep 17 00:00:00 2001 From: Jiajia Qin Date: Tue, 1 Aug 2023 08:37:24 +0800 Subject: [PATCH] [js/webgpu] Check profilingMode in each run (#16897) ### Description This PR moves checking profilingMode to each run instead of the initialization stage. In this way, users can start/stop profiling at any time. Otherwise, profiling only take effects at the very beginning and can't be stopped. --- js/web/lib/wasm/jsep/backend-webgpu.ts | 11 +++++++---- js/web/lib/wasm/jsep/webgpu/program-manager.ts | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/js/web/lib/wasm/jsep/backend-webgpu.ts b/js/web/lib/wasm/jsep/backend-webgpu.ts index 8aa825abfb..8103111dfc 100644 --- a/js/web/lib/wasm/jsep/backend-webgpu.ts +++ b/js/web/lib/wasm/jsep/backend-webgpu.ts @@ -90,10 +90,12 @@ export class WebGpuBackend { computePassEncoder: GPUComputePassEncoder|null = null; pendingDispatchNumber = 0; - profilingEnabled = false; + supportTimestampQuery = false; profilingQuerySet: GPUQuerySet; profilingTimeBase?: bigint; + env: Env; + async initialize(env: Env): Promise { if (!navigator.gpu) { // WebGPU is not available. @@ -105,6 +107,7 @@ export class WebGpuBackend { throw new Error('WebGpuBackend: Failed to get GPU adapter.'); } + this.env = env; const deviceDescriptor: GPUDeviceDescriptor = { requiredLimits: { maxComputeWorkgroupStorageSize: adapter.limits.maxComputeWorkgroupStorageSize, @@ -114,8 +117,8 @@ export class WebGpuBackend { }; // WebGPU Spec: Timestamp Queries Inside Passes // https://github.com/gpuweb/gpuweb/blob/main/proposals/timestamp-query-inside-passes.md - if (adapter.features.has('timestamp-query-inside-passes') && env.webgpu.profilingMode === 'default') { - this.profilingEnabled = true; + if (adapter.features.has('timestamp-query-inside-passes')) { + this.supportTimestampQuery = true; // eslint-disable-next-line @typescript-eslint/no-explicit-any deviceDescriptor.requiredFeatures = ['timestamp-query-inside-passes' as any]; } @@ -139,7 +142,7 @@ export class WebGpuBackend { } }; - if (this.profilingEnabled) { + if (this.supportTimestampQuery) { this.profilingQuerySet = this.device.createQuerySet({ type: 'timestamp', count: 2, diff --git a/js/web/lib/wasm/jsep/webgpu/program-manager.ts b/js/web/lib/wasm/jsep/webgpu/program-manager.ts index 951e76de54..0d5289522d 100644 --- a/js/web/lib/wasm/jsep/webgpu/program-manager.ts +++ b/js/web/lib/wasm/jsep/webgpu/program-manager.ts @@ -33,8 +33,8 @@ export class ProgramManager { run(buildArtifact: Artifact, inputs: GpuData[], outputs: GpuData[], dispatchGroup: [number, number, number]): void { const device = this.backend.device; const computePassEncoder = this.backend.getComputePassEncoder(); - - if (this.backend.profilingEnabled) { + const profilingEnabled = this.backend.supportTimestampQuery && this.backend.env.webgpu.profilingMode === 'default'; + if (profilingEnabled) { // profiling write start timestamp // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -56,7 +56,7 @@ export class ProgramManager { this.backend.pendingDispatchNumber++; - if (this.backend.profilingEnabled) { + if (profilingEnabled) { // profiling write end timestamp // eslint-disable-next-line @typescript-eslint/no-explicit-any