liveBook · Manning

Model parallelism

As explained in Deep Learning with PyTorch, Second Edition

A strategy that places different parts of a model on different devices.

Model parallelism splits a model across devices, so each device holds and computes one part. It makes training possible when the full model is too large for a single GPU. By contrast, data parallelism puts a complete model replica on each device and splits the data between them. section 16.5

Each device performs the forward and backward work for its part. In a sequential forward pass, one device processes a batch and sends its intermediate output, or activations, to the next device. That extra communication connects the partitions into a single model. section 16.5

Three model partitions

A simple PyTorch example places linear model parts on three ranks. Rank 0 processes the input and sends its activations to rank 1; rank 1 processes and sends its result to rank 2. The example uses torch.distributed.send() and recv() for this peer-to-peer communication. section 16.5

Listing 16.10 Implementing model parallelism using send and recv
...                 #1
    if rank == 0:                                                      #2
        model_part1 = nn.Linear(1, 2)
        input_batch = torch.tensor([[1.0]])
        part1_activations = model_part1(input_batch)
        print(f"Process {rank} activations from part 1: {part1_activations}")
        dist.send(part1_activations, dst=1)                           
    elif rank == 1:
        model_part2 = nn.Linear(2, 3)
        part1_activations = torch.zeros(2)
        dist.recv(part1_activations, src=0)       #3
        part2_activations = model_part2(part1_activations)
        dist.send(part2_activations, dst=2)
        print(f"Process {rank} activations from part 2: {part2_activations}")
    elif rank == 2:
        model_part3 = nn.Linear(3, 1)
        part2_activations = torch.zeros(3)
        dist.recv(part2_activations, src=1)
        part3_output = model_part3(part2_activations)
        print(f"Process {rank} output from part 3: {part3_output}")

Inspect the rank-specific linear layers and send/receive calls. The displayed excerpt shows the forward communication, not a complete training loop. section 16.5

A basic sequential arrangement can leave devices idle while another device works. Pipeline parallelism addresses this by splitting inputs into microbatches, so different stages can process different microbatches concurrently. It also requires attention to communication overhead and balanced stage workloads. section 16.5.1

Model and data parallelism can also be combined: multiple model replicas can each be partitioned across devices. PyTorch's DeviceMesh helps organize the device subsets and communication patterns needed for such combinations. section 16.6

Figure 16.9 Visualization of a 2 × 4 device mesh, where the rows represent model replicas and the columns represent model partitions. The dashed arrows represent how each device is communicating.
figure

See how a 2 × 4 mesh arranges model replicas and partitions across devices. section 16.6

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains partitioned forward and backward passes and demonstrates forward activation transfers, then extends the discussion to pipelines and combined strategies.
Examples
Uses three linear model partitions and a 2 × 4 device mesh with model replicas and partitions.
What you can do after
Apply it via PyTorch's distributed send and receive operations to pass activations between model partitions.
How it connects
Connects model parallelism to data, pipeline, tensor, and n-dimensional parallelism.

Also explained in

Compare how 5 books explain model parallelism →

Related concepts

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