Skip to content

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

Closed

Description

@joshstrike

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...

Activity

  1. robertleeplummerjr commented on Jul 9, 2022

    @robertleeplummerjr
    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?

  2. joshstrike commented on Jul 9, 2022

    @joshstrike
    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?

  3. robertleeplummerjr commented on Jul 9, 2022

    @robertleeplummerjr
    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.

  4. joshstrike commented on Jul 9, 2022

    @joshstrike
    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 =)

  5. robertleeplummerjr commented on Jul 10, 2024

    @robertleeplummerjr
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions