Skip to content

Commit

Permalink
[js/web] upgrade version of flatbuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
fs-eire committed Jan 30, 2025
1 parent 5407c69 commit fc85fdb
Show file tree
Hide file tree
Showing 47 changed files with 3,507 additions and 4,118 deletions.
8 changes: 3 additions & 5 deletions js/web/lib/onnxjs/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

import Long from 'long';

import { onnxruntime } from './ort-schema/flatbuffers/ort-generated';
import * as ortFbs from './ort-schema/flatbuffers/ort-generated';
import { onnx } from './ort-schema/protobuf/onnx';
import { Tensor } from './tensor';
import { decodeUtf8String, LongUtil } from './util';

import ortFbs = onnxruntime.experimental.fbs;

export declare namespace Attribute {
export interface DataTypeMap {
float: number;
Expand Down Expand Up @@ -136,12 +134,12 @@ export class Attribute {

// cast LONG to number
if (attrType === onnx.AttributeProto.AttributeType.INT && LongUtil.isLong(value)) {
return LongUtil.longToNumber(value as Long | flatbuffers.Long);
return LongUtil.longToNumber(value as bigint | Long);
}

// cast LONG[] to number[]
if (attrType === onnx.AttributeProto.AttributeType.INTS) {
const arr = value as Array<number | Long | flatbuffers.Long>;
const arr = value as Array<number | Long | bigint>;
const numberValue: number[] = new Array<number>(arr.length);

for (let i = 0; i < arr.length; i++) {
Expand Down
4 changes: 1 addition & 3 deletions js/web/lib/onnxjs/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
// Licensed under the MIT License.

import { Attribute } from './attribute';
import { onnxruntime } from './ort-schema/flatbuffers/ort-generated';
import * as ortFbs from './ort-schema/flatbuffers/ort-generated';
import { onnx } from './ort-schema/protobuf/onnx';
import { Tensor } from './tensor';
import { LongUtil, MAX_CLIP, MIN_CLIP, ProtoUtil } from './util';

import ortFbs = onnxruntime.experimental.fbs;

export declare namespace Graph {
export interface Shape {
readonly dims: readonly number[];
Expand Down
6 changes: 2 additions & 4 deletions js/web/lib/onnxjs/model.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { flatbuffers } from 'flatbuffers';
import * as flatbuffers from 'flatbuffers';

import { Graph } from './graph';
import { OpSet } from './opset';
import { onnxruntime } from './ort-schema/flatbuffers/ort-generated';
import * as ortFbs 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() {}
Expand Down
14 changes: 8 additions & 6 deletions js/web/lib/onnxjs/ort-schema/flatbuffers/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# ORT Format File

This directory contains [the generated ts file](ort-generated.ts) necessary to support the ORT file format. The file is generated from [the ORT file format schema](https://github.com/microsoft/onnxruntime/blob/d42399e1b07ce61e95aae88bc6b6ea5dcaae2011/onnxruntime/core/flatbuffers/schema/ort.fbs). Please do not directly modify [the generated ts header file](ort-generated.ts).
This directory contains [the generated ts file](ort-generated.ts) necessary to support the ORT file format. The file is generated from [the ORT file format schema](https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/core/flatbuffers/schema/ort.fbs). Please do not directly modify [the generated ts header file](ort-generated.ts).

[The ORT file format schema](https://github.com/microsoft/onnxruntime/blob/d42399e1b07ce61e95aae88bc6b6ea5dcaae2011/onnxruntime/core/flatbuffers/schema/ort.fbs) uses [FlatBuffers](https://github.com/google/flatbuffers) serialization library. To update [its generated ts file](ort-generated.ts),
[The ORT file format schema](https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/core/flatbuffers/schema/ort.fbs) uses [FlatBuffers](https://github.com/google/flatbuffers) serialization library. To update [its generated ts file](ort-generated.ts),

1. Download or locate the [ort.fbs](https://github.com/microsoft/onnxruntime/blob/d42399e1b07ce61e95aae88bc6b6ea5dcaae2011/onnxruntime/core/flatbuffers/schema/ort.fbs) file.
2. Download FlatBuffers compiler: Download the latest flatc tool (flatc_windows.zip) from [Flatbuffers Release Page](https://github.com/google/flatbuffers/releases). Unzip and run
1. Download or locate the [ort.fbs](https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/core/flatbuffers/schema/ort.fbs) file.
2. Download FlatBuffers compiler: Download the latest flatc tool (Windows.flatc.binary.zip) from [Flatbuffers Release Page](https://github.com/google/flatbuffers/releases). Unzip and run

`> flatc_windows.exe --ts <path to ort.fbs>`
`> flatc.exe --ts ort.fbs`

This should result in ort-generated.ts being updated.
copy the generated folder `onnxruntime` to `js/web/lib/onnxjs/ort-schema/flatbuffers/` directory.

Update ort-generated.ts to re-export from the generated file fbs.ts under the onnxruntime directory.
41 changes: 41 additions & 0 deletions js/web/lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// automatically generated by the FlatBuffers compiler, do not modify

/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */

export { ArgType } from './fbs/arg-type.js';
export { ArgTypeAndIndex } from './fbs/arg-type-and-index.js';
export { Attribute } from './fbs/attribute.js';
export { AttributeType } from './fbs/attribute-type.js';
export { DeprecatedKernelCreateInfos } from './fbs/deprecated-kernel-create-infos.js';
export { DeprecatedNodeIndexAndKernelDefHash } from './fbs/deprecated-node-index-and-kernel-def-hash.js';
export { DeprecatedSessionState } from './fbs/deprecated-session-state.js';
export { DeprecatedSubGraphSessionState } from './fbs/deprecated-sub-graph-session-state.js';
export { Dimension } from './fbs/dimension.js';
export { DimensionValue } from './fbs/dimension-value.js';
export { DimensionValueType } from './fbs/dimension-value-type.js';
export { EdgeEnd } from './fbs/edge-end.js';
export { Graph } from './fbs/graph.js';
export { InferenceSession } from './fbs/inference-session.js';
export { KernelTypeStrArgsEntry } from './fbs/kernel-type-str-args-entry.js';
export { KernelTypeStrResolver } from './fbs/kernel-type-str-resolver.js';
export { MapType } from './fbs/map-type.js';
export { Model } from './fbs/model.js';
export { Node } from './fbs/node.js';
export { NodeEdge } from './fbs/node-edge.js';
export { NodeType } from './fbs/node-type.js';
export { NodesToOptimizeIndices } from './fbs/nodes-to-optimize-indices.js';
export { OpIdKernelTypeStrArgsEntry } from './fbs/op-id-kernel-type-str-args-entry.js';
export { OperatorSetId } from './fbs/operator-set-id.js';
export { RuntimeOptimizationRecord } from './fbs/runtime-optimization-record.js';
export { RuntimeOptimizationRecordContainerEntry } from './fbs/runtime-optimization-record-container-entry.js';
export { RuntimeOptimizations } from './fbs/runtime-optimizations.js';
export { SequenceType } from './fbs/sequence-type.js';
export { Shape } from './fbs/shape.js';
export { SparseTensor } from './fbs/sparse-tensor.js';
export { StringStringEntry } from './fbs/string-string-entry.js';
export { Tensor } from './fbs/tensor.js';
export { TensorDataType } from './fbs/tensor-data-type.js';
export { TensorTypeAndShape } from './fbs/tensor-type-and-shape.js';
export { TypeInfo } from './fbs/type-info.js';
export { TypeInfoValue } from './fbs/type-info-value.js';
export { ValueInfo } from './fbs/value-info.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// automatically generated by the FlatBuffers compiler, do not modify

/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */

import * as flatbuffers from 'flatbuffers';

import { ArgType } from '../../onnxruntime/fbs/arg-type.js';

export class ArgTypeAndIndex {
bb: flatbuffers.ByteBuffer | null = null;
bb_pos = 0;
__init(i: number, bb: flatbuffers.ByteBuffer): ArgTypeAndIndex {
this.bb_pos = i;
this.bb = bb;
return this;
}

static getRootAsArgTypeAndIndex(bb: flatbuffers.ByteBuffer, obj?: ArgTypeAndIndex): ArgTypeAndIndex {
return (obj || new ArgTypeAndIndex()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
}

static getSizePrefixedRootAsArgTypeAndIndex(bb: flatbuffers.ByteBuffer, obj?: ArgTypeAndIndex): ArgTypeAndIndex {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new ArgTypeAndIndex()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
}

argType(): ArgType {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.readInt8(this.bb_pos + offset) : ArgType.INPUT;
}

index(): number {
const offset = this.bb!.__offset(this.bb_pos, 6);
return offset ? this.bb!.readUint32(this.bb_pos + offset) : 0;
}

static startArgTypeAndIndex(builder: flatbuffers.Builder) {
builder.startObject(2);
}

static addArgType(builder: flatbuffers.Builder, argType: ArgType) {
builder.addFieldInt8(0, argType, ArgType.INPUT);
}

static addIndex(builder: flatbuffers.Builder, index: number) {
builder.addFieldInt32(1, index, 0);
}

static endArgTypeAndIndex(builder: flatbuffers.Builder): flatbuffers.Offset {
const offset = builder.endObject();
return offset;
}

static createArgTypeAndIndex(builder: flatbuffers.Builder, argType: ArgType, index: number): flatbuffers.Offset {
ArgTypeAndIndex.startArgTypeAndIndex(builder);
ArgTypeAndIndex.addArgType(builder, argType);
ArgTypeAndIndex.addIndex(builder, index);
return ArgTypeAndIndex.endArgTypeAndIndex(builder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// automatically generated by the FlatBuffers compiler, do not modify

/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */

export enum ArgType {
INPUT = 0,
OUTPUT = 1,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// automatically generated by the FlatBuffers compiler, do not modify

/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */

export enum AttributeType {
UNDEFINED = 0,
FLOAT = 1,
INT = 2,
STRING = 3,
TENSOR = 4,
GRAPH = 5,
FLOATS = 6,
INTS = 7,
STRINGS = 8,
TENSORS = 9,
GRAPHS = 10,
SPARSE_TENSOR = 11,
SPARSE_TENSORS = 12,
}
Loading

0 comments on commit fc85fdb

Please sign in to comment.