AI 算力栈快速开始:30 分钟测出你机器的真实算力
七天实战
本页对应 7天学会 AI 算力栈 的 Day 1–3,子站提供一键测量脚本与结果对照表。
一、先看清硬件
nvidia-smi -L
# GPU 0: NVIDIA H100 80GB HBM3 (UUID: ...)
# GPU 1: ...
nvidia-smi --query-gpu=index,name,memory.total,clocks.max.sm,power.limit \
--format=csv
# 互联拓扑(决定并行策略)
nvidia-smi topo -m
# GPU0 GPU1 GPU2 ... NIC0 CPU Affinity
# GPU0 X NV18 NV18 ... PIX 0-31
# ...
读拓扑表的口诀:
| 标记 | 含义 | 带宽量级 |
|---|---|---|
NV# | NVLink,数字越大代次越高 | 300–900 GB/s |
PIX | 同一 PCIe 交换机下 | 25–50 GB/s |
PHB | 同 NUMA 经主机桥 | 25–50 GB/s |
SYS | 跨 NUMA(跨 CPU socket) | 更慢 |
NODE | 同一 NUMA 节点内 PCIe | 中等 |
只要看到 SYS 或 PIX 而不是 NV,TP 就不要跨这两张卡。这是最常见的隐性性能损失。
二、测单卡实际算力(10 分钟)
import torch, time
torch.backends.cuda.matmul.allow_tf32 = True # 或 bf16
def bench_matmul(n=8192, dtype=torch.bfloat16, iters=50, warmup=10):
a = torch.randn(n, n, device="cuda", dtype=dtype)
b = torch.randn(n, n, device="cuda", dtype=dtype)
for _ in range(warmup):
c = a @ b
torch.cuda.synchronize()
t0 = time.perf_counter()
for _ in range(iters):
c = a @ b
torch.cuda.synchronize()
dt = (time.perf_counter() - t0) / iters
flops = 2 * n**3 # 乘加各算 1 FLOP
return flops / dt / 1e12 # TFLOPS
print("实际 TFLOPS:", round(bench_matmul(), 1))
结果判读(单卡,稠密,BF16):
| 结果 | 含义 |
|---|---|
| 达到理论值 70%–85% | 正常(cuBLAS 效率) |
| 50%–70% | 频率被功耗墙压低,或矩阵尺寸太小 |
| < 50% | 检查 TF32/精度设置、是否开了 ECC、是否 MIG 切分 |
「理论值」怎么查
不要用厂商宣传的「带稀疏」数字。H100 SXM BF16 稠密是 989 TFLOPS,宣传的 1979 是稀疏值。用稀疏值当基准,你会永远觉得「只跑到 50%」。 另外要注意 Boost 频率与功耗墙:整机功耗受限时,8 卡同时跑会比单卡跑低 10%–20%。
三、测 HBM 带宽
def bench_copy(size_gb=8, iters=20):
n = size_gb * 1024**3 // 2 # bf16 = 2 bytes
x = torch.empty(n, device="cuda", dtype=torch.bfloat16)
y = torch.empty_like(x)
for _ in range(5): y.copy_(x)
torch.cuda.synchronize()
t0 = time.perf_counter()
for _ in range(iters): y.copy_(x)
torch.cuda.synchronize()
dt = (time.perf_counter() - t0) / iters
bytes_moved = n * 2 * 2 # 读 + 写
return bytes_moved / dt / 1e9 # GB/s
print("HBM 带宽:", round(bench_copy(), 1), "GB/s")
参考值:A100 80GB 约 1550 GB/s(理论 2039),H100 SXM 约 3000 GB/s(理论 3350)。达到理论值 85%–90% 即为正常。
这个数字的用途:所有「内存受限」的算子(LayerNorm、Softmax、逐元素)的下限都由它决定。如果一个 elementwise 算子只用了 30% 带宽,说明它写得不好。
四、测 8 卡 AllReduce(关键,15 分钟)
git clone https://github.com/NVIDIA/nccl-tests && cd nccl-tests
make MPI=1 MPI_HOME=/usr/lib/x86_64-linux-gnu/openmpi CUDA_HOME=/usr/local/cuda -j
# 单机 8 卡
./build/all_reduce_perf -b 8 -e 8G -f 2 -g 8
输出关键 几列(以 H100 NVLink 为例):
size count type algbw busbw #wrong time(us)
1G 134217728 float xxx.x xxx.x 0 xxxx
只看 busbw(总线带宽):
| 规模 | 健康值(NVLink 4,8 卡) |
|---|---|
| 大消息(> 100 MB) | 280–360 GB/s |
| 中等(1–10 MB) | 200–300 GB/s |
| 小消息(< 64 KB) | 由延迟主导,看 time(us) |
判读要点:
busbw应该接近「每卡 NVLink 单向带宽 × 2 × (N-1)/N」。8 卡 H100 时约 450×2×7/8 ≈ 787 GB/s 是理论峰值,实测 300–400 属正常(NCCL 效率 + 环拓扑)。- 如果 busbw 只有几十 GB/s → 说明走了 PCIe 而不是 NVLink。检查
nvidia-smi topo -m与容器是否直通(--gpus all且未限制 NVLink)。 - 如果 8 卡内正常、跨节点骤降 10 倍 → 网络或 NCCL 配置问题(见 常见问题)。
五、算扩展效率
# 用同一模型,跑 1/2/4/8 卡,记录每秒样本数
eff_8 = (samples_per_sec_8 / samples_per_sec_1) / 8
print("8 卡扩展效率:", round(eff_8 * 100, 1), "%")
| 结果 | 判读 |
|---|---|
| > 85% | 良好,互联与并行都健康 |
| 60%–85% | 有优化空间(通信 overlap、batch size) |
| < 60% | 严重问题:数据加载、通信、或拓扑 |
这个数字比「训练速度」有用得多,因为它去掉了「单卡本身慢」这个因素,直接反映「加卡是否有效」。
六、测数据加载是否拖后腿
import time, torch
from torch.utils.data import DataLoader
def dataloader_ratio(loader, n_batches=50):
t_data = t_total = 0.0
t0 = time.perf_counter()
for i, batch in enumerate(loader):
if i >= n_batches: break
t1 = time.perf_counter()
t_data += t1 - t0 # 等数据的时间
# 模拟 80 ms 计算
torch.cuda.synchronize(); time.sleep(0)
t2 = time.perf_counter()
t_total += t2 - t0
t0 = t2
return t_data / t_total
如果数据等待占比 > 10%,先优化 num_workers、pin_memory、prefetch_factor,或改用 webdataset/ffcv 格式。这一项常常能带来 20%–40% 提速,而成本为零。
七、验收清单
-
nvidia-smi topo -m显示 8 卡之间全是NV# - 单卡 BF16 GEMM 达到理论值的 70% 以上
- HBM 带宽达到理论值的 85% 以上
- 8 卡 AllReduce 大消息 busbw > 250 GB/s(NVLink 4)
- 8 卡扩展效率 > 80%(小模型)/ > 70%(7B)
- 数据加载等待占比 < 10%
- 训练时
nvidia-smi的 SM 利用率 > 80%,显存 < 90% - 功耗未触顶(
nvidia-smi -q -d POWER看是否长期在 limit 附近)