tf-backend-wasm.js
/**
   * 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).
   */
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 to avoid freezing Chrome (last tested
in Chrome 76).
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 to avoid freezing Chrome ( last tested in Chrome 76 ) .
function init() {
      return __awaiter(this, void 0, void 0, function () {
          var _a, simdSupported, threadsSupported;
          return __generator(this, function (_b) {
              switch (_b.label) {
                  case 0: return [4 , Promise.all([
                          tfjsCore.env().getAsync('WASM_HAS_SIMD_SUPPORT'),
                          tfjsCore.env().getAsync('WASM_HAS_MULTITHREAD_SUPPORT')
                      ])];
                  case 1:
                      _a = _b.sent(), simdSupported = _a[0], threadsSupported = _a[1];
                      return [2 , new Promise(function (resolve, reject) {
                              var factoryConfig = {};
                              factoryConfig.locateFile = function (path, prefix) {
                                  if (path.endsWith('.worker.js')) {
                                      var response = wasmWorkerContents;
                                      var blob = new Blob([response], { type: 'application/javascript' });
                                      return URL.createObjectURL(blob);
                                  }
                                  if (path.endsWith('.wasm')) {
                                      return getPathToWasmBinary(simdSupported, threadsSupported, wasmPathPrefix != null ? wasmPathPrefix : prefix);
                                  }
                                  return prefix + path;
                              };
                              if (customFetch) {
                                  factoryConfig.instantiateWasm =
                                      createInstantiateWasmFunc(getPathToWasmBinary(simdSupported, threadsSupported, wasmPathPrefix != null ? wasmPathPrefix : ''));
                              }
                              var initialized = false;
                              factoryConfig.onAbort = function () {
                                  if (initialized) {
                                      return;
                                  }
                                  if (initAborted) {
                                      return;
                                  }
                                  initAborted = true;
                                  var rejectMsg = 'Make sure the server can serve the `.wasm` file relative to the ' +
                                      'bundled js file. For more details see https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-wasm/README.md#using-bundlers';
                                  reject({ message: rejectMsg });
                              };
                              var wasm;
                              if (threadsSupported && simdSupported && wasmPath == null) {
                                  factoryConfig.mainScriptUrlOrBlob = new Blob(["var WasmBackendModuleThreadedSimd = " +
                                          tfjsBackendWasmThreadedSimd.toString()], { type: 'text/javascript' });
                                  wasm = tfjsBackendWasmThreadedSimd(factoryConfig);
                              }
                              else {
                                  wasm = tfjsBackendWasm(factoryConfig);
                              }
                              wasm.then(function (module) {
                                  initialized = true;
                                  initAborted = false;
                                  var voidReturnType = null;
                                  module.tfjs = {
                                      init: module.cwrap('init', null, []),
                                      registerTensor: module.cwrap('register_tensor', null, [
                                          'number',
                                          'number',
                                          'number',
                                      ]),
                                      disposeData: module.cwrap('dispose_data', voidReturnType, ['number']),
                                      dispose: module.cwrap('dispose', voidReturnType, []),
                                  };
                                  resolve({ wasm: module });
                              });
                          })];
              }
          });
      });
  }
function init ( ) { return __awaiter ( this , void 0 , void 0 , function ( ) { var _a , simdSupported , threadsSupported ; return __generator ( this , function ( _b ) { switch ( _b . label ) { case 0 : return [ 4 , Promise . all ( [ tfjsCore . env ( ) . getAsync ( 'WASM_HAS_SIMD_SUPPORT' ) , tfjsCore . env ( ) . getAsync ( 'WASM_HAS_MULTITHREAD_SUPPORT' ) ] ) ] ; case 1 : _a = _b . sent ( ) , simdSupported = _a [ 0 ] , threadsSupported = _a [ 1 ] ; return [ 2 , new Promise ( function ( resolve , reject ) { var factoryConfig = { } ; factoryConfig . locateFile = function ( path , prefix ) { if ( path . endsWith ( '.worker.js' ) ) { var response = wasmWorkerContents ; var blob = new Blob ( [ response ] , { type : 'application/javascript' } ) ; return URL . createObjectURL ( blob ) ; } if ( path . endsWith ( '.wasm' ) ) { return getPathToWasmBinary ( simdSupported , threadsSupported , wasmPathPrefix != null ? wasmPathPrefix : prefix ) ; } return prefix + path ; } ; if ( customFetch ) { factoryConfig . instantiateWasm = createInstantiateWasmFunc ( getPathToWasmBinary ( simdSupported , threadsSupported , wasmPathPrefix != null ? wasmPathPrefix : '' ) ) ; } var initialized = false ; factoryConfig . onAbort = function ( ) { if ( initialized ) { return ; } if ( initAborted ) { return ; } initAborted = true ; var rejectMsg = 'Make sure the server can serve the `.wasm` file relative to the ' + 'bundled js file. For more details see https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-wasm/README.md#using-bundlers' ; reject ( { message : rejectMsg } ) ; } ; var wasm ; if ( threadsSupported && simdSupported && wasmPath == null ) { factoryConfig . mainScriptUrlOrBlob = new Blob ( [ "var WasmBackendModuleThreadedSimd = " + tfjsBackendWasmThreadedSimd . toString ( ) ] , { type : 'text/javascript' } ) ; wasm = tfjsBackendWasmThreadedSimd ( factoryConfig ) ; } else { wasm = tfjsBackendWasm ( factoryConfig ) ; } wasm . then ( function ( module ) { initialized = true ; initAborted = false ; var voidReturnType = null ; module . tfjs = { init : module . cwrap ( 'init' , null , [ ] ) , registerTensor : module . cwrap ( 'register_tensor' , null , [ 'number' , 'number' , 'number' , ] ) , disposeData : module . cwrap ( 'dispose_data' , voidReturnType , [ 'number' ] ) , dispose : module . cwrap ( 'dispose' , voidReturnType , [ ] ) , } ; resolve ( { wasm : module } ) ; } ) ; } ) ] ; } } ) ; } ) ; }
Initializes the wasm module and creates the js <--> wasm bridge.
Initializes the wasm module and creates the js < -- > wasm bridge .
/*yield*/ /*return*/ /**
                               * This function overrides the Emscripten module locateFile utility.
                               * @param path The relative path to the file that needs to be loaded.
                               * @param prefix The path to the main JavaScript file's directory.
                               */ // Use the instantiateWasm override when system fetch is not available. // Reference: // https://github.com/emscripten-core/emscripten/blob/2bca083cbbd5a4133db61fbd74d04f7feecfa907/tests/manual_wasm_instantiate.html#L170 // Emscripten already called console.warn so no need to double log. // Emscripten calls `onAbort` twice, resulting in double error // messages. // If `wasmPath` has been defined we must initialize the vanilla module. // The wasmFactory works for both vanilla and SIMD binaries. // The WASM module has been successfully created by the factory. // Any error will be caught by the onAbort callback defined above. // Using the tfjs namespace to avoid conflict with emscripten's API.