Skip to content

Skip rebuilding FX forward after post-lowering and before adjacency split. - #4599

Open
micwill755 wants to merge 2 commits into
mainfrom
skip-conversion-recompile
Open

micwill755 wants to merge 2 commits into
mainfrom
skip-conversion-recompile

Conversation

@micwill755

@micwill755 micwill755 commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Skip gm.recompile() in the post-lowering flush (default recompile=False) and in fast_partition before split(). Conversion (TRTInterpreter) and the adjacency splitter walk graph.nodes and do not call gm.forward().
  • Keep DCE + lint. Callers that execute the module as Python pass post_lowering(..., recompile=True) (converter harness, LLM static-cache). split() still rebuilds forward() when it constructs _run_on_acc_* / _run_on_gpu_* GraphModules.
  • Klein (3364-node lowered graph): prelude reprint was 36 ms; after skip, fast_partition wall is 118 ms (was ~155–171 ms). split() still reprints the full ACC child twice (~37 ms each) plus an expensive 9-node wrapper reprint (~36 ms).

PR 8 of the compile-startup stack; based on #4598.

Test plan

  • tests/py/dynamo/lowering/test_skip_conversion_recompile.py (post_lowering does not recompile; DCE still runs; Interpreter still correct; recompile=True rebuilds forward; fast_partition does not recompile the input module)
  • tests/py/dynamo/models/test_lowering_cache.py (roundtrip still executes after explicit recompile of the in-memory module)
  • tests/py/dynamo/partitioning/test_000_fast_partitioning.py
  • Klein lowered graph: prelude recompile 36 ms; fast_partition wall 118 ms after skip

Stack created with GitHub Stacks CLI • Give Feedback 💬

@meta-cla meta-cla Bot added the cla signed label Aug 26, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Aug 26, 2026
@github-actions
github-actions Bot requested a review from zewenli98 August 26, 2026 22:41
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from d976ba1 to 17b63ed Compare September 1, 2026 18:30
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from 17b63ed to c722fbb Compare September 2, 2026 07:06
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from c722fbb to 6339d3b Compare September 9, 2026 22:55
@micwill755
micwill755 requested review from cehongwang and narendasan and removed request for zewenli98 September 10, 2026 17:22
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from 6339d3b to caed6cf Compare September 16, 2026 23:22
@micwill755
micwill755 force-pushed the lazy-graph-printing branch 2 times, most recently from c174fe9 to 6034d33 Compare September 16, 2026 23:23
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from caed6cf to 141e926 Compare September 16, 2026 23:23
@micwill755
micwill755 marked this pull request as ready for review September 29, 2026 17:20
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from 141e926 to 7762e49 Compare September 29, 2026 17:44
@micwill755 micwill755 added this to the v2.15.0 milestone Sep 29, 2026
gm: torch.fx.GraphModule,
settings: CompilationSettings = CompilationSettings(),
*,
recompile: bool = False,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out torch.fx._lazy_graph_module._LazyGraphModule.

AI:
Defaulting recompile=False assumes the lowered module is never run as Python. But compile_module returns it unconverted when a graph has fewer supported ops than min_block_size, and the torch.compile backend then runs its stale forward.

backends.py:367 calls post_lowering(gm, settings) with the default, then compile_module. _compiler.py:1381 returns gm when num_supported_ops < min_block_size, which happens often with small Dynamo graph-break fragments. That gm.forward was generated before lowering. It still runs pre-lowering code, and it can refer to attributes that constant_fold deleted.

This PR already had to pass recompile=True in harness.py and test_static_cache.py for this reason. The same applies to compile(..., dryrun=True) at :1415. There's no test for the early-return path. I found this by inspection and haven't run it.

The fix is to convert to torch.fx._lazy_graph_module._LazyGraphModule instead of adding the flag. Its recompile() only marks the code dirty, and code generation runs on first access to forward. I measured 56.75 ms for GraphModule.recompile() against 0.01 ms for the lazy version on a 2000-node graph. That's the same saving, with no stale-module risk.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by converting the lowered module to _LazyGraphModule. Recompilation now marks forward as dirty and regenerates it only when Python execution requires it. I also added regression coverage for the compile_module() early return path.

@micwill755
micwill755 force-pushed the lazy-graph-printing branch from c92404f to 907decb Compare October 6, 2026 20:32
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from 7762e49 to 949a239 Compare October 6, 2026 20:38
@micwill755
micwill755 force-pushed the lazy-graph-printing branch from 907decb to 292ccf4 Compare October 6, 2026 21:34
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from 949a239 to 78b03f6 Compare October 6, 2026 21:34

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/impl/moe.py	2026-10-06 21:35:06.314583+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/impl/moe.py	2026-10-06 21:35:38.389795+00:00
@@ -94,11 +94,13 @@
        ctx, offs, torch.int32, f"{name}_offs_i32", target, source_ir
    )
    offs_row = impl.shuffle.reshape(
        ctx, target, source_ir, f"{name}_offs_row", offs_i32, (1, -1)
    )
-    passed = impl.elementwise.le(ctx, target, source_ir, f"{name}_passed", offs_row, rows)
+    passed = impl.elementwise.le(
+        ctx, target, source_ir, f"{name}_passed", offs_row, rows
+    )
    group = impl.reduce.sum(
        ctx,
        target,
        source_ir,
        f"{name}_group",
--- /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/aten_ops_converters.py	2026-10-06 21:35:06.312815+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/aten_ops_converters.py	2026-10-06 21:35:42.883629+00:00
@@ -4640,13 +4640,11 @@
    if key in kwargs and kwargs[key] is not None:
        return kwargs[key]
    return args_bounds_check(args, index, default)


-def histc_validator(
-    node: Node, settings: Optional[CompilationSettings] = None
-) -> bool:
+def histc_validator(node: Node, settings: Optional[CompilationSettings] = None) -> bool:
    bins = _arg_or_kwarg(node.args, node.kwargs, 1, "bins", 100)
    lo = _arg_or_kwarg(node.args, node.kwargs, 2, "min", 0)
    hi = _arg_or_kwarg(node.args, node.kwargs, 3, "max", 0)
    if not all(
        isinstance(v, (int, float)) and not isinstance(v, bool) for v in (bins, lo, hi)

@lanluo-nvidia lanluo-nvidia modified the milestones: v2.15.0, v2.16.0 Oct 8, 2026
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch 3 times, most recently from c6f9a51 to a6afa53 Compare October 13, 2026 22:09
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch 2 times, most recently from c693a4b to 7a6ca3d Compare October 13, 2026 23:41
@micwill755
micwill755 force-pushed the lazy-graph-printing branch 2 times, most recently from 216ddd1 to 3688b2c Compare October 13, 2026 23:51
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from 7a6ca3d to 0693ab4 Compare October 13, 2026 23:51
@micwill755
micwill755 removed this pull request from stack #4533 October 13, 2026 23:52
@github-actions github-actions Bot added the component: conversion Issues re: Conversion stage label Oct 14, 2026
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from 0fdefb3 to 2ff330e Compare October 20, 2026 17:11
@5had3z
5had3z self-requested a review November 16, 2026 21:55
@micwill755
micwill755 dismissed cehongwang’s stale review November 19, 2026 23:37

Addressed: lowered module is now converted to _LazyGraphModule (no stale forward), and the compile_module() early-return path has regression coverage.

micwill755 and others added 2 commits November 19, 2026 15:52
…plit.

TRTInterpreter and AccNodesFinder walk graph.nodes; split still rebuilds forward when it constructs the runtime GraphModules.
@micwill755
micwill755 changed the base branch from lazy-graph-printing to main November 20, 2026 00:09
@micwill755
micwill755 force-pushed the skip-conversion-recompile branch from 2ff330e to 542aabb Compare November 20, 2026 00:10

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants