Skip to content

Conversation

@MekkCyber
Copy link
Contributor

What does this PR do?

This should simplify lazy kernel loading in Transformers.
We simply define a mapping between each kernel name and the repository it should be pulled from, then load it using the lazy_load_kernel function. This function adds the kernel to a global cache shared across all models.
If the kernel isn’t available, we check whether it’s installed as a module for backward compatibility; otherwise, we return None.

Comment on lines 235 to 236
def lazy_load_kernel(kernel_name: str, mapping: dict[str, Optional[ModuleType]]):
if kernel_name in mapping and isinstance(mapping[kernel_name], ModuleType):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the main utility function applied to the case of causal-conv1d

@MekkCyber
Copy link
Contributor Author

run-slow: falcon_mamba, mamba

@github-actions
Copy link
Contributor

This comment contains run-slow, running the specified jobs:

models: ['models/falcon_mamba', 'models/mamba']
quantizations: [] ...

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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.

ncie

raise RuntimeError("register_kernel_mapping requires `kernels` to be installed. Run `pip install kernels`.")


_KERNEL_SIMPLE_MAPPING: dict[str, str] = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why "simple" ?

Comment on lines 259 to 264
causal_conv1d = lazy_load_kernel("causal-conv1d", _KERNEL_MAPPING_ACROSS_MODELS)
causal_conv1d_update, causal_conv1d_fn = (
(causal_conv1d.causal_conv1d_update, causal_conv1d.causal_conv1d_fn)
if causal_conv1d is not None
else (None, None)
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should not need to pass the kernel mapping, lazy load kernel can handle 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.

Done

@github-actions
Copy link
Contributor

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

run-slow: falcon_mamba, mamba

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.

Thanks 🤗

ALL_MASK_ATTENTION_FUNCTIONS.register(attn_implementation, ALL_MASK_ATTENTION_FUNCTIONS["flash_attention_2"])


def lazy_load_kernel(kernel_name: str, mapping: dict[str, Optional[ModuleType]] = _KERNEL_MODULE_MAPPING):
Copy link
Collaborator

Choose a reason for hiding this comment

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

should be None, if none default to kernel mapping python will cry otherwise!

Comment on lines +243 to +248
causal_conv1d = lazy_load_kernel("causal-conv1d")
causal_conv1d_update, causal_conv1d_fn = (
(causal_conv1d.causal_conv1d_update, causal_conv1d.causal_conv1d_fn)
if causal_conv1d is not None
else (None, None)
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice and simple

@MekkCyber MekkCyber merged commit 1fb3fc4 into main Oct 16, 2025
23 checks passed
@MekkCyber MekkCyber deleted the add_function_mapping_kernels branch October 16, 2025 13:43
ngazagna-qc pushed a commit to ngazagna-qc/transformers that referenced this pull request Oct 23, 2025
* refactor function kernel callling

* nit

* don't pass the mapping

* use _kernels_available

* rm import
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.

4 participants