Skip to content

Commit

Permalink
[WebNN] Fix MLTensorUsage is undefined issue (#22831)
Browse files Browse the repository at this point in the history
`MLTensorUsage` has been removed from Chromium:
https://chromium-review.googlesource.com/c/chromium/src/+/6015318, but
we still need to make it compatible with old Chrome versions, so just
make it `undefined` for latest Chrome version.
  • Loading branch information
Honry authored Nov 14, 2024
1 parent f423b73 commit 8268120
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions js/web/lib/wasm/jsep/webnn/tensor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class TensorIdTracker {
}

// eslint-disable-next-line no-bitwise
const usage = MLTensorUsage.READ | MLTensorUsage.WRITE;
const usage = typeof MLTensorUsage == 'undefined' ? undefined : MLTensorUsage.READ | MLTensorUsage.WRITE;
this.wrapper = await this.tensorManager.getCachedTensor(dataType, shape, usage, true, true);

if (copyOld && this.activeUpload) {
Expand Down Expand Up @@ -349,7 +349,7 @@ class TensorManagerImpl implements TensorManager {
public async getCachedTensor(
dataType: MLOperandDataType,
shape: readonly number[],
usage: MLTensorUsageFlags,
usage: MLTensorUsageFlags | undefined,
writable: boolean,
readable: boolean,
): Promise<TensorWrapper> {
Expand Down
3 changes: 2 additions & 1 deletion js/web/lib/wasm/jsep/webnn/webnn.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ declare const MLTensorUsage: {
};

interface MLTensorDescriptor extends MLOperandDescriptor {
usage: MLTensorUsageFlags;
/** @deprecated Use readable/writeable instead of usage */
usage: MLTensorUsageFlags | undefined;
importableToWebGPU?: boolean;
readable?: boolean;
writable?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions js/web/test/test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ async function createMLTensorForOutput(mlContext: MLContext, type: ort.Tensor.Ty
shape: dims as number[],
// Assign both shape and dimensions while transitioning to new API.
dimensions: dims as number[],
usage: MLTensorUsage.READ,
usage: typeof MLTensorUsage == 'undefined' ? undefined : MLTensorUsage.READ,
readable: true,
});

Expand All @@ -686,7 +686,7 @@ async function createMLTensorForInput(mlContext: MLContext, cpuTensor: ort.Tenso
shape: cpuTensor.dims as number[],
// Assign both shape and dimensions while transitioning to new API.
dimensions: cpuTensor.dims as number[],
usage: MLTensorUsage.WRITE,
usage: typeof MLTensorUsage == 'undefined' ? undefined : MLTensorUsage.WRITE,
writable: true,
});
mlContext.writeTensor(mlTensor, cpuTensor.data);
Expand Down

0 comments on commit 8268120

Please sign in to comment.