Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tfjs-backend-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ setWasmPath(yourCustomPath); // or tf.wasm.setWasmPath when using <script> tags.
tf.setBackend('wasm').then(() => {...});
```

If you are using platform that does not support fetch directly, please set the
optional `usePlatformFetch` to true:

```ts
import {setWasmPath} from '@tensorflow/tfjs-backend-wasm';
const usePlatformFetch = true;
setWasmPath(yourCustomPath, usePlatformFetch); // or tf.wasm.setWasmPath when using <script> tags.
tf.setBackend('wasm').then(() => {...});
```
## Benchmarks

The benchmarks below show inference times (ms) for two different edge-friendly
Expand Down
40 changes: 35 additions & 5 deletions tfjs-backend-wasm/src/backend_wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,29 @@ registerBackend('wasm', async () => {
return new BackendWasm(wasm);
}, WASM_PRIORITY);

function createInstantiateWasmFunc(path: string) {
// tslint:disable-next-line:no-any
return (imports: any, callback: any) => {
util.fetch(path, {credentials: 'same-origin'}).then((response) => {
if (!response['ok']) {
imports.env.a(`failed to load wasm binary file at '${path}'`);
}
response.arrayBuffer().then(binary => {
WebAssembly.instantiate(binary, imports).then(output => {
callback(output.instance);
});
});
});
return {};
};
}

/**
* Initializes the wasm module and creates the js <--> wasm bridge.
*
* NOTE: We wrap the wasm module in a object with property 'wasm' instead of
* returning Promise<BackendWasmModule> to avoid freezing Chrome (last tested in
* Chrome 76).
* returning Promise<BackendWasmModule> to avoid freezing Chrome (last tested
* in Chrome 76).
*/
export async function init(): Promise<{wasm: BackendWasmModule}> {
return new Promise((resolve, reject) => {
Expand All @@ -192,6 +209,12 @@ export async function init(): Promise<{wasm: BackendWasmModule}> {
}
return prefix + path;
};
// use wasm instantiateWasm override when system fetch is not available.
// For detail references
// https://github.com/emscripten-core/emscripten/blob/2bca083cbbd5a4133db61fbd74d04f7feecfa907/tests/manual_wasm_instantiate.html#L170
if (customFetch) {
factoryConfig.instantiateWasm = createInstantiateWasmFunc(wasmPath);
}
}
const wasm = wasmFactory(factoryConfig);
const voidReturnType: string = null;
Expand Down Expand Up @@ -220,7 +243,8 @@ export async function init(): Promise<{wasm: BackendWasmModule}> {
return;
}
if (initAborted) {
// Emscripten calls `onAbort` twice, resulting in double error messages.
// Emscripten calls `onAbort` twice, resulting in double error
// messages.
return;
}
initAborted = true;
Expand Down Expand Up @@ -248,24 +272,30 @@ function typedArrayFromBuffer(

let wasmPath: string = null;
let initAborted = false;

let customFetch = false;
/**
* Sets the path to the `.wasm` file which will be fetched when the wasm
* backend is initialized. See
* https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-wasm/README.md#using-bundlers
* for more details.
* @param path wasm file path or url
* @param usePlatformFetch optional boolean to use platform fetch to download
* the wasm file, default to false.
*/
/** @doc {heading: 'Environment', namespace: 'wasm'} */
export function setWasmPath(path: string): void {
export function setWasmPath(path: string, usePlatformFetch = false): void {
if (initAborted) {
throw new Error(
'The WASM backend was already initialized. Make sure you call ' +
'`setWasmPath()` before you call `tf.setBackend()` or `tf.ready()`');
}
wasmPath = path;
customFetch = usePlatformFetch;
}

/** Used in unit tests. */
export function resetWasmPath(): void {
wasmPath = null;
customFetch = false;
initAborted = false;
}
1 change: 1 addition & 0 deletions tfjs-backend-wasm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
*/

import './kernels/all_kernels';

export {BackendWasm, setWasmPath} from './backend_wasm';
export {version as version_wasm} from './version';
31 changes: 30 additions & 1 deletion tfjs-backend-wasm/src/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import * as tf from '@tensorflow/tfjs-core';
import {registerBackend, removeBackend, test_util} from '@tensorflow/tfjs-core';
import {registerBackend, removeBackend, test_util, util} from '@tensorflow/tfjs-core';
// tslint:disable-next-line:no-imports-from-dist
import {ALL_ENVS, BROWSER_ENVS, describeWithFlags} from '@tensorflow/tfjs-core/dist/jasmine_util';

Expand Down Expand Up @@ -79,6 +79,35 @@ describeWithFlags('wasm init', BROWSER_ENVS, () => {
expect(wasmPath).toBe('invalid/path');
});

it('backend init works when the path is valid and use platform fetch',
async () => {
const usePlatformFetch = true;
const validPath = '/base/wasm-out/tfjs-backend-wasm.wasm';
setWasmPath(validPath, usePlatformFetch);
let wasmPath: string;
const realFetch = util.fetch;
spyOn(util, 'fetch').and.callFake((path: string) => {
wasmPath = path;
return realFetch(path);
});
expect(await tf.setBackend('wasm-test')).toBe(true);
expect(wasmPath).toBe(validPath);
});

it('backend init fails when the path is invalid and use platform fetch',
async () => {
const usePlatformFetch = true;
setWasmPath('invalid/path', usePlatformFetch);
let wasmPath: string;
const realFetch = util.fetch;
spyOn(util, 'fetch').and.callFake((path: string) => {
wasmPath = path;
return realFetch(path);
});
expect(await tf.setBackend('wasm-test')).toBe(false);
expect(wasmPath).toBe('invalid/path');
});

it('backend init succeeds with default path', async () => {
expect(await tf.setBackend('wasm-test')).toBe(true);
});
Expand Down
1 change: 1 addition & 0 deletions tfjs-backend-wasm/wasm-out/tfjs-backend-wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface BackendWasmModule extends EmscriptenModule {

export interface WasmFactoryConfig {
locateFile?(path: string, prefix: string): string;
instantiateWasm?: Function;
}

declare var moduleFactory: (settings: WasmFactoryConfig) => BackendWasmModule;
Expand Down