Repository navigation
causal-conv1d/mamba-ssm: add dilation to causal_conv1d_fn - #1404
github-actions[bot] merged 7 commits into
Conversation
|
This PR changes source code for the following kernel(s): If you have the kernel-bot build permissions, then trigger |
There was a problem hiding this comment.
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:
-
Batch 1 with
seq_idxraises for channel-first input. Whenseqlen % dilation == 0there's no padding, and with batch 1x.transpose(1, 2).reshape(batch * dilation, phase_len, dim)stays a view, so after the transposexis not channel-last (e.g. strides(1, 8, 2)for(2, 256, 4)) and the kernel fails withseq_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 withoutseq_idx, fp32/bf16). Abatch=1case in the test would cover it. (transformers passes channel-last input, so it doesn't hit this there.) -
mamba-ssm needs a version bump.
causal-conv1dis 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 oldcausal_conv1d_fnlayer, which takes**kwargsand only popsseq_idx, sodilation=3would be silently dropped and they'd return an undilated conv rather than fail. transformers mapscausal_conv1d_fnto mamba-ssm, so a model using it on those Torch versions would get wrong outputs. Either bump mamba-ssm, or keep this tocausal-conv1dand map the dilated conv to it in transformers. -
Small:
causal_conv1d_refignoresdilationwheninitial_statesis given, it should probably raise likecausal_conv1d_fndoes.
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.
…ial_states, drop the mamba-ssm changes
|
Thanks a lot for testing it on ROCm and for the review!
Agreed on decode: a dilated |
|
/kernel-bot build-and-stage causal-conv1d |
|
Build request processed. Command: Dispatched (1):
Hub uploads: |
vasqu
left a comment
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
lets follow seq idx above pop with default none (only get has none default no? at least automatically)
|
Thanks! Agreed, a dilated |
vasqu
left a comment
There was a problem hiding this comment.
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.
|
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 |
|
/kernel-bot build-and-stage causal-conv1d mamba-ssm |
|
Build request processed. Command: Dispatched (2):
Hub uploads: |
vasqu
left a comment
There was a problem hiding this comment.
pls dont merge and lets adjust on transformers side first so we merge one after the other after verifying everything (but lgtm)
|
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. |
|
/kernel-bot build-and-stage causal-conv1d mamba-ssm |
|
Build request processed. Command: Dispatched (2):
|
|
/kernel-bot build-and-stage causal-conv1d mamba-ssm |
|
Build request processed. Command: Dispatched (2):
Hub uploads: |
|
/kernel-bot merge-and-upload causal-conv1d mamba-ssm |
|
Build request processed. Command: Merge result: Pull Request successfully merged Dispatched (2):
Hub uploads: |
Related issue
Closes #1394
What does this PR do?
Adds a
dilationargument (default1) tocausal_conv1d_fnincausal-conv1dand 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
dilationphases (positionst mod dilation). The interface reshapesxandseq_idxto(batch * dilation, dim, seqlen / dilation)in the channel-last layoutseq_idxneeds, 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-conv1dis rebuilt for every Torch version, so it keeps version 3.Changes
causal_conv1d_interface.py:causal_conv1d_fn(..., dilation=1).dilation == 1takes the existing path unchanged;initial_statesandreturn_final_statesraiseNotImplementedErrorwithdilation > 1.causal_conv1d_refgetsdilationtoo, and raises withinitial_states.layers.py: passdilationthrough from the kwargs._causal_conv1d/causal_conv1d_interface.py,layers.pyandtests/ops/test_causal_conv1d.py; version 7 inbuild.tomland in the tests'get_kernelcalls.test_causal_conv1d_dilation(forward and gradients againstF.conv1d(..., dilation=d)split per sequence, batch 1 and 3, with and withoutseq_idx), pluskernels_cismoke cases.Testing
seq_idx, fp32 and bf16, plus the reference raising withinitial_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, socausal-conv1dneeds no version bump.Checklist
cc @vasqu