onnxruntime
35 строк · 1.4 Кб
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4/* eslint-disable import/no-internal-modules */
5import { Backend, InferenceSession, InferenceSessionHandler } from 'onnxruntime-common';
6
7import { Session } from './onnxjs/session';
8import { OnnxjsSessionHandler } from './onnxjs/session-handler-inference';
9
10class OnnxjsBackend implements Backend {
11// eslint-disable-next-line @typescript-eslint/no-empty-function
12async init(): Promise<void> {}
13
14async createInferenceSessionHandler(
15pathOrBuffer: string | Uint8Array,
16options?: InferenceSession.SessionOptions,
17): Promise<InferenceSessionHandler> {
18// NOTE: Session.Config(from onnx.js) is not compatible with InferenceSession.SessionOptions(from
19// onnxruntime-common).
20// In future we should remove Session.Config and use InferenceSession.SessionOptions.
21// Currently we allow this to happen to make test runner work.
22const session = new Session(options as unknown as Session.Config);
23
24// typescript cannot merge method override correctly (so far in 4.2.3). need if-else to call the method.
25if (typeof pathOrBuffer === 'string') {
26await session.loadModel(pathOrBuffer);
27} else {
28await session.loadModel(pathOrBuffer);
29}
30
31return new OnnxjsSessionHandler(session);
32}
33}
34
35export const onnxjsBackend = new OnnxjsBackend();
36