|
5 | 5 | # LICENSE file in the root directory of this source tree. |
6 | 6 |
|
7 | 7 | import contextlib |
8 | | -from typing import Callable, Optional |
| 8 | +from typing import Callable, List, Optional |
9 | 9 |
|
10 | 10 | import torch |
11 | 11 | from torch._dynamo.functional_export import dynamo_graph_capture_for_export |
|
16 | 16 | ) |
17 | 17 | from torch._guards import tracing, TracingContext |
18 | 18 | from torch.distributed.tensor import DTensor |
| 19 | +from torchtitan.config import JobConfig |
19 | 20 | from torchtitan.distributed import ParallelDims |
20 | 21 | from torchtitan.tools.logging import logger |
21 | 22 |
|
@@ -180,3 +181,88 @@ def forward(self, *args, **kwargs): |
180 | 181 | # calling the line below returns control to torchtitan's runner |
181 | 182 | # letting it call the backward, and optimizer. |
182 | 183 | return self.joint_graph_module(args, kwargs) |
| 184 | + |
| 185 | + |
| 186 | +# Default compiler pass configuration - no passes by default |
| 187 | +DEFAULT_COMPILER_PASSES = [] |
| 188 | + |
| 189 | + |
| 190 | +def compiler( |
| 191 | + name: str, |
| 192 | + gm: torch.fx.GraphModule, |
| 193 | + example_inputs, |
| 194 | + passes: List[Callable] = None, |
| 195 | +): |
| 196 | + """ |
| 197 | + Compile a graph module by applying a sequence of compiler passes. |
| 198 | +
|
| 199 | + Args: |
| 200 | + name: Name for logging purposes |
| 201 | + gm: The graph module to compile |
| 202 | + example_inputs: Example inputs for the graph module |
| 203 | + passes: List of compiler pass functions to apply. Each function should take |
| 204 | + (gm, example_inputs) and return a transformed gm. If None, uses |
| 205 | + DEFAULT_COMPILER_PASSES. |
| 206 | + """ |
| 207 | + if passes is None: |
| 208 | + passes = DEFAULT_COMPILER_PASSES |
| 209 | + |
| 210 | + logger.info(f"{name} before compiler:") |
| 211 | + logger.info(gm.print_readable(print_output=False)) |
| 212 | + |
| 213 | + for pass_fn in passes: |
| 214 | + logger.info(f"Applying pass: {pass_fn.__name__}") |
| 215 | + gm = pass_fn(gm, example_inputs) |
| 216 | + |
| 217 | + logger.info(f"{name} after compiler:") |
| 218 | + logger.info(gm.print_readable(print_output=False)) |
| 219 | + return gm |
| 220 | + |
| 221 | + |
| 222 | +def make_compiler_with_passes(passes: List[Callable] = None): |
| 223 | + """ |
| 224 | + Create forward and backward compilers with specified passes. |
| 225 | +
|
| 226 | + Args: |
| 227 | + passes: List of compiler pass functions to apply. If None, uses DEFAULT_COMPILER_PASSES. |
| 228 | +
|
| 229 | + Returns: |
| 230 | + Tuple of (fw_compiler, bw_compiler) functions |
| 231 | + """ |
| 232 | + |
| 233 | + def fw_compiler(gm: torch.fx.GraphModule, example_inputs) -> None: |
| 234 | + return compiler("fwd_gm", gm, example_inputs, passes=passes) |
| 235 | + |
| 236 | + def bw_compiler(gm: torch.fx.GraphModule, example_inputs) -> None: |
| 237 | + return compiler("bwd_gm", gm, example_inputs, passes=passes) |
| 238 | + |
| 239 | + return fw_compiler, bw_compiler |
| 240 | + |
| 241 | + |
| 242 | +def get_compiler_passes_from_config(job_config: JobConfig): |
| 243 | + """ |
| 244 | + Extract and validate compiler passes from job config. |
| 245 | +
|
| 246 | + Args: |
| 247 | + job_config: Job configuration containing compile.passes |
| 248 | +
|
| 249 | + Returns: |
| 250 | + List of compiler pass functions |
| 251 | + """ |
| 252 | + from torchtitan.experiments.compiler_toolkit.passes import AVAILABLE_PASSES |
| 253 | + |
| 254 | + pass_names = getattr(job_config.compile, "passes", []) |
| 255 | + compiler_passes = [] |
| 256 | + |
| 257 | + for pass_name in pass_names: |
| 258 | + if pass_name not in AVAILABLE_PASSES: |
| 259 | + raise ValueError( |
| 260 | + f"Unknown compiler pass: {pass_name}. " |
| 261 | + f"Available passes: {list(AVAILABLE_PASSES.keys())}" |
| 262 | + ) |
| 263 | + compiler_passes.append(AVAILABLE_PASSES[pass_name]) |
| 264 | + |
| 265 | + if pass_names: |
| 266 | + logger.info(f"Using compiler passes from config: {pass_names}") |
| 267 | + |
| 268 | + return compiler_passes |
0 commit comments