onnxruntime

Форк
0
/
backend-onnxjs.ts 
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 */
5
import { Backend, InferenceSession, InferenceSessionHandler } from 'onnxruntime-common';
6

7
import { Session } from './onnxjs/session';
8
import { OnnxjsSessionHandler } from './onnxjs/session-handler-inference';
9

10
class OnnxjsBackend implements Backend {
11
  // eslint-disable-next-line @typescript-eslint/no-empty-function
12
  async init(): Promise<void> {}
13

14
  async createInferenceSessionHandler(
15
    pathOrBuffer: string | Uint8Array,
16
    options?: 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.
22
    const 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.
25
    if (typeof pathOrBuffer === 'string') {
26
      await session.loadModel(pathOrBuffer);
27
    } else {
28
      await session.loadModel(pathOrBuffer);
29
    }
30

31
    return new OnnxjsSessionHandler(session);
32
  }
33
}
34

35
export const onnxjsBackend = new OnnxjsBackend();
36

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.