Skip to content

WeChat 8.0 has moved WebAssembly to WXWebAssembly so wasm for miniprogram is not working #5021

Closed
@deepkolos

Description

@deepkolos

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): WeChat 8.0.3
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: Android and IOS
  • TensorFlow.js installed from (npm or script link): npm
  • TensorFlow.js version (use command below): 3.6
  • Browser version: WeChat 8.0.3
  • Tensorflow.js Converter Version: 3.6

WebAssembly has changed to WXWebAssembly after WeChat 8.0

  1. not simd or thread support.
  2. only load local wasm file is allowed.
  3. WebAssembly.validate not working.

Snipaste_2021-05-03_14-38-57

https://developers.weixin.qq.com/community/develop/doc/000e2c019f8a003d5dfbb54c251c00?jumpto=comment&commentid=000eac66934960576d0cb1a7256c

my approch to fix this is replace some code in build time, using tfjs-backend-wasm/dist/index.js as entry

https://github.com/deepkolos/wxmp-tensorflow/blob/main/rollup.config.js#L30

function codeTransform() {
  return {
    transform(code, file) {
      // 修复wasm
      if (
        file.endsWith('tfjs-backend-wasm-threaded-simd.worker.js') ||
        file.endsWith('tfjs-backend-wasm-threaded-simd.js')
      ) {
        code = code.replace(`require("worker_threads")`, 'null');
        code = code.replace(`require("perf_hooks")`, 'null');
      }
      if (file.endsWith('backend_wasm.js')) {
        code = code.replace(`env().getAsync('WASM_HAS_SIMD_SUPPORT')`, 'false');
        code = code.replace(`env().getAsync('WASM_HAS_MULTITHREAD_SUPPORT')`, 'false');
        code = code.replace(
          `return (imports, callback) => {`,
          `return (imports, callback) => {
            WebAssembly.instantiate(path, imports).then(output => {
                callback(output.instance, output.module);
            });
            return {};`,
        );
      }
      code = code.replace(`WebAssembly.`, `WXWebAssembly.`);
      code = code.replace(`typeof WebAssembly`, `typeof WXWebAssembly`);
      return { code };
    },
  };
}

but this is a temporary solution, hope it can be fix in better way inside the @tensorflow/tfjs-backend-wasm package. thanks

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions