2025.10.1 CPU時間とメモリ使用量調べ【torch】
https://uyota.asablo.jp/blog/2022/04/08/9479734
code:p.py
import torch
def code_to_profile_0():
a = torch.randn(100, 100, dtype=torch.float, requires_grad=True)
b = a**2
c = a+b
d = torch.sum(c)
d.backward()
with torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
],
# profile_memory=True, # メモリ使用量を記録
# record_shapes=True, # テンソルshapeも記録
# with_stack=True # (必要なら) 呼び出しスタックも
) as p:
code_to_profile_0()
print(p.key_averages().table(sort_by="cpu_time_total", row_limit=-1))
#