Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
f438a2e
kernel native
Cyrilvallez Jul 13, 2026
7c1335b
fix warning
Cyrilvallez Jul 13, 2026
f8ff0db
have to fix other modular to be coherent
Cyrilvallez Jul 13, 2026
9a8127e
remove useless
Cyrilvallez Jul 13, 2026
72b45ca
use native lib as well
Cyrilvallez Jul 13, 2026
54d80df
modular
Cyrilvallez Jul 13, 2026
d2beaf6
combine them
Cyrilvallez Jul 13, 2026
c256c47
doc
Cyrilvallez Jul 13, 2026
30e0ff7
ignore kwargs, allow og
vasqu Jul 13, 2026
c57b520
Merge remote-tracking branch 'upstream/main' into kernels-fallback
vasqu Jul 29, 2026
38c2ece
fix
vasqu Jul 29, 2026
7788cac
gdn like paths (missing conv of olmo hybrid)
vasqu Jul 29, 2026
cf49de6
olmo hybrid fused conv style
vasqu Jul 29, 2026
73db89b
fix
vasqu Jul 29, 2026
8896696
fix
vasqu Jul 29, 2026
d3b27a6
poc mamba2, kernel must compile but torch seems to match
vasqu Jul 29, 2026
f0aa581
kernels match --> hf kernels will be needed
vasqu Jul 29, 2026
263f307
style
vasqu Jul 29, 2026
a189da9
quick fixes
vasqu Jul 30, 2026
8134d38
Merge remote-tracking branch 'upstream/main' into kernels-fallback
vasqu Jul 30, 2026
4ebeb11
mamba2 works
vasqu Jul 30, 2026
1786c75
mamba2 suite of models
vasqu Jul 30, 2026
0eca0d0
oops
vasqu Jul 30, 2026
82bfade
conv1ds across other models
vasqu Jul 30, 2026
520e340
let's try this
vasqu Jul 30, 2026
f69acfe
mamba base implementation
vasqu Jul 30, 2026
bfbf9f1
fixups as per review comments
vasqu Aug 3, 2026
511d4a6
fixup mamba tests and other issues
vasqu Aug 3, 2026
3ee52c3
make no shape check by default (single padded sample should also work)
vasqu Aug 3, 2026
34051c4
remove todo
vasqu Aug 3, 2026
c3a6c98
propogate mamba1
vasqu Aug 3, 2026
fcf7292
style
vasqu Aug 3, 2026
3823bcb
fix mambapy + enable on jamba
vasqu Aug 3, 2026
380e5fc
zamba1
vasqu Aug 3, 2026
ad78e09
style
vasqu Aug 3, 2026
3c51e11
fix early cast (leads to non fp32 norm)
vasqu Aug 3, 2026
b05fb52
fix padding free path
vasqu Aug 3, 2026
98f62d4
fix offload
vasqu Aug 3, 2026
66cff33
Merge remote-tracking branch 'upstream/main' into kernels-fallback
vasqu Aug 3, 2026
2ec940b
Merge remote-tracking branch 'upstream/main' into kernels-fallback
vasqu Aug 4, 2026
9b266aa
bump kernels
vasqu Aug 4, 2026
c4b785c
remove todo
vasqu Aug 4, 2026
c85e622
avoid onnx export and fix mamba2 test
vasqu Aug 4, 2026
2b4fd75
fix bamba test
vasqu Aug 4, 2026
2e485f3
update falcon mamba - aligned with all other devices
vasqu Aug 4, 2026
d81829f
oops
vasqu Aug 4, 2026
9dabdf2
Merge branch 'main' into kernels-fallback
vasqu Aug 4, 2026
4c1864d
adress review
vasqu Aug 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/transformers/conversion_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,15 @@ def _build_checkpoint_conversion_mapping():
"olmo_hybrid": [
WeightRenaming("attention_layer_norm", "input_layernorm"),
WeightRenaming("feedforward_layer_norm", "post_attention_layernorm"),
WeightConverter(
source_patterns=[
"linear_attn.q_conv1d.weight",
"linear_attn.k_conv1d.weight",
"linear_attn.v_conv1d.weight",
],
target_patterns="linear_attn.conv1d.weight",
operations=[Concatenate(dim=0)],
),
],
"qwen3_5_text": [PrefixChange(prefix_to_remove="language_model", model_prefix="model")],
"sam3_tracker": [
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"replace_kernel_forward_from_hub",
"use_kernel_forward_from_hub",
"use_kernel_func_from_hub",
"use_kernel_func_from_hub_with_fallback",
"use_kernelized_func",
],
"integration_utils": [
Expand Down Expand Up @@ -237,6 +238,7 @@
replace_kernel_forward_from_hub,
use_kernel_forward_from_hub,
use_kernel_func_from_hub,
use_kernel_func_from_hub_with_fallback,
use_kernelized_func,
)
from .integration_utils import (
Expand Down
37 changes: 22 additions & 15 deletions src/transformers/integrations/accelerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,31 +920,38 @@ def check_tied_parameters_on_same_device(tied_params, device_map):
)


def force_accelerate_hooks(child_module_name: str) -> Callable:
def force_accelerate_hooks(child_module_names: str | list[str]) -> Callable:
"""
Decorator to forcefully fire the accelerate hooks of `child_module_name`, before entering the forward of the parent itself.
Indeed, the hooks of the child are only fired through the `forward` child's method, so if the child weights are used directly,
Decorator to forcefully fire the accelerate hooks of `child_module_names`, before entering the forward of the parent itself.
Indeed, the hooks of a child are only fired through the `forward` child's method, so if the child weights are used directly,
as is the case inside `causal_conv1d_fn` and `causal_conv1d_update` for example, they will not be fired. This may cause device
issues, especially in the case of offloading, that this decorator will correct.
"""

if isinstance(child_module_names, str):
child_module_names = [child_module_names]

def decorator(forward_func: Callable) -> Callable:
def wrapped(self, *args, **kwargs):
hooked_module = getattr(self, child_module_name)
hook = getattr(hooked_module, "_hf_hook", None)
if hook is not None:
# Note that here we only call the hook with the module, not `*args` not `**kwargs`, as we assume the `forward`
# on which this decorator is applied is responsible to move the args and kwargs with its own hook if any. This makes
# sense as the module decorated with this should have all internal modules on the same device
hook.pre_forward(hooked_module)
hooked_modules = []
for child_module_name in child_module_names:
hooked_module = getattr(self, child_module_name)
hook = getattr(hooked_module, "_hf_hook", None)
hooked_modules.append((hooked_module, hook))
if hook is not None:
# Note that here we only call the hook with the module, not `*args` not `**kwargs`, as we assume the `forward`
# on which this decorator is applied is responsible to move the args and kwargs with its own hook if any. This makes
# sense as the module decorated with this should have all internal modules on the same device
hook.pre_forward(hooked_module)

output = forward_func(self, *args, **kwargs)

if hook is not None:
# Note that here we only call the hook with the module, not `output`, as we assume the `forward` on which
# this decorator is applied is responsible to move the output with its own hook if any. This makes sense
# as the module decorated with this should have all internal modules on the same device
hook.post_forward(hooked_module, ())
for hooked_module, hook in reversed(hooked_modules):
if hook is not None:
# Note that here we only call the hook with the module, not `output`, as we assume the `forward` on which
# this decorator is applied is responsible to move the output with its own hook if any. This makes sense
# as the module decorated with this should have all internal modules on the same device
hook.post_forward(hooked_module, ())

return output

Expand Down
175 changes: 169 additions & 6 deletions src/transformers/integrations/hub_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import functools
import importlib
import inspect
import os
import re
import sys
Expand All @@ -32,6 +33,7 @@
is_kernels_available,
is_rocm_platform,
is_torch_available,
resolve_internal_import,
)
from .flash_attention import flash_attention_forward

Expand Down Expand Up @@ -59,6 +61,18 @@
_kernels_enabled = _TRANSFORMERS_USE_HUB_KERNELS in ENV_VARS_TRUE_VALUES


# Maps from func name to the internal module path
_KERNELS_INTERNAL_PATH_MAPPINGS = {
"chunk_gated_delta_rule": "ops.gated_delta_rule",
"recurrent_gated_delta_rule": "ops.gated_delta_rule",
"mamba_split_conv1d_scan_combined": "ops.triton.ssd_combined",
"selective_state_update": "ops.triton.selective_state_update",
"mamba_chunk_scan_combined": "ops.triton.ssd_combined",
"mamba_inner_fn": "ops.selective_scan_interface",
"selective_scan_fn": "ops.selective_scan_interface",
}


if is_kernels_available():
from kernels import (
CUDAProperties,
Expand Down Expand Up @@ -154,12 +168,12 @@ def _build_kernel_mapping() -> dict:
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="causal_conv1d_fn",
version=1,
version=2,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="causal_conv1d_fn",
version=1,
version=2,
),
},
},
Expand All @@ -168,15 +182,113 @@ def _build_kernel_mapping() -> dict:
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="causal_conv1d_update",
version=1,
version=2,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="causal_conv1d_update",
version=2,
),
},
},
"chunk_gated_delta_rule": {
"cuda": {
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/fla",
layer_name="chunk_gated_delta_rule",
version=1,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/fla",
layer_name="chunk_gated_delta_rule",
version=1,
),
},
},
"recurrent_gated_delta_rule": {
"cuda": {
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/fla",
layer_name="recurrent_gated_delta_rule",
version=1,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/fla",
layer_name="recurrent_gated_delta_rule",
version=1,
),
},
},
"mamba_chunk_scan_combined": {
"cuda": {
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="mamba_chunk_scan_combined",
version=2,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="mamba_chunk_scan_combined",
version=2,
),
},
},
"mamba_split_conv1d_scan_combined": {
"cuda": {
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="mamba_split_conv1d_scan_combined",
version=2,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="mamba_split_conv1d_scan_combined",
version=2,
),
},
},
"mamba_inner_fn": {
"cuda": {
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="mamba_inner_fn",
version=2,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="mamba_inner_fn",
version=2,
),
},
},
"selective_scan_fn": {
"cuda": {
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="selective_scan_fn",
version=2,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="selective_scan_fn",
version=2,
),
},
},
"selective_state_update": {
"cuda": {
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="selective_state_update",
version=2,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/mamba-ssm",
layer_name="selective_state_update",
version=2,
),
},
},
"SwiGLUMLP": {
"cuda": {
Mode.INFERENCE | Mode.TORCH_COMPILE: LayerRepository(
Expand Down Expand Up @@ -267,6 +379,20 @@ def _build_kernel_mapping() -> dict:
),
},
},
"RMSNormGated": {
"cuda": {
Mode.TRAINING: LayerRepository(
repo_id="kernels-community/fla",
layer_name="FusedRMSNormGated",
version=1,
),
Mode.INFERENCE: LayerRepository(
repo_id="kernels-community/fla",
layer_name="FusedRMSNormGated",
version=1,
),
},
},
"MegaBlocksMoeMLP": {
"cuda": {
Mode.TRAINING: LayerRepository(
Expand Down Expand Up @@ -451,9 +577,6 @@ def register_kernel_mapping_transformers(*args, **kwargs):


_HUB_KERNEL_MAPPING: dict[str, dict[str, str]] = {
"causal-conv1d": {"repo_id": "kernels-community/causal-conv1d", "version": 1},
"mamba-ssm": {"repo_id": "kernels-community/mamba-ssm", "version": 1},
"falcon_mamba-ssm": {"repo_id": "kernels-community/mamba-ssm", "version": 1},
"finegrained-fp8": {"repo_id": "kernels-community/finegrained-fp8", "version": 4},
"deep-gemm": {"repo_id": "kernels-community/deep-gemm", "version": 2},
"sonic-moe": {"repo_id": "kernels-community/sonic-moe", "revision": "ep-support"},
Expand Down Expand Up @@ -638,6 +761,46 @@ def get_kernel(
)


def use_kernel_func_from_hub_with_fallback(func_name: str, package: str, internal_path: str | None = None):
"""
The same as `use_kernel_forward_from_hub` but with the optional fallback to an original package if it exists, e.g.,
FLA for Gated Delta Rule, mamba-ssm for mamba2, etc.

This combines all options with kernels, enabling kernels on top of the original package if requested as well.
The order of priority is
1. Hf kernels (if requested)
2. Original package
3. Torch only path
"""
kernel_wrapper_decorator = use_kernel_forward_from_hub(func_name)

# Allow internal path prefix if given to resolve non __init__ imports
internal_path = _KERNELS_INTERNAL_PATH_MAPPINGS.get(func_name, internal_path) # defaults
full_path = func_name if internal_path is None else f"{internal_path}.{func_name}"

def decorator(torch_function: Callable) -> Callable:
implementation = None
try:
module = importlib.import_module(package)
implementation = resolve_internal_import(module, full_path)
except Exception:
implementation = torch_function
finally:
implementation = torch_function if implementation is None else implementation

# Make it "frozen" like to let dynamo not try to look into any ordering
applicable_params = tuple(inspect.signature(implementation).parameters)

@functools.wraps(torch_function)
def wrapped(*args, **kwargs):
kwargs = {k: v for k, v in kwargs.items() if k in applicable_params}
return implementation(*args, **kwargs)
Comment thread
vasqu marked this conversation as resolved.

return kernel_wrapper_decorator(wrapped)

return decorator


# Whether to allow hub kernels coming from untrusted repos, i.e. repos outside `kernels-community`
ALLOW_ALL_KERNELS = False

Expand Down
Loading
Loading