1
from typing import Any, Dict, List, Optional
4
import torch.utils._pytree as pytree
6
__all__ = ["compile", "list_mode_options", "list_options", "cudagraph_mark_step_begin"]
10
gm: torch.fx.GraphModule,
11
example_inputs: List[torch.Tensor],
12
options: Optional[Dict[str, Any]] = None,
15
Compile a given FX graph with TorchInductor. This allows compiling
16
FX graphs captured without using TorchDynamo.
19
gm: The FX graph to compile.
20
example_inputs: List of tensor inputs.
21
options: Optional dict of config options. See `torch._inductor.config`.
24
Callable with same behavior as gm but faster.
26
from .compile_fx import compile_fx
28
return compile_fx(gm, example_inputs, config_patches=options)
32
gm: torch.fx.GraphModule,
33
example_inputs: List[torch.Tensor],
34
options: Optional[Dict[str, Any]] = None,
37
Ahead-of-time compile a given FX graph with TorchInductor into a shared library.
40
gm: The FX graph to compile.
41
example_inputs: List of tensor inputs.
42
options: Optional dict of config options. See `torch._inductor.config`.
45
Path to the generated shared library
47
from .compile_fx import compile_fx_aot
50
serialized_in_spec = ""
51
serialized_out_spec = ""
52
if isinstance(gm.graph._codegen, torch.fx.graph._PyTreeCodeGen):
53
codegen = gm.graph._codegen
54
gm.graph._codegen = torch.fx.graph.CodeGen()
57
if codegen.pytree_info.in_spec is not None:
58
serialized_in_spec = pytree.treespec_dumps(codegen.pytree_info.in_spec)
60
if codegen.pytree_info.out_spec is not None:
61
serialized_out_spec = pytree.treespec_dumps(codegen.pytree_info.out_spec)
65
"aot_inductor.serialized_in_spec": serialized_in_spec,
66
"aot_inductor.serialized_out_spec": serialized_out_spec,
71
"aot_inductor.serialized_in_spec": serialized_in_spec,
72
"aot_inductor.serialized_out_spec": serialized_out_spec,
76
return compile_fx_aot(
79
config_patches=options,
84
mode: Optional[str] = None, dynamic: Optional[bool] = None
86
r"""Returns a dictionary describing the optimizations that each of the available
87
modes passed to `torch.compile()` performs.
90
mode (str, optional): The mode to return the optimizations for.
91
If None, returns optimizations for all modes
92
dynamic (bool, optional): Whether dynamic shape is enabled.
95
>>> torch._inductor.list_mode_options()
98
mode_options: Dict[str, Dict[str, bool]] = {
102
"triton.cudagraphs": True,
105
"max-autotune-no-cudagraphs": {
106
"max_autotune": True,
111
"max_autotune": True,
112
"triton.cudagraphs": True,
115
return mode_options[mode] if mode else mode_options
118
def list_options() -> List[str]:
119
r"""Returns a dictionary describing the optimizations and debug configurations
120
that are available to `torch.compile()`.
122
The options are documented in `torch._inductor.config`.
126
>>> torch._inductor.list_options()
129
from torch._inductor import config
131
current_config: Dict[str, Any] = config.shallow_copy_dict()
133
return list(current_config.keys())
136
def cudagraph_mark_step_begin():
137
"Indicates that a new iteration of inference or training is about to begin."
138
from .cudagraph_trees import mark_step_begin