|
32 | 32 |
|
33 | 33 | import torch |
34 | 34 |
|
35 | | -# exit cleanly if we are on a device that doesn't support torch.compile |
36 | | -if torch.cuda.get_device_capability() < (7, 0): |
37 | | - print("Skipping because torch.compile is not supported on this device.") |
38 | | -else: |
39 | | - @torch.compile() |
40 | | - def fn(x, y): |
41 | | - z = x + y |
42 | | - return z + 2 |
| 35 | +# Determine device: CUDA if supported, otherwise CPU |
| 36 | +device = "cuda" if torch.cuda.is_available() and torch.cuda.get_device_capability() >= (7, 0) else "cpu" |
43 | 37 |
|
| 38 | +# Verify if torch.compile is supported in the current environment |
| 39 | +try: |
| 40 | + torch.compile(lambda x: x + 1)(torch.ones(1, device=device)) |
| 41 | + compile_supported = True |
| 42 | +except Exception: |
| 43 | + compile_supported = False |
44 | 44 |
|
45 | | - inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda")) |
| 45 | +if not compile_supported: |
| 46 | + print("Skipping because torch.compile is not supported on this device/environment.") |
| 47 | + import sys |
| 48 | + sys.exit(0) |
| 49 | + |
| 50 | + |
| 51 | +@torch.compile() |
| 52 | +def fn(x, y): |
| 53 | + z = x + y |
| 54 | + return z + 2 |
| 55 | + |
| 56 | + |
| 57 | +inputs = (torch.ones(2, 2, device=device), torch.zeros(2, 2, device=device)) |
46 | 58 |
|
47 | 59 |
|
48 | 60 | # print separator and reset dynamo |
49 | 61 | # between each example |
50 | | - def separator(name): |
51 | | - print(f"==================={name}=========================") |
52 | | - torch._dynamo.reset() |
| 62 | +def separator(name): |
| 63 | + print(f"==================={name}=========================") |
| 64 | + torch._dynamo.reset() |
53 | 65 |
|
54 | 66 |
|
55 | | - separator("Dynamo Tracing") |
| 67 | +separator("Dynamo Tracing") |
56 | 68 | # View dynamo tracing |
57 | 69 | # TORCH_LOGS="+dynamo" |
58 | | - torch._logging.set_logs(dynamo=logging.DEBUG) |
59 | | - fn(*inputs) |
| 70 | +torch._logging.set_logs(dynamo=logging.DEBUG) |
| 71 | +fn(*inputs) |
60 | 72 |
|
61 | | - separator("Traced Graph") |
| 73 | +separator("Traced Graph") |
62 | 74 | # View traced graph |
63 | 75 | # TORCH_LOGS="graph" |
64 | | - torch._logging.set_logs(graph=True) |
65 | | - fn(*inputs) |
| 76 | +torch._logging.set_logs(graph=True) |
| 77 | +fn(*inputs) |
66 | 78 |
|
67 | | - separator("Fusion Decisions") |
| 79 | +separator("Fusion Decisions") |
68 | 80 | # View fusion decisions |
69 | 81 | # TORCH_LOGS="fusion" |
70 | | - torch._logging.set_logs(fusion=True) |
71 | | - fn(*inputs) |
| 82 | +torch._logging.set_logs(fusion=True) |
| 83 | +fn(*inputs) |
72 | 84 |
|
73 | | - separator("Output Code") |
| 85 | +separator("Output Code") |
74 | 86 | # View output code generated by inductor |
75 | 87 | # TORCH_LOGS="output_code" |
76 | | - torch._logging.set_logs(output_code=True) |
77 | | - fn(*inputs) |
| 88 | +torch._logging.set_logs(output_code=True) |
| 89 | +fn(*inputs) |
78 | 90 |
|
79 | | - separator("") |
| 91 | +separator("") |
80 | 92 |
|
81 | 93 | ###################################################################### |
82 | 94 | # Conclusion |
|
0 commit comments