Skip to content

Commit 3533886

Browse files
authored
[kernels] download kernels when users request for it. (#14298)
* download kernels when users request for it. * address review feedback
1 parent a834536 commit 3533886

6 files changed

Lines changed: 44 additions & 4 deletions

File tree

docs/source/en/optimization/attention_backends.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ with attention_backend("_flash_3_hub"):
8282
> [!TIP]
8383
> Most attention backends support `torch.compile` without graph breaks and can be used to further speed up inference.
8484
85+
## Trusting remote kernels
86+
87+
Hub backends and other kernel-backed features (such as [GGUF](../quantization/gguf) and [Nunchaku Lite](../quantization/nunchaku)) download compute kernels from the Hub with [`kernels`](https://github.com/huggingface/kernels) and execute their code locally.
88+
89+
By default, `kernels` only loads a kernel when its publisher is a trusted kernel publisher on the Hub. Kernels published under the [`kernels-community`](https://huggingface.co/kernels-community) organization are trusted, so Diffusers loads them without any additional configuration. The `_flash_3_hub`, `flash_hub`, `sage_hub`, and the other Hub attention backends all resolve to `kernels-community` repositories.
90+
91+
Kernels from any other publisher are not vetted. Loading one downloads and runs code that Diffusers cannot vouch for, so Diffusers keeps it disabled unless you explicitly opt in with the `DIFFUSERS_TRUST_REMOTE_KERNELS` environment variable. When set, Diffusers forwards `trust_remote_code=True` to `kernels` so it loads kernels from untrusted publishers too.
92+
93+
```bash
94+
export DIFFUSERS_TRUST_REMOTE_KERNELS=true
95+
```
96+
97+
Only enable this after inspecting the kernel repository, since it grants the downloaded code the ability to run on your machine. Without it, loading a kernel from an untrusted publisher raises an error. Diffusers performs this check itself, so it also applies to `kernels<0.14.0`, which predates the `trust_remote_code` argument. Setting `DIFFUSERS_DISABLE_REMOTE_CODE=true` disables remote code globally and takes precedence over `DIFFUSERS_TRUST_REMOTE_KERNELS`.
98+
8599
## Checks
86100

87101
The attention dispatcher includes debugging checks that catch common errors before they cause problems.

docs/source/en/quantization/gguf.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pip install -U kernels
6363

6464
Once installed, set `DIFFUSERS_GGUF_CUDA_KERNELS=true` to use optimized kernels when available. Note that CUDA kernels may introduce minor numerical differences compared to the original GGUF implementation, potentially causing subtle visual variations in generated images. To disable CUDA kernel usage, set the environment variable `DIFFUSERS_GGUF_CUDA_KERNELS=false`.
6565

66+
The GGUF kernels are downloaded from the [`Isotr0py/ggml`](https://huggingface.co/Isotr0py/ggml) repository, whose publisher is not a trusted kernel publisher on the Hub. Loading it downloads and executes code from the Hub, so Diffusers requires you to explicitly opt in by setting `DIFFUSERS_TRUST_REMOTE_KERNELS=true`. See [Trusting remote kernels](../optimization/attention_backends#trusting-remote-kernels) for details.
67+
6668
## Supported Quantization Types
6769

6870
- BF16

docs/source/en/quantization/nunchaku.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ The kernels package supplies the optimized CUDA kernels, which load automaticall
2727
pip install -U kernels
2828
```
2929

30+
Nunchaku Lite loads its kernels from the [`rootonchair/nunchaku-lite-kernels`](https://huggingface.co/rootonchair/nunchaku-lite-kernels) repository, whose publisher is not a trusted kernel publisher on the Hub. Loading it downloads and executes code from the Hub, so Diffusers requires you to explicitly opt in by setting `DIFFUSERS_TRUST_REMOTE_KERNELS=true`. See [Trusting remote kernels](../optimization/attention_backends#trusting-remote-kernels) for details.
31+
3032
## Load a quantized pipeline
3133

3234
Load the prequantized pipeline with [`~DiffusionPipeline.from_pretrained`], which reads the quantization

src/diffusers/quantizers/gguf/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import torch
2121
import torch.nn as nn
2222

23-
from ...utils import is_accelerate_available, is_kernels_available
23+
from ...utils import is_accelerate_available, is_kernels_available, is_kernels_version
24+
from ...utils.constants import DIFFUSERS_TRUST_REMOTE_KERNELS
2425

2526

2627
if is_accelerate_available():
@@ -37,7 +38,15 @@
3738
if can_use_cuda_kernels and is_kernels_available():
3839
from kernels import get_kernel
3940

40-
ops = get_kernel("Isotr0py/ggml")
41+
if not DIFFUSERS_TRUST_REMOTE_KERNELS:
42+
raise ValueError(
43+
"`Isotr0py/ggml` is not published by a trusted kernel publisher on the Hub, so loading it downloads "
44+
"and executes remote code. Set `DIFFUSERS_TRUST_REMOTE_KERNELS=true` to allow it, or set "
45+
"`DIFFUSERS_GGUF_CUDA_KERNELS=false` to run without the CUDA kernels."
46+
)
47+
# `kernels<0.14.0` has no `trust_remote_code` argument and executes the downloaded code unconditionally.
48+
trust_kwargs = {"trust_remote_code": True} if is_kernels_version(">=", "0.14.0") else {}
49+
ops = get_kernel("Isotr0py/ggml", **trust_kwargs)
4150
else:
4251
ops = None
4352

src/diffusers/quantizers/nunchaku/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import torch
99
import torch.nn as nn
1010

11-
from ...utils import is_accelerate_available, is_kernels_available
11+
from ...utils import is_accelerate_available, is_kernels_available, is_kernels_version
12+
from ...utils.constants import DIFFUSERS_TRUST_REMOTE_KERNELS
1213

1314

1415
if is_accelerate_available():
@@ -22,7 +23,14 @@
2223
if is_kernels_available():
2324
from kernels import get_kernel
2425

25-
ops = get_kernel(_HF_KERNEL_REPO, version=_HF_KERNEL_VERSION, trust_remote_code=True).ops
26+
if not DIFFUSERS_TRUST_REMOTE_KERNELS:
27+
raise ValueError(
28+
f"`{_HF_KERNEL_REPO}` is not published by a trusted kernel publisher on the Hub, so loading it "
29+
"downloads and executes remote code. Set `DIFFUSERS_TRUST_REMOTE_KERNELS=true` to allow it."
30+
)
31+
# `kernels<0.14.0` has no `trust_remote_code` argument and executes the downloaded code unconditionally.
32+
trust_kwargs = {"trust_remote_code": True} if is_kernels_version(">=", "0.14.0") else {}
33+
ops = get_kernel(_HF_KERNEL_REPO, version=_HF_KERNEL_VERSION, **trust_kwargs).ops
2634
else:
2735
raise ImportError(
2836
"Loading Nunchaku checkpoints requires the Hugging Face `kernels` package. "

src/diffusers/utils/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
HF_ENABLE_PARALLEL_LOADING = os.environ.get("HF_ENABLE_PARALLEL_LOADING", "").upper() in ENV_VARS_TRUE_VALUES
4949
DIFFUSERS_DISABLE_REMOTE_CODE = os.getenv("DIFFUSERS_DISABLE_REMOTE_CODE", "false").upper() in ENV_VARS_TRUE_VALUES
5050
DIFFUSERS_SDNQ_TRANSFORMERS = os.getenv("DIFFUSERS_SDNQ_TRANSFORMERS", "false").upper() in ENV_VARS_TRUE_VALUES
51+
# Kernels published by untrusted publishers execute remote code, so a globally disabled remote code wins over the opt-in.
52+
DIFFUSERS_TRUST_REMOTE_KERNELS = (
53+
os.getenv("DIFFUSERS_TRUST_REMOTE_KERNELS", "false").upper() in ENV_VARS_TRUE_VALUES
54+
and not DIFFUSERS_DISABLE_REMOTE_CODE
55+
)
5156

5257
# Below should be `True` if the current version of `peft` and `transformers` are compatible with
5358
# PEFT backend. Will automatically fall back to PEFT backend if the correct versions of the libraries are

0 commit comments

Comments
 (0)