mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-18 21:21:17 +00:00
### Description * based on design document & following InferenceSession's run implementation, implemented TrainingSession.runTrainStep ### Motivation and Context * Adding web bindings for training #### Related work * #16521 allowed for training artifacts to be built * #17333 added interfaces for training * #17474 allowed for training package to be built + added training backend to web package * #17891 implementation for createTrainingSession on the TypeScript side **[SHOULD BE MERGED IN BEFORE THIS PR]** --------- Co-authored-by: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Co-authored-by: Ashwini Khade <askhade@microsoft.com>
53 lines
2 KiB
TypeScript
53 lines
2 KiB
TypeScript
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
import {cpus} from 'node:os';
|
|
import {Backend, env, InferenceSession, InferenceSessionHandler} from 'onnxruntime-common';
|
|
|
|
import {initializeWebAssemblyInstance} from './wasm/proxy-wrapper';
|
|
import {OnnxruntimeWebAssemblySessionHandler} from './wasm/session-handler-inference';
|
|
|
|
/**
|
|
* This function initializes all flags for WebAssembly.
|
|
*
|
|
* Those flags are accessible from `ort.env.wasm`. Users are allow to set those flags before the first inference session
|
|
* being created, to override default value.
|
|
*/
|
|
export const initializeFlags = (): void => {
|
|
if (typeof env.wasm.initTimeout !== 'number' || env.wasm.initTimeout < 0) {
|
|
env.wasm.initTimeout = 0;
|
|
}
|
|
|
|
if (typeof env.wasm.simd !== 'boolean') {
|
|
env.wasm.simd = true;
|
|
}
|
|
|
|
if (typeof env.wasm.proxy !== 'boolean') {
|
|
env.wasm.proxy = false;
|
|
}
|
|
|
|
if (typeof env.wasm.numThreads !== 'number' || !Number.isInteger(env.wasm.numThreads) || env.wasm.numThreads <= 0) {
|
|
const numCpuLogicalCores = typeof navigator === 'undefined' ? cpus().length : navigator.hardwareConcurrency;
|
|
env.wasm.numThreads = Math.min(4, Math.ceil((numCpuLogicalCores || 1) / 2));
|
|
}
|
|
};
|
|
|
|
export class OnnxruntimeWebAssemblyBackend implements Backend {
|
|
async init(): Promise<void> {
|
|
// populate wasm flags
|
|
initializeFlags();
|
|
|
|
// init wasm
|
|
await initializeWebAssemblyInstance();
|
|
}
|
|
createInferenceSessionHandler(path: string, options?: InferenceSession.SessionOptions):
|
|
Promise<InferenceSessionHandler>;
|
|
createInferenceSessionHandler(buffer: Uint8Array, options?: InferenceSession.SessionOptions):
|
|
Promise<InferenceSessionHandler>;
|
|
async createInferenceSessionHandler(pathOrBuffer: string|Uint8Array, options?: InferenceSession.SessionOptions):
|
|
Promise<InferenceSessionHandler> {
|
|
const handler = new OnnxruntimeWebAssemblySessionHandler();
|
|
await handler.loadModel(pathOrBuffer, options);
|
|
return Promise.resolve(handler);
|
|
}
|
|
}
|