For some tf.Models, training in tfjs-node is about 4x slower than eager tf.keras (Python)
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.
Is this still true, @caisq?
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.
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'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 secondsIn tfjs-node
Tensor [[0.4938855], [0.5194098], [0.4804204], [0.5029459]] Hello world 7.157 secondsIn 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
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?
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;
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.
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!