tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

For some tf.Models, training in tfjs-node is about 4x slower than eager tf.keras (Python)

Open caisq opened this issue 8 years ago • 9 comments

measured using the MNIST example (https://github.com/tensorflow/tfjs-examples/tree/master/mnist-node) and the equivalent eager tf.keras model (https://github.com/caisq/keras/blob/eager-mnist-cnn/examples/mnist_cnn.py).

When compared gains graph-mode tf.keras or keras, the different is even larger.

caisq avatar Aug 22 '18 19:08 caisq

Is this still true, @caisq?

nsthorat avatar Mar 11 '19 18:03 nsthorat

We have seen significant improvements since the bug is filed. But the training speed gap is still there, especially if we compare ourselves against the graph model.

caisq avatar Mar 11 '19 18:03 caisq

I'm struggling to get CUDA support working on WIndows 10, but I did also observe this is still a thing. This code runs way faster on tfjs than tfjs-node.

async function predict() {
    const model = tf.sequential();
    model.add(tf.layers.dense({ units: 8, inputShape: 2, activation: 'tanh' }));
    model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));
    model.compile({ optimizer: 'sgd', loss: 'meanSquaredError', lr: 0.6 });

    // Creating dataset
    const xs = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);
    //xs.print();
    const ys = tf.tensor2d([[0], [1], [1], [0]]);
    //ys.print();

    // Train the model
    await model.fit(xs, ys, {
        batchSize: 1,
        epochs: 250,
        verbose: 0
    });
    return model.predict(xs);
}
let start = Date.now();
predict().then(data => {
    console.log('Hello world', (data.print(), Date.now() - start)/1000, ' seconds');
}); 

In tfjs-node-gpu (gpu not working correctly, assuming this is a fallback.

Tensor
    [[0.4807978],
     [0.4495386],
     [0.564573 ],
     [0.485004 ]]
Hello world 7.158  seconds

In tfjs-node

Tensor
    [[0.4938855],
     [0.5194098],
     [0.4804204],
     [0.5029459]]
Hello world 7.157  seconds

In tfjs

Tensor
    [[0.4711909],
     [0.5097558],
     [0.5824007],
     [0.5946872]]
Hello world 1.641  seconds

depixeled-chris avatar May 03 '20 19:05 depixeled-chris

I'm struggling to get CUDA support working on WIndows 10, but I did also observe this is still a thing. This code runs way faster on tfjs than tfjs-node.

async function predict() {
    const model = tf.sequential();
    model.add(tf.layers.dense({ units: 8, inputShape: 2, activation: 'tanh' }));
    model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));
    model.compile({ optimizer: 'sgd', loss: 'meanSquaredError', lr: 0.6 });

    // Creating dataset
    const xs = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);
    //xs.print();
    const ys = tf.tensor2d([[0], [1], [1], [0]]);
    //ys.print();

    // Train the model
    await model.fit(xs, ys, {
        batchSize: 1,
        epochs: 250,
        verbose: 0
    });
    return model.predict(xs);
}
let start = Date.now();
predict().then(data => {
    console.log('Hello world', (data.print(), Date.now() - start)/1000, ' seconds');
}); 

In tfjs-node-gpu (gpu not working correctly, assuming this is a fallback.

Tensor
    [[0.4807978],
     [0.4495386],
     [0.564573 ],
     [0.485004 ]]
Hello world 7.158  seconds

In tfjs-node

Tensor
    [[0.4938855],
     [0.5194098],
     [0.4804204],
     [0.5029459]]
Hello world 7.157  seconds

In tfjs

Tensor
    [[0.4711909],
     [0.5097558],
     [0.5824007],
     [0.5946872]]
Hello world 1.641  seconds

I observe the same thing. Just tfjs works faster for me too. Any comment from the TFJS team? PS: I am on Windows 10, NodeJS version v14.5.0

bc238dev avatar Sep 16 '20 04:09 bc238dev

Very similar results here, pure tfjs is vastly faster (4x+) than tfjs-node and tfjs-node-gpu (which is even worse!).

Windows 10 Node @tensorflow/* v3.7.0 (latest) CUDA 11.3 (latest) tensorflow v2.5.0 (latest stable) Node v16.3.0 Python v3.9.5

Everything loads fine using the bindings, including GPU support, but performance is terrible talking to tensorflow with C++ bindings. I've tried reinstalling everything multiple times to no avail.

Did anyone ever determine what was wrong?

asilvas avatar Jun 10 '21 19:06 asilvas

Observing the same on Windows 10 machine:

@tensorflow/tfjs-node: Hello world 3.563 seconds
@tensorflow/tfjs: Hello world 1.064 seconds
@tensorflow/tfjs-node-gpu: Hello world 9.936 seconds

I ran the same test on Intel 2016 MacBook Pro and @tensorflow/tfjs is also the fastest:

@tensorflow/tfjs-node: Hello world 3.168 seconds
@tensorflow/tfjs: Hello world 1.782 seconds
@tensorflow/tfjs-node-gpu: Hello world 3.588 seconds

If you use @tensorflow/tfjs-node and run tf.setBackend('cpu'); manually before training, it would also be faster. This is useful for using the file:// handler which comes with @tensorflow/tfjs-node.

However, for larger models, @tensorflow/tfjs-node is still faster. So I think it is just some small overhead somewhere.

My slightly modified the script to account for tf.ready:

// import * as tf from '@tensorflow/tfjs-node';
// import * as tf from '@tensorflow/tfjs';
import * as tf from '@tensorflow/tfjs-node-gpu';

async function predict() {
  const model = tf.sequential();
  model.add(tf.layers.dense({ units: 8, inputShape: 2, activation: 'tanh' }));
  model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));
  model.compile({ optimizer: 'sgd', loss: 'meanSquaredError', lr: 0.6 });

  // Creating dataset
  const xs = tf.tensor2d([
    [0, 0],
    [0, 1],
    [1, 0],
    [1, 1],
  ]);
  //xs.print();
  const ys = tf.tensor2d([[0], [1], [1], [0]]);
  //ys.print();

  // Train the model
  await model.fit(xs, ys, {
    batchSize: 1,
    epochs: 250,
    verbose: 0,
  });
  return model.predict(xs);
}
tf.ready().then(() => {
  let start = Date.now();
  predict().then((data) => {
    console.log(
      'Hello world',
      (data.print(), Date.now() - start) / 1000,
      ' seconds'
    );
  });
});

To reproduce, save the script as test.mjs and run node test.mjs;

paradite avatar Dec 26 '21 07:12 paradite

Hi, @caisq

Thank you for opening this issue for tracking purposes. 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. We can keep the issue open if it is still relevant. Please confirm if we need to keep the issue open.

Thank you for your support and cooperation.

gaikwadrahul8 avatar Oct 11 '23 14:10 gaikwadrahul8

Hi, @caisq

We have not received any confirmation from you, so we are closing this issue now. If the issue is still relevant, please let us know, either we'll re-open this issue or please feel free to create new issue after trying with latest version of Tensorflow.js. Thank you!

gaikwadrahul8 avatar Oct 18 '23 19:10 gaikwadrahul8

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

google-ml-butler[bot] avatar Oct 18 '23 19:10 google-ml-butler[bot]