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

fix: use cache for snapshots even if refs does not exist #1306

Merged
merged 2 commits into from
Jan 24, 2023
Merged
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
29 changes: 18 additions & 11 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,25 +1345,32 @@ def try_to_load_from_cache(
if not os.path.isdir(repo_cache):
# No cache for this model
return None
for subfolder in ["refs", "snapshots"]:
if not os.path.isdir(os.path.join(repo_cache, subfolder)):
return None

# Resolve refs (for instance to convert main to the associated commit sha)
cached_refs = os.listdir(os.path.join(repo_cache, "refs"))
if revision in cached_refs:
with open(os.path.join(repo_cache, "refs", revision)) as f:
revision = f.read()
refs_dir = os.path.join(repo_cache, "refs")
snapshots_dir = os.path.join(repo_cache, "snapshots")
no_exists_dir = os.path.join(repo_cache, ".no_exist")

if os.path.isfile(os.path.join(repo_cache, ".no_exist", revision, filename)):
# Resolve refs (for instance to convert main to the associated commit sha)
if os.path.isdir(refs_dir):
cached_refs = os.listdir(refs_dir)
if revision in cached_refs:
with open(os.path.join(refs_dir, revision)) as f:
revision = f.read()

# Check if file is cached as "no_exists"
if os.path.isfile(os.path.join(no_exists_dir, revision, filename)):
return _CACHED_NO_EXIST

cached_shas = os.listdir(os.path.join(repo_cache, "snapshots"))
# Check if revision folder exists
if not os.path.exists(snapshots_dir):
return None
cached_shas = os.listdir(snapshots_dir)
if revision not in cached_shas:
# No cache for this revision and we won't try to return a random revision
return None

cached_file = os.path.join(repo_cache, "snapshots", revision, filename)
# Check if file exists in cache
cached_file = os.path.join(snapshots_dir, revision, filename)
return cached_file if os.path.isfile(cached_file) else None


Expand Down
49 changes: 48 additions & 1 deletion tests/test_file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def test_hf_hub_download_legacy(self):
metadata = filename_to_url(filepath, legacy_cache_layout=True)
self.assertEqual(metadata[1], f'"{DUMMY_MODEL_ID_PINNED_SHA1}"')

def test_try_to_load_from_cache(self):
def test_try_to_load_from_cache_exist(self):
# Make sure the file is cached
filepath = hf_hub_download(DUMMY_MODEL_ID, filename=CONFIG_NAME)

Expand Down Expand Up @@ -389,6 +389,53 @@ def test_try_to_load_from_cache_no_exist(self):
# If file non-existence is not cached, returns None
self.assertIsNone(try_to_load_from_cache(DUMMY_MODEL_ID, filename="dummy2"))

def test_try_to_load_from_cache_specific_commit_id_exist(self):
"""Regression test for #1306.

See https://github.com/huggingface/huggingface_hub/pull/1306."""
with SoftTemporaryDirectory() as cache_dir:
# Cache file from specific commit id (no "refs/"" folder)
commit_id = HfApi().model_info(DUMMY_MODEL_ID).sha
filepath = hf_hub_download(
DUMMY_MODEL_ID,
filename=CONFIG_NAME,
revision=commit_id,
cache_dir=cache_dir,
)

# Must be able to retrieve it "offline"
attempt = try_to_load_from_cache(
DUMMY_MODEL_ID,
filename=CONFIG_NAME,
revision=commit_id,
cache_dir=cache_dir,
)
self.assertEqual(filepath, attempt)

def test_try_to_load_from_cache_specific_commit_id_no_exist(self):
"""Regression test for #1306.

See https://github.com/huggingface/huggingface_hub/pull/1306."""
with SoftTemporaryDirectory() as cache_dir:
# Cache file from specific commit id (no "refs/"" folder)
commit_id = HfApi().model_info(DUMMY_MODEL_ID).sha
with self.assertRaises(EntryNotFoundError):
hf_hub_download(
DUMMY_MODEL_ID,
filename="missing_file",
revision=commit_id,
cache_dir=cache_dir,
)

# Must be able to retrieve it "offline"
attempt = try_to_load_from_cache(
DUMMY_MODEL_ID,
filename="missing_file",
revision=commit_id,
cache_dir=cache_dir,
)
self.assertEqual(attempt, _CACHED_NO_EXIST)

def test_get_hf_file_metadata_basic(self) -> None:
"""Test getting metadata from a file on the Hub."""
url = hf_hub_url(
Expand Down