Skip to content

Commit

Permalink
Add ONNX test file (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Nov 26, 2024
1 parent e66bfb5 commit 9ec942b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
60 changes: 27 additions & 33 deletions source/onnx.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,9 +890,6 @@ onnx.Context.Model = class {
this._functions = new Map();
for (const func of functions || []) {
const key = func.overload ? `${func.domain}:${func.name}:${func.overload}` : `${func.domain}:${func.name}`;
if (this._functions.has(key)) {
throw new onnx.Error(`Duplicate function identifier '${key}'.`);
}
func.initializer = [];
func.uses = [];
func.callers = new Set();
Expand Down Expand Up @@ -1443,6 +1440,33 @@ onnx.ProtoReader = class {

static open(context) {
const identifier = context.identifier;
const stream = context.stream;
if (stream && stream.length > 5) {
const buffer = stream.peek(Math.min(stream.length, 256));
if (buffer[0] === 0x08 && buffer[1] < 0x0B && buffer[2] === 0x12 && buffer[3] < 64 && (buffer[3] + 4) <= stream.length) {
if (buffer[3] === 0x00 && buffer[4] === 0x1A && buffer[5] === 0x00) {
return new onnx.ProtoReader(context, 'binary', 'model');
}
const producer = String.fromCharCode.apply(null, buffer.subarray(4, 4 + buffer[3]));
if (producer.match(/^[A-Za-z][A-Za-z0-9_+-. ]+$/)) {
return new onnx.ProtoReader(context, 'binary', 'model');
}
}
const length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24);
if (length === stream.length - 4) {
stream.seek(4);
try {
const reader = protobuf.BinaryReader.open(stream);
const tags = reader.signature();
if (tags.get(7) === 2) {
stream.seek(4);
return new onnx.ProtoReader(context, 'binary', 'model');
}
} catch {
// continue regardless of error
}
}
}
const binaryTags = context.tags('pb');
if (binaryTags.size > 0) {
const tags = binaryTags;
Expand Down Expand Up @@ -1526,36 +1550,6 @@ onnx.ProtoReader = class {
}
}
}
const stream = context.stream;
if (stream && stream.length > 5) {
const buffer = stream.peek(Math.min(stream.length, 256));
if (buffer[0] === 0x08 && buffer[1] < 0x0B && buffer[2] === 0x12 && buffer[3] < 64 && (buffer[3] + 4) <= stream.length) {
const producers = ['vai_q_onnx'];
const producer = String.fromCharCode.apply(null, buffer.subarray(4, 4 + buffer[3]));
if (producers.includes(producer) ||
producer.match(/^[A-Za-z][A-Za-z23]+([-_. ][A-Za-z][A-Za-z2350]+)*$/) ||
buffer[3] === 0x00 && buffer[4] === 0x1A && buffer[5] === 0x00) {
return new onnx.ProtoReader(context, 'binary', 'model');
}
}
}
if (stream && stream.length > 8) {
const buffer = stream.peek(4);
const length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24);
if (length === stream.length - 4) {
stream.seek(4);
try {
const reader = protobuf.BinaryReader.open(stream);
const tags = reader.signature();
if (tags.get(7) === 2) {
stream.seek(4);
return new onnx.ProtoReader(context, 'binary', 'model');
}
} catch {
// continue regardless of error
}
}
}
const textTags = context.tags('pbtxt');
if (textTags.size > 0) {
const tags = textTags;
Expand Down
10 changes: 10 additions & 0 deletions test/models.json
Original file line number Diff line number Diff line change
Expand Up @@ -4062,6 +4062,16 @@
"format": "ONNX v3",
"link": "https://github.com/lutzroeder/netron/issues/183"
},
{
"type": "onnx",
"target": "duplicate_function.onnx",
"source": "https://github.com/user-attachments/files/17901407/duplicate_function.onnx.zip[duplicate_function.onnx]",
"format": "ONNX v8",
"assert": "model.graphs[0].nodes[0].type.nodes[0].type.name == 'Sub'",
"link": "https://github.com/lutzroeder/netron/issues/6"
},


{
"type": "onnx",
"target": "DocumentClassification.onnx",
Expand Down

0 comments on commit 9ec942b

Please sign in to comment.