torch.autograd.profiler的record_shapes和profile_memory?torch.autograd.profiler的record_shapes和profile_memory?
record_shapes:在 torch.autograd.profiler 或 torch.profiler 中开启后,会记录每个 operator 的输入/输出 tensor 的 shape。便于在分析结果中看到「某 op 是在什么 shape 下被调用的」(如 batch、seq_len、hidden),用于判断是否因动态 shape 导致多次编译、或某 shape 特别耗时,以及做容量与优化决策。
profile_memory:记录 CUDA 显存分配与释放 事件及归属到哪个 op。可看到每个 op 的 显存增量(正为分配、负为释放)、时间线上的分配峰值,用于定位 OOM 对应的 op 与 tensor、以及显存泄漏(某 op 只增不释)。需在代表性 step 上采集足够长;注意 PyTorch 的 caching allocator 会复用块,看到的「分配」可能包含池化。
| 返回模块 | 返回总览 |