Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
title: Lock kernel versions
- local: env
title: Environment variables
- local: cache
title: Kernels cache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
title: Kernels cache
title: Managing the kernels cache

- local: integrating-kernels
title: Projects using kernels
- local: migration
Expand Down
242 changes: 242 additions & 0 deletions docs/source/cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
# Kernels cache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
# Kernels cache
# Managing the kernels cache


`kernels` downloads a kernel build from the Hub the first time you load it and
then loads it from a local cache. Later loads of the same kernel, revision and
build variant are served from that cache, so the kernel files are downloaded
only once.
Comment on lines +3 to +6

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
`kernels` downloads a kernel build from the Hub the first time you load it and
then loads it from a local cache. Later loads of the same kernel, revision and
build variant are served from that cache, so the kernel files are downloaded
only once.
`kernels` downloads a kernel build from the Hub the first time you load it. After that, loads of the same kernel, revision and build variant come from a local cache.


The kernels cache is a regular Hugging Face Hub cache: kernel repositories live
in the same directory as `models--…` and `datasets--…` repositories, and they
use the same layout (`refs/`, `blobs/`, `snapshots/`). This page describes how
the cache is configured, what is stored in it, and how to use it when the Hub
is not reachable.

## Cache location

The cache directory is resolved in this order, first match wins:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
The cache directory is resolved in this order, first match wins:
`kernels` uses the first of these settings that's set:


| Setting | Cache directory |
| --------------------------- | ---------------------------------------- |
| `KERNELS_CACHE` | `$KERNELS_CACHE` |
| `HF_HUB_CACHE` | `$HF_HUB_CACHE` |
| `HUGGINGFACE_HUB_CACHE` | `$HUGGINGFACE_HUB_CACHE` |
| `HF_HOME` | `$HF_HOME/hub` |
| None of the above | `$XDG_CACHE_HOME/huggingface/hub`, or `~/.cache/huggingface/hub` when `XDG_CACHE_HOME` is not set |

`KERNELS_CACHE` is the only setting that is specific to `kernels`. Set it when
you want kernel builds somewhere other than the cache of your models, for
example on a fast local disk of a GPU machine, on a shared volume, or in a
directory that is baked into a container image:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
directory that is baked into a container image:
directory inside a container image:


```bash
export KERNELS_CACHE=/mnt/fast-disk/kernels
```

When `KERNELS_CACHE` is set, it is used only by `kernels`. Other Hugging Face
tools keep using the Hub cache, so kernels stored in `KERNELS_CACHE` are not
visible to them, and `hf cache` commands do not list them.

A few details on how the setting is read:

- A leading `~` is expanded to your home directory.
- An empty value counts as unset.
Comment on lines +41 to +42

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i could be wrong, but these two bullets are only true in Rust right? In Python, _get_cache_dir() passes the value to snapshot_download and never expands ~. maybe it'd be more accurate to delete these two bullets or replace them with something like:

Use an absolute path. `~` isn't expanded when kernels are downloaded.

- The variable is read on every call, not at import time. You can change
`KERNELS_CACHE` (or `os.environ["KERNELS_CACHE"]`) between two `get_kernel`
calls and the second call uses the new directory.
Comment on lines +43 to +45

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
- The variable is read on every call, not at import time. You can change
`KERNELS_CACHE` (or `os.environ["KERNELS_CACHE"]`) between two `get_kernel`
calls and the second call uses the new directory.
- The variable is read on every call, not at import time. You can change `os.environ["KERNELS_CACHE"]` between two `get_kernel` calls and the second call uses the new directory.


Kernels are downloaded with the same credentials as any other Hub download:
`HF_TOKEN`, or the token written by `hf auth login`. A private kernel
Comment on lines +47 to +48

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Kernels are downloaded with the same credentials as any other Hub download:
`HF_TOKEN`, or the token written by `hf auth login`. A private kernel
`kernels` uses the same token as other Hub downloads, either `HF_TOKEN` or the token saved by `hf auth login`.

repository is cached like any public kernel, so on a shared machine keep the
cache directory readable only by you.

If you use a Hub mirror through `HF_ENDPOINT`, kernels are downloaded from the
mirror; the cache location is unaffected.
Comment on lines +52 to +53

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
If you use a Hub mirror through `HF_ENDPOINT`, kernels are downloaded from the
mirror; the cache location is unaffected.
If you use a Hub mirror through `HF_ENDPOINT`, kernels are downloaded from the
mirror. The cache location is unaffected.


## On-disk layout

Kernel repositories use the `kernels--<org>--<name>` prefix. A cache holding
one version of [`kernels-community/activation`](https://huggingface.co/kernels-community/activation)
looks like this:

```text
<cache-dir>/
├── kernels--kernels-community--activation/
│ ├── refs/
│ │ ├── main
│ │ └── v1
│ ├── blobs/
│ │ └── 9f2c1d… # file contents, addressed by hash
│ └── snapshots/
│ └── 4d1a8e…/ # one directory per downloaded commit
│ └── build/
│ └── torch28-cxx11-cu128-x86_64-linux/
│ ├── metadata.json
│ └── activation/
│ └── __init__.py
└── .verified-kernels/
├── digest/v1/
└── signature/v1/
```

### `snapshots/`

Each downloaded revision gets one snapshot directory, named after its commit.
The directory is a checkout of that commit that only contains the files that
were actually downloaded. Kernels are downloaded by commit, and `kernels` only
fetches the build variant that matches your system (`build/<variant>/*`), so a
snapshot typically holds a single variant even when the repository has many.
Comment on lines +84 to +87

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
The directory is a checkout of that commit that only contains the files that
were actually downloaded. Kernels are downloaded by commit, and `kernels` only
fetches the build variant that matches your system (`build/<variant>/*`), so a
snapshot typically holds a single variant even when the repository has many.
A snapshot contains only the files that were downloaded. `kernels` downloads only the build variant that matches your system (`build/<variant>/*`), so a snapshot usually holds one variant even when the repository has many.


To populate more than one variant, use [`kernels download`](cli-download.md)
with `--all-variants`, which is useful for building a cache that is shared
between machines with different hardware or PyTorch versions.

### `blobs/`

File contents, addressed by their hash. Entries in a snapshot are symlinks into
`blobs/` (copies on systems where symlinks are not available), so two revisions
that share a file also share its blob and the space it takes.
Comment on lines +95 to +97

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
File contents, addressed by their hash. Entries in a snapshot are symlinks into
`blobs/` (copies on systems where symlinks are not available), so two revisions
that share a file also share its blob and the space it takes.
`blobs/` stores file contents, named by their hash. Files in a snapshot are symlinks into `blobs`, or copies on systems without symlinks. Two revisions that share a file store it only once.


### `refs/`

Maps a branch or tag, such as `v1`, to the commit it points at. Since kernels

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Maps a branch or tag, such as `v1`, to the commit it points at. Since kernels
`refs/` maps a branch or tag, such as `v1`, to its commit.

are downloaded by commit, `snapshot_download` would not create these refs on its
own. `kernels` therefore records the ref itself when a kernel is fetched by
version or revision, which is what makes `get_kernel(..., version=1)` resolvable
later without a Hub request (see [offline use](#offline-use)).
Comment on lines +101 to +105

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Maps a branch or tag, such as `v1`, to the commit it points at. Since kernels
are downloaded by commit, `snapshot_download` would not create these refs on its
own. `kernels` therefore records the ref itself when a kernel is fetched by
version or revision, which is what makes `get_kernel(..., version=1)` resolvable
later without a Hub request (see [offline use](#offline-use)).
Maps a branch or tag, such as `v1`, to the commit it points at. `kernels` downloads each kernel by commit, so it writes the ref itself when you load a kernel by version or revision. That's what lets `get_kernel(..., version=1)` work offline later.


Refs are a local mirror of the Hub branches and tags and can be deleted: they
are rewritten the next time the kernel is downloaded while online. Deleting a
ref only breaks resolving that branch or tag from the local cache.
Comment on lines +107 to +109

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Refs are a local mirror of the Hub branches and tags and can be deleted: they
are rewritten the next time the kernel is downloaded while online. Deleting a
ref only breaks resolving that branch or tag from the local cache.
You can delete refs. `kernels` writes them again the next time it downloads the kernel online. Until then, you can't download that branch or tag offline.


### `.verified-kernels/`

Verification receipts. A kernel that has been verified once gets a receipt, so
that its files are not hashed and its signature is not verified again on every
load. Receipts live at the top of the cache directory, not inside a repository,
Comment on lines +113 to +115

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Verification receipts. A kernel that has been verified once gets a receipt, so
that its files are not hashed and its signature is not verified again on every
load. Receipts live at the top of the cache directory, not inside a repository,
`verified-kernels/` stores verification receipts. After a kernel passes verification, `kernels` writes a receipt, so later loads skip hashing its files and checking its signature.

and are keyed by repo ID, commit and build variant. Deleting them is safe; the
kernel is simply verified in full again on the next load. Since receipts are
Comment on lines +116 to +117

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
and are keyed by repo ID, commit and build variant. Deleting them is safe; the
kernel is simply verified in full again on the next load. Since receipts are
and are keyed by repo ID, commit and build variant. You can delete them safely. The kernel is then verified in full on its next load.

stored inside the cache directory, pointing `KERNELS_CACHE` at a different
directory also means the kernels there are verified again. See
[Kernels security](security.md) for what is verified and what the receipts
attest.

## Inspecting the cache

[`kernels cache ls`](cli-cache.md) lists the cached kernel versions with their
build variants, sizes and access times:

```bash
kernels cache ls
```

```text
ID SIZE LAST_ACCESSED LAST_MODIFIED VERSION VARIANTS
----------------------------------- ---- -------------- -------------- ------- --------------------------------
kernel/kernels-community/activation 2.5M 45 minutes ago 45 minutes ago v1 torch28-cxx11-cu126-x86_64-linux
torch28-cxx11-cu128-x86_64-linux
```

Use `--json` for machine-readable output, `--cache-dir` to inspect a cache other
than the one currently configured, and `--no-truncate` to see full values when
the table is shortened to fit your terminal. If the cache contains
inconsistencies, the command prints a short summary; `--show-warnings` shows the
details. The command works on the local cache only and does not download or load
kernel code.

## Offline use

Set `HF_HUB_OFFLINE=1` to make `kernels` resolve everything from the local
cache, without Hub requests:
Comment on lines +148 to +149

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Set `HF_HUB_OFFLINE=1` to make `kernels` resolve everything from the local
cache, without Hub requests:
Set `HF_HUB_OFFLINE=1` to make `kernels` resolve everything from the local
cache, without Hub requests. `HF_HUB_OFFLINE` is read when `huggingface_hub` is imported, so set it before you start Python.


```bash
export HF_HUB_OFFLINE=1
```

In offline mode, kernel versions and revisions are resolved from `refs/`, and
the build variant is taken from the local snapshot. The kernel must already be
Comment on lines +155 to +156

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
In offline mode, kernel versions and revisions are resolved from `refs/`, and
the build variant is taken from the local snapshot. The kernel must already be
In offline mode, `kernels` looks up versions and revisions in `refs/` and picks the build variant from the local snapshot. The kernel must already be

in the cache. The simplest way to populate it is to load the kernel once while

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
in the cache. The simplest way to populate it is to load the kernel once while
in the cache. To add it to the cache, load the kernel once while

online:

```python
from kernels import get_kernel

get_kernel("kernels-community/activation", version=1)
```

This writes the `v1` ref as well, so that `version=1` can be resolved offline
later. If your project [locks its kernels](locking.md), run
[`kernels download`](cli-download.md) instead:

```bash
kernels download .
```

A locked project needs no refs offline, since the commit comes from
`kernels.lock`. Add `--all-variants` to cache every build variant rather than

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the --all-variants is mentioned earlier so it may be better to link to it from here instead of explaining again

only the one for the current system, which is what you want when the cache is
shared with machines that have a different GPU or PyTorch version.

If the kernel is not cached, loading fails with a clear message. For a version:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
If the kernel is not cached, loading fails with a clear message. For a version:
If the kernel is not cached, loading fails with an error.


```text
Version 1 of 'kernels-community/activation' is not available in the local cache
and Hugging Face Hub is in offline mode. Download the kernel while online first,
or pass an explicit `revision=<commit>`.
```

and for a branch or tag:

```text
Cannot resolve revision 'v1' of 'kernels-community/activation' to a commit: the
ref is not in the local cache and Hugging Face Hub is in offline mode. Download
the kernel while online first, or pass an explicit `revision=<commit>`.
```

Passing an explicit commit with `revision=<commit>`, or loading a locked kernel,
needs no ref lookup, so it works offline as long as that commit's snapshot is
cached.

> [!NOTE]
> The publisher trust check cannot be performed offline. When loading from the
> cache in offline mode, `kernels` skips the check and logs a warning. Trust was
> established when the kernel was downloaded.
To bake a cache into an image, populate it during the build and point

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this can probably be its own section

Suggested change
To bake a cache into an image, populate it during the build and point
### Download kernels into a container image
To ship kernels in a container image, download them during the build and set

`KERNELS_CACHE` (or `HF_HOME`) at the resulting directory at runtime:

```dockerfile
ENV KERNELS_CACHE=/opt/kernels-cache
RUN python -c 'from kernels import get_kernel; get_kernel("kernels-community/activation", version=1)'
ENV HF_HUB_OFFLINE=1
```

The Python API can also be kept offline-only per call, without the environment
variable, with `install_kernel`:
Comment on lines +213 to +214

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
The Python API can also be kept offline-only per call, without the environment
variable, with `install_kernel`:
To read from the cache for a single call without setting `HF_HUB_OFFLINE`, pass `local_files_only=True` to `install_kernel`:


```python
from kernels import install_kernel

variant_path = install_kernel("kernels-community/activation", version=1, local_files_only=True)
```

## Cleaning up

- Removing a `kernels--…` directory from the cache is safe. The kernel is
downloaded again the next time it is loaded, so this is the way to reclaim
space or to force a fresh download.
Comment on lines +224 to +226

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
- Removing a `kernels--…` directory from the cache is safe. The kernel is
downloaded again the next time it is loaded, so this is the way to reclaim
space or to force a fresh download.
- Removing a `kernels--…` directory from the cache is safe. Delete a `kernels--...` directory to free space or force a fresh download. `kernels` downloads the kernel again the next time you load it.

- Removing `.verified-kernels/` only costs re-verification on the next load.
- When copying or moving a cache, preserve symlinks (`cp -a`, `tar`, `rsync -a`)
to keep blobs deduplicated. Copying with symlinks resolved also works, but
stores every file once per snapshot.
- The cache is trusted input. Editing files in place is not supported: a
modified file makes digest verification fail the next time the kernel is
verified in full.
Comment on lines +231 to +233

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
- The cache is trusted input. Editing files in place is not supported: a
modified file makes digest verification fail the next time the kernel is
verified in full.
- Don't edit cached files. A modified file fails digest verification the next time the kernel is verified in full.


## See also

- [Environment variables](env.md) - `KERNELS_CACHE`, `HF_HUB_OFFLINE` and other settings
- [`kernels cache ls`](cli-cache.md) - List cached kernels, versions and variants
- [`kernels download`](cli-download.md) - Pre-populate the cache for locked kernels
- [Lock kernel versions](locking.md) - Pin commits so no version lookup is needed
- [Kernels security](security.md) - Verification and receipts
- [Hugging Face Hub cache reference](https://huggingface.co/docs/huggingface_hub/package_reference/cache) - The cache structures that kernels reuse
1 change: 1 addition & 0 deletions docs/source/cli-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ version-level values describe just the corresponding snapshot.

## See also

- [Kernels cache](cache.md) - Cache configuration, on-disk layout, and offline use
- [kernels variants](cli-variants.md) - Inspect build variants available on the Hub
- [kernels download](cli-download.md) - Download locked kernels
- [Environment variables](env.md) - Configure cache locations
9 changes: 9 additions & 0 deletions docs/source/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
The directory to use as the local kernel cache. If not set, the cache
of the `huggingface_hub` package is used.

See [Kernels cache](cache.md) for the full lookup order, the cache layout, and
how to inspect or pre-populate the cache.

## `HF_HUB_OFFLINE`

Set to `1` to resolve kernels from the local cache only, without requests to
the Hub. The kernel must already be in the cache. See
[offline use](cache.md#offline-use).

## `DISABLE_KERNEL_MAPPING`

Disables kernel mappings for [`layers`](layers.md).
Expand Down
3 changes: 2 additions & 1 deletion docs/source/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ Both checks can be performed on demand with the
To avoid the high cost of verification, a kernel from the Hub is only verified
in full once. Upon successful verification, we write a receipt file to the
kernels cache. Signature and digest verification each have their own receipts,
stored in `.verified-kernels/signature` and `.verified-kernels/digest`.
stored in `.verified-kernels/signature` and `.verified-kernels/digest` at the
top of the [kernels cache](cache.md).

When a digest receipt is found on a later load, the kernel files are not hashed
again. When a signature receipt is found, the signature is not verified again.
Expand Down
Loading