Skip to content

".platform" empty in env() in react-native #7991

@ALexandreM75013

Description

@ALexandreM75013

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): Windows 8.1, React-native bare app 0.72.5 (latest version)
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: Samsung S10
  • TensorFlow.js version: 4.11.0
  • Tensorflow.js react-native version: ^0.8.0

Hi,

Whenever I call tf.scalar(255);, tensorflow is failling returning this error message : Cannot read property 'isTypedArray' of undefined.
After digging into the scalar() function in the @tensorflow node_modules folder, I see that there is a call to the isTypedArray() function which tries to access to the key "platform" from the env() variable and this is empty.

From tf.node.js:

function isTypedArray(a) {
    // TODO(mattsoulanille): Remove this fallback in 5.0.0
    if (env().platform.isTypedArray != null) {
        return env().platform.isTypedArray(a);
    }
    else {
        return isTypedArrayBrowser(a);
    }
}

env() is from the class Environment and in this class I see that there is a function setPlatform() but it seems it is not called in react-native and it is not setting anything in the platform/platformName keys of the class Environment.

From tf.node.js:

class Environment {
    // tslint:disable-next-line: no-any
    constructor(global) {
        this.global = global;
        this.flags = {};
        this.flagRegistry = {};
        this.urlFlags = {};
        // Jasmine spies on this in 'environment_test.ts'
        this.getQueryParams = getQueryParams;
        this.populateURLFlags();
    }
    setPlatform(platformName, platform) {
        if (this.platform != null) {
            if (!(env().getBool('IS_TEST') || env().getBool('PROD'))) {
                console.warn(`Platform ${this.platformName} has already been set. ` +
                    `Overwriting the platform with ${platformName}.`);
            }
        }
        this.platformName = platformName;
        this.platform = platform;
    }
...

If I just do a console.log() of tf.env(), I see indeed that platform/platformName are not set but the other keys are well set (global /flags/flagRegistry...).

I was previously using the version 3.18.0 of Tensor and this check was not existing in the scalar() function.

This is my code in react-natice just to try the scalar() function :

import * as tf from '@tensorflow/tfjs';
import '@tensorflow/tfjs-react-native';

class App extends PureComponent {
  componentDidMount() {
    try {
      tf.ready(); => works
      const scalar = tf.scalar(255); => fails
    } catch (err) {
      console.log(err); => error Cannot read property 'isTypedArray' of undefined
    }
  }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions