| import os |
| import tempfile |
| import time |
| import unittest |
| from io import BytesIO |
|
|
| from huggingface_hub import ( |
| HfApi, |
| create_repo, |
| delete_repo, |
| hf_hub_download, |
| snapshot_download, |
| upload_file, |
| ) |
| from huggingface_hub.utils import logging |
|
|
| from .testing_constants import ENDPOINT_STAGING, TOKEN, USER |
| from .testing_utils import repo_name, with_production_testing |
|
|
|
|
| logger = logging.get_logger(__name__) |
| MODEL_IDENTIFIER = "hf-internal-testing/hfh-cache-layout" |
|
|
|
|
| def get_file_contents(path): |
| with open(path) as f: |
| content = f.read() |
|
|
| return content |
|
|
|
|
| @with_production_testing |
| class CacheFileLayoutHfHubDownload(unittest.TestCase): |
| def test_file_downloaded_in_cache(self): |
| with tempfile.TemporaryDirectory() as cache: |
| hf_hub_download(MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache) |
|
|
| expected_directory_name = f'models--{MODEL_IDENTIFIER.replace("/", "--")}' |
| expected_path = os.path.join(cache, expected_directory_name) |
|
|
| refs = os.listdir(os.path.join(expected_path, "refs")) |
| snapshots = os.listdir(os.path.join(expected_path, "snapshots")) |
|
|
| expected_reference = "main" |
|
|
| |
| self.assertListEqual(refs, [expected_reference]) |
|
|
| with open(os.path.join(expected_path, "refs", expected_reference)) as f: |
| snapshot_name = f.readline().strip() |
|
|
| |
| self.assertListEqual(snapshots, [snapshot_name]) |
|
|
| snapshot_path = os.path.join(expected_path, "snapshots", snapshot_name) |
| snapshot_content = os.listdir(snapshot_path) |
|
|
| |
| self.assertEqual(len(snapshot_content), 1) |
|
|
| snapshot_content_path = os.path.join(snapshot_path, snapshot_content[0]) |
|
|
| |
| self.assertTrue(os.path.islink(snapshot_content_path)) |
|
|
| resolved_blob_relative = os.readlink(snapshot_content_path) |
| resolved_blob_absolute = os.path.normpath( |
| os.path.join(snapshot_path, resolved_blob_relative) |
| ) |
|
|
| with open(resolved_blob_absolute) as f: |
| blob_contents = f.read().strip() |
|
|
| |
| self.assertEqual(blob_contents, "File 0") |
|
|
| def test_file_downloaded_in_cache_with_revision(self): |
| with tempfile.TemporaryDirectory() as cache: |
| hf_hub_download( |
| MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache, revision="file-2" |
| ) |
|
|
| expected_directory_name = f'models--{MODEL_IDENTIFIER.replace("/", "--")}' |
| expected_path = os.path.join(cache, expected_directory_name) |
|
|
| refs = os.listdir(os.path.join(expected_path, "refs")) |
| snapshots = os.listdir(os.path.join(expected_path, "snapshots")) |
|
|
| expected_reference = "file-2" |
|
|
| |
| self.assertListEqual(refs, [expected_reference]) |
|
|
| with open(os.path.join(expected_path, "refs", expected_reference)) as f: |
| snapshot_name = f.read().strip() |
|
|
| |
| self.assertListEqual(snapshots, [snapshot_name]) |
|
|
| snapshot_path = os.path.join(expected_path, "snapshots", snapshot_name) |
| snapshot_content = os.listdir(snapshot_path) |
|
|
| |
| self.assertEqual(len(snapshot_content), 1) |
|
|
| snapshot_content_path = os.path.join(snapshot_path, snapshot_content[0]) |
|
|
| |
| self.assertTrue(os.path.islink(snapshot_content_path)) |
|
|
| resolved_blob_relative = os.readlink(snapshot_content_path) |
| resolved_blob_absolute = os.path.normpath( |
| os.path.join(snapshot_path, resolved_blob_relative) |
| ) |
|
|
| with open(resolved_blob_absolute) as f: |
| blob_contents = f.readline().strip() |
|
|
| |
| self.assertEqual(blob_contents, "File 0") |
|
|
| def test_file_download_happens_once(self): |
| |
|
|
| with tempfile.TemporaryDirectory() as cache: |
| path = hf_hub_download(MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache) |
| creation_time_0 = os.path.getmtime(path) |
|
|
| time.sleep(2) |
|
|
| path = hf_hub_download(MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache) |
| creation_time_1 = os.path.getmtime(path) |
|
|
| self.assertEqual(creation_time_0, creation_time_1) |
|
|
| def test_file_download_happens_once_intra_revision(self): |
| |
|
|
| with tempfile.TemporaryDirectory() as cache: |
| path = hf_hub_download(MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache) |
| creation_time_0 = os.path.getmtime(path) |
|
|
| time.sleep(2) |
|
|
| path = hf_hub_download( |
| MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache, revision="file-2" |
| ) |
| creation_time_1 = os.path.getmtime(path) |
|
|
| self.assertEqual(creation_time_0, creation_time_1) |
|
|
| def test_multiple_refs_for_same_file(self): |
| with tempfile.TemporaryDirectory() as cache: |
| hf_hub_download(MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache) |
| hf_hub_download( |
| MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache, revision="file-2" |
| ) |
|
|
| expected_directory_name = f'models--{MODEL_IDENTIFIER.replace("/", "--")}' |
| expected_path = os.path.join(cache, expected_directory_name) |
|
|
| refs = os.listdir(os.path.join(expected_path, "refs")) |
| refs.sort() |
|
|
| snapshots = os.listdir(os.path.join(expected_path, "snapshots")) |
| snapshots.sort() |
|
|
| |
| self.assertListEqual(refs, ["file-2", "main"]) |
|
|
| refs_contents = [ |
| get_file_contents(os.path.join(expected_path, "refs", f)) for f in refs |
| ] |
| refs_contents.sort() |
|
|
| |
| self.assertListEqual(refs_contents, snapshots) |
|
|
| snapshot_links = [ |
| os.readlink( |
| os.path.join(expected_path, "snapshots", filename, "file_0.txt") |
| ) |
| for filename in snapshots |
| ] |
|
|
| |
| self.assertEqual(*snapshot_links) |
|
|
|
|
| @with_production_testing |
| class CacheFileLayoutSnapshotDownload(unittest.TestCase): |
| def test_file_downloaded_in_cache(self): |
| with tempfile.TemporaryDirectory() as cache: |
| snapshot_download(MODEL_IDENTIFIER, cache_dir=cache) |
|
|
| expected_directory_name = f'models--{MODEL_IDENTIFIER.replace("/", "--")}' |
| expected_path = os.path.join(cache, expected_directory_name) |
|
|
| refs = os.listdir(os.path.join(expected_path, "refs")) |
|
|
| snapshots = os.listdir(os.path.join(expected_path, "snapshots")) |
| snapshots.sort() |
|
|
| |
| self.assertListEqual(refs, ["main"]) |
|
|
| ref_content = get_file_contents( |
| os.path.join(expected_path, "refs", refs[0]) |
| ) |
|
|
| |
| self.assertListEqual([ref_content], snapshots) |
|
|
| snapshot_path = os.path.join(expected_path, "snapshots", snapshots[0]) |
|
|
| files_in_snapshot = os.listdir(snapshot_path) |
|
|
| snapshot_links = [ |
| os.readlink(os.path.join(snapshot_path, filename)) |
| for filename in files_in_snapshot |
| ] |
|
|
| resolved_snapshot_links = [ |
| os.path.normpath(os.path.join(snapshot_path, link)) |
| for link in snapshot_links |
| ] |
|
|
| self.assertTrue(all([os.path.isfile(l) for l in resolved_snapshot_links])) |
|
|
| def test_file_downloaded_in_cache_several_revisions(self): |
| with tempfile.TemporaryDirectory() as cache: |
| snapshot_download(MODEL_IDENTIFIER, cache_dir=cache, revision="file-3") |
| snapshot_download(MODEL_IDENTIFIER, cache_dir=cache, revision="file-2") |
|
|
| expected_directory_name = f'models--{MODEL_IDENTIFIER.replace("/", "--")}' |
| expected_path = os.path.join(cache, expected_directory_name) |
|
|
| refs = os.listdir(os.path.join(expected_path, "refs")) |
| refs.sort() |
|
|
| snapshots = os.listdir(os.path.join(expected_path, "snapshots")) |
| snapshots.sort() |
|
|
| |
| self.assertListEqual(refs, ["file-2", "file-3"]) |
|
|
| refs_content = [ |
| get_file_contents(os.path.join(expected_path, "refs", ref)) |
| for ref in refs |
| ] |
| refs_content.sort() |
|
|
| |
| self.assertListEqual(refs_content, snapshots) |
|
|
| snapshots_paths = [ |
| os.path.join(expected_path, "snapshots", s) for s in snapshots |
| ] |
|
|
| files_in_snapshots = {s: os.listdir(s) for s in snapshots_paths} |
| links_in_snapshots = { |
| k: [os.readlink(os.path.join(k, _v)) for _v in v] |
| for k, v in files_in_snapshots.items() |
| } |
|
|
| resolved_snapshots_links = { |
| k: [os.path.normpath(os.path.join(k, link)) for link in v] |
| for k, v in links_in_snapshots.items() |
| } |
|
|
| all_links = [b for a in resolved_snapshots_links.values() for b in a] |
| all_unique_links = set(all_links) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| self.assertEqual(len(all_links), 8) |
|
|
| |
| self.assertEqual(len(all_unique_links), 5) |
|
|
|
|
| class ReferenceUpdates(unittest.TestCase): |
| _api = HfApi(endpoint=ENDPOINT_STAGING) |
|
|
| @classmethod |
| def setUpClass(cls): |
| """ |
| Share this valid token in all tests below. |
| """ |
| cls._token = TOKEN |
| cls._api.set_access_token(TOKEN) |
|
|
| def test_update_reference(self): |
| repo_id = f"{USER}/{repo_name()}" |
| create_repo(repo_id, token=self._token, exist_ok=True) |
|
|
| try: |
| upload_file( |
| path_or_fileobj=BytesIO(b"Some string"), |
| path_in_repo="file.txt", |
| repo_id=repo_id, |
| token=self._token, |
| ) |
|
|
| with tempfile.TemporaryDirectory() as cache: |
| hf_hub_download(repo_id, "file.txt", cache_dir=cache) |
|
|
| expected_directory_name = f'models--{repo_id.replace("/", "--")}' |
| expected_path = os.path.join(cache, expected_directory_name) |
|
|
| refs = os.listdir(os.path.join(expected_path, "refs")) |
|
|
| |
| self.assertListEqual(refs, ["main"]) |
|
|
| initial_ref_content = get_file_contents( |
| os.path.join(expected_path, "refs", refs[0]) |
| ) |
|
|
| |
| upload_file( |
| path_or_fileobj=BytesIO(b"Some new string"), |
| path_in_repo="file.txt", |
| repo_id=repo_id, |
| token=self._token, |
| ) |
|
|
| hf_hub_download(repo_id, "file.txt", cache_dir=cache) |
|
|
| final_ref_content = get_file_contents( |
| os.path.join(expected_path, "refs", refs[0]) |
| ) |
|
|
| |
| |
| self.assertNotEqual(initial_ref_content, final_ref_content) |
| self.assertTrue( |
| os.path.isdir( |
| os.path.join(expected_path, "snapshots", initial_ref_content) |
| ) |
| ) |
| self.assertTrue( |
| os.path.isfile( |
| os.path.join( |
| expected_path, "snapshots", initial_ref_content, "file.txt" |
| ) |
| ) |
| ) |
| self.assertTrue( |
| os.path.isdir( |
| os.path.join(expected_path, "snapshots", final_ref_content) |
| ) |
| ) |
| self.assertTrue( |
| os.path.isfile( |
| os.path.join( |
| expected_path, "snapshots", final_ref_content, "file.txt" |
| ) |
| ) |
| ) |
| except Exception: |
| raise |
| finally: |
| delete_repo(repo_id, token=self._token) |
|
|