Skip to content

Skip executing large aliased constant folds that 4531 will not install. - #4597

Open
micwill755 wants to merge 3 commits into
one-shot-decomp-in-tracefrom
skip-aliased-fold-execute
Open

micwill755 wants to merge 3 commits into
one-shot-decomp-in-tracefrom
skip-aliased-fold-execute

Conversation

@micwill755

@micwill755 micwill755 commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Honor Inductor's skip_folding_node_fn during constant_fold() so large aliased weight permutes/views are not executed, then thrown away.
  • 4531 already skipped installing those folds (cpu().contiguous()). This skips computing them: _TorchTensorRTConstantFolder.run_node returns unknown without calling the op when the node is a true view of a large module tensor.
  • aten.contiguous and other materializing ops still run. The 4531 install-time skip stays as a safety net.

PR 6 of the compile-startup stack; based on #4596.

Test plan

  • tests/py/dynamo/lowering/test_skip_aliased_fold_execute.py (large permute skipped and not executed, small permute still folds, large contiguous still executes, materialized weight + weight still folds)
  • Klein/Flux compile not re-run for this PR: leftover ConstantFolder.run is ~50 ms, of which the aliased permutes are ~2 ms (4531 already took the ~0.25 s cpu().contiguous() copies)

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:04
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from a41ed2f to 20b8bcd Compare September 1, 2026 18:30
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from d844300 to b61b68c Compare September 1, 2026 18:30
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from 20b8bcd to d48152c Compare September 2, 2026 07:06
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from b61b68c to cacfbc5 Compare September 2, 2026 07:06
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from d48152c to 6f91a46 Compare September 9, 2026 22:54
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from cacfbc5 to 59fd02a Compare September 9, 2026 22:54
@micwill755
micwill755 requested review from cehongwang and narendasan and removed request for zewenli98 September 10, 2026 17:21
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from ee681ee to fe0fb9c Compare September 16, 2026 23:22
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from 59fd02a to e35fac9 Compare September 16, 2026 23:22
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from fe0fb9c to c9aee71 Compare September 16, 2026 23:23
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from e35fac9 to f0695ce 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 one-shot-decomp-in-trace branch from c9aee71 to cd041e7 Compare September 29, 2026 17:44
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from f0695ce to 8ccd56d Compare September 29, 2026 17:44
@micwill755 micwill755 added this to the v2.15.0 milestone Sep 29, 2026
"""
if node.op != "call_function" or node.target not in _ALIASING_VIEW_OPS:
return False
return _source_attr_nbytes(gm, node) > _MAX_CONSTANT_FOLD_BYTES

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.

The skip decision uses the size of the root get_attr, not the view's output size. So taking a small select.int of a large weight skips the fold, and conversion then fails.

#4531's install-skip uses the folded output size plus a real storage-aliasing check. This check uses _source_attr_nbytes, the size of the weight at the root. For a 4 KiB select.int of a 4 MiB weight, the op isn't folded, so it reaches impl/select.py with a raw constant: RuntimeError: slice_tensor received input tensor(...) that is not part of the TensorRT region!. slice.Tensor has the same size mismatch but compiles, because its converter handles constant inputs.

I reproduced this on this PR, and it still fails at the top of the stack. Only permute is tested.

The fix is to decide from the node's output size (node.meta["val"]), matching #4531. Or drop select.int (and anything else whose converter rejects a constant input) from _ALIASING_VIEW_OP_NAMES.

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.

Fixed in eebd4de. The skip now uses the view output size from node.meta["val"], and the allowlist is limited to aten.permute.default, whose converter accepts constant inputs. Small select.int and slice.Tensor outputs from large weights are therefore still folded. I added regression tests covering both the skip decision and the small-select case.

@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from cd041e7 to a56ecf0 Compare October 6, 2026 19:19
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from 8ccd56d to 6daf644 Compare October 6, 2026 19:30
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from a56ecf0 to 6e5e81f Compare October 6, 2026 20:28
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from 6daf644 to 90f8285 Compare October 6, 2026 20:30
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from 6e5e81f to 9ae4895 Compare October 6, 2026 21:34
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from 90f8285 to 79c54b0 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:00.255699+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/impl/moe.py	2026-10-06 21:35:20.981758+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:00.254191+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/conversion/aten_ops_converters.py	2026-10-06 21:35:23.368919+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 one-shot-decomp-in-trace branch from 9ae4895 to dba7b55 Compare October 13, 2026 18:48
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch 3 times, most recently from 5b7b71b to 1565f07 Compare October 13, 2026 22:09
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from fb39431 to 7c47c8b Compare October 13, 2026 23:12
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch 2 times, most recently from b9d3040 to eebd4de Compare October 13, 2026 23:41
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from 7c47c8b to e99b651 Compare October 13, 2026 23:41
Inductor's skip_folding_node_fn never fires on our cf.run() path; honor it so large weight permutes/views are not computed, then discarded.
@micwill755
micwill755 force-pushed the one-shot-decomp-in-trace branch from e99b651 to 8923268 Compare October 13, 2026 23:51
@micwill755
micwill755 force-pushed the skip-aliased-fold-execute branch from eebd4de to 98ad60b Compare October 13, 2026 23:51
@micwill755
micwill755 removed this pull request from stack #4533 October 13, 2026 23:52

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: 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 component: torch_compile

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants