cloud_tpu_colabs/JAX_NeurIPS_2020_demo.ipynb
import jax
import jax.numpy as jnp
from jax import random
key = random.key(0)
key, subkey = random.split(key)
x = random.normal(key, (5000, 5000))
print(x.shape)
print(x.dtype)
y = jnp.dot(x, x)
print(y[0, 0])
x
import matplotlib.pyplot as plt
plt.plot(x[0])
print(jnp.dot(x, x.T))
print(jnp.dot(x, 2 * x)[[0, 2, 1, 0], ..., None, ::-1])
import numpy as np
x_cpu = np.array(x)
%timeit -n 5 -r 2 np.dot(x_cpu, x_cpu)
%timeit -n 5 -r 5 jnp.dot(x, x).block_until_ready()
from jax import grad
def f(x):
if x > 0:
return 2 * x ** 3
else:
return 3 * x
key = random.key(0)
x = random.normal(key, ())
print(grad(f)(x))
print(grad(f)(-x))
print(grad(grad(f))(-x))
print(grad(grad(grad(f)))(-x))
Other JAX autodiff highlights:
gelu)For much more, see the JAX Autodiff Cookbook (Part 1).
jitfrom jax import jit
key = random.key(0)
x = random.normal(key, (5000, 5000))
def f(x):
y = x
for _ in range(10):
y = y - 0.1 * y + 3.
return y[:100, :100]
f(x)
g = jit(f)
g(x)
%timeit f(x).block_until_ready()
%timeit -n 100 g(x).block_until_ready()
grad(jit(grad(jit(grad(jnp.tanh)))))(1.0)
jax.device_count()
from jax import pmap
y = pmap(lambda x: x ** 2)(jnp.arange(8))
print(y)
y
import matplotlib.pyplot as plt
plt.plot(y)
from functools import partial
from jax.lax import psum
@partial(pmap, axis_name='i')
def f(x):
total = psum(x, 'i')
return x / total, total
normalized, total = f(jnp.arange(8.))
print(f"normalized:\n{normalized}\n")
print("total:", total)
For more, see the pmap cookbook.
from jax.experimental import sharded_jit, PartitionSpec as P
from jax import lax
conv = lambda image, kernel: lax.conv(image, kernel, (1, 1), 'SAME')
image = jnp.ones((1, 8, 2000, 1000)).astype(np.float32)
kernel = jnp.array(np.random.random((8, 8, 5, 5)).astype(np.float32))
np.set_printoptions(edgeitems=1)
conv(image, kernel)
%timeit conv(image, kernel).block_until_ready()
image_partitions = P(1, 1, 4, 2)
sharded_conv = sharded_jit(conv,
in_parts=(image_partitions, None),
out_parts=image_partitions)
sharded_conv(image, kernel)
%timeit -n 10 sharded_conv(image, kernel).block_until_ready()