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

M1 Mac, nodejs GPU train time way longer than CPU #818

Closed
joshstrike opened this issue Jul 8, 2022 · 5 comments
Closed

M1 Mac, nodejs GPU train time way longer than CPU #818

joshstrike opened this issue Jul 8, 2022 · 5 comments
Labels

Comments

@joshstrike
Copy link

joshstrike commented Jul 8, 2022

A GIF or MEME to give some spice of the internet

What is wrong?

Running the basic XOR example, 10k interations. With brain.NeuralNetwork(), 46 milliseconds. With brain.NeuralNetworkGPU(), 14.6 seconds.

Where does it happen?

brain.js@2.0.0-beta.15. Node 18.3.0 running on the command line, Python 3.9.13 (don't think this matters?), M1 Macbook Pro (Max, 32 GPU cores).

How do we replicate the issue?

1 - Run the first XOR example in the Readme.md, (speed shown was attained using default config options, not passing a config to NeuralNetwork()).

Expected behavior (i.e. solution)

The GPU version... shouldn't be ...slower?

Version information

Nodejs:

Node 18.3.0

Browser:

N/A

Brain.js:

brain.js@2.0.0-beta.15

How important is this (1-5)?

5 if it's a bug, 0 if it's a mistake on my end.

Other Comments

This is my first time trying brain.js so it's quite possible I've just got something misconfigured. Love the project and the ease of use! The fact that on CPU that training took 46ms is hard to believe.

As a side note, I was unable to get the browser version to work at all from the CDN, it kept throwing js_gpu is undefined or something along those lines, then when I side-loaded gpu.js I got js_gpu.GPU is not a constructor. I'd rather use it in Node anyway...

@joshstrike joshstrike added the bug label Jul 8, 2022
@robertleeplummerjr
Copy link
Contributor

XOR uses approximately 3 neurons. It will always be faster on CPU. For GPU to break even you need something like 512x512 neurons. This is a limitation of hardware and not a bug. However the browser issue you raised may be legitimate. Did you load GPU.js prior to brain.js in a browser?

@joshstrike
Copy link
Author

I tried both ways, initially just loading brain.js from the CDN:
gpu_js is undefined

then adding a script tag before first to load from https://unpkg.com/gpu.js@latest/dist/gpu-browser.min.js results in:
gpu_js.GPU is not a constructor @ brain.js:5460
this.gpu = new gpu_js.GPU({ mode: options.mode });

Same results in both Chrome and Firefox. gpu.js as loaded off the CDN looks like an ES module (loading it type="module" doesn't change anything, though).

Just out of curiosity, is the minimum time to run on the GPU a result of uploading batches of data, or modifying kernels with each epoch or both?

@robertleeplummerjr
Copy link
Contributor

The data is first uploaded (and a tiny compiled kernel for uploading training data) and then the kernels are compiled in the first iteration of learning. After that everything exists and is ran from the GPU.

@joshstrike
Copy link
Author

Ahha, I didn't realize the hidden layers were where the advantage was. The trade-off point seems to be around [128,128]. With 512 rows of training data, benchmark on the M1 is about 11 seconds for both CPU and GPU at that size. Beyond that GPU takes off =)

@robertleeplummerjr
Copy link
Contributor

Closing this issue because of having a sufficient explanation.

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

No branches or pull requests

2 participants