Text of docs.pytorch.org › tutorials › intermediate › torch_compile_tutorial.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Introduction to torch.compile — PyTorch Tutorials 2.15.0+cu132 documentation

Skip to main content v2.15.0+cu132 Go to pytorch.org X GitHub Discourse PyPi Rate this Page ★ ★ ★ ★ ★ torch.compile "> intermediate/torch_compile_tutorial Run in Google Colab Colab Download Notebook Notebook View on GitHub GitHub Note Go to the end to download the full example code. Introduction to torch.compile # Created On: Mar 15, 2023 | Last Updated: Apr 01, 2026 | Last Verified: Nov 05, 2024 Author: William Wen torch.compile is the new way to speed up your PyTorch code! torch.compile makes PyTorch code run faster by JIT-compiling PyTorch code into optimized kernels, while requiring minimal code changes. torch.compile accomplishes this by tracing through your Python code, looking for PyTorch operations.

Code that is difficult to trace will result a graph break , which are lost optimization opportunities, rather than errors or silent incorrectness. torch.compile is available in PyTorch 2.0 and later. This introduction covers basic torch.compile usage and demonstrates the advantages of torch.compile over our previous PyTorch compiler solution, TorchScript . For an end-to-end example on a real model, check out our end-to-end torch.compile tutorial .

To troubleshoot issues and to gain a deeper understanding of how to apply torch.compile to your code, check out the torch.compile programming model . Contents Required pip dependencies for this tutorial torch >= 2.0 numpy scipy System requirements - A C++ compiler, such as g++ - Python development package ( python-devel / python-dev ) Basic Usage # We turn on some logging to help us to see what torch.compile is doing under the hood in this tutorial.

The following code will print out the PyTorch ops that torch.compile traced. import torch torch . _logging . set_logs ( graph_code = True ) torch.compile is a decorator that takes an arbitrary Python function. def foo ( x , y ): a = torch . sin ( x ) b = torch . cos ( y ) return a + b opt_foo1 = torch . compile ( foo ) print ( opt_foo1 ( torch . randn ( 3 , 3 ), torch . randn ( 3 , 3 ))) @torch . compile def opt_foo2 ( x , y ): a = torch . sin ( x ) b = torch . cos ( y ) return a + b print ( opt_foo2 ( torch . randn ( 3 , 3 ), torch . randn ( 3 , 3 ))) TRACED GRAPH ===== __compiled_fn_1_2f232a33_e0d8_43b8_856b_706a8a2039c3 ===== /usr/local/lib/python3.12/dist-packages/torch/fx/_lazy_graph_module.py class GraphModule(torch.nn.Module): def forward(self, L_x_: "f32[3, 3][3, 1]cpu", L_y_: "f32[3, 3][3, 1]cpu"): l_x_ = L_x_ l_y_ = L_y_ # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:74 in foo, code: a = torch.sin(x) a: "f32[3, 3][3, 1]cpu" = torch.sin(l_x_); l_x_ = None # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:75 in foo, code: b = torch.cos(y) b: "f32[3, 3][3, 1]cpu" = torch.cos(l_y_); l_y_ = None # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:76 in foo, code: return a + b add: "f32[3, 3][3, 1]cpu" = a + b; a = b = None return (add,) /usr/local/lib/python3.12/dist-packages/triton/knobs.py:504: UserWarning: TRITON_DEFAULT_FP_FUSION is unset: defaulting to FP fusion ENABLED for backward compatibility in Triton 3.9.

The default will change to DISABLED in Triton 3.10; set TRITON_DEFAULT_FP_FUSION=1 to keep fusion enabled or =0 to adopt the new default early and silence this warning. warn_once(FP_FUSION_DEFAULT_WARNING) tensor([[ 0.8198, 0.7071, 0.5760], [ 0.1990, -0.7729, 0.2355], [ 0.4543, 0.5683, 0.0037]]) TRACED GRAPH ===== __compiled_fn_3_1b683bb6_8d16_4cf7_834e_6214b567bac6 ===== /usr/local/lib/python3.12/dist-packages/torch/fx/_lazy_graph_module.py class GraphModule(torch.nn.Module): def forward(self, L_x_: "f32[3, 3][3, 1]cpu", L_y_: "f32[3, 3][3, 1]cpu"): l_x_ = L_x_ l_y_ = L_y_ # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:85 in opt_foo2, code: a = torch.sin(x) a: "f32[3, 3][3, 1]cpu" = torch.sin(l_x_); l_x_ = None # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:86 in opt_foo2, code: b = torch.cos(y) b: "f32[3, 3][3, 1]cpu" = torch.cos(l_y_); l_y_ = None # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:87 in opt_foo2, code: return a + b add: "f32[3, 3][3, 1]cpu" = a + b; a = b = None return (add,) tensor([[ 1.0256, 0.6622, 1.2293], [ 1.1146, 1.7289, 1.1825], [ 0.7528, -0.8261, -0.1917]]) torch.compile is applied recursively, so nested function calls within the top-level compiled function will also be compiled. def inner ( x ): return torch . sin ( x ) @torch . compile def outer ( x , y ): a = inner ( x ) b = torch . cos ( y ) return a + b print ( outer ( torch . randn ( 3 , 3 ), torch . randn ( 3 , 3 ))) TRACED GRAPH ===== __compiled_fn_5_86f69db4_ecb5_4e44_af2c_2776f26bbc10 ===== /usr/local/lib/python3.12/dist-packages/torch/fx/_lazy_graph_module.py class GraphModule(torch.nn.Module): def forward(self, L_x_: "f32[3, 3][3, 1]cpu", L_y_: "f32[3, 3][3, 1]cpu"): l_x_ = L_x_ l_y_ = L_y_ # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:98 in inner, code: return torch.sin(x) a: "f32[3, 3][3, 1]cpu" = torch.sin(l_x_); l_x_ = None # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:104 in outer, code: b = torch.cos(y) b: "f32[3, 3][3, 1]cpu" = torch.cos(l_y_); l_y_ = None # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:105 in outer, code: return a + b add: "f32[3, 3][3, 1]cpu" = a + b; a = b = None return (add,) tensor([[ 1.5961, 0.9128, -0.8261], [-0.5093, -0.0217, -0.2431], [ 0.1975, -1.5779, 0.5385]]) We can also optimize torch.nn.Module instances by either calling its .compile() method or by directly torch.compile -ing the module.

This is equivalent to torch.compile -ing the module’s __call__ method (which indirectly calls forward ). t = torch . randn ( 10 , 100 ) class MyModule ( torch . nn . Module ): def __init__ ( self ): super () . __init__ () self . lin = torch . nn . Linear ( 3 , 3 ) def forward ( self , x ): return torch . nn . functional . relu ( self . lin ( x )) mod1 = MyModule () mod1 . compile () print ( mod1 ( torch . randn ( 3 , 3 ))) mod2 = MyModule () mod2 = torch . compile ( mod2 ) print ( mod2 ( torch . randn ( 3 , 3 ))) TRACED GRAPH ===== __compiled_fn_7_42482f78_44b4_48d7_87e7_ee441a06a772 ===== /usr/local/lib/python3.12/dist-packages/torch/fx/_lazy_graph_module.py class GraphModule(torch.nn.Module): def forward(self, L_self_modules_lin_parameters_weight_: "f32[3, 3][3, 1]cpu", L_self_modules_lin_parameters_bias_: "f32[3][1]cpu", L_x_: "f32[3, 3][3, 1]cpu"): l_self_modules_lin_parameters_weight_ = L_self_modules_lin_parameters_weight_ l_self_modules_lin_parameters_bias_ = L_self_modules_lin_parameters_bias_ l_x_ = L_x_ # File: /__w/tutorials/tutorials/pytorch/tutorials/intermediate_source/torch_compile_tutorial.py:126 in forward, code: return torch.nn.functional.relu(self.lin(x)) linear: "f32[3, 3][3, 1]cpu" = torch._C._nn.linear(l_x_, l_self_modules_lin_parameters_weight_, l_self_modules_lin_parameters_bias_); l_x_ = l_self_modules_lin_parameters_weight_ = l_self_modules_lin_parameters_bias_ = None relu: "f32[3, 3][3, 1]cpu" = torch.nn.functional.relu(linear); linear = None return (relu,) tensor([[0.3617, 0.2013, 0.3791], [0.9668, 0.5713, 0.0000], [0.1447, 0.0000, 0.0000]], grad_fn=<CompiledFunctionBackward>) tensor([[0.0000, 0.0000, 0.0000], [0.5763, 0.7793, 0.0000], [1.0176, 0.2081, 0.0000]], grad_fn=<CompiledFunctionBackward>) Demonstrating Speedups # Now let’s demonstrate how torch.compile speeds up a simple PyTorch example.

For a demonstration on a more complex model, see our end-to-end torch.compile tutorial . def foo3 ( x ): y = x + 1 z = torch . nn . functional . relu ( y ) u = z * 2 return u opt_foo3 = torch . compile ( foo3 ) # Returns the result of running `fn()` and the time it

The rest of this page is on the live site.