liveBook · Manning

Parallel map

As explained in Deep Learning with JAX

The JAX mapping transformation that parallelizes a computation across multiple local devices.

Need to run the same computation on several accelerators at once? pmap() parallelizes a function across local devices using single-program multiple-data (SPMD) execution. It compiles the function with XLA, replicates it, and runs each replica on its own device, so a separate jit() transformation is unnecessary. section 7.0 section 7.1.2

Use pmap() when the work can be divided into independent chunks:

  • Prepare identical participating devices and reshape each input so its leading mapping axis contains one chunk per device. section 7.1 section 7.1.1
  • Keep the mapped axis no larger than jax.local_device_count(); otherwise compilation fails because JAX cannot provide enough local replicas. section 7.1.2 section 7.5
  • Control mapped inputs with in_axes, output placement with out_axes, and cross-device communication with axis_name and collective operations. section 7.2 section 7.5
  • Expect scaling to be sublinear when communication overhead offsets the benefit of adding devices. section 7.0

Parallel dot products

A dot product handles one pair of vectors. Split ten million pairs across eight devices, then add vmap() inside pmap() to process each device's local batch: v1sp = v1s.reshape((8, 1_250_000, 3)) v2sp = v2s.reshape((8, 1_250_000, 3)) dot_parallel = jax.pmap(jax.vmap(dot)) result = dot_parallel(v1sp, v2sp) The result has shape (8, 1_250_000). Reshape it to (10_000_000,) to remove the device and local-batch axes; its values match the vmap() result. section 7.1

pmap() also supports data-parallel neural-network training. Each device receives a data shard and a local parameter copy, computes gradients, and uses a collective sum such as lax.psum() so every device applies the same update. Across hosts, each process sees local devices while collective operations communicate across global devices. section 7.3.2 section 7.4

Listing 7.2 Emulating a multidevice system on a CPU
import os
os.environ['XLA_FLAGS'] = '--xla_force_host_platform_device_count=8' #1
 
import jax                                                           #2
 
jax.devices("cpu")
 
WARNING:jax._src.lib.xla_bridge:No GPU/TPU found, falling back to CPU. (Set 
TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
[CpuDevice(id=0),                                                    #3
 CpuDevice(id=1),                                                    #3
 CpuDevice(id=2),                                                    #3
 CpuDevice(id=3),                                                    #3
 CpuDevice(id=4),    
 CpuDevice(id=5),    
 CpuDevice(id=6),    
 CpuDevice(id=7)]
#1 Sets the env variable with the desired number of devices before importing JAX
#2 Imports JAX
#3 Eight CPU devices are available.

Emulate multiple CPU devices locally to test pmap() semantics before using a multi-GPU or TPU system. section 7.1.1

How Deep Learning with JAX covers it

How deep it goes
Explains device setup, input reshaping, mapping parameters, collectives, nested transformations, training, and multihost execution.
Examples
Demonstrates parallel dot products, CPU device emulation, reshaped vector batches, and data-parallel MNIST-style training.
What it assumes
Assumes familiarity with JAX functions and arrays while teaching device preparation, chunking, reshaping, and functional transformation composition.
What you can do after
Apply pmap() to split array workloads across local devices and aggregate results or gradients with collective operations.
How it connects
Connects pmap() to vmap(), jit(), grad(), XLA, SPMD, collective operations, tensor sharding, and functional transformations.

Also explained in

Compare how 2 books explain parallel map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with JAX. The section links point to the parts of the book it draws on.