Kernels API Reference

Main Functions

get_kernel

kernels.get_kernel

< >

( repo_id: str revision: typing.Optional[str] = None version: typing.Optional[str] = None ) ModuleType

Parameters

  • repo_id (str) — The Hub repository containing the kernel.
  • revision (str, optional, defaults to "main") — The specific revision (branch, tag, or commit) to download. Cannot be used together with version.
  • version (str, optional) — The kernel version to download. This can be a Python version specifier, such as ">=1.0.0,<2.0.0". Cannot be used together with revision.

Returns

ModuleType

The imported kernel module.

Load a kernel from the kernel hub.

This function downloads a kernel to the local Hugging Face Hub cache directory (if it was not downloaded before) and then loads the kernel.

Example:

import torch
from kernels import get_kernel

activation = get_kernel("kernels-community/activation")
x = torch.randn(10, 20, device="cuda")
out = torch.empty_like(x)
result = activation.silu_and_mul(out, x)

has_kernel

kernels.has_kernel

< >

( repo_id: str revision: typing.Optional[str] = None version: typing.Optional[str] = None ) bool

Parameters

  • repo_id (str) — The Hub repository containing the kernel.
  • revision (str, optional, defaults to "main") — The specific revision (branch, tag, or commit) to download. Cannot be used together with version.
  • version (str, optional) — The kernel version to download. This can be a Python version specifier, such as ">=1.0.0,<2.0.0". Cannot be used together with revision.

Returns

bool

True if a kernel is available for the current environment.

Check whether a kernel build exists for the current environment (Torch version and compute framework).

Loading locked kernels

load_kernel

kernels.load_kernel

< >

( repo_id: str lockfile: typing.Optional[pathlib.Path] = None ) ModuleType

Parameters

  • repo_id (str) — The Hub repository containing the kernel.
  • lockfile (Path, optional) — Path to the lockfile. If not provided, the lockfile will be loaded from the caller’s package metadata.

Returns

ModuleType

The imported kernel module.

Get a pre-downloaded, locked kernel.

If lockfile is not specified, the lockfile will be loaded from the caller’s package metadata.

get_locked_kernel

kernels.get_locked_kernel

< >

( repo_id: str local_files_only: bool = False ) ModuleType

Parameters

  • repo_id (str) — The Hub repository containing the kernel.
  • local_files_only (bool, optional, defaults to False) — Whether to only use local files and not download from the Hub.

Returns

ModuleType

The imported kernel module.

Get a kernel using a lock file.

< > Update on GitHub