question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

running some models using webgl is 10x slower than using nodejs

See original GitHub issue

Model executes in NodeJS using tensorflow backend in ~100ms, but above 1sec in browser using WebGL backend That is 10x difference in performance between NodeJS and WebGL

I’ve tried to figure out why a model is executing an order of magnitude slower than expected using WebGL backend,
but profiler info for it makes no sense

const t0 = performance.now();
const res = await tf.profile(() => model.executeAsync(input));
const t1 = performance.now();
const wallTime = t1 - t0;
const kernelTime = res.kernels.reduce((a, b) => a += b.kernelTimeMs, 0);

wallTime is 900-1200ms
kernelTime is ~20ms

I’m re-running inference on the same input twice and looking at second run to allow for warmup time of WebGL backend (shader compile, etc.)

I even tried tf.enableDebugMode() and I still don’t see anything that gets even close to overall wall time

And I have no idea where is time spent?

Model in question: https://github.com/vladmandic/nanodet

Environment: TFJS 3.3.0 on Ubuntu 20.10 and Chrome 89

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
vladmandiccommented, Mar 29, 2021

inference time is simple:

in browser:

const t0 = performance.now();
const res = await model.executeAsync(input));
const t1 = performance.now();
const inferenceTime = Math.round(t1 - t0); // elapsed time in ms

and in nodejs:

const t0 = process.hrtime.bigint();
const res = await model.executeAsync(input)); // also can be model.predict(input);
const t1 = process.hrtime.bigint();
const inferenceTime = Math.round((t1 - t0) / 1000 /1000); // to convert to ms

and i’m only measuring subsequent runs, skipping first run so webgl has time to warmup (compile&upload shaders)

for most models, webgl is fast and it’s the best option (since node-gpu doesn’t work due to obsolete cuda dependency from tf1). however, for some models (i’ve provided examples), webgl is 10x slower than nodejs. but running profile() shows nothing useful.

0reactions
vladmandiccommented, Nov 26, 2021

@OlivierMns I never stated I saw the problem with EfficientNet, I spoke about EfficientDet. But in general, issue in my case was clipping due to quantization - try unquantized model first. Also, EfficientNet is notoriously slow for warmup, I suggest to enable WebGL uniforms which speeds up warmup 2-4x (no difference on actual inference)

tf.ENV.set(‘WEBGL_USE_SHAPES_UNIFORMS’, true);

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebGl rendering is very slow with JSON file, is it because of ...
I am trying to render a very complex model using json file. The size of the json file is 40MB it is a...
Read more >
Platform and environment | TensorFlow.js
Versus WebGL: WebGL is faster than WASM for most models, but for tiny models WASM can outperform WebGL due to the fixed overhead...
Read more >
Fast client-side ML with TensorFlow.js, by Ann Yuan (Google)
In this case, our WebAssembly backend is between 8 and 20 times faster than our plain JS backend. And it's actually comparable with...
Read more >
Environments for running AI in Node.js - IBM Developer
With regards to TensorFlow.js, the WASM back end offers much faster CPU execution than the plain JavaScript CPU back end, making it a...
Read more >
TensorFlow.js: Deploying and Training Machine Learning ...
js is better for training small models. According to its FAQ, TensorFlow.js with WebGL acceleration is 1.5–2x slower than TensorFlow with AVX.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found