onnxruntime/js/web/lib/onnxjs/session-handler.ts
Yulong Wang 4ebc9c3b5e
[JS] onnxruntime-web (#7394)
* add web

* add script and test

* fix lint

* add test/data/ops

* add test/data/node/ to gitignore

* modify scripts

* add onnxjs

* fix tests

* fix test-runner

* fix sourcemap

* fix onnxjs profiling

* update test list

* update README

* resolve comments

* set wasm as default backend

* rename package

* update copyright header

* do not use class "Buffer" in browser context

* revise readme
2021-04-27 00:04:25 -07:00

44 lines
1.5 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {InferenceSession, SessionHandler, Tensor} from 'onnxruntime-common';
import {Session} from './session';
import {Tensor as OnnxjsTensor} from './tensor';
export class OnnxjsSessionHandler implements SessionHandler {
constructor(private session: Session) {
this.inputNames = this.session.inputNames;
this.outputNames = this.session.outputNames;
}
async dispose(): Promise<void> {}
inputNames: readonly string[];
outputNames: readonly string[];
async run(
feeds: SessionHandler.FeedsType, _fetches: SessionHandler.FetchesType,
_options: InferenceSession.RunOptions): Promise<SessionHandler.ReturnType> {
const inputMap = new Map<string, OnnxjsTensor>();
for (const name in feeds) {
if (Object.hasOwnProperty.call(feeds, name)) {
const feed = feeds[name];
inputMap.set(
name,
new OnnxjsTensor(
feed.dims, feed.type as OnnxjsTensor.DataType, undefined, undefined,
feed.data as OnnxjsTensor.NumberType));
}
}
const outputMap = await this.session.run(inputMap);
const output: SessionHandler.ReturnType = {};
outputMap.forEach((tensor, name) => {
output[name] = new Tensor(tensor.type, tensor.data, tensor.dims);
});
return output;
}
startProfiling(): void {
this.session.startProfiling();
}
endProfiling(): void {
this.session.endProfiling();
}
}