Skip to content

associative_scan lowering - #4518

Open
jloftin-nv wants to merge 2 commits into
pytorch:mainfrom
jloftin-nv:dev-jloftin-associative-scan
Open

associative_scan lowering#4518
jloftin-nv wants to merge 2 commits into
pytorch:mainfrom
jloftin-nv:dev-jloftin-associative-scan

Conversation

@jloftin-nv

Copy link
Copy Markdown
Contributor

Description

higher_order.associative_scan (CUDA combine_mode="pointwise") survives export and has no converter, breaking the graph on classical selective-scan SSM layers.

Add a post-lowering pass that matches the Mamba affine combine (a_la_r, a_rb_l+b_r) and, for static scan length, replaces the HOP with a Hillis–Steele scan of existing aten ops. Unrecognized combines and dynamic S are left alone.

Fixes # (issue)

Type of change

Please delete options that are not relevant and/or add your own.

  • New feature (non-breaking change which adds functionality)

Checklist:

  • [ X] My code follows the style guidelines of this project (You can use the linters)
  • [ X] I have performed a self-review of my own code
  • [ X] I have commented my code, particularly in hard-to-understand areas and hacks
  • [ X] I have made corresponding changes to the documentation
  • [X ] I have added tests to verify my fix or my feature
  • [X ] New and existing unit tests pass locally with my changes
  • [ X] I have added the relevant labels to my PR in so that relevant reviewers are notified

@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 18, 2026
@meta-cla meta-cla Bot added the cla signed label Aug 18, 2026
@github-actions
github-actions Bot requested a review from zewenli98 August 18, 2026 21:57
@narendasan
narendasan requested review from bowang007 and micwill755 and removed request for zewenli98 August 18, 2026 22:56

@micwill755 micwill755 left a comment

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.

Requesting changes on the identity pad (ones_like / zeros_like after decompositions won’t convert) and on the TRT numeric test, which can still pass via PyTorch fallback.

b_head = gm.graph.call_function(
torch.ops.aten.slice.Tensor, (b, 0, 0, step, 1)
)
ones = gm.graph.call_function(torch.ops.aten.ones_like.default, (a_head,))

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.

ones_like and zeros_like are inserted after run_decompositions, and Dynamo has no converter for them. The identity pad can run in PyTorch even if the mul/add scan converts, so tests can pass without the scan in TRT.

You already slice a_head/b_head and a_prefix/b_prefix. Use those directly instead of padding with dummy 1s and 0s:

keep the head (a_head, b_head)
combine the tail (prefix with a/b from step onward)
cat(head, combined_tail)

If you prefer to keep the pad, emit aten.full.default rather than ones_like / zeros_like.

self.assertIn(torch.ops.aten.mul.Tensor, targets)
self.assertIn(torch.ops.aten.add.Tensor, targets)

def test_pointwise_scan_numerics_match_eager(self):

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.

This can still pass with the PyTorch fallback from the ones_like / zeros_like comment: min_block_size=1 will compile the convertible pieces and leave those ops in PyTorch. I would add an assert to check the lowered graph has no HOP and no ones_like / zeros_like, and TRT-compile a non-power-of-two S.

@lanluo-nvidia lanluo-nvidia added this to the v2.15.0 milestone Aug 20, 2026
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.

3 participants