Skip to content

[Draft] feat: flag-gated upstream complex decomposition adapter #4430

Draft
apbose wants to merge 1 commit into
mainfrom
abose/complex-decomposition-adapter
Draft

[Draft] feat: flag-gated upstream complex decomposition adapter #4430
apbose wants to merge 1 commit into
mainfrom
abose/complex-decomposition-adapter

Conversation

@apbose

@apbose apbose commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Adds an alternative complex-numerics lowering path that delegates the interior complex->real expansion to PyTorch's upstream decomposition (decompose_complex_in_graph, pytorch/pytorch#169832) instead of the hand-maintained per-op rewriter in complex_graph_rewrite.py.

The new complex_decomposition_adapter pass:

  • captures the complex I/O signature (reusing the existing helpers) so the post-partition _insert_complex_io_adapters boundary code works unchanged;
  • calls decompose_complex_in_graph on the exported graph, feeding it the placeholders' fake tensors to preserve the export ShapeEnv/SymInts;
  • normalizes the SoA aten.complex/real/imag seams it leaves behind into the interleaved [...,2] layout the rest of the TRT flow expects.

Wiring:

  • use_complex_decomposition setting (default False) selects new vs legacy pass via complex_lowering_pass in _aten_lowering_pass;
  • torch-version capability gate has_complex_decomposition() in _features (torch>=2.14.dev), with graceful fallback to complex_graph_detection.

Tests:

  • test_complex_ops.py parametrized [legacy]/[decomp] via an autouse fixture so the existing suite runs on both paths (decomp auto-skips on torch<2.14);
  • new GPU-free test_complex_decomposition_adapter.py covering the SoA->[...,2] normalization and the fallback gate.

Default off; legacy path unchanged. Boundary normalization assumptions (exact upstream op set, dynamic-shape survival through make_fx) still to be confirmed by a spike; see TODO(spike) in the adapter.

#4390

Adds an alternative complex-numerics lowering path that delegates the
interior complex->real expansion to PyTorch's upstream decomposition
(decompose_complex_in_graph, pytorch/pytorch#169832) instead of the
hand-maintained per-op rewriter in complex_graph_rewrite.py.

The new complex_decomposition_adapter pass:
  - captures the complex I/O signature (reusing the existing helpers) so
    the post-partition _insert_complex_io_adapters boundary code works
    unchanged;
  - calls decompose_complex_in_graph on the exported graph, feeding it the
    placeholders' fake tensors to preserve the export ShapeEnv/SymInts;
  - normalizes the SoA aten.complex/real/imag seams it leaves behind into
    the interleaved [...,2] layout the rest of the TRT flow expects.

Wiring:
  - use_complex_decomposition setting (default False) selects new vs legacy
    pass via complex_lowering_pass in _aten_lowering_pass;
  - torch-version capability gate has_complex_decomposition() in _features
    (torch>=2.14.dev), with graceful fallback to complex_graph_detection.

Tests:
  - test_complex_ops.py parametrized [legacy]/[decomp] via an autouse
    fixture so the existing suite runs on both paths (decomp auto-skips on
    torch<2.14);
  - new GPU-free test_complex_decomposition_adapter.py covering the SoA->[...,2]
    normalization and the fallback gate.

Default off; legacy path unchanged. Boundary normalization assumptions
(exact upstream op set, dynamic-shape survival through make_fx) still to be
confirmed by a spike; see TODO(spike) in the adapter.
@meta-cla meta-cla Bot added the cla signed label Jul 23, 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 Jul 23, 2026
@github-actions
github-actions Bot requested a review from zewenli98 July 23, 2026 21:36
@apbose
apbose marked this pull request as draft July 23, 2026 21:37

@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/lowering/passes/_aten_lowering_pass.py	2026-07-23 21:36:54.866947+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/lowering/passes/_aten_lowering_pass.py	2026-07-23 21:37:15.722131+00:00
@@ -42,10 +42,11 @@
    otherwise the legacy hand-rolled rewriter runs.
    """
    if getattr(settings, "use_complex_decomposition", False):
        return complex_decomposition_adapter(gm, settings)
    return complex_graph_detection(gm, settings)
+

post_lowering_pass_list = [
    replace_fused_rms_norm,
    remove_input_alias_fixing_clones,
    constant_fold,
--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_complex_decomposition_adapter.py	2026-07-23 21:36:54.908527+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_complex_decomposition_adapter.py	2026-07-23 21:37:19.335969+00:00
@@ -69,13 +69,11 @@
    assert aten.complex.default not in targets
    assert aten.cat.default in targets
    assert aten.unsqueeze.default in targets

    # the packed cat node must carry the complex-layout tag
-    cat_nodes = [
-        n for n in gm.graph.nodes if n.target == aten.cat.default
-    ]
+    cat_nodes = [n for n in gm.graph.nodes if n.target == aten.cat.default]
    assert cat_nodes and cat_nodes[0].meta.get("is_complex_layout") is True

    # numerically: output is the [...,2] interleaved layout of re/im
    out = gm(re, im)
    assert out.shape == (3, 2)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant