Skip to content

Commit

Permalink
Fix worker tests using the wrong paths in importScripts (#8050)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
mattsoulanille authored Nov 2, 2023
1 parent 18c4224 commit e003c32
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions tfjs-core/src/worker_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
10 changes: 5 additions & 5 deletions tfjs-tflite/src/worker_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
Expand Down

0 comments on commit e003c32

Please sign in to comment.