Skip to content

feat: make kernel loading work when offline mode ie enabled.#580

Open
sayakpaul wants to merge 2 commits into
mainfrom
offline-kernels
Open

feat: make kernel loading work when offline mode ie enabled.#580
sayakpaul wants to merge 2 commits into
mainfrom
offline-kernels

Conversation

@sayakpaul
Copy link
Copy Markdown
Member

Fixes #553

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@sayakpaul sayakpaul marked this pull request as ready for review May 22, 2026 11:25
get_kernel("kernels-test-untrusted/ci-test-kernel", version=1, trust_remote_code=True)


def test_install_kernel_offline_with_revision(monkeypatch, local_kernel_path):
Copy link
Copy Markdown
Member

@danieldk danieldk May 22, 2026

Choose a reason for hiding this comment

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

Love these tests!

Comment on lines +70 to +72
f"Skipping publisher trust check for '{repo_id}' because Hugging Face "
"Hub is in offline mode. Pass trust_remote_code=True to suppress this "
"warning.",
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
f"Skipping publisher trust check for '{repo_id}' because Hugging Face "
"Hub is in offline mode. Pass trust_remote_code=True to suppress this "
"warning.",
f"Skipping publisher trust check for '{repo_id}' because Hugging Face "
"Hub is in offline mode.",

Just to avoid incentivizing people to use trust_remote_code.

Comment on lines +552 to +572
if constants.HF_HUB_OFFLINE:
# Enumerate variants from the local snapshot rather than the Hub.
try:
local_repo_path = Path(
str(
api.snapshot_download(
repo_id,
repo_type="kernel",
cache_dir=CACHE_DIR,
revision=locked_sha,
local_files_only=True,
)
)
)
except LocalEntryNotFoundError as e:
raise FileNotFoundError(
f"Locked kernel `{repo_id}` was not downloaded. Make sure it's downloaded locally."
) from e
variants = get_variants_local(local_repo_path / "build")
else:
variants = get_variants(api, repo_id=repo_id, revision=locked_sha)
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.

These should actually be the same code path. I think the current implementation si wrong in that it calls get_variants, load_kernel should already be local/offline, but I think the network dependency got accidentally introduced when adding the get_variants code.

So, it should be your code in both cases (constants.HF_HUB_OFFLINE and constants.HF_HUB_OFFLINE).

def load_kernel(
repo_id: str,
*,
lockfile: Path | None,
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 think if we add revision here as well, then install_kernel could just call load_kernel when constants.HF_HUB_OFFLINE is set. So the cases would be:

  • revision=None, lockfile=None -> get lock file from the caller package's metadata and get revision.
  • revision=something, lockfile=None -> use the given revision.
  • revision=None, lockfile=some_path -> get the revision from the lockfile.
  • revision=something, lockfile=some_path -> raise an exception

Comment on lines +265 to +285
# The Hub cannot be reached, so the local snapshot must exist. Locate
# it and enumerate variants from disk rather than calling list_repo_tree.
try:
local_repo_path = Path(
str(
api.snapshot_download(
repo_id,
repo_type="kernel",
cache_dir=CACHE_DIR,
revision=revision,
local_files_only=True,
)
)
)
except LocalEntryNotFoundError as e:
raise FileNotFoundError(
f"Cannot find a local snapshot for {repo_id} (revision: {revision}). "
"When Hugging Face Hub is in offline mode the kernel must already "
"be present in the local cache."
) from e
variants = get_variants_local(local_repo_path / "build")
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.

See below, I think with a small change to load_kernel, we could call out to load_kernel here.

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.

Offline kernel loading fails despite cached snapshot

3 participants