onnxruntime/js/web/lib/wasm/proxy-messages.ts
Yulong Wang 9a61388f0a
[js/web] revise backend registration (#18715)
### Description
This PR revises the backend registration.

The following describes the expected behavior after this change:
(**bolded are changed behavior**)

- (ort.min.js - built without webgpu support)
    - loading: do not register 'webgpu' backend
- creating session without EP list: use default EP list ['webnn', 'cpu',
'wasm']
- creating session with ['webgpu'] as EP list: should fail with backend
not available
- (ort.webgpu.min.js - built with webgpu support)
    - loading: **always register 'webgpu' backend**
( previous behavior: only register 'webgpu' backend when `navigator.gpu`
is available)
- creating session without EP list: use default EP list ['webgpu',
'webnn', 'cpu', 'wasm']
        - when WebGPU is available (win): use WebGPU backend
- when WebGPU is unavailable (android): **should fail backend init,**
and try to use next backend in the list, 'webnn'
(previous behavior: does not fail backend init, but fail in JSEP init,
which was too late to switch to next backend)
    - creating session with ['webgpu'] as EP list
        - when WebGPU is available (win): use WebGPU backend
- when WebGPU is unavailable (android): **should fail backend init, and
because no more EP listed, fail.


related PRs: #18190 #18144
2023-12-20 14:45:55 -08:00

91 lines
2.7 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import type {Env, InferenceSession, Tensor} from 'onnxruntime-common';
/**
* Among all the tensor locations, only 'cpu' is serializable.
*/
export type SerializableTensorMetadata =
[dataType: Tensor.Type, dims: readonly number[], data: Tensor.DataType, location: 'cpu'];
export type GpuBufferMetadata = {
gpuBuffer: Tensor.GpuBufferType;
download?: () => Promise<Tensor.DataTypeMap[Tensor.GpuBufferDataTypes]>;
dispose?: () => void;
};
/**
* Tensors on location 'cpu-pinned' and 'gpu-buffer' are not serializable.
*/
export type UnserializableTensorMetadata =
[dataType: Tensor.Type, dims: readonly number[], data: GpuBufferMetadata, location: 'gpu-buffer']|
[dataType: Tensor.Type, dims: readonly number[], data: Tensor.DataType, location: 'cpu-pinned'];
/**
* Tensor metadata is a tuple of [dataType, dims, data, location], where
* - dataType: tensor data type
* - dims: tensor dimensions
* - data: tensor data, which can be one of the following depending on the location:
* - cpu: Uint8Array
* - cpu-pinned: Uint8Array
* - gpu-buffer: GpuBufferMetadata
* - location: tensor data location
*/
export type TensorMetadata = SerializableTensorMetadata|UnserializableTensorMetadata;
export type SerializableSessionMetadata = [sessionHandle: number, inputNames: string[], outputNames: string[]];
export type SerializableInternalBuffer = [bufferOffset: number, bufferLength: number];
interface MessageError {
err?: string;
}
interface MessageInitWasm extends MessageError {
type: 'init-wasm';
in ?: Env;
out?: never;
}
interface MessageInitEp extends MessageError {
type: 'init-ep';
in ?: {env: Env; epName: string};
out?: never;
}
interface MessageCopyFromExternalBuffer extends MessageError {
type: 'copy-from';
in ?: {buffer: Uint8Array};
out?: SerializableInternalBuffer;
}
interface MessageCreateSession extends MessageError {
type: 'create';
in ?: {model: SerializableInternalBuffer|Uint8Array; options?: InferenceSession.SessionOptions};
out?: SerializableSessionMetadata;
}
interface MessageReleaseSession extends MessageError {
type: 'release';
in ?: number;
out?: never;
}
interface MessageRun extends MessageError {
type: 'run';
in ?: {
sessionId: number; inputIndices: number[]; inputs: SerializableTensorMetadata[]; outputIndices: number[];
options: InferenceSession.RunOptions;
};
out?: SerializableTensorMetadata[];
}
interface MesssageEndProfiling extends MessageError {
type: 'end-profiling';
in ?: number;
out?: never;
}
export type OrtWasmMessage = MessageInitWasm|MessageInitEp|MessageCopyFromExternalBuffer|MessageCreateSession|
MessageReleaseSession|MessageRun|MesssageEndProfiling;