tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.37k stars 1.92k forks source link

ES6 Module Import of Tensorflow no longer works #3875

Open bengfarrell opened 4 years ago

bengfarrell commented 4 years ago

To get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

TensorFlow.js version

Tensorflow Core 2.1.0

Browser version

Doesn't matter, but Chrome 85

Describe the problem or feature request

Previously, with Tensorflow (ex 1.3.2), I could use ES6 module imports along with a simple bare-module resolving server like es-dev-server (https://www.npmjs.com/package/es-dev-server), or quite possibly the Chrome only import maps.

Unfortunately, now, that's not possible. It looks like the newer dependency tfjs-backend-cpu contains a seedrandom import, and this module is not a proper ES6 import (uses require() for importing). Perhaps this small module could be bundled as an ES6 import prior to shipping such that it all works again?

thanks!

tafsiri commented 4 years ago

@bengfarrell would you be able to push a small reproduction to a repo that we can access so that we can take a look?

Seedrandom has been one of our dependencies pretty much since the beginning.

We were planning on adding a single file esm bundle which may also solve this issue in a different way and this would be a good test case.

bengfarrell commented 4 years ago

Sure thing, here's a repo: https://github.com/bengfarrell/tf-import-issue.

I suppose it would have helped originally if I had mentioned I was using Posenet. Maybe that dictates what modules get dynamically pulled in where.

bengfarrell commented 4 years ago

Oh and here's my old dependencies where I had things working before:

"@tensorflow-models/posenet": "^2.2.1", "@tensorflow/tfjs": "~1.3.1",

AmitMY commented 3 years ago

This is still an issue.


Using Angular (webpack), it is possible to import, but the following message is given: tfjs 3.16.0

Warning: \node_modules\@tensorflow\tfjs-backend-cpu\dist\kernels\Multinomial.js depends on 'seedrandom'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Warning: \node_modules\@tensorflow\tfjs-core\dist\ops\rand_util.js depends on 'seedrandom'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Karthik-code1010 commented 2 years ago

Warning: node_modules\@tensorflow\tfjs-core\dist\ops\rand_util.js depends on 'seedrandom'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Error: node_modules/@tensorflow/tfjs-core/dist/io/types.d.ts:337:37 - error TS2344: Type 'IOHandler[K]' does not satisfy the constraint 'PromiseFunction'. Type 'SaveHandler | LoadHandler | undefined' is not assignable to type 'PromiseFunction'. Type 'undefined' is not assignable to type 'PromiseFunction'.

337 [K in keyof IOHandler]: Syncify<IOHandler[K]>;

rthadur commented 2 years ago

This has been fixed here , closing this request. Thank you

google-ml-butler[bot] commented 2 years ago

Are you satisfied with the resolution of your issue? Yes No

mattsoulanille commented 2 years ago

@Karthik-code1010's issue is likely fixed, and will be live in the next release, but I don't think ESModule imports of TensorFlow.js are working properly. Here's what I've found:

The @tensorflow/tfjs union package seems to work as long as you import the flat ESM bundle:

import * as tf from './node_modules/@tensorflow/tfjs/dist/tf.fesm.js';

tf.tensor1d([1,2,3]).add(tf.tensor1d([6,5,4])).print();
Tensor
    [7, 7, 7]

Importing it via the module path specified by the package.json, @tensorflow/tfjs/dist/index.js, does not work (and it loads 13Mb with ~1000 requests):

long.js:1 Uncaught ReferenceError: module is not defined
    at long.js:1:1

We should consider changing the package.json's module field to point to the flat ESM bundle.

I also tried loading @tensorflow/tfjs-core and @tensorflow/tfjs-backend-cpu separately. First, I changed tfjs-core and tfjs-backend-cpu's package.json module fields to point to the flat ESM bundles. For core, this would be dist/tf-core.fesm.js. Then, I tried the following:

import * as tf from '@tensorflow/tfjs-core';
import * as cpu from '@tensorflow/tfjs-backend-cpu';

tf.tensor1d([1,2,3]).add(tf.tensor1d([6,5,4])).print();
index.js:13 Uncaught ReferenceError: require is not defined
    at index.js:13:12

That index.js file is from seedrandom, which doesn't appear to have a module output. We've marked it as external to the cpu bundle, but we should probably not do that. Without it marked as external, (and with node's crypto excluded), the following works as expected:

import * as tf from '@tensorflow/tfjs-core';
import * as cpu from '@tensorflow/tfjs-backend-cpu';

tf.add(tf.tensor1d([1,2,3]), tf.tensor1d([6,5,4])).print();
Tensor
    [7, 7, 7]

All these tests were based off of @bengfarrell's example repo and used es-dev-server to host the site.

I'll try to implement these fixes, but I'll need to look into the effects of setting module to the flat ESM bundle. This might make certain imports from @tensorflow/tfjs-core/dist/..... behave incorrectly (e.g. duplicate instances of the same class since it's declared in dist/ and in the bundle). We should probably be using the "exports" field to declare exactly what parts of the API can be imported, which would make it easier to reason about this.

bengfarrell commented 2 years ago

@mattsoulanille Thanks for checking on this, I hadn't gotten a chance to try it yet

mattsoulanille commented 2 years ago

Here's the branch where I'm working on this. If you want to try it locally, you can build local npm packages from it by running yarn build in link-package. The outputs show up in link-package/node_modules/@tensorflow/.

shmishra99 commented 1 year ago

Hi @bengfarrell , Apology for the late response. Could you please let us know if you are still facing this issue or your issue has been resolved ? Thank You.

bengfarrell commented 1 year ago

@shmishra99 Hi, its been awhile since I've used this. So I'm a little unsure which packages work together - but my first attempt seems to indicate it's not fixed. Or perhaps one aspect has been fixed, opening up another related issue.

So, with my previous test repo, I tried to update my TFJS dependencies.

The following installed fine (I left it to NPM to figure out which version to grab): "@tensorflow-models/posenet": "^2.2.2", "@tensorflow/tfjs-core": "^4.4.0"

But then when I tried to add tfjs-converter, there were some dependency conflicts that I wasn't sure how to resolve.

Regardless, I tried my test repo without this installed, and had a similar ES module issue. With only posenet and tfjs-core installed, I get an error from long.js

As you can see in the screenshot, long.js uses the commonjs module.export and does not work as native JS.

Screen Shot 2023-05-01 at 12 08 15 PM
shmishra99 commented 1 year ago

Hi @bengfarrell, Thanks for the update. Could you please try with below set of dependencies "@tensorflow-models/posenet": "^2.2.2", "@tensorflow/tfjs-converter": "^4.4.0", "@tensorflow/tfjs-core": "^4.4.0 ". If issue still persist npm ERR! Could not resolve dependency: npm ERR! peer please follow the below steps:
1) Delete package-lock.json file. 2) Execute npm cache clean --force. 3) Execute npm install --force.

I've replicated the code and getting same error caught ReferenceError: module is not defined . It is because of commonJs syntax are loaded in ES6 module. We would dig more into this issue we will update you soon. CC: @mattsoulanille . Do you have any update on this ? Thank You !

bengfarrell commented 1 year ago

@shmishra99 Yeah, it took using npm install --force to get this working. I'm sure once ya'll iterate the packages in the future, these dependencies will iron themselves out.

gaikwadrahul8 commented 1 year ago

Hi, @bengfarrell

Thank you for opening this issue. Since this issue has been open for a long time, the code/debug information for this issue may not be relevant with the current state of the code base.

The TFJs team is constantly improving the framework by fixing bugs and adding new features. We suggest you try the latest TFJs version with the latest compatible hardware configuration which could potentially resolve the issue. If you are still facing the issue, please create a new GitHub issue with your latest findings, with all the debugging information which could help us investigate.

Please follow the release notes to stay up to date with the latest developments which are happening in the Tensorflow.js space.

Thank you for your support and cooperation.

bengfarrell commented 1 year ago

I'm not sure why I'd create a new issue - it still exists and the very simple repro steps I've given are still applicable. You'd simply up the version numbers as you suggest in the package.json:

"dependencies": {
    "@tensorflow/tfjs-core": "^4.10.0",
    "@tensorflow/tfjs-converter": "^4.10.0"
  },

There's been movement since I opened the issue 3 years ago, but its the same type of error - this project can't be loaded as ES6 modules: Uncaught ReferenceError: module is not defined at long.js:1:1

JLUssh commented 2 months ago

I'm not sure why I'd create a new issue - it still exists and the very simple repro steps I've given are still applicable. You'd simply up the version numbers as you suggest in the package.json:

"dependencies": {
    "@tensorflow/tfjs-core": "^4.10.0",
    "@tensorflow/tfjs-converter": "^4.10.0"
  },

There's been movement since I opened the issue 3 years ago, but its the same type of error - this project can't be loaded as ES6 modules: Uncaught ReferenceError: module is not defined at long.js:1:1

Hi, Have you solved the problem? Could you tell me how to use tf by using ES6 module import? Thank you very much!