In [6]:
import jax
import jaxlib
!cat /var/colab/hostname
print(jax.__version__)
print(jaxlib.__version__)
m-s-1p12yf76kgzz 0.1.64 0.1.45
Confirm Device¶
In [2]:
from jaxlib import xla_extension
import jax
key = jax.random.PRNGKey(1701)
arr = jax.random.normal(key, (1000,))
device = arr.device_buffer.device()
print(f"JAX device type: {device}")
assert device.platform == "cpu", f"unexpected JAX device type: {device.platform}"
/usr/local/lib/python3.6/dist-packages/jax/lib/xla_bridge.py:123: UserWarning: No GPU/TPU found, falling back to CPU.
warnings.warn('No GPU/TPU found, falling back to CPU.')
JAX device type: cpu:0
Matrix Multiplication¶
In [3]:
import jax
import numpy as np
# matrix multiplication on GPU
key = jax.random.PRNGKey(0)
x = jax.random.normal(key, (3000, 3000))
result = jax.numpy.dot(x, x.T).mean()
print(result)
1.0216691
Linear Algebra¶
In [4]:
import jax.numpy as jnp
import jax.random as rand
N = 10
M = 20
key = rand.PRNGKey(1701)
X = rand.normal(key, (N, M))
u, s, vt = jnp.linalg.svd(X)
assert u.shape == (N, N)
assert vt.shape == (M, M)
print(s)
[6.9178133 5.9580317 5.581113 4.506963 4.111582 3.973543 3.3307292 2.8664916 1.8229378 1.5478933]
XLA Compilation¶
In [5]:
@jax.jit
def selu(x, alpha=1.67, lmbda=1.05):
return lmbda * jax.numpy.where(x > 0, x, alpha * jax.numpy.exp(x) - alpha)
x = jax.random.normal(key, (5000,))
result = selu(x).block_until_ready()
print(result)
[ 0.34676832 -0.7532232 1.7060695 ... 2.1208048 -0.42621925 0.13093236]