-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguard_drop.py
More file actions
55 lines (43 loc) · 1.63 KB
/
Copy pathguard_drop.py
File metadata and controls
55 lines (43 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import torch
import logging
torch._logging.set_logs(recompiles=True, guards=True)
def simple_model(x):
return x * 2
def reset_compile():
torch._dynamo.reset()
torch.compiler.reset()
torch._dynamo.utils.counters.clear()
def print_stats(msg):
print("="*80)
print(f"*************** {msg} ****************")
print("="*80)
stats = torch._dynamo.utils.compile_times()
print(stats)
print("-"*40)
print(torch._dynamo.utils.counters)
print("-"*40)
print("="*80)
def run_without_optimization(in1, in2):
compiled_model = torch.compile(simple_model)
compiled_model(in1)
# the second compile is for another shape and its expected to recompile the graph which can be confirmed from the logs
# [__recompiles] Recompiling function simple_model in /home/jeromean/experiment/experiments/pytorch_guards/guard_drop.py:5
# [__recompiles] triggered by the following guard failure(s):
# [__recompiles] - 0/0: tensor 'x' size mismatch at index 0. expected 4, actual 5
compiled_model(in2)
print_stats("Base Stats")
def run_with_optimization(in1, in2):
#dynanic=True aids to make use of SymInts and relax guards
compiled_model = torch.compile(simple_model, dynamic=True)
compiled_model(in1)
# the second compile should not result in a recompilation
compiled_model(in2)
print_stats("Dynamic=True")
if __name__ == "__main__":
input1 = torch.randn(4)
print("input 1 : ", input1)
input2 = torch.randn(5)
print("input 2 : ", input2)
run_without_optimization(input1, input2)
reset_compile()
run_with_optimization(input1, input2)