Skip to content

Add opt-in CPU JIT fallback for missing kernel builds - #920

Closed
Qubitium wants to merge 1 commit into
huggingface:mainfrom
Qubitium:feat/cpu-jit-fallback
Closed

Qubitium wants to merge 1 commit into
huggingface:mainfrom
Qubitium:feat/cpu-jit-fallback

Conversation

@Qubitium

@Qubitium Qubitium commented Oct 21, 2026 •

Copy link
Copy Markdown

Summary

Adds an opt-in CPU JIT fallback to get_kernel. Fresh JIT cache entries are now loaded before any prebuilt/Hub resolution, with a one-hour default TTL. On a cache miss or expiry, normal resolution runs and a compatible prebuilt variant is still preferred when one exists. If no prebuilt exists, jit=True resolves the kernel's source repository and pinned commit from build metadata, builds it against the installed Torch, caches the result, and loads it through the existing local-kernel path.

from kernels import get_kernel

gptq = get_kernel(
    "kernels-community/quantization-gptq",
    version=1,
    backend="cpu",
    jit=True,
)

JITOptions supports an explicit local source tree or GitHub URL for kernels whose metadata predates source recording. JITOptions(cache_ttl=...) controls cache-first reuse: the default is 3600 seconds, None disables expiry, and 0 disables the fast path.

Design

  • A fresh local JIT cache entry is returned before prebuilt/Hub resolution. On a cache miss or expired entry, normal resolution runs; compatible prebuilt variants remain preferred.
  • Each cache entry has a jit.json sidecar recording the requested version/revision, resolved revision, source, source commit/subdir, cache key, and validated_at.
  • A cache hit recomputes the existing cache key against the current Torch/Python/platform/compiler and local source fingerprint, so stale builds are not loaded after an environment or source change.
  • CPU only for now.
  • Remote source is pinned to a full 40-character commit. kernels-community/* repositories without a source field map to their directory in huggingface/kernels-community.
  • Cache key covers source commit/fingerprint, Hub revision, Torch version, Python ABI, platform, and compiler/build flags.
  • Cross-process file lock and atomic variant install prevent concurrent duplicate builds.
  • Source archives reject path traversal, symlinks, hardlinks, and device entries.
  • Free-threaded Python below 3.15 is rejected before building because the current extension ABI is unsafe there.
  • Missing kernel-builder, CMake, Ninja, or compiler produces an actionable error.

Validation

On this host (AMD EPYC 9V33X, torch 2.16.0.dev20261020+cpu, Python 3.14.4):

  • Normal get_kernel("kernels-community/quantization-gptq", version=1, backend="cpu") rejects every prebuilt variant.
  • jit=True builds torch216-cpu-x86_64-linux from pinned source and exposes gemm_int4_forward.
  • Cold build: 22.6 s. Cache hit: 0.171 s after imports, or 1.41 s fresh-process wall time. Expired-cache revalidation took 2.159 s, then returned to 0.171 s. The previous non-cache-first reload was approximately 3.0–3.4 s.
  • Numerical checks against the independent Torch reference: max abs error 0.25, 0.25, and 0.125 for three representative shapes.
  • On torch 2.14.1, jit=True uses the existing prebuilt torch214-cxx11-cpu-x86_64-linux variant and creates no JIT cache entry.
  • Concurrent builds from two processes both succeeded with one cache entry.
  • Explicit local-source build succeeded.
  • 18 passed in tests/test_jit.py; Ruff passes with ruff==0.15.10.

Limitations

  • JIT builds require kernel-builder, CMake >= 3.26, Ninja, and a C++17 compiler.
  • CPU-only in this change.
  • Locally compiled variants are unsigned, so existing signature verification reports them as unverified.
  • Mutable versions may reuse the last resolved commit for up to one hour. Newly published prebuilt variants are discovered after the TTL expires; set cache_ttl=0 to force revalidation on every load.

Validation update

Validated jit=True against all 55 kernel repositories advertising a CPU build at validation time, using Python 3.14.4 and torch 2.16.0.dev20261020+cpu.

Publisher Repositories Result
kernels-community/* 10 10/10 passed; 8 JIT-built, 2 used prebuilt
SZLHOLDINGS/* 14 14/14 passed via compatible prebuilt CPU variants
phanerozoic/* 12 No JIT source metadata available
thyn-ai/* 19 No JIT source/provenance; Mojo-only binaries

JIT-built successfully: activation, flash-attn2, megablocks, quantization-bitsandbytes, quantization-gptq, relu, rmsnorm, and rotary.

In the full sweep, cold builds took approximately 22–26 seconds. Cache hits now take 0.171 seconds after imports or 1.41 seconds fresh-process wall time, down from approximately 3.0–3.4 seconds. einops and gpt-oss-triton-kernels used compatible prebuilt variants and created no JIT cache entries.

Source discovery now considers CPU variants only, avoiding metadata downloads for CUDA/XPU/Metal variants. 18 passed in tests/test_jit.py; Ruff passes with ruff==0.15.10.

JIT requires a reproducible source revision. Kernels publishing only build artifacts cannot be compiled automatically unless source is supplied through JITOptions(source=...). Runtime Python dependencies are not installed automatically; for example, megablocks requires triton at import time (triton==3.8.0 was installed for validation).

Current fork branch: Qubitium/kernels:feat/cpu-jit-fallback at 3e93f57. GitHub's closed PR head remains pinned to the original b67e885, so the follow-up commits are visible on the fork branch rather than in this PR's commit list.

Follow-up commits: e611d5d (CPU-only source discovery), 1d7830d (reproducible-source documentation), and 3e93f57 (TTL cache-first JIT reloads). Relevant paths: kernels/src/kernels/jit.py:53, :84, :147, :389, :591; kernels/tests/test_jit.py:224; kernels/README.md:66.

@github-actions

Copy link
Copy Markdown

Hi @Qubitium, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/huggingface/kernels/blob/main/CONTRIBUTING.md for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant