mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-17 21:10:43 +00:00
### Description This change upgrades a lot of dependencies. There are 2 motivations of doing this change: - fix the security issue reported by dependabot (protobufjs Prototype Pollution vulnerability - https://github.com/advisories/GHSA-h755-8qp9-cq85) - resolve the requirement of using ONNX IR_VERSION 9 (#16638) This requires: - upgrade protobufjs to v7.2.4 - upgrade library 'onnx-proto' to consume latest ONNX release (v1.14.0). Problems: - protobufjs v7.2.4 depends on long.js v5, which does not work well with typescript (commonjs). - onnx-proto depends on this fix with a new release of long.js - long.js is in maintenance and it takes longer than expected to put in new changes Solutions: - use a patch script in `preprepare` to copy type declarations to make long.js work with typescript (commonjs) - generate onnx protobuf JS/TS files and put them under js/web/lib/onnxjs/ort-schema/protobuf folder - remove 'onnx-proto' from dependency. - apply fixes to generated onnx.d.ts
72 lines
2.2 KiB
TypeScript
72 lines
2.2 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 {
|
|
if (!isOrtFormat) {
|
|
// isOrtFormat === false || isOrtFormat === undefined
|
|
try {
|
|
this.loadFromOnnxFormat(buf, graphInitializer);
|
|
return;
|
|
} catch (e) {
|
|
if (isOrtFormat !== undefined) {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
this.loadFromOrtFormat(buf, graphInitializer);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|