xref: /aosp_15_r20/external/pytorch/benchmarks/dynamo/microbenchmarks/dynamo_microbenchmarks.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1import cProfile
2import pstats
3import timeit
4
5import torch
6
7
8@torch.compile(backend="eager", fullgraph=True)
9def symbolic_convert_overhead_stress_test(x, y, n):
10    while n > 0:
11        n -= 1
12        x, y = y, x
13    return x + y
14
15
16def main():
17    def fn():
18        torch._dynamo.reset()
19        symbolic_convert_overhead_stress_test(x, y, 100000)
20
21    x = torch.randn(16)
22    y = torch.randn(16)
23    t = min(timeit.repeat(fn, number=1, repeat=3))
24    print(f"symbolic_convert_overhead_stress_test: {t:.1f}s")
25
26
27def profile():
28    x = torch.randn(16)
29    y = torch.randn(16)
30    torch._dynamo.reset()
31    pr = cProfile.Profile()
32    pr.enable()
33    # 100k > 33k roughly cancels out the overhead of cProfile
34    symbolic_convert_overhead_stress_test(x, y, 33000)
35    pr.disable()
36    ps = pstats.Stats(pr)
37    ps.dump_stats("dynamo_microbenchmarks.prof")
38    print("snakeviz dynamo_microbenchmarks.prof")
39
40
41if __name__ == "__main__":
42    main()
43    profile()
44