Diff Coverage

Diff: origin/master...HEAD, staged and unstaged changes

Source File Diff Coverage (%) Missing Lines
hyper_parallel/compile/compiler.py 100%  
hyper_parallel/compile/pass_config.py 100%  
hyper_parallel/compile/passes/inductor_pass.py 70.7% 118-119,130,139,159,213,215,217-227,229-234,239-243,245-247,260-262,264,268-269,289,339,387-388,396,401,460,468-471,475-476
hyper_parallel/compile/passes/parallel/fsdp_pass.py 100%  
hyper_parallel/compile/passes/parallel/pp_schedule.py 57.1% 213,1131-1132
hyper_parallel/compile/passes/pipeline.py 100%  
hyper_parallel/compile/passes/inductor_pass.py
114
115
116
117
118
119
120
121
122
123
    builds.
    """
    try:
        importlib.import_module("torch.fx.passes.regional_inductor")
    except ImportError:
        return False
    return True


def _get_fake_mode_from_gm(gm: fx.GraphModule) -> Optional[Any]:
126
127
128
129
130
131
132
133
134
        if node.op == "placeholder":
            val = node.meta.get("val")
            if isinstance(val, FakeTensor):
                return val.fake_mode
    return None


def _placeholder_fake_args(gm: fx.GraphModule) -> list:
    """Return the placeholder values in order, requiring fake ``meta["val"]``."""
135
136
137
138
139
140
141
142
143
    args = []
    for node in gm.graph.find_nodes(op="placeholder"):
        val = node.meta.get("val")
        if val is None:
            raise ValueError(
                f"InductorPass requires a fake meta['val'] on placeholder "
                f"'{node.name}' to size compiled regions; the graph must be "
                f"make_fx-traced (PP stage subgraphs copy their placeholders' "
                f"meta from the joint graph)"
155
156
157
158
159
160
161
162
163
    Side-effectful free nodes return ``None`` and legitimately carry no val.
    """
    fake_mode = _get_fake_mode_from_gm(gm)
    if fake_mode is None:
        raise ValueError(
            "InductorPass requires a make_fx-traced graph whose placeholders "
            "carry fake meta['val'] tensors"
        )
    fake_args = _placeholder_fake_args(gm)
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
        if isinstance(node, fx.Node)
    ):
        return None, graph_module

    flat_nodes: list = []

    def build(node: fx.Node) -> tuple:
        """Emit ``getitem`` leaves for a nested value, else record the leaf."""
        val = node.meta.get("val")
        if isinstance(val, (tuple, list)) and val:
            children = []
            for index, element in enumerate(val):
                sub = graph.call_function(operator.getitem, (node, index))
                sub.meta["val"] = element
                children.append(build(sub))
            kind = "tuple" if isinstance(val, tuple) else "list"
            return kind, tuple(children)
        flat_nodes.append(node)
        return "leaf", len(flat_nodes) - 1

    with graph.inserting_before(output):
        spec = tuple(build(node) for node in returned)
    output.args = (tuple(flat_nodes),)
    graph.lint()
    graph_module.recompile()
    return spec, graph_module


def _rebuild_nested_output(spec: tuple, flat: Sequence[Any]) -> list:
    """Rebuild the original output structure from ``spec`` and flat leaves."""
    leaves = iter(flat)
    rebuilt = []
    for kind, payload in spec:
        if kind == "leaf":
            rebuilt.append(next(leaves))
        else:
            children = _rebuild_nested_output(payload, leaves)
            rebuilt.append(tuple(children) if kind == "tuple" else children)
    return rebuilt


class _NestedOutputWrapper(nn.Module):
    """Thin wrapper restoring a compiled graph's nested output structure."""
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
        spec: tuple,
        meta: Optional[Dict[str, Any]] = None,
    ) -> None:
        """Store the compiled inner module and its output marker tree."""
        super().__init__()
        self.inner = inner
        self._output_spec = spec
        # Mirrors GraphModule.meta so callers can introspect compilation mode.
        self.meta = dict(meta or {})

    def forward(self, *args: Any, **kwargs: Any) -> tuple:
        """Run the flat compiled graph and rebuild the original structure."""
        flat = self.inner(*args, **kwargs)
        return tuple(_rebuild_nested_output(self._output_spec, flat))


def _tag_node(node: fx.Node, inductor_configs: Optional[Dict[str, Any]]) -> None:
    """Annotate ``node`` for regional_inductor with the user's Inductor config."""
285
286
287
288
289
290
291
292
293

    @property
    def name(self) -> str:
        """Pass name."""
        return "inductor_compilation"

    def run(
        self,
        graph_module: fx.GraphModule,
335
336
337
338
339
340
341
342
343
                    "JointGraph.example_inputs) to size compiled regions"
                )
            if _regional_inductor_available():
                self._compile_graph(graph_module, pass_config, example_inputs)
                compiled = True
            else:
                self._warn_unavailable(mode)

        graph_module.meta[_INDUCTOR_COMPILED_META] = (
383
384
385
386
387
388
389
390
391
392
                )
            schedules.append(submodule)

        if not _regional_inductor_available():
            self._warn_unavailable(pass_config.inductor_mode)
            return False

        for schedule in schedules:
            schedule.map_stage_graphs(
                lambda stage_gm: self._compile_graph(
392
393
394
395
396
397
398
399
400
401
402
403
404
405
                lambda stage_gm: self._compile_graph(
                    stage_gm, pass_config, example_inputs=None
                )
            )
        _LOG.info(
            "InductorPass: compiled %d PP schedule(s) in %r mode",
            len(schedules),
            pass_config.inductor_mode,
        )
        return True

    def _compile_graph(
        self,
        graph_module: fx.GraphModule,
456
457
458
459
460
461
462
463
464
        ):
            # ``regional_inductor`` consumes these on torch releases whose
            # signature expects example inputs; newer releases size regions
            # from the nodes' fake ``meta["val"]`` instead.
            regional_inductor(graph_module, example_inputs)

        # regional_inductor compiles regions with the boxed calling
        # convention (one mutable args list); reset to the default positional
        # convention so run_traced_graph's graph_module(*flat_inputs) (and the
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
        # convention so run_traced_graph's graph_module(*flat_inputs) (and the
        # PP schedule's fwd_gm(*args) / bwd_gm(*args)) keeps working. Setting
        # the default unconditionally is a no-op when the region compiler
        # already used positional codegen and avoids peeking at private state.
        graph_module.graph.set_codegen(CodeGen())
        graph_module.recompile()
        graph_module.meta[_INDUCTOR_COMPILED_META] = mode
        if output_spec is not None:
            # Rebuild the stage's original (possibly nested) output structure
            # for the schedule / backward subgraph; only the compiled graph is
            # flat.
            return _NestedOutputWrapper(graph_module, output_spec, graph_module.meta)
        return graph_module

    @staticmethod
    def _tag_full(
        graph_module: fx.GraphModule, inductor_configs: Optional[Dict[str, Any]]
hyper_parallel/compile/passes/parallel/pp_schedule.py
209
210
211
212
213
214
215
216
217
        them through this hook and writes the returned module back (in-place
        compilers may return the same object).
        """
        self.fwd_gm = fn(self.fwd_gm)
        self.bwd_gm = fn(self.bwd_gm)

    # ------------------------------------------------------------------
    # Input / microbatch resolution
    # ------------------------------------------------------------------
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
        self._pending_sends: Dict[Any, List[Tuple[Any, torch.Tensor]]] = {}

    def map_stage_graphs(self, fn: Callable[[nn.Module], nn.Module]) -> None:
        """Apply ``fn`` to every virtual stage's fwd and bwd subgraph."""
        self.fwd_gms = nn.ModuleList([fn(gm) for gm in self.fwd_gms])
        self.bwd_gms = nn.ModuleList([fn(gm) for gm in self.bwd_gms])

    def _resolve_num_microbatches(self, user_inputs: Sequence[Any]) -> int:
        """Derive the microbatch count from the first tensor model input."""
        batch_size = next(