共计 1833 个字符,预计需要花费 5 分钟才能阅读完成。
从物理做功理解 AI 算力
算力最直观的理解就是计算能力,我们可以用物理学中的 ’ 做功 ’ 来类比。FLOPs(Floating Point Operations Per Second)是衡量算力的基本单位,表示每秒浮点运算次数。计算公式为:

FLOPs = 运算次数 / 耗时 (秒)
TOPS(Tera Operations Per Second)则是更高量级的单位,1 TOPS = 10^12 次运算 / 秒。这两个指标就像物理学中的功率(P=W/t),功率越大表示单位时间做功能力越强。
硬件演进时间轴
-
CPU 时代(2012 前):通用处理器执行串行任务,AlexNet(2012)首次使用 GPU 加速训练,标志着深度学习对算力的需求爆发
-
GPU 革命(2012-2016):NVIDIA CUDA 生态成熟,ResNet(2015)等模型推动显存容量需求突破 8GB
-
专用芯片崛起(2016-2020):Google TPUv1 专为矩阵运算优化,Transformer(2017)架构催生对注意力机制专用硬件支持
-
异构计算时代(2020 至今):NPU+GPU 混合架构成为主流,ViT(2020)等模型推动算力需求突破 100TFLOPS
实战性能对比
import torch
import time
from functools import wraps
# 环境检查
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
def timer(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
torch.cuda.reset_peak_memory_stats()
result = func(*args, **kwargs)
elapsed = time.time() - start
mem = torch.cuda.max_memory_allocated() / 1024**2
print(f"{func.__name__} 耗时: {elapsed:.2f}s, 显存占用: {mem:.2f}MB")
return result
return wrapper
@timer
def train(model, device):
# 模拟训练过程
dummy_input = torch.randn(64, 3, 224, 224).to(device)
for _ in range(100):
model(dummy_input).sum().backward()
model = torch.hub.load('pytorch/vision', 'resnet18')
cpu_time = train(model, 'cpu')
gpu_time = train(model.cuda(), 'cuda')
print(f"加速比: {cpu_time/gpu_time:.1f}x")
新手避坑指南
-
内存带宽瓶颈 :TOPS 数值就像发动机马力,但内存带宽相当于输油管,V100(900GB/s)比 3080(760GB/s)实际表现更好
-
框架优化差异 :PyTorch 对 Conv2D 的优化可能优于 MXNet,实际部署前要用目标框架验证
-
混合精度陷阱 :使用 AMP 时注意检查梯度裁剪和 Loss scaling,避免数值下溢
进阶分析工具
推荐使用 torch.profiler 进行瓶颈分析:
with torch.profiler.profile(activities=[torch.profiler.ProfilerActivity.CUDA],
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3),
on_trace_ready=torch.profiler.tensorboard_trace_handler('./log')
) as p:
for _ in range(5):
model(train_batch)
p.step()
通过 TensorBoard 可以看到各算子的耗时占比,通常 Conv 层是优化重点。建议先尝试 kernel fusion 等技巧,再考虑硬件升级。
个人实践心得
刚开始接触 AI 项目时,我也曾陷入盲目追求顶级显卡的误区。后来发现,合理的 batch size 设置和内存优化往往能带来更大提升。建议先用小规模数据测试不同配置,找到性价比最高的方案。算力就像赛车引擎,但最终成绩还取决于驾驶员的调校技术。
