# The perils of tensor.dataSync()

DevFeed: [The perils of tensor.dataSync()](<https://devfeed.tech/articles/the-perils-of-tensor-datasync-35532.md>)

Original publisher: [Read original article](<https://meowni.ca/posts/on-tfjs-datasync/>)

Author: Monica Dinculescu

Published: 2019-02-22T00:00:00Z

Content type: tutorial

Language: en

Sources: [Monica Dinculescu](<https://devfeed.tech/sources/monica-dinculescu.md>)

Topics: [Tensorflow](<https://devfeed.tech/topics/tensorflow.md>), [WebGL](<https://devfeed.tech/topics/webgl.md>), [GPU](<https://devfeed.tech/topics/gpu.md>), [cpu](<https://devfeed.tech/topics/cpu.md>)

Tags: [cpu](<https://devfeed.tech/tags/cpu.md>), [gpu](<https://devfeed.tech/tags/gpu.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [tensorflow](<https://devfeed.tech/tags/tensorflow.md>), [webgl](<https://devfeed.tech/tags/webgl.md>)

## AI overview

This TensorFlow.js post explains why synchronously converting tensors to JavaScript values with tensor.dataSync() can block the UI and cause janky rendering. It describes how tensor data moves between CPU memory and GPU-backed WebGL textures, and why repeated transfers are costly.

## Source excerpt

One of the first things you stumble on when you start using TensorFlow.js is that sometimes you need your data as a Tensor, and sometimes you need it as a JavaScript number. Maybe it's for logging it, maybe it's for displaying it somewhere during training, maybe it's because you don't trust the robots to be better than you at math. This is a quick post that tries to clarify why doing this synchronously is probably bad and will leave your UI really janky. Nikhil (who like, birthed TensorFlow.js, bless) was kind enough to explain this to me recently, so I figured I'd return the favour, with fewer meeps and more mistakes. Downloading and Uploading When you create a Tensor, it lives on the CPU. The mere fact that it's a Tensor doesn't automatically move that data into its GPU mansion - it needs to be used in a WebGL program. (I'm playing fast and loose here with the words GPU and CPU btw, so hold back the pedantics: when I say "it lives on the CPU" I mean "in main memory, where the CPU processes stuff"; the GPU has it's own memory, that's where it processes stuff, and that's where that data has to get transferred to. It's fine. You know I know.) You upload the tensor to the GPU when you call one of the tf. operations on it. Tensor operations are matrix math, and matrix math is really fast on the GPU, so every time you call something like sum or sqrt on a Tensor, TensorFlow.js creates a little WebGL operation, and sends it to the backend. Whatever data lived on the CPU is now "uploaded" to the GPU (to a WebGL texture). You download a Tensor when you want to get that data from the GPU back onto the CPU. The data now lives in a WebGL texture, so TensorFlow.js needs to call readPixels to ... read... those pixels... from the texture and convert them back into something you can use. Here's the problem: calling readPixels is fundamentally a blocking operation: when you ask the GPU to give you data, you have to wait for it to respond; this means you can't really do anything else on t