intel / webml-polyfill

Deprecated, the Web Neural Network Polyfill project has been moved to https://github.com/webmachinelearning/webnn-polyfill
Apache License 2.0
161 stars 46 forks source link

[Backend] Upgrade tfjs-backend-wasm and webgl to 2.7.0 #1369

Closed ibelem closed 3 years ago

ibelem commented 3 years ago

Based on the article Supercharging the TensorFlow.js WebAssembly backend with SIMD and multi-threading, as of TensorFlow.js version 2.3.0, the Wasm backend has become up to 10X faster by leveraging SIMD (vector) instructions and multithreading via XNNPACK.

https://www.npmjs.com/package/@tensorflow/tfjs-backend-wasm 2.4.0 https://www.npmjs.com/package/@tensorflow/tfjs-backend-webgl 2.4.0

Please refer to https://github.com/intel/webml-polyfill/pull/1321 and also check if https://github.com/intel/webml-polyfill/pull/1332 is still needed or not.

BruceDai commented 3 years ago

@ibelem @huningxin According to tfjs-backend-wasm source code of tfjs-backend-was, the init function of wasm backend will firstly check 'WASM_HAS_SIMD_SUPPORT' and 'WASM_HAS_MULTITHREAD_SUPPORT' env variables which map 'WebAssembly SIMD support' and 'WebAssembly threads support' flags configured on chrome://flags page.

  // src/backend_wasm.ts#L239
  const [simdSupported, threadsSupported] = await Promise.all([
    env().getAsync('WASM_HAS_SIMD_SUPPORT'),
    env().getAsync('WASM_HAS_MULTITHREAD_SUPPORT')
  ]);
  ......

  return new Promise((resolve, reject) => {
    ......
   // src/backend_wasm.ts#L278
    if (threadsSupported && simdSupported && wasmPath == null) {
      wasm = wasmFactoryThreadedSimd(factoryConfig);
      wasm.mainScriptUrlOrBlob = new Blob(
          [`var _scriptDir = undefined; var WasmBackendModuleThreadedSimd = ` +
           wasmFactoryThreadedSimd.toString()],
          {type: 'text/javascript'});
    } else {
      // The wasmFactory works for both vanilla and SIMD binaries.
      wasm = wasmFactory(factoryConfig);
    }
    .....
  });

I met following error when running example with upgraded tfjs-backend-wasm.

worker.js onmessage() captured an uncaught exception: ReferenceError: t is not defined
ReferenceError: t is not defined
    at WasmBackendModuleThreadedSimd (57c7acc8-4364-463b-88cb-36511c04354f:1)
    at onmessage (6211efcd-69bf-49c3-ad76-f4dfae618eeb:1)

Any suggestions? Thanks.

BruceDai commented 3 years ago

The same error happened with latest 2.6.0.

BruceDai commented 3 years ago

Linked tensorflow/tfjs#4107

BruceDai commented 3 years ago

I try latest tfjs-backend-wasm of 2.7.0, it works.