Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 40 ยท Arrays and NumPy API

JAX Arrays and Data Types

Create and inspect JAX arrays, understand dtypes, float32 defaults, bfloat16, weak typing and type promotion, and convert between JAX, NumPy and Python values.

Everything in JAX starts with the array. The type you work with is jax.Array: an n-dimensional, immutable, typed block of numbers that lives on a device (CPU, GPU or TPU). You create arrays with jax.numpy functions that mirror NumPy, and you inspect them with the familiar shape, ndim, size and dtype attributes.

This lesson covers creation, data types, JAX's slightly unusual type-promotion rules, and conversion. The runnable editors use NumPy to demonstrate the matching behavior; JAX blocks are read-only because the library cannot run in the in-browser runtime (see What Is JAX?).

Creating arrays

Python
import jax.numpy as jnp

a = jnp.array([1, 2, 3])               # from a Python list
b = jnp.zeros((2, 3))                  # 2 x 3 of zeros
c = jnp.ones(4)
d = jnp.arange(0, 10, 2)               # [0 2 4 6 8]
e = jnp.linspace(0.0, 1.0, 5)          # [0.   0.25 0.5  0.75 1.  ]
f = jnp.eye(3)                         # identity matrix
g = jnp.full((2, 2), 7.0)
h = jnp.zeros_like(a)

All the creation functions you know from NumPy exist. Two differences worth remembering:

  • Integer and float defaults are 32-bit (int32, float32).
  • Passing a Python list is fine, but passing a list of arrays repeatedly is slow. Build data in NumPy first and convert once with jnp.asarray.
Python Stand-in: shapes and creation with NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Data types

The common dtypes in JAX are:

Group dtypes
Boolean bool
Signed integers int8, int16, int32, int64
Unsigned integers uint8, uint16, uint32, uint64
Floating point float16, bfloat16, float32, float64
Complex complex64, complex128

bfloat16 ("brain float") keeps float32's range but with only about 3 decimal digits of precision, and it is a favorite for neural-network training on TPUs and modern GPUs. NumPy has no bfloat16, so the stand-in below uses float16 to show the idea of reduced precision.

Python
x = jnp.array([1, 2, 3], dtype=jnp.float32)
y = x.astype(jnp.bfloat16)
print(y.dtype)          # bfloat16

The 64-bit types (int64, float64, complex128) exist, but they are silently truncated to 32-bit unless you enable x64 mode, which is set once at start-up with jax.config.update("jax_enable_x64", True):

Python
z = jnp.array([1.0], dtype=jnp.float64)
print(z.dtype)          # float32, with a warning, unless x64 is enabled
Python Stand-in: precision of different float types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Integer overflow wraps silently on accelerators as it does in NumPy's fixed-width types; there is no exception.

Type promotion and weak types

When you mix dtypes in one expression, JAX promotes to a common type. For example int32 + float32 gives float32. JAX uses its own, associative promotion lattice, which differs slightly from NumPy in places; the key thing to know is that Python scalars are "weakly typed":

Python
x = jnp.array([1, 2, 3], dtype=jnp.float16)
print((x * 2.0).dtype)                   # float16: the Python float is weak
print((x * jnp.float32(2.0)).dtype)      # float32: an explicit float32 wins

A weakly typed scalar adapts to the array's dtype instead of upgrading it. That keeps float16 and bfloat16 models from accidentally becoming float32 because you multiplied by 0.5. You can check promotion directly with jnp.result_type(a, b).

Python Stand-in: promotion with Python scalars

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line is a real difference: NumPy promotes int32 with float32 to float64, while JAX keeps the result float32. When you port code, check the dtype instead of assuming it matches.

Converting to and from other types

Python
import numpy as np

n = np.arange(4.0)
j = jnp.asarray(n)                 # NumPy -> JAX (float32 unless x64)
back = np.asarray(j)               # JAX -> NumPy
py = j[0].item()                   # a Python float
lst = j.tolist()                   # nested Python lists

Remember: converting to NumPy waits for any pending device computation and copies the data to host memory. Converting scalars with float(x) or int(x) does the same and is not allowed on traced values inside jit.

Useful array attributes and methods

Most NumPy methods exist on jax.Array: reshape, sum, mean, max, argmax, astype, T, ravel, flatten, clip, round. A few extras are JAX-specific:

  • x.at[...]: functional update syntax (next lesson).
  • x.devices(): the device(s) holding the array.
  • x.block_until_ready(): wait for the computation to finish.
  • jax.device_get(x): fetch to host as NumPy.

Pretty-printing and inspecting

print(x) shows the values, repr(x) shows Array([...], dtype=float32). To look at an array's type and shape without values (useful for debugging), jax.eval_shape runs a function abstractly and returns shapes and dtypes with no real computation:

Python
shape_only = jax.eval_shape(lambda v: v @ v.T, jnp.ones((3, 4)))
print(shape_only)      # ShapeDtypeStruct(shape=(3, 3), dtype=float32)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which are JAX's default integer and float dtypes?

    JAX defaults to 32-bit types unless x64 mode is enabled.
  2. 2. What is the dtype of (x * 2.0) when x is a float16 array in JAX?

    Python scalars are weakly typed, so they adopt the array's dtype instead of promoting it.
  3. 3. What happens if you request dtype=jnp.float64 without enabling x64 mode?

    64-bit types are truncated to 32-bit unless jax_enable_x64 is turned on at startup.
  4. 4. Why is bfloat16 popular for neural networks?

    bfloat16 has the same exponent range as float32 but a much shorter mantissa, which suits training workloads.

Summary

  • jax.Array is an immutable, typed n-d array on a device; creation functions match NumPy.
  • Defaults are int32 and float32; 64-bit needs jax_enable_x64.
  • bfloat16 trades precision for range and speed.
  • Python scalars are weakly typed, so they do not upcast array dtypes; JAX promotion differs from NumPy in places.
  • Convert with jnp.asarray / np.asarray; conversion to NumPy waits for the device.

Next: Immutability and .at Updates