From 7875e340cd77c2f6e772f7d65bf0b6f5ad01772f Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Sun, 17 Mar 2024 12:08:23 +0900 Subject: [PATCH 1/5] Improve: Make error friendly when isOrtFormat is undefined --- js/web/lib/onnxjs/model.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/js/web/lib/onnxjs/model.ts b/js/web/lib/onnxjs/model.ts index f9a1b6e76089d..1679330a00864 100644 --- a/js/web/lib/onnxjs/model.ts +++ b/js/web/lib/onnxjs/model.ts @@ -16,6 +16,7 @@ export class Model { constructor() {} load(buf: Uint8Array, graphInitializer?: Graph.Initializer, isOrtFormat?: boolean): void { + let onnxError: Error|undefined; if (!isOrtFormat) { // isOrtFormat === false || isOrtFormat === undefined try { @@ -25,10 +26,20 @@ export class Model { if (isOrtFormat !== undefined) { throw e; } + onnxError = e; } } - this.loadFromOrtFormat(buf, graphInitializer); + try { + this.loadFromOrtFormat(buf, graphInitializer); + return; + } 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 { From ec110f33cb83c02a2b4492037ca81bae7c6b9784 Mon Sep 17 00:00:00 2001 From: Nanashi Date: Mon, 18 Mar 2024 09:38:07 +0900 Subject: [PATCH 2/5] Delete: Delete redundant return --- js/web/lib/onnxjs/model.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/js/web/lib/onnxjs/model.ts b/js/web/lib/onnxjs/model.ts index 1679330a00864..8e689626011be 100644 --- a/js/web/lib/onnxjs/model.ts +++ b/js/web/lib/onnxjs/model.ts @@ -32,7 +32,6 @@ export class Model { try { this.loadFromOrtFormat(buf, graphInitializer); - return; } catch (e) { if (isOrtFormat !== undefined) { throw e; From d62d9425ba7b9e5ff0d0a2ae6998dd53817d5db9 Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Mon, 18 Mar 2024 09:57:11 +0900 Subject: [PATCH 3/5] Add: Add unit test --- js/node/test/unittests/lib/inference-session.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/js/node/test/unittests/lib/inference-session.ts b/js/node/test/unittests/lib/inference-session.ts index d8d961cc94398..cc27d12a59e60 100644 --- a/js/node/test/unittests/lib/inference-session.ts +++ b/js/node/test/unittests/lib/inference-session.ts @@ -68,6 +68,11 @@ describe('UnitTests - InferenceSession.create()', () => { await InferenceSession.create(new Uint8Array(0)); }, {name: 'Error', message: /No graph was found in the protobuf/}); }); + it('EXPECTED FAILURE - invalid buffer', async () => { + await assert.rejects(async () => { + await InferenceSession.create(new Uint8Array(Array.from({length: 100}, () => 42))); + }, {name: 'Error', message: /as ONNX.+as ORT/m}); + }); // #endregion it('metadata: inputNames', async () => { From 9dfc4460a87ea09773c58bc3cfe5a1149d130432 Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Wed, 20 Mar 2024 09:20:23 +0900 Subject: [PATCH 4/5] Fix: Fix unit test --- js/node/test/unittests/lib/inference-session.ts | 5 ----- js/web/test/e2e/browser-test-webgl.js | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/js/node/test/unittests/lib/inference-session.ts b/js/node/test/unittests/lib/inference-session.ts index cc27d12a59e60..d8d961cc94398 100644 --- a/js/node/test/unittests/lib/inference-session.ts +++ b/js/node/test/unittests/lib/inference-session.ts @@ -68,11 +68,6 @@ describe('UnitTests - InferenceSession.create()', () => { await InferenceSession.create(new Uint8Array(0)); }, {name: 'Error', message: /No graph was found in the protobuf/}); }); - it('EXPECTED FAILURE - invalid buffer', async () => { - await assert.rejects(async () => { - await InferenceSession.create(new Uint8Array(Array.from({length: 100}, () => 42))); - }, {name: 'Error', message: /as ONNX.+as ORT/m}); - }); // #endregion it('metadata: inputNames', async () => { diff --git a/js/web/test/e2e/browser-test-webgl.js b/js/web/test/e2e/browser-test-webgl.js index e503f38ae5735..9d55175a2db66 100644 --- a/js/web/test/e2e/browser-test-webgl.js +++ b/js/web/test/e2e/browser-test-webgl.js @@ -6,3 +6,18 @@ it('Browser E2E testing - WebGL backend', async function() { await testFunction(ort, {executionProviders: ['webgl']}); }); + +it('Browser E2E testing - invalid buffer', async () => { + try { + await ort.InferenceSession.create( + new Uint8Array(Array.from({length: 100}, () => 42)), + {executionProviders: ['webgl']} + ); + + // Should not reach here. + assert(false); + } catch (e) { + assert(e.message.includes("as ONNX format")); + assert(e.message.includes("as ORT format")); + } +}); From e1963d5a32f084d3764d2cc1a5db33d1b7fc68c2 Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Tue, 26 Mar 2024 12:50:15 +0900 Subject: [PATCH 5/5] Code: npm run format --- js/web/test/e2e/browser-test-webgl.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/js/web/test/e2e/browser-test-webgl.js b/js/web/test/e2e/browser-test-webgl.js index 9d55175a2db66..974c81d064c89 100644 --- a/js/web/test/e2e/browser-test-webgl.js +++ b/js/web/test/e2e/browser-test-webgl.js @@ -10,14 +10,12 @@ it('Browser E2E testing - WebGL backend', async function() { it('Browser E2E testing - invalid buffer', async () => { try { await ort.InferenceSession.create( - new Uint8Array(Array.from({length: 100}, () => 42)), - {executionProviders: ['webgl']} - ); + new Uint8Array(Array.from({length: 100}, () => 42)), {executionProviders: ['webgl']}); // Should not reach here. assert(false); } catch (e) { - assert(e.message.includes("as ONNX format")); - assert(e.message.includes("as ORT format")); + assert(e.message.includes('as ONNX format')); + assert(e.message.includes('as ORT format')); } });