Lab 18 (multi_gpu_profiling_cudagraph_lab) — Bố cục Đa GPU (TP/PP/DP/CP), MoE Wide-EP, Định tuyến Nhận thức Cache, Truy vết Từ Yêu cầu Đến Nhân GPU & Piecewise CUDA Graphs
Hướng dẫn thực hành, cài đặt môi trường .venv/uv và tiêu chuẩn kiểm định pytest.
Unit ID:
T2-P4-U04| Chương Song hành: ch-23-multi-gpu-moe-routing-profiling-and-cuda-graphs.md
Truy xuất Giáo trình Gốc (R42& Chuyên đề#31–#35): Dive into LLM-serving (KV cache,LLM serving challenges and solutions,vLLM,TensorRT-LLM,SGLang); Serving Mixture-of-Experts Across GPUs (#31); Picking a Multi-GPU Layout (#32); Cache-Aware Routing Across LLM Replicas (#33); Profiling LLM Inference from Request Trace to GPU Kernel (#34); CUDA Graphs for LLM Serving with Variable Shapes (#35).
Kiến trúc Hai Tầng (100%Offline Verifiable): Tầng 1 cung cấp cấu hình triển khai sạch (configs/moe_wide_ep_vllm_config.yaml,configs/cuda_graph_and_router_config.yaml—0bí mật văn bản thuần,0thẻ:latest); Tầng 2 (multi_gpu_profiling_cudagraph_lab) thực thi5thí nghiệm xác định (seed = 42) kết hợpPyTorch CPU Profilerthực tế và truy vếtOpenTelemetry(BoundedInMemorySpanExporter), đạt28/28pytest pass (0skip),0lỗiruff/mypy,0# type: ignore,uv.lockxác thực sạch.
1. Mục tiêu Bài thực hành & 6 Bất biến Kỹ thuật (seed = 42)#
- Bố cục Đa GPU
TP/PP/DP/CP& Phản ví dụPCIe(#32,Tests 02–05, 27): Trên cụm8x H100phục vụ mô hình70B FP8(SLOTTFT <= 500 ms,TPOT <= 25 ms),TP2+DP4trênNVLink(900 GB/s) đạt7,790.5 tok/s(30.43 req/sGoodput,$0.8557/1Mtokens) — tăng1.41xthông lượng và giảm1.41xchi phí so vớiTP8 NVLink(5,544.0 tok/s,$1.2025/1M);TP4+PP2nhất quán \(L/\text{PP}=40\) lớp đạt8.28 msTPOT (3,629.8 tok/s). Nếu triển khai nhầmTP8quaPCIe Gen5(64 GB/s), chi phí đồng bộAll-Reducechiếm84.81%đẩyTPOTlên28.91 ms(5.03xchậm hơn, vỡ SLO);PP8 NVLinkchịu30.43%bong bóng (27.92 msTPOT, vỡ SLO). Với prefill128k,CP4+TP2 Ring Attentioncắt1.44xbộ nhớ kích hoạt đỉnh (9.7vs14.0 GiB/GPU). - Phục vụ
MoE(TP8vsDP8+EP8vsWide-EP32,#31,Tests 06–07, 28): Trên mô hình thưa chuẩn DeepSeek-V3 (256chuyên gia,Top-K=8,1shared,FP8) với lệch tải Zipfian (s = 1.15),DP8+EP8không nhân bản bị straggler lệch6.09x(43.55 ms/step,734.8 tok/s/GPU). Bổ sung48khe chuyên gia nóng dư thừa (EPLBtham lam + đóng góiLPT+ điều phối Power-of-Choice) kéo tỷ lệ lệch tải xuống tự nhiên còn1.05x(5.80xcân bằng hơn) và nâng tốc độ lên1,916.3 tok/s/GPU(15,330.2 tok/s). Mở rộng sangWide-EP32(DP32+EP32,64khe nóng dư thừa, gối đầuAll-to-All) đạt2,246.8 tok/s/GPU(71,898.3 tok/stoàn cụm,1.09xskew, gấp3.10xmỗi GPU so vớiTP8_DenseStyle) với sức chứaKV-Cachegấp32x. - Định tuyến Nhận thức
KV-CacheCó Giới hạn Tải (#33,Tests 08–09): Trên240yêu cầu lệch Zipfian (46%lưu lượng rơi vào tiền tố nóng1,536token) qua8bản sao (TTFT SLO <= 550 ms):Round-Robinchỉ trúng50.83%(p99 TTFT = 831.83 ms, vỡ SLO);Prefix-Aware Unboundedtrúng97.5%nhưng dồn118/240yêu cầu vào1bản sao gây nghẽnmax load = 3.77xvàp99 TTFT = 2,261.66 ms(58.33%SLO).Cache-Aware Load-Bounded(load_bound_factor = 1.25) giữ74.17%KV hit (+23.34 ppvs RR), giới hạn tải1.41x, cắtp99 TTFTxuống275.49 ms(8.21xnhanh hơn Unbounded) và đạt100.0%SLO (58.25 req/sGoodput). - Hồ sơ Từ Truy vết Yêu cầu Đến Nhân GPU (
#34,Tests 10–12): ChạyPyTorch CPU Profilerthực tế (torch.profiler.profile) chứng minh hợp nhất tác vụ làm giảm3.76xsố lệnh ATen (1,668 -> 444trong 3 vòng lặp, sai khác số học2.6e-06), đồng thời bộ phân loại thácOpenTelemetryphân tách chính xác4chế độ nghẽn (cpu_launch_boundvới63.78%GPU bubble,hbm_bandwidth_boundvới75.26%đọc HBM,nccl_comm_boundvới69.58%NCCL, vàbalanced_optimized_stepvới13.35%bubble). Piecewise CUDA Graphs& Xô Lô Hỗn hợp (#35,Tests 13–14): Qua120bước batch & chiều dài chuỗi động:Eagertốn5.18 ms/stepchỉ để khởi chạy kernel (2,698.5 tok/s);Monolithic 2D Capturengốn8.64 GiBVRAM (tràn trần3.5 GiB) và đệm thừa26.08%batch +26.78%seq;Piecewise Hybrid Buckets([1,2,4,8,16,24,32,48,64,96,128]) cắt thời gian launch xuống0.38 ms(13.63x), xóa sạch0.0%đệm chiều dàiKV, giảm padding batch xuống11.05%(1.67xthấp hơn Power-of-2) trong1.76 GiBVRAM, đạt7,450.3 tok/s(2.76xnhanh hơnEager).- Kiểm toán YAML & Truy vết
Bounded OTel(Tests 15–26): Quét sạch2/2YAML (0bí mật tĩnh,0thẻ:latest,3image ghim phiên bản,127.0.0.1), ghi nhận39OpenTelemetry spans sạch (0rò rỉ prompt/token).
2. Cấu trúc Thư mục Lab 18#
lab-18-trace-to-kernel-profiling-and-cuda-graphs/
├── configs/
│ ├── moe_wide_ep_vllm_config.yaml # Cấu hình TP2+DP4 & Wide-EP32 MoE + EPLB sạch bí mật
│ └── cuda_graph_and_router_config.yaml # Chính sách Router Load-Bounded 1.25x & Piecewise CUDA Graphs
├── src/multi_gpu_profiling_cudagraph_lab/
│ ├── __init__.py
│ ├── multi_gpu_moe_and_routing.py # Exp 1 (#32), Exp 2 (#31 MoE + Zipf sweep) & Exp 3 (#33 Router)
│ └── trace_profiling_and_cudagraphs.py # Exp 4 (#34 PyTorch Profiler + OTel), Exp 5 (#35) & YAML Audit
├── scripts/
│ └── run_lab_demo.py # CLI sinh artifacts/t2_ch23_lab18_results.json (6 KPI, 4 Bar, 4 Table)
├── tests/
│ └── test_multi_gpu_profiling_cudagraph_lab.py # 28 hàm pytest (0 skip, Hypothesis, tamper tests & AST gate)
├── artifacts/
│ └── t2_ch23_lab18_results.json # Kết quả đo đạc xác định (seed=42)
├── pyproject.toml
├── uv.lock
└── README.md
3. Hướng dẫn Chạy & Kiểm thử Tự động#
cd 03-track-2-data-and-ai-engineering/part-04-ml-and-llm-serving-systems/labs/lab-18-trace-to-kernel-profiling-and-cuda-graphs
# 1. Sinh báo cáo kết quả chuẩn hóa (seed=42)
PYTHONPATH=src python scripts/run_lab_demo.py --seed 42 --output artifacts/t2_ch23_lab18_results.json
# 2. Kiểm định lint, kiểu tĩnh mypy strict, uv.lock và toàn bộ 28 test cases
ruff check src scripts tests
mypy --strict src tests
pytest -p no:cacheprovider -v
uv lock --check