# Use layers

> [!NOTE] Open the
> [Kernelize your model with ease](https://colab.research.google.com/github/huggingface/kernels/blob/main/examples/cookbook/use_layers.ipynb)
> notebook to build a kernel, replace a layer definition, and use a kernel layer
> with functions.

A kernel can provide layers in addition to kernel functions. A layer from the
Hub can replace the `forward` method of an existing layer for a certain device
type. This makes it possible to provide more performant kernels for existing
layers.

See [Kernel requirements](kernel-requirements) for more information on the
requirements of Hub layers.

## Making a layer extensible with kernels from the hub

### Using a decorator

A layer can be made extensible with the [use_kernel_forward_from_hub()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernel_forward_from_hub)
decorator. For example:

```python
@use_kernel_forward_from_hub("SiluAndMul")
class SiluAndMul(nn.Module):
    def forward(self, input: torch.Tensor) -> torch.Tensor:
        d = input.shape[-1] // 2
        return F.silu(input[..., :d]) * input[..., d:]
```

The decorator does not change the behavior of the class -- it annotates the
class with the given name (here `SiluAndMul`). The [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize)
function described below uses this name to look up kernels for the layer.

Sometimes you only want to kernelize a layer depending on some state inside that
layer. For instance, an MLP layer could support multiple activations, but a
kernel that you want to register only supports one particular activation. In
such cases, you can add a condition to a `use_kernel_forward_from_hub`
decorator. The layer will then only be kernelized when the condition holds. The
condition must be a callable that takes the instantiated layer and returns a
`bool`. For example:

```python
@use_kernel_forward_from_hub(
    "SwiGLUMLP",
    condition=lambda module: module.config.hidden_act == "silu",
)
class MyMLP(nn.Module):
    ...
```

### External layers

An existing layer that does not (yet) have the
[use_kernel_forward_from_hub()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernel_forward_from_hub) decorator can be made extensible using
the [replace_kernel_forward_from_hub()](/docs/kernels/v0.19.0/en/api/layers#kernels.replace_kernel_forward_from_hub) function:

```python
from somelibrary import SiluAndMul

replace_kernel_forward_from_hub(SiluAndMul, "SiluAndMul")
```

**Warning:** we strongly recommend using layers with a decorator, since it
signifies that the maintainer intends to keep the `forward` signature compatible
with layers from the hub.

### Using a function as a layer

> [!WARNING] Decorating functions is mainly intended for compatibility cases,
> such as in `transformers`, where a function cannot be converted into a module.
> If you control the model code, prefer converting the function to an
> `nn.Module` and decorating that class with
> [use_kernel_forward_from_hub()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernel_forward_from_hub), as shown in
> [Using a decorator](#using-a-decorator). In the parent module's `__init__`,
> assign an instance of the decorated class to an attribute, such as
> `self.activation = SiluAndMul()`. PyTorch then registers it as a submodule,
> making it part of the model graph.

When a function cannot be replaced by a layer, you can use the
[use_kernel_forward_from_hub()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernel_forward_from_hub) decorator on the function:

```python
@use_kernel_forward_from_hub("silu_and_mul")
def silu_and_mul(x: torch.Tensor) -> torch.Tensor:
    d = x.shape[-1] // 2
    return F.silu(x[..., :d]) * x[..., d:]
```

This will replace the function by an instantiated `torch.nn.Module` (singleton)
that calls the function itself in its forward method. It will still behave as a
function, since `torch.nn.Module` provides an implementation of `__call__` that
delegates to `forward`.

For [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) to see the function, you must use
[use_kernelized_func()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernelized_func) on an `torch.nn.Module` that is part of the
to-be kernlized model to make the function visible to [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize).
The function is typically attached to the module that uses it. For example:

```python
@use_kernelized_func(silu_and_mul)
class FeedForward(nn.Module):
  def __init__(self, in_features: int, out_features: int):
      self.linear = nn.Linear(in_features, out_features)

  def forward(self, x: torch.Tensor) -> torch.Tensor:
      return silu_and_mul(self.linear(x))
```

Functions used by [use_kernelized_func()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernelized_func) must always have a
[use_kernel_forward_from_hub()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernel_forward_from_hub) decorator.

## Kernelizing a model

A model will not use Hub kernels by default, even if it contains extensible
layers. To enable the use of Hub kernels in the model, it needs to be
'kernelized' using the [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) function. This function traverses
the model graph and replaces the `forward` methods of extensible layers for
which Hub kernels are registered. [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) can be used as follows:

```python
model = MyModel(...)
model = kernelize(model, mode=Mode.INFERENCE)
```

The [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) function modifies the model in-place, the model
itself is returned as a convenience. The `mode` specifies that the model will be
used in inference. Similarly, you can ask [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) to prepare the
model for training:

```python
model = MyModel(...)
model = kernelize(model, mode=Mode.TRAINING)
```

A model that is kernelized for training can also be used for inference, but not
the other way around. If you want to change the mode of the kernelized model,
you can just run [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) on the model again with the new mode.

If you want to compile a model with `torch.compile`, this should be indicated in
the mode as well. You can do this by combining `Mode.INFERENCE` or
`Mode.TRAINING` with `Mode.TORCH_COMPILE` using the set union (`|`) operator:

```python
model = MyModel(...)

# Inference
model = kernelize(model, mode=Mode.INFERENCE | Mode.TORCH_COMPILE)

# Training
model = kernelize(model, mode=Mode.TRAINING | Mode.TORCH_COMPILE)
```

### Kernel device

Kernels can be registered per device type. For instance, separate `cuda` and
`metal` kernels could be registered for the name `SiluAndMul`. By default,
[kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) will try to infer the device type from the model's
parameters. You can pass the device type to [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) if the device
type cannot be inferred (e.g. because the model has no parameters):

```python
model = MyModel(...)
model = kernelize(model, device="cuda", mode=Mode.INFERENCE)
```

### Fallback `forward`

If the `TRAINING` and/or `TORCH_COMPILE` modes are used, but a registered kernel
does not support backward passes or `torch.compile` respectively,
[kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) will fall back to the original, non-kernelized, layer.
You can let [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) raise an exception instead by using
`use_fallback=False`:

```python
model = MyModel(...)
model = kernelize(model, mode=Mode.INFERENCE | Mode.TORCH_COMPILE, use_fallback=False)
```

This can be useful if you want to guarantee that Hub kernels are used.

### Inspecting which kernels are used

The kernels that are used are logged at the `INFO` level by
[kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize). See the
[Python logging](https://docs.python.org/3/library/logging.html) documentation
for information on how to configure logging.

## Registering a hub kernel for a layer

[kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) relies on kernel mappings to find Hub kernels for layers.
Kernel mappings map a kernel name such as `SiluAndMul` to a kernel on the Hub.
For example:

```python
kernel_layer_mapping = {
    "SiluAndMul": {
        "cuda": LayerRepository(
            repo_id="kernels-community/activation",
            layer_name="SiluAndMul",
            version=1,
        ),
        "rocm": LayerRepository(
            repo_id="kernels-community/activation",
            layer_name="SiluAndMul",
            version=1,
        )
    }
}
```

This uses version `1` of the `SiluAndMul` kernel layer from
`kernels-community/activation` for the `cuda` and `rocm` backends. Kernel layers
are versioned using a major version number. Using `version=1` will get the
latest kernel build from the `v1` branch. Kernel layers within a version branch
must never break the API or remove builds for older PyTorch versions. This
ensures that your code will continue to work. Hub-backed
[LayerRepository](/docs/kernels/v0.19.0/en/api/layers#kernels.LayerRepository) and [FuncRepository](/docs/kernels/v0.19.0/en/api/layers#kernels.FuncRepository) entries must
specify either a `version` or an explicit `revision`.

> [!NOTE] Version `0` kernels are excluded from the API compatibility
> requirement, since it is used for alpha/beta-quality kernels that may still
> have rapidly changing APIs.

You can register a mapping, like the one above, using
[register_kernel_mapping()](/docs/kernels/v0.19.0/en/api/layers#kernels.register_kernel_mapping):

```python
register_kernel_mapping(kernel_layer_mapping)
```

This will register the kernel mapping in the current context, which is normally
global. It is recommended to scope the mapping to where it is used with the
[use_kernel_mapping()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernel_mapping) context manager:

```python
with use_kernel_mapping(kernel_layer_mapping):
    # Use the layer for which the mapping is applied.
    model = kernelize(model, mode=Mode.TRAINING | Mode.TORCH_COMPILE)
```

This ensures that the mapping is not active anymore outside the `with`-scope.

### Attributing Hub requests

Libraries and applications can identify the Hub requests made while loading a
layer by passing optional user-agent metadata to [LayerRepository](/docs/kernels/v0.19.0/en/api/layers#kernels.LayerRepository):

```python
layer_repo = LayerRepository(
    repo_id="kernels-community/activation",
    layer_name="SiluAndMul",
    version=1,
    user_agent={"my-library": "1.0.0"},
)
```

`user_agent` accepts either a string or a dictionary. If it is omitted, no
application-specific metadata is added. Setting `HF_HUB_DISABLE_TELEMETRY=1`
disables user-agent telemetry, including metadata supplied this way.

If the layer is stateless (it does not use member variables in its forward _or_
it was originally a function that was converted into a kernel layer with
[use_kernel_func_from_hub()](/docs/kernels/v0.19.0/en/api/layers#kernels.use_kernel_func_from_hub)), it can also be mapped to a kernel
function:

```python
kernel_layer_mapping = {
    "SiluAndMul": {
        "cuda": FuncRepository(
            repo_id="kernels-community/activation",
            func_name="silu_and_mul",
            version=1,
        ),
    }
}
```

### Registering kernels for specific modes

You might want to register two different kernels for a particular layer, where
one kernel is optimized for a specific mode. You can do so by registering layer
repositories for specific modes. For example:

```python
kernel_layer_mapping = {
    "SiluAndMul": {
        "cuda": {
          Mode.INFERENCE: LayerRepository(
              repo_id="kernels-community/activation-inference-optimized",
              layer_name="SiluAndMul",
              version=1,
          ),
          Mode.TRAINING | Mode.TORCH_COMPILE: LayerRepository(
              repo_id="kernels-community/activation-training-optimized",
              layer_name="SiluAndMul",
              version=1,
          ),
      }
    }
}
```

The [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) function will attempt to use the following registered
kernels for a given mode:

- `INFERENCE`: `INFERENCE` → `INFERENCE | TORCH_COMPILE` → `TRAINING` →
  `TRAINING | TORCH_COMPILE` → `FALLBACK`
- `INFERENCE | TORCH_COMPILE`: `INFERENCE | TORCH_COMPILE` →
  `TRAINING | TORCH_COMPILE` → `FALLBACK`
- `TRAINING`: `TRAINING` → `TRAINING | TORCH_COMPILE` → `FALLBACK`
- `TRAINING | TORCH_COMPILE`: `TRAINING | TORCH_COMPILE` → `FALLBACK`

`Mode.FALLBACK` is a special mode that is used when no other mode matches. It is
also used when a kernel is registered without a mode, as described in the
previous section.

```python
kernel_layer_mapping = {
    "SiluAndMul": {
        "cuda": {
            Mode.FALLBACK: LayerRepository(
                repo_id="kernels-community/activation",
                layer_name="SiluAndMul",
                version=1,
            ),
            Mode.INFERENCE: LayerRepository(
                repo_id="kernels-community/activation-inference-optimized",
                layer_name="SiluAndMul",
                version=1,
            ),
            Mode.TRAINING: LayerRepository(
                repo_id="kernels-community/activation-training-optimized",
                layer_name="SiluAndMul",
                version=1,
            ),
        }
    }
}
```

In this case, both `Mode.INFERENCE | Mode.TORCH_COMPILE` and
`Mode.TRAINING | Mode.TORCH_COMPILE` will use the `Mode.FALLBACK` kernel, since
the other kernels do not support `torch.compile`.

### Registering kernels for specific CUDA capabilities

Some kernels only work with newer CUDA architectures. For instance, some kernels
require capability 9.0 for the TMA unit on Hopper GPUs. `kernels` supports
registering layers for a range of CUDA capabilities. To do so, you need to
register the layer for a [Device](/docs/kernels/v0.19.0/en/api/layers#kernels.Device) with type `cuda` and set the
supported range of CUDA capabilities with using `CUDAProperties`:

```python
kernel_layer_mapping = {
    "SiluAndMul": {
        Device(
            type="cuda",
            properties=CUDAProperties(
                min_capability=75, max_capability=89
            ),
        ): LayerRepository(
            repo_id="kernels-community/activation",
            layer_name="SiluAndMul",
            version=1,
        ),
        Device(
            type="cuda",
            properties=CUDAProperties(
                min_capability=90, max_capability=sys.maxsize
            ),
        ): LayerRepository(
            repo_id="kernels-community/activation-hopper",
            layer_name="SiluAndMul",
            version=1,
        ),
    }
}
```

Capabilities behave as follows:

- The minimum and maximum capabilities are inclusive.
- When a new kernel is registered with the same min/max capabilities as an
  existing kernel, the new kernel will replace the old kernel.
- When there are multiple kernels that support a capability, the kernel with the
  smaller capability interval will be used. E.g. given:
  - `KernelA` with `min_capability=80` and `max_capability=89`;
  - `KernelB` with `min_capability=75` and `max_capability=89`;
  - [kernelize()](/docs/kernels/v0.19.0/en/api/layers#kernels.kernelize) runs on a system with capability 8.6.

  Then `KernelA` will be used because the interval 80..89 is smaller than
  75..89. The motivation is that kernels with smaller ranges tend to be more
  optimized for a specific set of GPUs. **This behavior might still change in
  the future.**

### Registering kernels for specific ROCm capabilities

Registering kernels for the ROCm architecture follows the exact same pattern as
CUDA kernels, using `min_capability` and `max_capability` to restrict a kernel
to a range of ROCm capabilities.

### Loading from a local repository for testing

The [LocalLayerRepository](/docs/kernels/v0.19.0/en/api/layers#kernels.LocalLayerRepository) class is provided to load a repository
from a local directory. For example:

```python
with use_kernel_mapping(
    {
        "SiluAndMul": {
            "cuda": LocalLayerRepository(
                repo_path="/home/daniel/kernels/activation",
                package_name="activation",
                layer_name="SiluAndMul",
            )
        }
    },
    inherit_mapping=False,
):
    kernelize(linear, mode=Mode.INFERENCE)
```

Similarly, the [LocalFuncRepository](/docs/kernels/v0.19.0/en/api/layers#kernels.LocalFuncRepository) class can be used to load a
kernel function from a local directory:

```python
with use_kernel_mapping(
    {
        "silu_and_mul": {
            "cuda": LocalFuncRepository(
                repo_path="/home/daniel/kernels/activation",
                package_name="activation",
                func_name="silu_and_mul",
            )
        }
    },
    inherit_mapping=False,
):
    kernelize(model, mode=Mode.INFERENCE)
```

## Inspecting a kernelized model

To check which layers are currently using a kernel, use
[get_kernelized_layers()](/docs/kernels/v0.19.0/en/api/layers#kernels.get_kernelized_layers). It returns a [KernelizedLayers](/docs/kernels/v0.19.0/en/api/layers#kernels.KernelizedLayers)
object that contains a dict `layers` with kernelized layers and a dict `funcs`
with kernelized functions. Each entry holds the kernelized module and the
repository that its kernel was retrieved from:

```python
from kernels import get_kernelized_layers

kernelized = get_kernelized_layers(model)
kernelized.layers
# {'layers.0.mlp.act_fn': KernelizedLayer(module=..., repo=LayerRepository(...))}
kernelized.funcs
# {('layers.0.attn', 'apply_rotary_pos_emb'): KernelizedFunc(module=..., repo=FuncRepository(...))}
```

