jax icon indicating copy to clipboard operation
jax copied to clipboard

"Inplace" update is 100 times slower on CPU than GPU

Open xidulu opened this issue 3 years ago • 8 comments

Description

I noticed that inplace update is much slower on CPU than GPU, I wonder if it is that normal or there is something wrong with my code, thanks.

import jax
import jax.numpy as jnp
from jax import jit
from jax.random import PRNGKey
def update(x, idx, y):
  x = x.at[idx].set(y)
  return x
# CPU without JIT
with jax.default_device(jax.devices("cpu")[0]):
  x = jnp.zeros((60000, 3000))
  y = jnp.ones(3000)
  idx = jax.random.permutation(PRNGKey(0), jnp.arange(0, 60000))[:100]
  %timeit update(x, idx, y).block_until_ready()
  idx = jnp.arange(100)
  %timeit update(x, idx, y).block_until_ready()

# 473 ms ± 1.94 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# 512 ms ± 52.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# GPU with JIT
with jax.default_device(jax.devices("gpu")[0]):
  x = jnp.zeros((60000, 3000))
  y = jnp.ones(3000)
  idx = jax.random.permutation(PRNGKey(0), jnp.arange(0, 60000))[:100]
  %timeit update(x, idx, y).block_until_ready()
  idx = jnp.arange(100)
  %timeit update(x, idx, y).block_until_ready()
# 9.44 ms ± 319 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# 9.21 ms ± 105 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

https://colab.research.google.com/drive/1kpIZ9VfJ92AAc55Cygj2nQTRQtl1NL0H?usp=sharing

What jax/jaxlib version are you using?

No response

Which accelerator(s) are you using?

Both

Additional system info

Colab

NVIDIA GPU info

No response

xidulu avatar Apr 20 '23 16:04 xidulu

Hi - I think this may be working as expected. GPU is able to execute this kind of code in a vectorized manner, with multiple threads, while CPU is limited to a single thread. With that in mind, it would make sense that on GPU this sort of operation would show much faster execution. What do you think?

jakevdp avatar Apr 20 '23 21:04 jakevdp

Thanks for the reply! I think it totally makes now.

xidulu avatar Apr 21 '23 04:04 xidulu

No..., just the in-place update doesn't happen, it should be in the jitted function and add the donate parameter.

# CPU with JIT
def test_cpu():
  with jax.default_device(jax.devices("cpu")[0]):
    x = jnp.zeros((60000, 3000))
    y = jnp.ones(3000)
    idx = jax.random.permutation(PRNGKey(0), jnp.arange(0, 60000))[:100]
    up_jit = jax.jit(update, donate_argnums=(0,))
    def f():
      nonlocal x
      x = up_jit(x, idx, y).block_until_ready()
    %timeit f()
test_cpu() # ~370us
# GPU with JIT
def test_gpu():
  with jax.default_device(jax.devices("gpu")[0]):
    x = jnp.zeros((60000, 3000))
    y = jnp.ones(3000)
    idx = jax.random.permutation(PRNGKey(0), jnp.arange(0, 60000))[:100]
    up_jit = jax.jit(update, donate_argnums=(0,))
    def f():
      nonlocal x
      x = up_jit(x, idx, y).block_until_ready()
    %timeit f()
test_gpu() # ~349us

uuirs avatar Apr 21 '23 11:04 uuirs

@uuirs Thanks! This is helpful.

@jakevdp I think it would be great to add the JIT donate parameter in the document array.at[ix].set(value)

xidulu avatar Apr 21 '23 15:04 xidulu

To be honest, I don't think the argument donation issue is relevant except in the case of contrived micro-benchmarks like this one, so I think putting it in the array.at docs would be more distracting than useful.

jakevdp avatar Apr 21 '23 15:04 jakevdp

The reason CPU is slower here is not because of lack of in-place update due to not using argument donation (which, by the way, doesn't actually work on CPU, only on GPU) The reason CPU is slower here is because of the implicit threading of GPU computations that I mentioned in my first answer.

jakevdp avatar Apr 21 '23 15:04 jakevdp

@jakevdp Actually argument donation does work on CPU these days.

hawkinsp avatar Apr 21 '23 16:04 hawkinsp

@jakevdp Actually argument donation does work on CPU these days.

Cool, TIL!

jakevdp avatar Apr 21 '23 16:04 jakevdp