Skip to content

Added kernels from kernel hub for Bamba model#41540

Merged
ArthurZucker merged 28 commits intohuggingface:mainfrom
romitjain:romit/feature-bamba-kernels-from-hub
Dec 16, 2025
Merged

Added kernels from kernel hub for Bamba model#41540
ArthurZucker merged 28 commits intohuggingface:mainfrom
romitjain:romit/feature-bamba-kernels-from-hub

Conversation

@romitjain
Copy link
Contributor

What does this PR do?

Adds support for mamba_ssm and causal_conv1d kernels from the kernel-hub in bamba models.

Fixes # (issue)

#41208

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@vasqu @MekkCyber @drbh

@romitjain romitjain marked this pull request as draft October 13, 2025 08:59
@romitjain
Copy link
Contributor Author

Downstream changes have not been done (eg: modeling_granitemoehybrid.py)
Since it was mentioned I can't update those files, not sure how to go about them.

@MekkCyber
Copy link
Contributor

Hi @romitjain I'm working on a pr to make the kernel function mapping easier to do so we don't have to use new functions like lazy_load_mamba_ssm in the modeling files. Once merged we can refactor your PR to make it work with the new API, and then we can apply the changes to all other models using make fix-copies

@romitjain
Copy link
Contributor Author

@MekkCyber Sure, no worries. Let me know (or you can share your PR here) once done, and I will udpate my PR

@MekkCyber
Copy link
Contributor

sure here is the pr : #41577

@romitjain
Copy link
Contributor Author

@MekkCyber I believe I can refactor my PR by using your new mapping function now?

@MekkCyber
Copy link
Contributor

Yes, i did that for falcon models here : #41664, you can do that for bamba models then using the same API, however the mamba-ssm kernel needs to be fixed now before merging the PRs.

Signed-off-by: romit <romit@ibm.com>
@romitjain romitjain marked this pull request as ready for review November 10, 2025 17:06
@romitjain
Copy link
Contributor Author

@MekkCyber @vasqu

The mamba_ssm kernels were breaking for me because it was not able to import mamba_chunk_scan_combined, mamba_split_conv1d_scan_combined. This was working earlier but has broken in the latest revision. The default revision uses the incorrect output class (state-spaces/mamba#807).

For my local testing, I made local changes to the mamba_ssm repo and tested the flow. However, we would need to fix it on kernels hub for this PR to work end to end.

Other than that, structurally, this is ready for review. Can you please have a look?


_HUB_KERNEL_MAPPING: dict[str, dict[str, str]] = {
"causal-conv1d": {"repo_id": "kernels-community/causal-conv1d"},
"mamba-ssm": {"repo_id": "kernels-community/mamba-ssm", "revision": "clean-mamba-ssm"},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copied over from: #41664

Copy link
Contributor

@vasqu vasqu left a comment

Choose a reason for hiding this comment

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

I don't have many comments except we should align with #41664 on how to lazy load.

These kernels are essentially the same so we ought to standardize it. The mamba-ssm side should be fixed on their repo, thx for the PR there!

cc @MekkCyber if you can take look here since it's close to what you did for the other mamba related model (falcon)

Comment on lines 1179 to 1181
@lru_cache
def is_einops_available() -> bool:
return _is_package_available("einops")
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldnt be needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that I recall it, we would need to this check before trying to load mamba-ssm kernels from kernels hub according to README here: https://huggingface.co/kernels-community/mamba-ssm

Should I inject these requirements in _HUB_KERNEL_MAPPING in src/transformers/integrations/hub_kernels.py

Copy link
Contributor

Choose a reason for hiding this comment

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

But we don't use this check anywhere, no? It doesn't hurt to have it either way, just a bit confused about the usage

Copy link
Contributor

Choose a reason for hiding this comment

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

for the clean-mamba-ssm implementation I don't think we need this, let's just remove it for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will remove this

Also, re: @vasqu's comment, I had added this earlier but forgot to remove this in my latest commit. Will remove it since clean-mamba-ssm won't need it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MekkCyber clean-mamba-ssm has a issue. It does not expose all the kernel functions: mamba_chunk_scan_combined, mamba_split_conv1d_scan_combined

see: #41540 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Hemmm, sure will export them in the branch

@romitjain
Copy link
Contributor Author

@vasqu In the PR that you referenced, the lazy_load_kernel function will be called for every forward step ref

Since it is not cached, IMO either the approach in this PR or adding lru_cache decorator to lazy_load_kernel would be a better solve.

WDYT?

@vasqu
Copy link
Contributor

vasqu commented Nov 12, 2025

@vasqu Since it is not cached, IMO either the approach in this PR or adding lru_cache decorator to lazy_load_kernel would be a better solve.

SGTM. I will still leave it to @MekkCyber tho as he's the main guy for anything kernels. In essence, it just should be the same way everywhere to avoid unnecessary tech debt

Copy link
Contributor

@MekkCyber MekkCyber left a comment

Choose a reason for hiding this comment

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

Thanks a lot @romitjain !

Comment on lines 1179 to 1181
@lru_cache
def is_einops_available() -> bool:
return _is_package_available("einops")
Copy link
Contributor

Choose a reason for hiding this comment

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

for the clean-mamba-ssm implementation I don't think we need this, let's just remove it for now

Signed-off-by: romit <romit@ibm.com>
Signed-off-by: romit <romit@ibm.com>
Signed-off-by: romit <romit@ibm.com>
@romitjain romitjain requested review from MekkCyber and vasqu November 12, 2025 17:19
@romitjain
Copy link
Contributor Author

romitjain commented Nov 12, 2025

@MekkCyber I have addressed your comments, PTAL

PS: It would require resolution of this issue: #41540 (comment)

Signed-off-by: romit <romit@ibm.com>
@romitjain
Copy link
Contributor Author

I did not run make fix-copies since I believe I should not edit the modeling files. But due to that the CI is failing, lmk what should be the steps?

@SunMarc
Copy link
Member

SunMarc commented Nov 13, 2025

In your case, you indeed need to run make fix-copies to propagate the modification you did in the modular files to the real modeling files

Copy link
Contributor

@MekkCyber MekkCyber left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this @romitjain, I will find some time this week to rework how imports are done inside the kernel because it's really not optimal to have such nested imports

Comment on lines 64 to 66
mamba_ssm_triton = getattr(getattr(mamba_ssm, "ops", None), "triton", None)
selective_state_update = getattr(
getattr(mamba_ssm_triton, "selective_state_update", None), "selective_state_update", None
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to rework how are these imports nested. It doesn't make sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't get you @MekkCyber?
I am also not in favor of these nested imports, but that is what you had previously requested

Copy link
Contributor

Choose a reason for hiding this comment

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

yes i mean it shouldn't be done this way in the kernel, like we should have an init file with all necessary functions, and then we only use getattr once, and not in a nested way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, let me know once you update the kernels init file, I can make the changes here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks a lot ! And sorry this is taking a lot of time 🙏, I’ll work on fixing things this week, at the latest.

Copy link
Contributor Author

@romitjain romitjain Dec 9, 2025

Choose a reason for hiding this comment

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

No worries at all. Thank you for your continued review on this!

@MekkCyber
Copy link
Contributor

MekkCyber commented Dec 10, 2025

Hi @romitjain ! It should be good now, you can use the kernels from this version : https://huggingface.co/kernels-community/mamba-ssm/tree/v0.0.4 instead of the clean-mamba-ssm one, let me know if you have any problems when you test it

Signed-off-by: romit <romit@ibm.com>
@romitjain
Copy link
Contributor Author

@MekkCyber Thanks for making the upstream fix, all the imports have been improved now to avoid nested calls. PTAL

@romitjain romitjain requested a review from MekkCyber December 15, 2025 08:33
Copy link
Contributor

@MekkCyber MekkCyber left a comment

Choose a reason for hiding this comment

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

Thanks @romitjain ! lgtm, only one nit and good to go

Comment on lines 492 to 499
causal_conv1d = lazy_load_kernel("causal-conv1d")
causal_conv1d_update = getattr(causal_conv1d, "causal_conv1d_update", None)
causal_conv1d_fn = getattr(causal_conv1d, "causal_conv1d_fn", None)

mamba_ssm = lazy_load_kernel("mamba-ssm")
selective_state_update = getattr(mamba_ssm, "selective_state_update", None)
mamba_chunk_scan_combined = getattr(mamba_ssm, "mamba_chunk_scan_combined", None)
mamba_split_conv1d_scan_combined = getattr(mamba_ssm, "mamba_split_conv1d_scan_combined", None)
Copy link
Contributor

@MekkCyber MekkCyber Dec 15, 2025

Choose a reason for hiding this comment

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

let's move this inside the Mixer to avoid loading the kernels at import time, the same we did here : https://github.com/romitjain/transformers/blob/03590f18cf59be7a9e215b946bd7ade3d8b12a7a/src/transformers/models/mamba/modeling_mamba.py#L201:L214

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MekkCyber Done for mamba2, jamba and bamba

Signed-off-by: romit <romit@ibm.com>
@romitjain romitjain requested a review from MekkCyber December 15, 2025 09:44
@MekkCyber
Copy link
Contributor

Thanks @romitjain ! can you fix the styling issues with make style ?

Signed-off-by: romit <romit@ibm.com>
@romitjain
Copy link
Contributor Author

Oops, I forgot to run that @MekkCyber
Done now.

Copy link
Contributor

@MekkCyber MekkCyber left a comment

Choose a reason for hiding this comment

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

lgtm! thanks for iterating on this PR

@romitjain
Copy link
Contributor Author

Thanks @MekkCyber
What would be the next steps for the merge?

@MekkCyber
Copy link
Contributor

MekkCyber commented Dec 15, 2025

Failling tests seem unrelated! we just need to wait for a green CI to merge

@romitjain
Copy link
Contributor Author

Seems like it is failing for the latest commits: https://github.com/huggingface/transformers/commits/main/

@github-actions
Copy link
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: bamba, granitemoehybrid, jamba, mamba2, qwen3_next

@github-actions
Copy link
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=41540&sha=65e283

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

ty

@ArthurZucker ArthurZucker merged commit 0f89661 into huggingface:main Dec 16, 2025
21 of 23 checks passed
@romitjain romitjain deleted the romit/feature-bamba-kernels-from-hub branch December 16, 2025 11:42
SangbumChoi pushed a commit to SangbumChoi/transformers that referenced this pull request Jan 23, 2026
* Added kernels from kernel hub for Bamba model

* Updated kernel loading

Signed-off-by: romit <romit@ibm.com>

* Remove einops

Signed-off-by: romit <romit@ibm.com>

* Removed global vars

Signed-off-by: romit <romit@ibm.com>

* Fixed make style

Signed-off-by: romit <romit@ibm.com>

* Nit

Signed-off-by: romit <romit@ibm.com>

* Added modeling files

Signed-off-by: romit <romit@ibm.com>

* Fixed merge conflict

Signed-off-by: romit <romit@ibm.com>

* fixed lint

Signed-off-by: romitjain <romit@ibm.com>

* Removed global import

* Small updates

* Updated

* Resolved merge conflicts

* Fixed the nested import

Signed-off-by: romit <romit@ibm.com>

* Moved imports inside mixer

Signed-off-by: romit <romit@ibm.com>

* CI CD fix

Signed-off-by: romit <romit@ibm.com>

---------

Signed-off-by: romit <romit@ibm.com>
Signed-off-by: romitjain <romit@ibm.com>
Co-authored-by: Mohamed Mekkouri <93391238+MekkCyber@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants