onnxruntime/js/web/lib/onnxjs/session-handler.ts
Caroline Zhu 6a5f469d44
Add training interfaces to js/common (#17333)
### Description
Following the design document:
* Added CreateTrainingSessionHandler to the Backend interface
* All existing Backend implementations throw an error for the new method
createTrainingSessionHandler
* Created TrainingSession namespace, interface, and
TrainingSessionFactory interface
* Created TrainingSessionImpl class implementation 

As methods are implemented, the TrainingSession interface will be added
to or modified.

### Motivation and Context
Adding the public-facing interfaces to the onnxruntime-common package is
one of the first steps to support ORT training for web bindings.

---------

Co-authored-by: Caroline Zhu <carolinezhu@microsoft.com>
2023-09-29 19:05:10 -07:00

45 lines
1.5 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {InferenceSession, InferenceSessionHandler, SessionHandler, Tensor} from 'onnxruntime-common';
import {Session} from './session';
import {Tensor as OnnxjsTensor} from './tensor';
export class OnnxjsSessionHandler implements InferenceSessionHandler {
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();
}
}