Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
deepkolos opened this issue May 3, 2021 · 2 comments
Assignees

Comments

@deepkolos
Copy link
Contributor

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

@deepkolos deepkolos added the type:bug Something isn't working label May 3, 2021
@rthadur rthadur added comp:wasm type:feature New feature or request and removed type:bug Something isn't working labels May 3, 2021
@pyu10055
Copy link
Collaborator

pyu10055 commented May 7, 2021

@deepkolos We currently generate a WX specific file for tfjs-backend-wasm, I think your transform code can be added to that part of the rollup configuration. Do you want to submit a PR for the fix? Thanks!

@deepkolos
Copy link
Contributor Author

@pyu10055 ok, my pleasure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants