Example

Price the hand-off in a prefill/decode split

Compare a co-located schedule with a two-stage simulator, then vary the transfer cost to find where splitting stops paying.

Prefill and decode have different resource profiles. Prefill processes many tokens at once and is compute-heavy; decode processes one token per request and is often memory- and launch-bound. Disaggregation puts them on separate workers so each can be sized independently. The price is a hand-off: the KV state must move from the prefill worker to the decode worker, and each stage gains a queue.

This example runs a hand-built simulator for four requests with prefill cost 5, decode cost 1 per token, and transfer cost 2. The co-located schedule completes in a total latency of 106; the disaggregated schedule reports 38 with a transfer cost of 8. Those numbers are not a measurement of disaggregated hardware. The simulator is deliberately simple, and its completion accounting has approximations that the page must state.

Run it

From the canonical repository at commit f958967d70992d7b09d9c75000bd8c543f1644d4:

python examples/ch29/disaggregation.py --json

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

Implementation

The co-located path processes one request to completion before starting the next. The disaggregated path has a prefill queue, a transfer list and a decode queue. The code moves requests between stages and sums completion times.

def co_located(requests: List[int], prefill_cost: int, decode_cost: int) -> RegimeResult:
    # requests: list of output lengths.
    time = 0
    completed = 0
    total_latency = 0
    queue = deque(requests)
    # Process one request at a time to completion.
    while queue:
        out_len = queue.popleft()
        time += prefill_cost + decode_cost * out_len
        completed += 1
        total_latency += time
    return RegimeResult(regime="co-located", completed=completed, total_latency=total_latency, transfer_cost=0, metadata={})


def disaggregated(requests: List[int], prefill_cost: int, decode_cost: int, transfer_cost: int, prefill_workers: int = 1, decode_workers: int = 1) -> RegimeResult:
    prefill_queue = deque(requests)
    decode_queue = deque()
    in_transfer = []
    time = 0
    completed = 0
    total_latency = 0
    # Track completion times.
    while prefill_queue or decode_queue or in_transfer:
        # Prefill workers.
        for _ in range(prefill_workers):
            if prefill_queue:
                out_len = prefill_queue.popleft()
                # After prefill, transfer.
                in_transfer.append((out_len, time + transfer_cost))
        # Move transferred to decode.
        still = []
        for out_len, ready_time in in_transfer:
            if ready_time <= time:
                decode_queue.append((out_len, time))
            else:
                still.append((out_len, ready_time))
        in_transfer = still
        # Decode workers: process one token per worker per time.
        # Simplified: each decode request needs decode_cost*out_len time; process in FIFO.
        # We'll just complete one request per time if decode_queue nonempty.
        if decode_queue and completed < len(requests):
            out_len, start = decode_queue.popleft()
            time_for_decode = decode_cost * out_len
            # schedule completion at time + time_for_decode (approx)
            # For simplicity, complete immediately after processing all tokens in this time step? Instead, track.
            # We'll mark completed and add latency.
            completion = time + time_for_decode
            total_latency += completion
            completed += 1
        time += 1
        if time > 10000:
            break

Expected outcome

Observed with the command above (exit 0):

{
  "co_located": {
    "regime": "co-located",
    "completed": 4,
    "total_latency": 106,
    "transfer_cost": 0,
    "metadata": {}
  },
  "disaggregated": {
    "regime": "disaggregated",
    "completed": 4,
    "total_latency": 38,
    "transfer_cost": 8,
    "metadata": {}
  }
}

The disaggregated total is lower in this trace even after paying eight units of transfer. The result is a property of the simulator’s schedule, not evidence that a split will be faster on real hardware. The co-located total is the sum of cumulative completion times, so it grows as requests wait behind earlier ones.

Mechanism and limitations

Documented: co_located processes requests sequentially and adds prefill plus decode per request. disaggregated moves requests through prefill, transfer and decode queues and reports completed count and total latency. run_demo uses output lengths [4, 8, 4, 8], prefill 5, decode 1 and transfer 2. The tests check both regimes complete, transfer cost is recorded, the demo structure and JSON serialisation.

Observed: the command exited 0 with the two totals above. The targeted tests.test_ch29_disaggregation is part of the canonical suite; no independent test count is claimed here.

Not shown: real prefill and decode throughput, network bandwidth, KV-state serialisation, separate-device execution, queueing delay, or a production disaggregated system. The simulator does not fully charge prefill time in the disaggregated completion path and does not model worker parallelism faithfully; it is a teaching schedule, not a capacity model. The chapter’s disagg_lab.py contains the fuller experiment.

Challenge this result

Rerun with transfer_cost=30 and then transfer_cost=60. Predict whether the disaggregated total remains lower. Explain what the hand-off must cost for the split to break even, and why the answer depends on the request mix and on whether prefill and decode workers can overlap. Then list the measurements a real disaggregation experiment would need that this simulator omits.

Source and verification

Canonical source: examples/ch29/disaggregation.py at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests: tests/test_ch29_disaggregation.py . Chapter: Prefill/Decode Disaggregation . The run above is a fresh local execution on Windows 11 with Python 3.13.15; it is a deterministic simulator, not a disaggregated hardware 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.