Skip to content
IJK

Custom Kernels, Flash Attention and the Packaging Problem

By IJK ·

What a Hugging Face post says custom kernels are for, how it describes Flash Attention and why shipping many kernel libraries is a problem.

Custom kernels for frequent combinations

In addition, the community has created custom kernels for frequent combinations of operations, not just individual PyTorch ops like matmul. [1]

Flash Attention as an example

For example, Flash Attention was created to optimize the critical attention block that defines the transformers architecture, and is present in many models including most LLMs. [2]

By carefully combining all the attention operations inside a single kernel, memory transfers are minimized, memory use is reduced, and speedups can be achieved. [3]

for examplebutCustom kernelsfor frequent combinations of operationsFlash Attentioncombines the attention operations inside a single...Separate librariesdependency bloat and a build system per kernel li...
Why is packaging kernels hard?

Why packaging kernels is hard

The problem is that all these various kernels are available in separate libraries, which creates a dependency bloat if they were to be added to the transformers library. [4]

Furthermore, these kernels are not just Python code, they consist of low-level cuda code, glued together with C++ and exposed through a Python layer. [5]

This means they have to be compiled in the target system, which in turn requires whatever build system is required by each kernel library. [6]

Sources

  1. The Hugging Face source says: In addition, the community has created custom kernels for frequent combinations of operations, not just individual PyTorch ops like matmul. Tricks from OpenAI gpt-oss you can use with transformers
  2. The Hugging Face source says: For example, Flash Attention was created to optimize the critical attention block that defines the transformers architecture, and is present in many models including most LLMs. Tricks from OpenAI gpt-oss you can use with transformers
  3. The Hugging Face source says: By carefully combining all the attention operations inside a single kernel, memory transfers are minimized, memory use is reduced, and speedups can be achieved. Tricks from OpenAI gpt-oss you can use with transformers
  4. The Hugging Face source says: The problem is that all these various kernels are available in separate libraries, which creates a dependency bloat if they were to be added to the transformers library. Tricks from OpenAI gpt-oss you can use with transformers
  5. The Hugging Face source says: Furthermore, these kernels are not just Python code, they consist of low-level cuda code, glued together with C++ and exposed through a Python layer. Tricks from OpenAI gpt-oss you can use with transformers
  6. The Hugging Face source says: This means they have to be compiled in the target system, which in turn requires whatever build system is required by each kernel library. Tricks from OpenAI gpt-oss you can use with transformers

Editorial (AI-assisted; automatically checked, not human-reviewed). Each statement is linked to its source; check the source before relying on a detail.

  • kernels
  • flash attention
  • packaging
  • hugging face