Rate this Page

torch.compiler.nested_compile_region#

torch.compiler.nested_compile_region(fn=None)[source]#

Tells``torch.compile`` that the marked set of operations forms a nestedcompile region (which is often repeated in the full model) whose code can becompiled once and safely reused.nested_compile_region can also be usedas a decorator.

During``torch.compile`` tracing, the compiler applieshierarchicalcompilation withnested_compile_region: it emits optimized code for themarked region the first time it is encountered and re-emits (or “stampsout”) the previously compiled code on every subsequent invocation. This cansubstantially reduce overall compile time for deeply-stacked,structurally-identical components such as the transformer layers of alarge-language-model (LLM).

Outside atorch.compile context—i.e., in standard eager execution—thecall is a no-op, so existing workflows remain unaffected.

Note thatnested_compile_regiondoes not promise that a region willbe compiled exactly once. If the compiler detects that new input conditions(shape, dtype, device, stride, globals etc.) make the cached version invalidto reuse, it will transparently re-compile the region. Using it isthereforesafe: correctness is always preserved, and you pay the extracompilation cost only when required.