Skip to content

[WebNN] Fallback unsupported integer input and output of a WebNN graph to int32 #24425

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

Merged
merged 3 commits into from
Apr 20, 2025
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
21 changes: 16 additions & 5 deletions js/web/lib/wasm/jsep/backend-webnn.ts
Original file line number Diff line number Diff line change
@@ -8,11 +8,11 @@

import { Env, Tensor } from 'onnxruntime-common';

import { DataType } from '../wasm-common';
import { DataType, tensorDataTypeStringToEnum } from '../wasm-common';
import { getInstance } from '../wasm-factory';

import { createView } from './tensor-view';
import { TensorId, createTensorManager, convertInt64ToInt32 } from './webnn/tensor-manager';
import { TensorId, createTensorManager, convertDataToInt32 } from './webnn/tensor-manager';
import { configureLogger, LOG_DEBUG } from './log';

/*
@@ -341,7 +341,8 @@ export class WebNNBackend {
case 'int64':
if (shouldConvertInt64ToInt32) {
// Int64 is not supported by current context, use int32 instead.
bufferView = convertInt64ToInt32(new Uint8Array(buffer), false) as Int32Array;
const int32Buffer = convertDataToInt32(new Uint8Array(buffer), 'int64');
bufferView = new Int32Array(int32Buffer.buffer);
desc.dataType = 'int32';
} else {
bufferView = new BigInt64Array(buffer);
@@ -397,9 +398,19 @@ export class WebNNBackend {
return outputNames.includes(outputName);
}

public isInt64Supported(sessionId: number): boolean {
public isGraphInputOutputTypeSupported(sessionId: number, type: Tensor.Type, isInput = true): boolean {
const context = this.mlContextBySessionId.get(sessionId);
return !!context?.opSupportLimits().input.dataTypes.includes('int64');
const dataType = onnxDataTypeToWebnnDataType.get(tensorDataTypeStringToEnum(type));

if (typeof dataType === 'undefined') {
return false;
}

if (isInput) {
return !!context?.opSupportLimits().input.dataTypes.includes(dataType);
} else {
return !!context?.opSupportLimits().output.dataTypes.includes(dataType);
}
}

public flush(): void {
Loading