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.

method `toPixels` is not optimized for `webgl` or `webgpu` backends

See original GitHub issue

method fromPixels uses optimized code paths depending on the browser and using webgl calls results in 10x faster performance than fallback using canvas.getImageData

however, method toPixels does not have any optimizations and relies strictly on downloading tensor, creating ImageData object from it and then drawing it onto canvas
(implementation is in src/tfjs-core/src/opts/browser.ts:toPixels

for models where result is already in GPU memory, tensor download is by far the most expensive (and unnecessary) operation

for example, look at the following timing values (in ms):

  • prepare: 1 //includes call to fromPixels and normalizing inputs to -1…1
  • inference: 2 // call to await model.executeAsync
  • process: 0 // includes denormalizing and converting data from rgb to rgba
  • download: 147 // this is just await tensor.data()
  • draw: 2 // actual canvas.drawImage
  • total: 153

as you can see this may be an extreme case, but 96% of time is spent on unnecessary download of data from gpu memory

ask is to implement optimized webgl and webgpu path for toPixels method

environment: tfjs 3.19.0 with chrome 103

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:32 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
19521178commented, Dec 22, 2022

@FabioRomagnolo It worked for me. This solution is the best. Thanks for your codes.

1reaction
qjia7commented, Jul 28, 2022

@vladmandic I ever wrote a PR to optimize toPixels. After some internal discussions, dataToGPU is a more powerful interface for users. So I suspended that work. It seems that it’s still useful to provide an efficient way for toPixels. One question here is what kind of canvas would you like to draw to? A 2d-canvas or let the backend to decide the canvas context (webgl/webgpu context canvas)? The original toPixels seems directly use it as a 2d-canvas.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Platform and environment | TensorFlow.js
The WebGL backend, 'webgl', is currently the most powerful backend for the browser. This backend is up to 100x faster than the vanilla...
Read more >
Deep Learning in the Browser - Exploring TF.js, WebDNN and ...
My last post discussed a server-side method for deploying the model. This post will discuss client side frameworks and techniques to deploy ...
Read more >
Why WebGPU backend is slower? - Questions - Babylon.js
I tried to play around with WebGPU renderer and found that on average it produces less FPS comparing to WebGL while having longer ......
Read more >
@tensorflow/tfjs-data: Versions | Openbase
Layers new layers: Conv3D, Noise; Started development on a WebGPU backend. This is a preliminary prototype and will not be added to tfjs-core...
Read more >
What do when OpenGL just hates multithreading?
It's not a super great way of doing it, but it's simple, ... I don't want to have to deal with backend minutia...
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