onnxruntime/js/web/lib/backend-onnxjs.ts
Caroline Zhu e3b043ba17
[js/web/training] runTrainStep implementation (#18006)
### 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>
2023-11-02 08:32:50 -07:00

33 lines
1.4 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/* eslint-disable import/no-internal-modules */
import {Backend, InferenceSession, InferenceSessionHandler} from 'onnxruntime-common';
import {Session} from './onnxjs/session';
import {OnnxjsSessionHandler} from './onnxjs/session-handler-inference';
class OnnxjsBackend implements Backend {
// eslint-disable-next-line @typescript-eslint/no-empty-function
async init(): Promise<void> {}
async createInferenceSessionHandler(pathOrBuffer: string|Uint8Array, options?: InferenceSession.SessionOptions):
Promise<InferenceSessionHandler> {
// NOTE: Session.Config(from onnx.js) is not compatible with InferenceSession.SessionOptions(from
// onnxruntime-common).
// In future we should remove Session.Config and use InferenceSession.SessionOptions.
// Currently we allow this to happen to make test runner work.
const session = new Session(options as unknown as Session.Config);
// typescript cannot merge method override correctly (so far in 4.2.3). need if-else to call the method.
if (typeof pathOrBuffer === 'string') {
await session.loadModel(pathOrBuffer);
} else {
await session.loadModel(pathOrBuffer);
}
return new OnnxjsSessionHandler(session);
}
}
export const onnxjsBackend = new OnnxjsBackend();