Documentation for parajax
Enabling parallel execution on CPU
By default, JAX on CPU only uses a single core. To enable parallel execution on all available CPU cores, set the jax_num_cpu_devices configuration option appropriately. This should be done at the beginning of your code as follows:
import multiprocessing
import jax
jax.config.update("jax_num_cpu_devices", multiprocessing.cpu_count())
parajax
Parallelization utilities for JAX.
parallelize(func: Callable[_P, _T] | None = None, /, *, max_devices: int | None = None, remainder_strategy: Literal['pad', 'drop', 'strict'] = 'pad') -> Callable[_P, _T] | Callable[[Callable[_P, _T]], Callable[_P, _T]]
parallelize(func: Callable[_P, _T], /, *, max_devices: int | None = ..., remainder_strategy: Literal['pad', 'drop', 'strict'] = ...) -> Callable[_P, _T]
parallelize(*, max_devices: int | None = ..., remainder_strategy: Literal['pad', 'drop', 'strict'] = ...) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]
Automatic parallelizing map.
Creates a parallelized version of func that distributes computation of the
leading axis of array arguments across multiple devices.
func: The function to be parallelized. It should accept array arguments with a
leading batch dimension. If your function cannot work in a batched manner, you
can wrap it with jax.vmap first. For passing non-batched arguments, consider
using functools.partial or a lambda function.
max_devices: The maximum number of JAX devices to use for parallelization.
remainder_strategy: Specifies how to handle cases where the batch size is not
divisible by the number of devices, which is not directly supported by JAX. The
available strategies are:
- "pad" (default): Transparently pad the input arrays along the leading axis
to make the batch size divisible by the number of devices. The padding is
done by repeating the last element. The output is then automatically
unpadded to match the original batch size, with no visible effect to the
caller.
- "drop": Use with caution. The extra elements that do not fit evenly into the
devices are dropped from the computation, resulting in a smaller output
size.
- "strict": Will only work if the batch size is divisible by the number of
devices. Otherwise, a ValueError will be raised.
Returns:
| Type | Description |
|---|---|
Callable[_P, _T] | Callable[[Callable[_P, _T]], Callable[_P, _T]]
|
The decorator returns a parallel version of |
Basic usage
import jax.numpy as jnp
from parajax import parallelize
@parallelize
def square(xs):
return xs ** 2
xs = jnp.arange(12_345)
ys = square(xs) # This will run in parallel across available JAX devices
Composability with vmap
import jax
import jax.numpy as jnp
from parajax import parallelize
@parallelize
@jax.vmap
def relu_single(x):
return jnp.maximum(x, 0)
xs = jnp.arange(-6_000, 6_000)
ys = relu_single(xs) # Parallelized over the batch
Source code in parajax/__init__.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |