mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-16 21:00:14 +00:00
### Description This PR rewrite the backend resolve logic to support specifying multiple EPs. #### Backend The first version of ONNX Runtime Web actually carried some existing code from [ONNX.js](https://github.com/microsoft/onnxjs), which includes the "backend" concept. The original "backend" in ONNX.js is designed in a way assuming there is only one backend from user's backend hint list will be used. For example, in ONNX.js, if user specify a backend hint as `['webgl', 'wasm']`, ONNX.js will first try to use WebGL backend - if it loads successfully (the browser supports webgl), then "webgl" backend will be used and "wasm" will be ignored; otherwise, "webgl" will be ignored and try to load "wasm" backend. In short: only one backend will be used when initializing a session. #### Execution Provider Execution Provider, or EP, in ONNX Runtime is a different concept. One of the differences is that users are allow to specify multiple EPs, and if one does not support a particular kernel, it can fallback to other EP. This is a very common case when using a GPU EP in ONNX Runtime. #### Current Status: Backend v.s. EP Because of the history reasons mentioned above, the current status is quite confusing. There are **real backend**s, which means it's different implementation in code; and there are **backend hint**s, which are used as string names for backend hint; and there are **EP**s of the ONNX Runtime concepts. currently there are only 2 **backend**s in our code base: The "onnxjs backend", and the "wasm backend". The "onnxjs backend" currently only powers backend hint "webgl", which go into the old onnx.js code path. All other backend hints including "wasm", "cpu"(alias to wasm), "webgpu" and "webnn" are all powered by "wasm backend". And because ORT Web treat "backend" as an internal concept and want to align with ONNX Runtime, so those names of backend hints are becoming EP names. The following table shows today's status: | Execution Provider Name (public) / Backend Hint (internal) | Backend | EP in ORT | -------- | ------- | ------- | | "wasm"/"cpu" | WasmBackend | CPU EP | "webgl" | OnnxjsBackend | \* technically not an EP | "webgpu" | WasmBackend | JSEP | "webnn" | WasmBackend | WebNN EP #### Problem While the API allows to specify multiple EPs, the backend resolving only allows one backend. This causes issues when user specify multiple EP names in session options, the backend resolve behavior and EP registration behavior is inconsistent. Specifically, in this issue: https://github.com/microsoft/onnxruntime/issues/15796#issuecomment-1925363908: EP list `['webgpu', 'wasm']` on a browser without WebGPU support resolves to 'wasm' backend, but the full EP list is passed in session options, so JSEP is still enabled, causing the runtime error. #### Solution Since we still need WebGL backend, we cannot totally remove the backend register/resolve system. In this PR I made the following changes: - initialize every backend from the EP list, instead of only do that for the first successful one. - for the first resolved backend, filter all EP using the exact same backend. Remove all EPs not using this backend from session options - for every explicitly specified EP, if it's removed, show a warning message in console
251 lines
9.8 KiB
TypeScript
251 lines
9.8 KiB
TypeScript
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
import {resolveBackendAndExecutionProviders} from './backend-impl.js';
|
|
import {SessionHandler, TrainingSessionHandler} from './backend.js';
|
|
import {InferenceSession as InferenceSession} from './inference-session.js';
|
|
import {OnnxValue} from './onnx-value.js';
|
|
import {Tensor} from './tensor.js';
|
|
import {TrainingSession as TrainingSessionInterface, TrainingSessionCreateOptions} from './training-session.js';
|
|
|
|
type SessionOptions = InferenceSession.SessionOptions;
|
|
type FeedsType = InferenceSession.FeedsType;
|
|
type FetchesType = InferenceSession.FetchesType;
|
|
type ReturnType = InferenceSession.ReturnType;
|
|
type RunOptions = InferenceSession.RunOptions;
|
|
|
|
const noBackendErrMsg: string = 'Training backend could not be resolved. ' +
|
|
'Make sure you\'re using the correct configuration & WebAssembly files.';
|
|
|
|
export class TrainingSession implements TrainingSessionInterface {
|
|
private constructor(handler: TrainingSessionHandler, hasOptimizerModel: boolean, hasEvalModel: boolean) {
|
|
this.handler = handler;
|
|
this.hasOptimizerModel = hasOptimizerModel;
|
|
this.hasEvalModel = hasEvalModel;
|
|
}
|
|
private handler: TrainingSessionHandler;
|
|
private hasOptimizerModel: boolean;
|
|
private hasEvalModel: boolean;
|
|
|
|
get trainingInputNames(): readonly string[] {
|
|
return this.handler.inputNames;
|
|
}
|
|
get trainingOutputNames(): readonly string[] {
|
|
return this.handler.outputNames;
|
|
}
|
|
|
|
get evalInputNames(): readonly string[] {
|
|
if (this.hasEvalModel) {
|
|
return this.handler.evalInputNames;
|
|
} else {
|
|
throw new Error('This training session has no evalModel loaded.');
|
|
}
|
|
}
|
|
get evalOutputNames(): readonly string[] {
|
|
if (this.hasEvalModel) {
|
|
return this.handler.evalOutputNames;
|
|
} else {
|
|
throw new Error('This training session has no evalModel loaded.');
|
|
}
|
|
}
|
|
|
|
static async create(trainingOptions: TrainingSessionCreateOptions, sessionOptions?: SessionOptions):
|
|
Promise<TrainingSession> {
|
|
const evalModel: string|Uint8Array = trainingOptions.evalModel || '';
|
|
const optimizerModel: string|Uint8Array = trainingOptions.optimizerModel || '';
|
|
const options: SessionOptions = sessionOptions || {};
|
|
|
|
// resolve backend, update session options with validated EPs, and create session handler
|
|
const [backend, optionsWithValidatedEPs] = await resolveBackendAndExecutionProviders(options);
|
|
if (backend.createTrainingSessionHandler) {
|
|
const handler = await backend.createTrainingSessionHandler(
|
|
trainingOptions.checkpointState, trainingOptions.trainModel, evalModel, optimizerModel,
|
|
optionsWithValidatedEPs);
|
|
return new TrainingSession(handler, !!trainingOptions.optimizerModel, !!trainingOptions.evalModel);
|
|
} else {
|
|
throw new Error(noBackendErrMsg);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Helper function for runTrainStep and future runStep methods that handles the type-narrowing conversion from
|
|
* the given parameters to SessionHandler.FetchesType and RunOptions.
|
|
*
|
|
* @param inputNames the feeds object is checked that they contain all input names in the provided list of input
|
|
* names.
|
|
* @param outputNames the fetches object is checked that their keys match up with valid names in the list of output
|
|
* names.
|
|
* @param feeds the required input
|
|
* @param arg1 narrowed & converted into the SessionHandler.FetchesType or RunOptions object
|
|
* @param arg2 optional RunOptions object.
|
|
* @returns
|
|
*/
|
|
typeNarrowingForRunStep(
|
|
inputNames: readonly string[], outputNames: readonly string[], feeds: FeedsType, arg1?: FetchesType|RunOptions,
|
|
arg2?: RunOptions): [SessionHandler.FetchesType, RunOptions] {
|
|
const fetches: {[name: string]: OnnxValue|null} = {};
|
|
let options: RunOptions = {};
|
|
// check inputs
|
|
if (typeof feeds !== 'object' || feeds === null || feeds instanceof Tensor || Array.isArray(feeds)) {
|
|
throw new TypeError(
|
|
'\'feeds\' must be an object that use input names as keys and OnnxValue as corresponding values.');
|
|
}
|
|
|
|
let isFetchesEmpty = true;
|
|
// determine which override is being used
|
|
if (typeof arg1 === 'object') {
|
|
if (arg1 === null) {
|
|
throw new TypeError('Unexpected argument[1]: cannot be null.');
|
|
}
|
|
if (arg1 instanceof Tensor) {
|
|
throw new TypeError('\'fetches\' cannot be a Tensor');
|
|
}
|
|
|
|
if (Array.isArray(arg1)) {
|
|
if (arg1.length === 0) {
|
|
throw new TypeError('\'fetches\' cannot be an empty array.');
|
|
}
|
|
isFetchesEmpty = false;
|
|
// output names
|
|
for (const name of arg1) {
|
|
if (typeof name !== 'string') {
|
|
throw new TypeError('\'fetches\' must be a string array or an object.');
|
|
}
|
|
if (outputNames.indexOf(name) === -1) {
|
|
throw new RangeError(`'fetches' contains invalid output name: ${name}.`);
|
|
}
|
|
fetches[name] = null;
|
|
}
|
|
|
|
if (typeof arg2 === 'object' && arg2 !== null) {
|
|
options = arg2;
|
|
} else if (typeof arg2 !== 'undefined') {
|
|
throw new TypeError('\'options\' must be an object.');
|
|
}
|
|
} else {
|
|
// decide whether arg1 is fetches or options
|
|
// if any output name is present and its value is valid OnnxValue, we consider it fetches
|
|
let isFetches = false;
|
|
const arg1Keys = Object.getOwnPropertyNames(arg1);
|
|
for (const name of outputNames) {
|
|
if (arg1Keys.indexOf(name) !== -1) {
|
|
const v = (arg1 as InferenceSession.NullableOnnxValueMapType)[name];
|
|
if (v === null || v instanceof Tensor) {
|
|
isFetches = true;
|
|
isFetchesEmpty = false;
|
|
fetches[name] = v;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isFetches) {
|
|
if (typeof arg2 === 'object' && arg2 !== null) {
|
|
options = arg2;
|
|
} else if (typeof arg2 !== 'undefined') {
|
|
throw new TypeError('\'options\' must be an object.');
|
|
}
|
|
} else {
|
|
options = arg1 as RunOptions;
|
|
}
|
|
}
|
|
} else if (typeof arg1 !== 'undefined') {
|
|
throw new TypeError('Unexpected argument[1]: must be \'fetches\' or \'options\'.');
|
|
}
|
|
|
|
// check if all inputs are in feed
|
|
for (const name of inputNames) {
|
|
if (typeof feeds[name] === 'undefined') {
|
|
throw new Error(`input '${name}' is missing in 'feeds'.`);
|
|
}
|
|
}
|
|
|
|
// if no fetches is specified, we use the full output names list
|
|
if (isFetchesEmpty) {
|
|
for (const name of outputNames) {
|
|
fetches[name] = null;
|
|
}
|
|
}
|
|
|
|
return [fetches, options];
|
|
}
|
|
|
|
/**
|
|
* Helper method for runTrainStep and any other runStep methods. Takes the ReturnType result from the SessionHandler
|
|
* and changes it into a map of Tensors.
|
|
*
|
|
* @param results
|
|
* @returns
|
|
*/
|
|
convertHandlerReturnTypeToMapOfTensors(results: SessionHandler.ReturnType): ReturnType {
|
|
const returnValue: {[name: string]: OnnxValue} = {};
|
|
for (const key in results) {
|
|
if (Object.hasOwnProperty.call(results, key)) {
|
|
const result = results[key];
|
|
if (result instanceof Tensor) {
|
|
returnValue[key] = result;
|
|
} else {
|
|
returnValue[key] = new Tensor(result.type, result.data, result.dims);
|
|
}
|
|
}
|
|
}
|
|
return returnValue;
|
|
}
|
|
|
|
async lazyResetGrad(): Promise<void> {
|
|
await this.handler.lazyResetGrad();
|
|
}
|
|
|
|
runTrainStep(feeds: FeedsType, options?: RunOptions): Promise<ReturnType>;
|
|
runTrainStep(feeds: FeedsType, fetches: FetchesType, options?: RunOptions): Promise<ReturnType>;
|
|
async runTrainStep(feeds: FeedsType, arg1?: FetchesType|RunOptions, arg2?: RunOptions): Promise<ReturnType> {
|
|
const [fetches, options] =
|
|
this.typeNarrowingForRunStep(this.trainingInputNames, this.trainingOutputNames, feeds, arg1, arg2);
|
|
const results = await this.handler.runTrainStep(feeds, fetches, options);
|
|
return this.convertHandlerReturnTypeToMapOfTensors(results);
|
|
}
|
|
|
|
async runOptimizerStep(options?: InferenceSession.RunOptions|undefined): Promise<void> {
|
|
if (this.hasOptimizerModel) {
|
|
await this.handler.runOptimizerStep(options || {});
|
|
} else {
|
|
throw new Error('This TrainingSession has no OptimizerModel loaded.');
|
|
}
|
|
}
|
|
|
|
runEvalStep(feeds: FeedsType, options?: RunOptions|undefined): Promise<ReturnType>;
|
|
runEvalStep(feeds: FeedsType, fetches: FetchesType, options?: RunOptions|undefined): Promise<ReturnType>;
|
|
async runEvalStep(feeds: FeedsType, arg1?: FetchesType|RunOptions, arg2?: RunOptions): Promise<ReturnType> {
|
|
if (this.hasEvalModel) {
|
|
const [fetches, options] =
|
|
this.typeNarrowingForRunStep(this.evalInputNames, this.evalOutputNames, feeds, arg1, arg2);
|
|
const results = await this.handler.runEvalStep(feeds, fetches, options);
|
|
return this.convertHandlerReturnTypeToMapOfTensors(results);
|
|
} else {
|
|
throw new Error('This TrainingSession has no EvalModel loaded.');
|
|
}
|
|
}
|
|
|
|
async getParametersSize(trainableOnly = true): Promise<number> {
|
|
return this.handler.getParametersSize(trainableOnly);
|
|
}
|
|
|
|
async loadParametersBuffer(array: Uint8Array, trainableOnly = true): Promise<void> {
|
|
const paramsSize = await this.getParametersSize(trainableOnly);
|
|
// checking that the size of the Uint8Array is equivalent to the byte length of a Float32Array of the number
|
|
// of parameters
|
|
if (array.length !== 4 * paramsSize) {
|
|
throw new Error(
|
|
'Size of the buffer passed into loadParametersBuffer must match the number of parameters in ' +
|
|
'the model. Please use getParametersSize method to check.');
|
|
}
|
|
return this.handler.loadParametersBuffer(array, trainableOnly);
|
|
}
|
|
|
|
async getContiguousParameters(trainableOnly = true): Promise<OnnxValue> {
|
|
return this.handler.getContiguousParameters(trainableOnly);
|
|
}
|
|
|
|
async release(): Promise<void> {
|
|
return this.handler.dispose();
|
|
}
|
|
}
|