Skip to content

tfjs does not work with hermes engine #6526

@paradite

Description

@paradite

System information

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

Describe the current behavior

When using tfjs for React Native, tfjs does not work when using hermes JavaScript engine. It works with the default jsc engine.

It fails at instanceof Tensor assertion, even though the constructor is correctly identified as Tensor:
Screenshot 2022-06-11 at 4 54 15 PM

Screenshot 2022-06-11 at 6 54 02 PM copy

Describe the expected behavior

tfjs should run without errors when using hermes engine.

Standalone code to reproduce the issue

Minimum reproduction repo: https://github.com/paradite/tfjs-hermes-bug

  • Install dependencies with yarn (it is not necessary to setup tfjs git submodule)
  • Run npm run tfjs:node
    • Runs tfjs code with Node.js
    • Prints "result 2" without error
  • Run npm run tfjs:buildrun
    • Bundles tfjs code with Metro
    • Runs tfjs code with Hermes
    • Gives the error "Uncaught Error: Argument 'x' passed to 'argMax' must be a Tensor or TensorLike, but got 'e'"

Other info / logs

I have tried a hacky fix and it seems to work:

Instead of using

if (x instanceof Tensor) {
    assertDtype(parseAsDtype, x.dtype, argName, functionName);
    return x;
}

, use

if (x instanceof Tensor || x.constructor.name === 'Tensor') {
    assertDtype(parseAsDtype, x.dtype, argName, functionName);
    return x;
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions