Skip to content

Commit 4538d31

Browse files
authored
[js/webgpu] expose a few properties in WebGPU API (#19857)
### Description This change exposes a few properties in `ort.env.webgpu` to resolve feature requirement mentioned in properties in #14579 (comment). - Add `powerPreference` and `forceFallbackAdapter` in `ort.env.webgpu`, to allow users to set the value of the properties before the first inference session is created. - Add readonly property `adapter` in `ort.env.webgpu` to allow users to get the adapter instance. Now users can access `ort.env.webgpu.device` and `ort.env.webgpu.adapter`. @xenova @beaufortfrancois
1 parent 22ad629 commit 4538d31

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Diff for: js/common/lib/env.ts

+35
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,44 @@ export declare namespace Env {
143143
*/
144144
ondata?: (data: WebGpuProfilingData) => void;
145145
};
146+
/**
147+
* Set or get the power preference.
148+
*
149+
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
150+
* used as options for `navigator.gpu.requestAdapter()`.
151+
*
152+
* See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.
153+
*
154+
* @defaultValue `undefined`
155+
*/
156+
powerPreference?: 'low-power'|'high-performance';
157+
/**
158+
* Set or get the force fallback adapter flag.
159+
*
160+
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
161+
* used as options for `navigator.gpu.requestAdapter()`.
162+
*
163+
* See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.
164+
*
165+
* @defaultValue `undefined`
166+
*/
167+
forceFallbackAdapter?: boolean;
168+
/**
169+
* Get the adapter for WebGPU.
170+
*
171+
* This property is only available after the first WebGPU inference session is created.
172+
*
173+
* When use with TypeScript, the type of this property is `GPUAdapter` defined in "@webgpu/types".
174+
* Use `const adapter = env.webgpu.adapter as GPUAdapter;` in TypeScript to access this property with correct type.
175+
*
176+
* see comments on {@link GpuBufferType}
177+
*/
178+
readonly adapter: unknown;
146179
/**
147180
* Get the device for WebGPU.
148181
*
182+
* This property is only available after the first WebGPU inference session is created.
183+
*
149184
* When use with TypeScript, the type of this property is `GPUDevice` defined in "@webgpu/types".
150185
* Use `const device = env.webgpu.device as GPUDevice;` in TypeScript to access this property with correct type.
151186
*

Diff for: js/web/lib/wasm/jsep/backend-webgpu.ts

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ export class WebGpuBackend {
231231
};
232232

233233
Object.defineProperty(this.env.webgpu, 'device', {value: this.device});
234+
Object.defineProperty(this.env.webgpu, 'adapter', {value: adapter});
234235

235236
// init queryType, which is necessary for InferenceSession.create
236237
this.setQueryType();

Diff for: js/web/lib/wasm/wasm-core-impl.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ export const initEp = async(env: Env, epName: string): Promise<void> => {
8989
if (typeof navigator === 'undefined' || !navigator.gpu) {
9090
throw new Error('WebGPU is not supported in current environment');
9191
}
92-
const adapter = await navigator.gpu.requestAdapter();
92+
const powerPreference = env.webgpu?.powerPreference;
93+
if (powerPreference !== undefined && powerPreference !== 'low-power' && powerPreference !== 'high-performance') {
94+
throw new Error(`Invalid powerPreference setting: "${powerPreference}"`);
95+
}
96+
const forceFallbackAdapter = env.webgpu?.forceFallbackAdapter;
97+
if (forceFallbackAdapter !== undefined && typeof forceFallbackAdapter !== 'boolean') {
98+
throw new Error(`Invalid forceFallbackAdapter setting: "${forceFallbackAdapter}"`);
99+
}
100+
const adapter = await navigator.gpu.requestAdapter({powerPreference, forceFallbackAdapter});
93101
if (!adapter) {
94102
throw new Error(
95103
'Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.');

0 commit comments

Comments
 (0)