mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-16 21:00:14 +00:00
* working on re-organizing js code for ortweb * remove dup files * move folder * fix common references * fix common es5 * add webpack to common * split interfact/impl * use cjs for node * add npmignore for common * update sourcemap config for common * update node * adjust folder/path in CI and build * update folder * nit: readme * add bundle for dev * correct nodejs paths * enable ORT_API_MANUAL_INIT * set name for umd library * correct name for commonjs export * add priority into registerBackend() * fix npm ci pwd * update eslintrc * revise code * revert package-lock lockfileVersion 2->1 * update prebuild * resolve comments * update document * revise eslint config * update eslint for typescript rules * revert changes by mistake in backend.ts * add env * resolve comments
64 lines
2 KiB
TypeScript
64 lines
2 KiB
TypeScript
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
import {Backend, InferenceSession, SessionHandler} from 'onnxruntime-common';
|
|
import {Binding, binding} from './binding';
|
|
|
|
class OnnxruntimeSessionHandler implements SessionHandler {
|
|
#inferenceSession: Binding.InferenceSession;
|
|
|
|
constructor(pathOrBuffer: string|Uint8Array, options: InferenceSession.SessionOptions) {
|
|
this.#inferenceSession = new binding.InferenceSession();
|
|
if (typeof pathOrBuffer === 'string') {
|
|
this.#inferenceSession.loadModel(pathOrBuffer, options);
|
|
} else {
|
|
this.#inferenceSession.loadModel(pathOrBuffer.buffer, pathOrBuffer.byteOffset, pathOrBuffer.byteLength, options);
|
|
}
|
|
this.inputNames = this.#inferenceSession.inputNames;
|
|
this.outputNames = this.#inferenceSession.outputNames;
|
|
}
|
|
|
|
async dispose(): Promise<void> {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
readonly inputNames: string[];
|
|
readonly outputNames: string[];
|
|
|
|
|
|
async run(feeds: SessionHandler.FeedsType, fetches: SessionHandler.FetchesType, options: InferenceSession.RunOptions):
|
|
Promise<SessionHandler.ReturnType> {
|
|
return new Promise((resolve, reject) => {
|
|
process.nextTick(() => {
|
|
try {
|
|
resolve(this.#inferenceSession.run(feeds, fetches, options));
|
|
} catch (e) {
|
|
// reject if any error is thrown
|
|
reject(e);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
class OnnxruntimeBackend implements Backend {
|
|
async init(): Promise<void> {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async createSessionHandler(pathOrBuffer: string|Uint8Array, options?: InferenceSession.SessionOptions):
|
|
Promise<SessionHandler> {
|
|
return new Promise((resolve, reject) => {
|
|
process.nextTick(() => {
|
|
try {
|
|
resolve(new OnnxruntimeSessionHandler(pathOrBuffer, options || {}));
|
|
} catch (e) {
|
|
// reject if any error is thrown
|
|
reject(e);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
export const onnxruntimeBackend = new OnnxruntimeBackend();
|