Boost your model performance with pre-optimized kernels, easily loaded from the Hub.

Recent findings suggest that today, we'll explore an exciting development from Hugging Face: the Kernel Hub! As ML practitioners, we know that maximizing performance often involves diving deep into optimized code, custom CUDA kernels, or complex build systems. The Kernel Hub simplifies this process dramatically! (Related: Tech Report: Rockstar accused of tracing furry artwork ...)

Below is a short example of how to use a kernel in your code. (Related: British Airways Avios — Short Nonstop Domestic Flights...)

In the next sections we'll cover the following topics:.

We'll introduce these concepts promptly – the core idea can be grasped in about 5 minutes (though experimenting and benchmarking might take a bit longer!).

The Kernel Hub (👈 Check it out!) allows Python libraries and applications to load optimized compute kernels directly from the Hugging Face Hub. Think of it like the Model Hub, but for low-level, high-performance code snippets (kernels) that accelerate specific operations, often on GPUs.

Examples include advanced attention mechanisms (like FlashAttention for dramatic speedups and memory savings). Custom quantization kernels (enabling efficient computation with lower-precision data types like INT8 or INT4). Specialized kernels required for complex architectures like Mixture of Experts (MoE) layers, which involve intricate routing and computation patterns. As well as activation functions, and normalization layers (like LayerNorm or RMSNorm).

Instead of manually managing complex dependencies, wrestling with compilation flags, or building libraries like Triton or CUTLASS from source, you can use the kernels library to instantly fetch and run pre-compiled, optimized kernels.

For example, to enable FlashAttention you need just one line—no builds, no flags:.

kernels detects your exact Python, PyTorch, and CUDA versions, then downloads the matching pre‑compiled binary—typically in seconds (or a minute or two on a slow connection).

By contrast, compiling FlashAttention yourself requires:.

Kernel Hub erases all that friction: one function call, instant acceleration.

Industry observers note that as many machine learning developers know, managing dependencies and building low-level code from source can be a time-consuming and error-prone process. The Kernel Hub aims to simplify this by providing a centralized repository of optimized compute kernels that can be easily loaded and run.

Spend more time building great models and less time fighting build systems!

Industry observers note that using the Kernel Hub is designed to be straightforward. The kernels library provides the main interface. Here's a quick example that loads an optimized GELU activation function kernel. (Later on, we'll see another example about how to integrate a kernel in our model).

File: activation_validation_example.py.

(Note: If you have uv installed, you can save this script as script.py and run uv run script.py to automatically handle dependencies.).

Industry observers note that this simple example highlights how easily you can fetch and execute highly optimized code. Now let's look at a more practical integration using RMS Normalization.

Let's integrate an optimized RMS Normalization kernel into a basic model. We'll use the LlamaRMSNorm implementation provided in the kernels-community/triton-layer-norm repository (note: this repo contains various normalization kernels) and compare it against a baseline PyTorch implementation of RMSNorm.

First, define a simple RMSNorm module in PyTorch and a baseline model using it:.

Now, let's create a version using the LlamaRMSNorm kernel loaded via kernels.

Notable Notes on the KernelModel:.

Industry observers note that how much faster is the optimized Triton RMSNorm kernel compared to the standard PyTorch version? Let’s benchmark the forward pass to find out.

Expected Outcome: As with LayerNorm, a well-tuned RMSNorm implementation using Triton can deliver substantial speedups over PyTorch’s default version—especially for memory-bound workloads on compatible hardware (e.g., NVIDIA Ampere or Hopper GPUs) and with low-precision types like float16 or bfloat16.

Actual results will depend on your hardware and the specific kernel implementation. Here's an example of what you might see (on a L4 GPU):.

The kernels library is still growing but is already being used in various real world projects, including:.

According to recent reports, you've seen how easy it is to fetch and use optimized kernels with the Hugging Face Kernel Hub. Ready to try it yourself?

Ensure you have a compatible PyTorch version and gpu driver installed.

Browse the Hub: Explore available kernels on the Hugging Face Hub under the kernels tag or within organizations like kernels-community. Look for kernels relevant to your operations (activations, attention, normalization like LayerNorm/RMSNorm, etc.).

According to recent reports, experiment: Try replacing components in your own models. Use get_kernel("user-or-org/kernel-name"). Crucially, inspect the loaded kernel object (e.g., print(dir(loaded_kernel))) or check its Hub repository documentation to understand how to correctly call its functions/methods and what parameters (weights, biases, inputs, epsilon) it expects.

According to recent reports, benchmark: Measure the performance impact on your specific hardware and workload. Don't forget to check for numerical correctness (torch.testing.assert_close).

(Advanced) Contribute: If you develop optimized kernels, consider sharing them on the Hub!

According to recent reports, the Hugging Face Kernel Hub provides a powerful yet simple way to access and leverage optimized compute kernels. By replacing standard PyTorch components with optimized versions for operations like RMS Normalization, you can potentially unlock significant performance improvements without the traditional complexities of custom builds. Remember to check the specifics of each kernel on the Hub for correct usage. Give it a try and see how it can accelerate your workflows!

Industry observers note that my implementation is incomplete, and the output results are just noise, but the SDXL pipeline still works up to the output stage even in a Zero GPU space. The replacement seems to be relatively stable.Moreover, the main body of the kernels module is a C++-compiled binary, so it can be installed without the CUDA Toolkit! This is a helpful feature in the Zero GPU environment.If Hugging Face's main libraries gradually adopt the kernels library internally (via an optional switch), we can expect smart performance improvements.The arguments and return values do not match the original library, so a wrapper will likely be necessary.

How do these compare/work with Liger Kernels?

→ Read original article