手写最基础的大模型推理并使用Profile监控GPU性能消耗情况

用 torch.profiler 来监控大模型推理,这样可以得到 GPU/CPU 使用情况、时间消耗、内存占用 ,比简单的 psutil 更精确。下面完整示例:


python 复制代码
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import time

# 1️⃣ 模型和 tokenizer
model_name = "distilgpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

# 输入 prompt
prompt = "Hello, I am a small language model. I can"
inputs = tokenizer(prompt, return_tensors="pt").to(device)

# 2️⃣ 使用 torch.profiler 监控推理
with torch.profiler.profile(
    schedule=torch.profiler.schedule(wait=1, warmup=1, active=2, repeat=1),
    on_trace_ready=torch.profiler.tensorboard_trace_handler("./log"),
    record_shapes=True,
    profile_memory=True,
    with_stack=True
) as prof:
    
    # 模拟多步推理,方便 profiler 捕捉
    for step in range(4):
        outputs = model.generate(
            **inputs,
            max_length=50,
            do_sample=True,
            temperature=0.7,
            top_k=50,
            top_p=0.95,
            no_repeat_ngram_size=2
        )
        prof.step()  # 标记 step,方便分析

# 3️⃣ 打印统计信息
print(prof.key_averages().table(
    sort_by="self_cpu_time_total", row_limit=20
))

# 4️⃣ TensorBoard 查看
print("Profiler traces saved to ./log, run:")
print("tensorboard --logdir=./log")

✅ 功能说明

  1. Profiler 配置
  • record_shapes=True → 记录每个操作的 tensor 形状
  • profile_memory=True → 记录显存/内存占用
  • with_stack=True → 打印调用堆栈,定位耗时操作
  1. Schedule
python 复制代码
schedule=torch.profiler.schedule(wait=1, warmup=1, active=2, repeat=1)
  • wait:等待 step 数不记录
  • warmup:热身 step,不计入统计
  • active:记录 step 数
  • repeat:重复几次 schedule
  • 用于多步生成或批量生成时统计更准确
  1. Profiler 输出
  • prof.key_averages() → CPU/GPU 每个操作耗时、显存消耗
  • 可以排序(self_cpu_time_total / cuda_time_total)找到瓶颈
  1. TensorBoard 可视化
bash 复制代码
tensorboard --logdir=./log
  • 可以看到每步操作的 GPU/CPU 时间、内存曲线
  • 直观分析模型推理性能

💡 优化建议

  • 如果模型更大(7B+),可以结合 torch.autocast("cuda") 做混合精度,节省显存并提升速度:
python 复制代码
with torch.autocast(device_type="cuda", dtype=torch.float16):
    outputs = model.generate(...)
  • 可以在 profiler 的 on_trace_ready 回调中写自定义分析,比如打印每层显存占用。

访问

https://ui.perfetto.dev/#!/viewer?local_cache_key=0-json


相关推荐
回眸&啤酒鸭4 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智4 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅4 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein4 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu4 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台4 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb4 天前
智能网联汽车安全能力框架
人工智能
龙亘川4 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI4 天前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai