onnxruntime/js/web/lib/onnxjs/model.ts
Yulong Wang abdc31de40
[js] change default formatter for JavaScript/TypeScript from clang-format to Prettier (#21728)
### Description

See
454996d496
for manual changes (excluded auto-generated formatting changes)

### Why

Because the toolsets for old clang-format is out-of-date. This reduces
the development efficiency.

- The NPM package `clang-format` is already in maintenance mode. not
updated since 2 years ago.
- The VSCode extension for clang-format is not maintained for a while,
and a recent Node.js security update made it not working at all in
Windows.

No one in community seems interested in fixing those.

Choose Prettier as it is the most popular TS/JS formatter.

### How to merge

It's easy to break the build:
- Be careful of any new commits on main not included in this PR.
- Be careful that after this PR is merged, other PRs that already passed
CI can merge.

So, make sure there is no new commits before merging this one, and
invalidate js PRs that already passed CI, force them to merge to latest.
2024-08-14 16:51:22 -07:00

84 lines
2.6 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { flatbuffers } from 'flatbuffers';
import { Graph } from './graph';
import { OpSet } from './opset';
import { onnxruntime } from './ort-schema/flatbuffers/ort-generated';
import { onnx } from './ort-schema/protobuf/onnx';
import { LongUtil } from './util';
import ortFbs = onnxruntime.experimental.fbs;
export class Model {
// empty model
constructor() {}
load(buf: Uint8Array, graphInitializer?: Graph.Initializer, isOrtFormat?: boolean): void {
let onnxError: Error | undefined;
if (!isOrtFormat) {
// isOrtFormat === false || isOrtFormat === undefined
try {
this.loadFromOnnxFormat(buf, graphInitializer);
return;
} catch (e) {
if (isOrtFormat !== undefined) {
throw e;
}
onnxError = e;
}
}
try {
this.loadFromOrtFormat(buf, graphInitializer);
} catch (e) {
if (isOrtFormat !== undefined) {
throw e;
}
// Tried both formats and failed (when isOrtFormat === undefined)
throw new Error(`Failed to load model as ONNX format: ${onnxError}\nas ORT format: ${e}`);
}
}
private loadFromOnnxFormat(buf: Uint8Array, graphInitializer?: Graph.Initializer): void {
const modelProto = onnx.ModelProto.decode(buf);
const irVersion = LongUtil.longToNumber(modelProto.irVersion);
if (irVersion < 3) {
throw new Error('only support ONNX model with IR_VERSION>=3');
}
this._opsets = modelProto.opsetImport.map((i) => ({
domain: i.domain as string,
version: LongUtil.longToNumber(i.version!),
}));
this._graph = Graph.from(modelProto.graph!, graphInitializer);
}
private loadFromOrtFormat(buf: Uint8Array, graphInitializer?: Graph.Initializer): void {
const fb = new flatbuffers.ByteBuffer(buf);
const ortModel = ortFbs.InferenceSession.getRootAsInferenceSession(fb).model()!;
const irVersion = LongUtil.longToNumber(ortModel.irVersion());
if (irVersion < 3) {
throw new Error('only support ONNX model with IR_VERSION>=3');
}
this._opsets = [];
for (let i = 0; i < ortModel.opsetImportLength(); i++) {
const opsetId = ortModel.opsetImport(i)!;
this._opsets.push({ domain: opsetId?.domain() as string, version: LongUtil.longToNumber(opsetId.version()!) });
}
this._graph = Graph.from(ortModel.graph()!, graphInitializer);
}
private _graph: Graph;
get graph(): Graph {
return this._graph;
}
private _opsets: OpSet[];
get opsets(): readonly OpSet[] {
return this._opsets;
}
}