Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support kernel cache directory with HF_KERNELS_CACHE env var #18

Merged
merged 1 commit into from
Feb 4, 2025
Merged
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
16 changes: 14 additions & 2 deletions src/hf_kernels/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from hf_kernels.compat import tomllib
from hf_kernels.lockfile import KernelLock

CACHE_DIR: Optional[str] = os.environ.get("HF_KERNELS_CACHE", None)


def build_variant():
import torch
Expand Down Expand Up @@ -43,6 +45,7 @@ def install_kernel(repo_id: str, revision: str, local_files_only: bool = False):
repo_path = snapshot_download(
repo_id,
allow_patterns=f"build/{build_variant()}/*",
cache_dir=CACHE_DIR,
revision=revision,
local_files_only=local_files_only,
)
Expand All @@ -55,6 +58,7 @@ def install_kernel_all_variants(
snapshot_download(
repo_id,
allow_patterns="build/*",
cache_dir=CACHE_DIR,
revision=revision,
local_files_only=local_files_only,
)
Expand All @@ -63,7 +67,11 @@ def install_kernel_all_variants(
def get_metadata(repo_id: str, revision: str, local_files_only: bool = False):
with open(
hf_hub_download(
repo_id, "build.toml", revision=revision, local_files_only=local_files_only
repo_id,
"build.toml",
cache_dir=CACHE_DIR,
revision=revision,
local_files_only=local_files_only,
),
"rb",
) as f:
Expand All @@ -83,7 +91,11 @@ def load_kernel(repo_id: str):
raise ValueError(f"Kernel `{repo_id}` is not locked")

filename = hf_hub_download(
repo_id, "build.toml", local_files_only=True, revision=locked_sha
repo_id,
"build.toml",
cache_dir=CACHE_DIR,
local_files_only=True,
revision=locked_sha,
)
with open(filename, "rb") as f:
metadata = tomllib.load(f)
Expand Down