Skip to content

causal-conv1d/mamba-ssm: add dilation to causal_conv1d_fn - #1404

Merged
github-actions[bot] merged 7 commits into
huggingface:mainfrom
albertvillanova:causal-conv1d-dilation
Dec 3, 2026
Merged

github-actions[bot] merged 7 commits into
huggingface:mainfrom
albertvillanova:causal-conv1d-dilation

Conversation

@albertvillanova

@albertvillanova albertvillanova commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Related issue

Closes #1394

What does this PR do?

Adds a dilation argument (default 1) to causal_conv1d_fn in causal-conv1d and in the mamba-ssm copy, with no CUDA changes.

Motivation

Qwen3.8-Flash-Next's PLE layer has a depthwise causal conv with dilation (width 4, dilation 3). transformers can't run it with causal_conv1d_fn, so with packed sequences that conv mixes tokens across sequences (huggingface/transformers#51572). Following the review on huggingface/transformers#51576, the fix belongs in the kernel rather than in a torch workaround in transformers.

A dilated conv is the undilated conv applied to each of the dilation phases (positions t mod dilation). The interface reshapes x and seq_idx to (batch * dilation, dim, seqlen / dilation) in the channel-last layout seq_idx needs, calls the existing kernel, and reshapes back. The backward comes from the existing autograd function.

mamba-ssm builds per Torch version and only the last two are rebuilt, so it is bumped to version 7: the older v6 builds keep the old layer, which would silently drop dilation. causal-conv1d is rebuilt for every Torch version, so it keeps version 3.

Changes

  • causal_conv1d_interface.py: causal_conv1d_fn(..., dilation=1). dilation == 1 takes the existing path unchanged; initial_states and return_final_states raise NotImplementedError with dilation > 1. causal_conv1d_ref gets dilation too, and raises with initial_states.
  • layers.py: pass dilation through from the kwargs.
  • mamba-ssm: the same changes in _causal_conv1d/causal_conv1d_interface.py, layers.py and tests/ops/test_causal_conv1d.py; version 7 in build.toml and in the tests' get_kernel calls.
  • Tests: test_causal_conv1d_dilation (forward and gradients against F.conv1d(..., dilation=d) split per sequence, batch 1 and 3, with and without seq_idx), plus kernels_ci smoke cases.

Testing

  • On an H100, against the published CUDA ops: 129 passed (batch 1/3, seqlen 8/150/151/1024, width 2/4, dilation 2/3, with and without seq_idx, fp32 and bf16, plus the reference raising with initial_states). Same 129 passed for the mamba-ssm copy against the published mamba-ssm CUDA ops.
  • scripts/check_public_api.py: the appended optional argument keeps the existing signature, so causal-conv1d needs no version bump.

Checklist

  • This PR is linked to an issue that was discussed and approved
  • I have tested these changes locally
  • New/changed functionality has test coverage
  • LLM disclosure:
    • I did not use an LLM to create this PR.
    • I used and LLM for assistance while creating this PR.
    • This PR was mostly or completely generated by an LLM.

cc @vasqu

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

This PR changes source code for the following kernel(s): causal-conv1d, mamba-ssm.

If you have the kernel-bot build permissions, then trigger /kernel-bot build causal-conv1d mamba-ssm yourself. Otherwise, a maintainer will review the PR and will take an appropriate action. Thanks for your patience.

@kashif kashif left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks Albert, the phase split is a nice way to get this without touching the CUDA code. I ran the interface against the published causal-conv1d v3 on ROCm (gfx1150), forward and grads vs F.conv1d(dilation=d) per sequence, and found a couple of things:

  1. Batch 1 with seq_idx raises for channel-first input. When seqlen % dilation == 0 there's no padding, and with batch 1 x.transpose(1, 2).reshape(batch * dilation, phase_len, dim) stays a view, so after the transpose x is not channel-last (e.g. strides (1, 8, 2) for (2, 256, 4)) and the kernel fails with seq_idx is only supported for channel last layout. Batch 1 is exactly the packed case. The tests use batch 3, where the reshape copies, so they don't catch it. Adding .contiguous() before the final .transpose(1, 2) fixes it: 48/48 on both channel-first and channel-last inputs (seqlen 8/151/1024, width 2/4, dilation 2/3, with and without seq_idx, fp32/bf16). A batch=1 case in the test would cover it. (transformers passes channel-last input, so it doesn't hit this there.)

  2. mamba-ssm needs a version bump. causal-conv1d is stable-ABI so every variant is rebuilt, but mamba-ssm builds per Torch version and only the last two get rebuilt. The older v6 variants (torch 2.11–2.13) keep the old causal_conv1d_fn layer, which takes **kwargs and only pops seq_idx, so dilation=3 would be silently dropped and they'd return an undilated conv rather than fail. transformers maps causal_conv1d_fn to mamba-ssm, so a model using it on those Torch versions would get wrong outputs. Either bump mamba-ssm, or keep this to causal-conv1d and map the dilated conv to it in transformers.

  3. Small: causal_conv1d_ref ignores dilation when initial_states is given, it should probably raise like causal_conv1d_fn does.

Decode isn't covered (causal_conv1d_update / initial_states), so transformers will have to run fn over the cached context plus the new tokens for now; a dilated update would be an easy follow-up.

@albertvillanova albertvillanova changed the title causal-conv1d/mamba-ssm: add dilation to causal_conv1d_fn causal-conv1d: add dilation to causal_conv1d_fn Dec 2, 2026
@albertvillanova

albertvillanova commented Dec 2, 2026 •

Copy link
Copy Markdown
Member Author

Thanks a lot for testing it on ROCm and for the review!

  1. Fixed: .contiguous() before the final transpose, and the tests now cover batch 1 and 3 (the CI case uses seqlen=150, so batch 1 has no padding). The 16 batch-1 cases that failed now pass.
  2. Update: ported to mamba-ssm too, with a version bump to 7 (as vasqu asked), so the older v6 builds aren't used.
  3. Fixed: causal_conv1d_ref now raises with initial_states and dilation > 1.

Agreed on decode: a dilated causal_conv1d_update can be a follow-up.

@albertvillanova

Copy link
Copy Markdown
Member Author

/kernel-bot build-and-stage causal-conv1d

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build-and-stage causal-conv1d
Mode: build and stage
Target branch: pr-1404
PR head SHA: 5ebd101a59012b40e5d7bafd01e09162e39d1a9c
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (1):

  • causal-conv1d (build.yaml): dispatched, but run URL is not available yet

Hub uploads:

@vasqu vasqu 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.

1 nit, agree update would be neat because it should boost on decode quite a bit no? just hesitant on this if its not worth it also ok to defer for now

bias=bias,
activation=activation,
seq_idx=seq_idx,
dilation=kwargs.pop("dilation", 1),

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.

lets follow seq idx above pop with default none (only get has none default no? at least automatically)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

@albertvillanova

Copy link
Copy Markdown
Member Author

Thanks! Agreed, a dilated causal_conv1d_update should help decode. I'd defer it to a follow-up so this one can land first for training.

@vasqu vasqu 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.

we are missing the mamba ssm updates respectively no? this is only for causal conv1d but we port it to mamba ssm as well

Mirror the causal-conv1d change in the mamba-ssm copy and bump mamba-ssm to version 7, so the older Torch builds, which would silently drop dilation, are not used.
@albertvillanova albertvillanova changed the title causal-conv1d: add dilation to causal_conv1d_fn causal-conv1d/mamba-ssm: add dilation to causal_conv1d_fn Dec 2, 2026
@albertvillanova

Copy link
Copy Markdown
Member Author

Done: ported to mamba-ssm as well, with a version bump to 7 (as in #1300), so the older v6 builds that would silently drop dilation aren't picked up.

@albertvillanova

Copy link
Copy Markdown
Member Author

/kernel-bot build-and-stage causal-conv1d mamba-ssm

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build-and-stage causal-conv1d mamba-ssm
Mode: build and stage
Target branch: pr-1404
PR head SHA: ada35df012307c9626fceb5e35aca8510d2960bc
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (2):

Hub uploads:

vasqu
vasqu previously approved these changes Dec 2, 2026

@vasqu vasqu 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.

pls dont merge and lets adjust on transformers side first so we merge one after the other after verifying everything (but lgtm)

@albertvillanova

Copy link
Copy Markdown
Member Author

The transformers follow-up is ready and verified against the staged builds: with mamba-ssm 7, the padding-free test of the dilated short conv passes, and it fails on main. Good to merge from my side; I'll open the transformers PR once mamba-ssm 7 is released.

@vasqu

vasqu commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot build-and-stage causal-conv1d mamba-ssm

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build-and-stage causal-conv1d mamba-ssm
Mode: build and stage
Target branch: pr-1404
PR head SHA: 3e05bb9437086e704294df7c52dc6ff8f93e98f7
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (2):

@vasqu

vasqu commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot build-and-stage causal-conv1d mamba-ssm

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build-and-stage causal-conv1d mamba-ssm
Mode: build and stage
Target branch: pr-1404
PR head SHA: 84a1525bcfef03f2c77dae2c4c99f87eefc111d4
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (2):

Hub uploads:

@drbh drbh 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.

lgtm!

@drbh

drbh commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot merge-and-upload causal-conv1d mamba-ssm

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot merge-and-upload causal-conv1d mamba-ssm
Mode: merge, build and upload
Target branch: ``
PR head SHA: 84a1525bcfef03f2c77dae2c4c99f87eefc111d4
Workflows: `build.yaml, build-mac.yaml, build-windows.yaml`

Merge result: Pull Request successfully merged

Dispatched (2):

Hub uploads:

@github-actions
github-actions Bot merged commit d62bd91 into huggingface:main Dec 3, 2026
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Version bumps, releases, misc maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

causal-conv1d: support dilation in causal_conv1d_fn

4 participants