Example

Compare four ways to split a model across devices

Run a deterministic cost model for data, tensor, pipeline and expert parallelism and see which quantity each one moves.

Distributed inference is not one technique. Data parallelism keeps a full model on each device and splits requests. Tensor parallelism splits each layer’s matrices and pays an all-reduce. Pipeline parallelism splits layers into stages and pays point-to-point transfers. Expert parallelism routes tokens to different experts and pays an all-to-all.

This example runs a deterministic cost model for four devices with a 1,000-byte model and 100 bytes of activations. Data parallelism reports full compute per device and 100 bytes of communication. Tensor parallelism reports one quarter of the compute but 100 * (4 - 1) = 300 bytes of communication. Pipeline and expert parallelism report one quarter of the compute and 100 bytes of communication. These are model outputs, not measurements on a multi-GPU machine. The page is about the accounting, not a throughput claim.

Run it

From the canonical repository at commit f958967d70992d7b09d9c75000bd8c543f1644d4:

python examples/ch28/topology.py --json

Python 3.11 or later, standard library only. No model, network, GPU or credentials. It finishes immediately.

Implementation

simulate maps a strategy name to one communication formula and one compute fraction. It rejects a non-positive device count and an unknown strategy. The formulas are intentionally simple; the page’s job is to make each assumption visible.

def simulate(strategy: str, num_devices: int, model_bytes: int, activation_bytes: int) -> CostResult:
    if num_devices < 1:
        raise ValueError("num_devices must be positive")
    if strategy == "data":
        comm = activation_bytes  # gradient/activation all-reduce approx
        compute = 1.0  # each device full model
    elif strategy == "tensor":
        comm = activation_bytes * (num_devices - 1)  # all-reduce activations
        compute = 1.0 / num_devices
    elif strategy == "pipeline":
        comm = activation_bytes  # point-to-point between stages
        compute = 1.0 / num_devices
    elif strategy == "expert":
        comm = activation_bytes  # all-to-all tokens
        compute = 1.0 / num_devices
    else:
        raise ValueError(f"unknown strategy: {strategy}")
    return CostResult(
        strategy=strategy,
        num_devices=num_devices,
        model_bytes=model_bytes,
        activation_bytes=activation_bytes,
        communication_bytes=comm,
        compute_per_device=compute,
        metadata={},
    )

Expected outcome

Observed with the command above (exit 0):

{
  "results": [
    {
      "strategy": "data",
      "num_devices": 4,
      "model_bytes": 1000,
      "activation_bytes": 100,
      "communication_bytes": 100,
      "compute_per_device": 1.0,
      "metadata": {}
    },
    {
      "strategy": "tensor",
      "num_devices": 4,
      "model_bytes": 1000,
      "activation_bytes": 100,
      "communication_bytes": 300,
      "compute_per_device": 0.25,
      "metadata": {}
    },
    {
      "strategy": "pipeline",
      "num_devices": 4,
      "model_bytes": 1000,
      "activation_bytes": 100,
      "communication_bytes": 100,
      "compute_per_device": 0.25,
      "metadata": {}
    },
    {
      "strategy": "expert",
      "num_devices": 4,
      "model_bytes": 1000,
      "activation_bytes": 100,
      "communication_bytes": 100,
      "compute_per_device": 0.25,
      "metadata": {}
    }
  ]
}

The tensor case is the only one whose communication grows with device count in this model. Pipeline and expert cases happen to share the same numbers because the formulas use the same activation term; that does not make the strategies equivalent. The model does not include link bandwidth, latency, stragglers or memory.

Mechanism and limitations

Documented: simulate rejects an invalid device count and unknown strategy. Data parallelism keeps full compute per device; tensor, pipeline and expert parallelism divide compute by device count. Tensor communication scales with num_devices - 1; the others use one activation term. The tests check every strategy, invalid strategy, invalid device count, the four-result demo and JSON serialisation.

Observed: the command exited 0 with the four result objects above. The targeted tests.test_ch28_topology is part of the canonical suite; no independent test count is claimed here.

Not shown: real multi-GPU execution, collective latency, link bandwidth, pipeline bubbles, expert load imbalance, memory per device, or scaling efficiency. The model’s communication bytes are not a time, and equal bytes do not mean equal cost. No distributed hardware was used.

Challenge this result

Run the same model with eight devices. Compute tensor communication by hand before running: 100 * (8 - 1) = 700. Then explain why tensor parallelism’s communication grows while its compute fraction falls, and what a real measurement would need to add to decide whether the split is worthwhile. Finally, set model_bytes to a value that does not fit on one device and explain why this cost model cannot answer the fit question at all.

Source and verification

Canonical source: examples/ch28/topology.py at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests: tests/test_ch28_topology.py . Chapter: Distributed Inference . The run above is a fresh local execution on Windows 11 with Python 3.13.15; it is a deterministic cost model, not a distributed measurement.

Understand this example

Copy this prompt into your AI tool. No code runs here.

Apply this example

Copy this prompt into your AI tool. No code runs here.