Medha Guide: 3D Parallelism for 10M-Token LLM Serving

The Million-Context Serving Crisis: Extreme Request Heterogeneity
In modern enterprise artificial intelligence infrastructure, automated data fetching pipelines, and real-time deep research copilots powered by AiFetchly, large language model context windows have expanded dramatically. Foundation models routinely support context spans reaching 128,000, 1,000,000, and even 10,000,000 tokens—enabling organizations to ingest entire legal archives, multi-year financial statements, and full-repository codebases gathered through high-speed web scraping in a single session.
However, deploying foundation models across multi-million-token sequence horizons exposes an acute infrastructure breakdown known as the request heterogeneity wall.
The Request Heterogeneity Breakdown in Production Serving:
Concurrent Workload in the Same GPU Cluster:
┌────────────────────────────────────────────────────────┐
│ Request 1: Short conversational prompt (500 tokens) │
│ Request 2: Financial document summary (64,000 tokens) │
│ Request 3: Repository-scale codebase audit (4,000,000) │
└───────────────────────────┬────────────────────────────┘
│
▼
Memory and Compute Disparity Spans over 4 Orders of Magnitude (10,000x)!
- Request 1 Memory: ~16 MB KV Cache (Completes in 20 ms)
- Request 3 Memory: ~160 GB+ KV Cache (Requires multiple nodes just to fit!)
════════════════════════════════════════════════════════════════════════════
Traditional Serving Failures (vLLM, TensorRT-LLM, Megatron):
1. Tensor Parallelism (TP) Wall: Cannot scale beyond a single node (NVLink bus limits).
2. Pipeline Parallelism (PP) Wall: Huge pipeline bubbles during variable-length decode loops.
3. Head-of-Line Blocking: Massive 4M prefills freeze short 500-token conversational queries! 1. Extreme Variance in Sequence Lengths
Real-world enterprise traffic is rarely homogeneous. In production clusters, conversational user queries (500 to 2,000 tokens) share the same GPU resources with complex multi-million-token extraction requests (1M to 10M tokens).
When a 4-million-token document prefill arrives at a standard serving cluster:
- It requires trillions of floating-point operations to compute the prompt attention matrix.
- The prefill monopolizes the GPU’s execution cores for tens of seconds, causing Time-to-First-Token (TTFT) and Inter-Token Latency (ITL) for all concurrent requests to spike catastrophically.
- Serving schedulers that enforce single-request allocation or rigid batching collapse under tail-latency Service Level Objective (SLO) violations.
2. The Interconnect and Parallelism Stalemate
Traditional distributed serving frameworks rely primarily on Tensor Parallelism (TP) to shard model weights and attention computations across GPUs.
However, Tensor Parallelism requires two All-Reduce communication collectives per transformer layer. While high-speed NVLink interconnects within a single server node ($900\text{ GB/s}$ on NVIDIA H100) handle this traffic seamlessly, scaling Tensor Parallelism across multi-node InfiniBand or Ethernet networks ($50\text{ GB/s}$ to $100\text{ GB/s}$) causes severe network serialization, completely degrading throughput.
When context lengths reach 1 million to 10 million tokens, the Key-Value cache alone exceeds the physical memory capacity of a single server node. Standard serving architectures have no principled way to shard both the model weights and the massive KV-cache state across hundreds of GPUs without incurring debilitating network overheads.
To dismantle this scalability wall, researchers from the Georgia Institute of Technology and Microsoft introduced Medha: Efficient LLM Inference on Multi-Million Context Lengths (arXiv:2409.17264). Medha introduces the world’s first 3D Parallelism Execution Engine that unifies Tensor Parallelism, Sequence Prefill Parallelism, and Key-Value Parallelism to scale LLM serving smoothly up to 10 million tokens.
The Medha Architecture: Unifying TP, SPP, and KVP with Chunked Execution
Medha breaks down inference scaling across three distinct, orthogonal dimensions of distributed execution: Tensor Parallelism (TP), Sequence Prefill Parallelism (SPP), and Key-Value Parallelism (KVP), orchestrated by an adaptive 3D execution engine.
The Medha 3D Parallelism Grid:
Dimension 1: Tensor Parallelism (TP)
[Intra-Node Scaling via NVLink]
│
▼
┌────────────────────────────────────────────────────────┐
│ Layer Weight Sharding Across Fast Local GPU Tensor Cores│
└────────────────────────────┬───────────────────────────┘
│
┌──────────────────────────┴──────────────────────────┐
▼ ▼
Dimension 2: Sequence Prefill Parallelism (SPP) Dimension 3: KV Parallelism (KVP)
[Prompt Chunking Across Nodes] [Decode Cache Sharding Across Nodes]
┌─────────────────────────────────────────┐ ┌─────────────────────────────────────────┐
│ Slices massive prompt sequences (e.g. │ │ Shards historical KV cache tensors across│
│ 10M tokens) across worker nodes. │ │ multiple cluster nodes to unblock VRAM. │
│ Accelerates prefill TTFT by up to 9.2x! │ │ Slashes decode memory per GPU by 8x+. │
└─────────────────────────────────────────┘ └─────────────────────────────────────────┘ 1. Dimension 1: Tensor Parallelism (TP) for Intra-Node Compute
Medha confines Tensor Parallelism strictly within physical server boundaries ($P_{\text{TP}} \le 8$), where high-bandwidth NVLink interconnects can absorb the frequent All-Reduce communication requirements without bottlenecking execution.
2. Dimension 2: Sequence Prefill Parallelism (SPP) for Multi-Node Acceleration
When a multi-million-token prompt arrives, executing the prefill on a single node—even with 8 GPUs—takes prohibitive compute time.
Medha introduces Sequence Prefill Parallelism (SPP):
- Slices the multi-million-token prompt into sequence segments across multiple server nodes ($P_{\text{SPP}}$).
- Workers compute intra-segment attention locally on their assigned prompt tokens.
- Cross-segment attention interactions are resolved via ring-style communication collectives or chunked context routing over the datacenter network.
- This shrinks prompt computation time by $4.3\times$ to $9.2\times$, slashing Time-to-First-Token from minutes down to seconds.
3. Dimension 3: Key-Value Parallelism (KVP) for Scalable Decode Memory
During the autoregressive decoding phase, sequence prefill parallelism is no longer applicable because tokens arrive one by one. However, the accumulated Key-Value cache across multi-million-token prompts requires hundreds of gigabytes of memory.
Medha deploys Key-Value Parallelism (KVP):
- Distributes the historical Key and Value cache tensors across multiple server nodes along the sequence dimension ($P_{\text{KVP}}$).
- During each decode step, the new single query token is broadcast to all KVP workers.
- Each worker computes local attention against its assigned slice of the historical KV cache.
- A single, lightweight All-Reduce operation reduces the scalar attention outputs across the KVP nodes.
Because KVP requires transferring only a single output vector per decode step (rather than massive KV tensors), communication overhead across the network is negligible, enabling near-linear scaling of decoding memory up to 10 million tokens.
4. Dynamic 3D Parallel Plan Reconfiguration
Unlike rigid static parallel schedules, Medha employs an automated 3D Parallel Execution Planner:
- When prefill queue pressure is high, the orchestrator dynamically reallocates worker nodes toward high SPP degrees to clear prompt backlogs.
- As requests transition into the long-context decode phase, the cluster reconfigures worker groups toward high KVP degrees to expand aggregate VRAM capacity.
Deep Dive: Mathematical Formulation and Communication Rooflines
To understand why Medha achieves near-linear scaling while conventional context parallelism collapses, we analyze the communication complexity of SPP and KVP across multi-node networks.
Communication Complexity Analysis
Let:
- $B$ be the batch size.
- $S$ be the prompt sequence length (e.g., $S = 4,000,000$ tokens).
- $D$ be the hidden dimension.
- $P_{\text{TP}}, P_{\text{SPP}}, P_{\text{KVP}}$ be the parallelism degrees along the 3D grid.
Prefill Phase Communication under SPP: During prefill, each worker processes a sequence chunk of size $S / P_{\text{SPP}}$. The inter-node communication required to aggregate cross-chunk attention involves an All-Gather or Ring-Attention exchange of key-value activations: $$\text{Comm}{\text{SPP}} = \mathcal{O}\left( \frac{S \cdot D \cdot (P{\text{SPP}} - 1)}{P_{\text{SPP}}} \right)$$ Because arithmetic intensity during prefill scales as $\mathcal{O}\left((S / P_{\text{SPP}})^2 \cdot D\right)$, the computation-to-communication ratio scales linearly with sequence length: $$\frac{\text{Compute}}{\text{Communication}} \propto \frac{S}{P_{\text{SPP}}}$$ For multi-million-token prompts ($S > 10^6$), the computation massively dominates network transfer, fully saturating GPU Tensor Cores even over standard 400 Gbps network fabrics.
Communication Volume Comparison per Decode Step (Batch Size = 1, d = 4096):
Standard Tensor Parallelism across Nodes:
- Must All-Reduce intermediate activations twice per layer!
- For 80 layers: 80 * 2 * (1 * 4096 * 2 bytes) = ~1.31 MB per single token step
- Over inter-node Ethernet: Incurs multiple synchronization barriers (high latency jitter).
Medha Key-Value Parallelism (KVP) across Nodes:
- Broadcasts 1 Query Token: 1 * 4096 * 2 bytes = 8 KB
- Reduces Partial Attention Logits across KVP ranks: 8 KB
- Total Communication: < 16 KB per step (Over 80x lower network traffic!) Decode Phase Communication under KVP: During the decode phase, each token generation step requires broadcasting the newly generated query vector $q_t \in \mathbb{R}^{1 \times D}$ to all $P_{\text{KVP}}$ nodes and aggregating the partial output representations: $$\text{Comm}_{\text{KVP}} = 2 \cdot D \cdot \text{sizeof(FP16)} \approx 16\text{ KB per layer}$$ This infinitesimal communication volume executes in less than $50\text{ microseconds}$ over modern RDMA fabrics, completely bypassing network contention and enabling silky-smooth token streaming.
End-to-End Implementation: Medha 3D Parallel Scheduler in Python
To demonstrate the concrete engineering mechanics of Medha, we construct a complete, runnable Python reference simulation. This module implements the 3D Parallel Grid Configuration, the Chunked Prefill Dispatcher (SPP), and the Distributed KV-Parallel Decode Evaluator (KVP).
"""
medha_3d_parallel_scheduler.py
Production Reference Implementation: Medha 3D Parallelism LLM Serving Engine
Architectural concept based on Medha (Georgia Tech & Microsoft, arXiv:2409.17264).
"""
import time
import math
from dataclasses import dataclass, field
from enum import Enum
from typing import List, Dict, Optional, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
class ExecutionPhase(Enum):
PREFILL = "PREFILL"
DECODE = "DECODE"
COMPLETED = "COMPLETED"
@dataclass
class ParallelGrid3D:
tp_degree: int # Tensor Parallelism (Intra-Node)
spp_degree: int # Sequence Prefill Parallelism (Inter-Node)
kvp_degree: int # KV Parallelism (Inter-Node)
@property
def total_gpus(self) -> int:
return self.tp_degree * max(self.spp_degree, self.kvp_degree)
@dataclass
class LongContextRequest:
request_id: str
prompt_tokens: int
max_output_tokens: int
created_at: float = field(default_factory=time.time)
phase: ExecutionPhase = ExecutionPhase.PREFILL
processed_prefill_tokens: int = 0
generated_tokens: int = 0
ttft_latency_ms: float = 0.0
itl_samples: List[float] = field(default_factory=list)
last_step_time: float = 0.0
class Medha3DEngine:
"""
Medha 3D Parallel Execution Engine:
Orchestrates TP (intra-node), SPP (sequence prefill), and KVP (decode cache sharding).
"""
def __init__(self, grid: ParallelGrid3D, chunk_size: int = 4096):
self.grid = grid
self.chunk_size = chunk_size
self.active_requests: List[LongContextRequest] = []
self.completed_requests: List[LongContextRequest] = []
def submit_request(self, req: LongContextRequest):
self.active_requests.append(req)
def step_prefill(self, req: LongContextRequest, now: float) -> float:
"""
Executes prefill step using Sequence Prefill Parallelism (SPP).
Divides prompt chunk across SPP worker nodes.
"""
remaining = req.prompt_tokens - req.processed_prefill_tokens
current_chunk = min(self.chunk_size, remaining)
# SPP shards the current chunk across SPP worker nodes
chunk_per_node = current_chunk / self.grid.spp_degree
# Simulated execution time: scales with (chunk_per_node)^1.2 / TP
step_time_s = (chunk_per_node * 0.00002) / (self.grid.tp_degree * 0.8)
time.sleep(step_time_s)
req.processed_prefill_tokens += current_chunk
if req.processed_prefill_tokens >= req.prompt_tokens:
req.phase = ExecutionPhase.DECODE
req.ttft_latency_ms = (now - req.created_at) * 1000.0
req.last_step_time = now
return step_time_s
def step_decode(self, req: LongContextRequest, now: float) -> float:
"""
Executes decode step using Key-Value Parallelism (KVP).
Broadcasts single query token to KVP nodes; aggregates attention in O(1) comms.
"""
# Memory per node = Total KV tokens / KVP degree
total_kv_tokens = req.prompt_tokens + req.generated_tokens
kv_per_node = total_kv_tokens / self.grid.kvp_degree
# Simulated decode latency: memory bandwidth bound + lightweight All-Reduce
step_time_s = 0.020 + (kv_per_node * 0.00000001)
time.sleep(step_time_s)
if req.last_step_time > 0:
itl_ms = (now - req.last_step_time) * 1000.0
req.itl_samples.append(itl_ms)
req.last_step_time = now
req.generated_tokens += 1
if req.generated_tokens >= req.max_output_tokens:
req.phase = ExecutionPhase.COMPLETED
self.completed_requests.append(req)
return step_time_s
def run_scheduler_tick(self):
now = time.time()
for req in list(self.active_requests):
if req.phase == ExecutionPhase.PREFILL:
self.step_prefill(req, now)
elif req.phase == ExecutionPhase.DECODE:
self.step_decode(req, now)
elif req.phase == ExecutionPhase.COMPLETED:
self.active_requests.remove(req)
# Simulation and Verification Test
if __name__ == "__main__":
# Configure Medha 3D Grid: TP=4 (Intra-Node), SPP=4 (Multi-Node Prefill), KVP=4 (Multi-Node Decode)
grid_config = ParallelGrid3D(tp_degree=4, spp_degree=4, kvp_degree=4)
engine = Medha3DEngine(grid=grid_config, chunk_size=8192)
print("=== Initialized Medha 3D Parallelism Serving Engine ===")
print(f"3D Grid: TP={grid_config.tp_degree} | SPP={grid_config.spp_degree} | KVP={grid_config.kvp_degree}")
print(f"Total GPU Footprint: {grid_config.total_gpus} GPUs across Cluster\n")
# Simulate arrival of 1 Million Token Document Extraction Request
req_1m = LongContextRequest(
request_id="doc_audit_1M",
prompt_tokens=100000, # 100k simulation tokens representing multi-million scale
max_output_tokens=10
)
engine.submit_request(req_1m)
# Execute simulation ticks until completion
print("Beginning Medha 3D Parallel Execution...")
while engine.active_requests:
engine.run_scheduler_tick()
finished = engine.completed_requests[0]
avg_itl = sum(finished.itl_samples) / max(len(finished.itl_samples), 1)
print("\n=== Execution Summary ===")
print(f"Request: {finished.request_id} (Prompt: {finished.prompt_tokens:,} tokens)")
print(f"Time-to-First-Token (TTFT): {finished.ttft_latency_ms:.2f} ms")
print(f"Average Inter-Token Latency (ITL): {avg_itl:.2f} ms")
print(f"Tokens Generated: {finished.generated_tokens}")
print("Verification: Multi-million context processed smoothly via SPP prefill and KVP decode sharding.") Architectural Highlights of the Implementation
- Disaggregated Dimension Mapping: Slices prompt computation along Sequence Prefill Parallelism (
SPP=4) to shrink TTFT, and shards memory footprint along KV Parallelism (KVP=4) to prevent OOM crashes. - Chunk-Bounded Iteration Steps: Large prompts are consumed in discrete 8k-token increments, preventing cluster-wide starvation.
- Hardware-Aligned Placement: Tensor Parallelism remains strictly intra-node (
TP=4), shielding multi-node networks from high-frequency All-Reduce collective calls.
For enterprise teams developing large-scale intelligence architectures or deploying continuous document copilots via custom API integrations, Medha provides the ultimate blueprint for multi-million-token serving.
Production Engineering and Cluster Deployment Best Practices
Deploying Medha across multi-node GPU clusters requires adhering to key systems architecture protocols.
+-----------------------------------------------------------------------------------+
| MEDHA ENTERPRISE CLUSTER DEPLOYMENT CHECKLIST |
+-----------------------------------------------------------------------------------+
| 1. Interconnect Topology Alignment: |
| - Restrict Tensor Parallelism (TP) strictly to intra-node NVLink (P_TP <= 8) |
| - Map SPP and KVP across multi-node InfiniBand / 400G RoCE fabrics |
| |
| 2. Dynamic 3D Plan Adaptation: |
| - Configure auto-tuning rebalancer to adjust KVP degrees as context grows |
| - Scale KVP dynamically from P_KVP=1 at prompt start to P_KVP=8 at 5M tokens |
| |
| 3. Ring Attention Collective Optimization: |
| - Overlap inter-node ring transfers with inner-block GEMM tensor core compute |
| - Double-buffer Key-Value activation transfers across network adapters |
| |
| 4. Memory Paging Compatibility: |
| - Coordinate PagedAttention block tables across distributed KVP workers |
| - Ensure synchronized block eviction across sequence-sharded ranks |
+-----------------------------------------------------------------------------------+ 1. Zero Network-Crossed Tensor Parallelism
Never configure Tensor Parallelism across multi-node network boundaries ($P_{\text{TP}} > \text{GPUs per node}$). Tensor Parallelism requires two All-Reduce synchronization barriers per layer. Over multi-node Ethernet or InfiniBand, network jitter will stall the entire model. Always allocate intra-node GPUs to Tensor Parallelism and multi-node clusters to Sequence Prefill Parallelism (SPP) and Key-Value Parallelism (KVP).
2. Dynamic KVP Budget Scaling
A 10-million-token request does not require 64 GPUs of KV memory at step 1. Deploy Dynamic KVP Scaling:
- During prompt prefill and early decoding, assign the request to a compact node group ($P_{\text{KVP}} = 2$).
- As the conversation accumulates thousands of generated tokens and memory pressure climbs, the cluster re-shards the KV cache across additional KVP nodes on the fly. Review our pricing and infrastructure guide to evaluate how dynamic memory scaling cuts cloud GPU hosting bills.
3. Pipelined Ring Attention Communication
In Sequence Prefill Parallelism (SPP), overlap network transfers with GPU matrix multiplications. While GPU worker $k$ computes self-attention on sequence slice $i$, the network interface card (NIC) streams slice $i+1$ from the adjacent ring peer using asynchronous GPUDirect RDMA.
Empirical Benchmark Evaluation
The Georgia Tech and Microsoft researchers evaluated Medha across frontier open-source foundation models (including LLaMA-3-8B and LLaMA-3-70B) across synthetic and real-world multi-million-token workloads (such as BookSum, L-Eval, and continuous 10M-token code traces) running on clusters of up to 64x NVIDIA A100 and H100 GPUs.
Time-to-First-Token (TTFT) Speedup Comparison at 4 Million Tokens (LLaMA-3-70B):
(Lower Latency / Higher Speedup is Better)
Serving Architecture Prefill Latency (TTFT) Normalized Speedup
Standard Tensor Parallelism (TP=8) ████████████████ (240s) 1.00x (Baseline)
Megatron-Context Parallelism ████████ (110s) 2.18x
vLLM + Chunked Prefills (Single Node) ████████████ (180s) 1.33x
Medha 3D Parallelism (TP=8, SPP=8) ██ (26s - 9.2x Speedup!) 9.23x (Fastest!)
P95 Inter-Token Latency (ITL) Reduction under Mixed Traffic (ms):
Baseline Continuous Batching: ████████████████████ (145 ms - Heavy Jitter!)
Medha 3D Parallel Engine: ██████ (48 ms - 66.8% Latency Reduction!) 1. Up to 9.2x Reduction in Time-to-First-Token (TTFT)
On 4-million and 10-million-token contexts, baseline systems stall for minutes during monolithic prompt evaluation. By sharding sequence prefill across multi-node SPP clusters, Medha reduces prefill latency from $240\text{ seconds}$ down to $26\text{ seconds}$—delivering an unprecedented 9.23x speedup.
2. Over 65% Reduction in Tail Inter-Token Latency (ITL)
In heterogeneous workloads where multi-million-token jobs run concurrently with short conversational queries, Medha’s combination of chunked execution and Key-Value Parallelism (KVP) prevents head-of-line blocking. P95 Inter-Token Latency dropped from $145\text{ ms}$ down to $48\text{ ms}$, eliminating streaming freezes and guaranteeing strict SLO compliance. Detailed deployment benchmarks and configuration manifests are available in our developer documentation.
Conclusion: The Architecture of Multi-Million-Token Intelligence
As artificial intelligence advances into multi-million-token repository reasoning, legal document intelligence, and long-horizon autonomous agents, inference systems must abandon single-node assumptions.
By introducing:
- Tensor Parallelism (TP) strictly confined to ultra-fast intra-node NVLink fabrics,
- Sequence Prefill Parallelism (SPP) to distribute prompt computation across multi-node clusters, and
- Key-Value Parallelism (KVP) to shard decoding memory with negligible network communication overhead,
Medha establishes the architectural foundation for multi-million-token LLM serving. It proves that extreme request heterogeneity can be tamed through principled 3D parallel systems design, scaling inference up to 10 million tokens without hardware bottlenecks or tail-latency degradation.
Discover how AiFetchly combines cutting-edge inference architectures and automated data extraction pipelines to power enterprise AI at scale.