From e003c32b9283977a743b08b73aa39eee7bfdbf33 Mon Sep 17 00:00:00 2001 From: Matthew Soulanille Date: Thu, 2 Nov 2023 14:46:39 -0700 Subject: [PATCH] Fix worker tests using the wrong paths in importScripts (#8050) Worker tests were getting the path for `importScripts` by referencing `location.origin`, but this is `blob://` when accessed from the worker context. Template `location.origin` from the main context into the worker source code instead of accessing the worker's version of `location.origin`. --- tfjs-core/src/worker_test.ts | 4 ++-- tfjs-tflite/src/worker_test.ts | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tfjs-core/src/worker_test.ts b/tfjs-core/src/worker_test.ts index 7b0c932b5df..7c7ac6f07bf 100644 --- a/tfjs-core/src/worker_test.ts +++ b/tfjs-core/src/worker_test.ts @@ -26,8 +26,8 @@ const str2workerURL = (str: string): string => { // The source code of a web worker. const workerTest = ` -importScripts(location.origin + '/base/tfjs/tfjs-core/tf-core.min.js'); -importScripts(location.origin +importScripts('${location.origin}/base/tfjs/tfjs-core/tf-core.min.js'); +importScripts('${location.origin}' + '/base/tfjs/tfjs-backend-cpu/tf-backend-cpu.min.js'); let a = tf.tensor1d([1, 2, 3]); diff --git a/tfjs-tflite/src/worker_test.ts b/tfjs-tflite/src/worker_test.ts index 4865470c2df..01ef039ec8f 100644 --- a/tfjs-tflite/src/worker_test.ts +++ b/tfjs-tflite/src/worker_test.ts @@ -25,18 +25,18 @@ const str2workerURL = (str: string): string => { // The source code of a web worker. const workerTest = ` -importScripts(location.origin + '/base/tfjs/tfjs-core/tf-core.min.js'); -importScripts(location.origin +importScripts('${location.origin}/base/tfjs/tfjs-core/tf-core.min.js'); +importScripts('${location.origin}' + '/base/tfjs/tfjs-backend-cpu/tf-backend-cpu.min.js'); // Import order matters. TFLite must be imported after tfjs core. -importScripts(location.origin + '/base/tfjs/tfjs-tflite/tf-tflite.min.js'); +importScripts('${location.origin}/base/tfjs/tfjs-tflite/tf-tflite.min.js'); // Setting wasm path is required. It can be set to CDN if needed, // but that's not a good idea for a test. -tflite.setWasmPath('/base/tfjs/tfjs-tflite/wasm/'); +tflite.setWasmPath('${location.origin}/base/tfjs/tfjs-tflite/wasm/'); async function main() { // This is a test model that adds two tensors of shape [1, 4]. - const model = await tflite.loadTFLiteModel(location.origin + '/base/tfjs/tfjs-tflite/test_files/add4.tflite'); + const model = await tflite.loadTFLiteModel('${location.origin}/base/tfjs/tfjs-tflite/test_files/add4.tflite'); const a = tf.tensor2d([[1, 2, 3, 4]]); const b = tf.tensor2d([[5, 6, 7, 8]]);