File size: 15,052 Bytes
abeebae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | 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"
# Only reference should be `main`.
self.assertListEqual(refs, [expected_reference])
with open(os.path.join(expected_path, "refs", expected_reference)) as f:
snapshot_name = f.readline().strip()
# The `main` reference should point to the only snapshot we have downloaded
self.assertListEqual(snapshots, [snapshot_name])
snapshot_path = os.path.join(expected_path, "snapshots", snapshot_name)
snapshot_content = os.listdir(snapshot_path)
# Only a single file in the snapshot
self.assertEqual(len(snapshot_content), 1)
snapshot_content_path = os.path.join(snapshot_path, snapshot_content[0])
# The snapshot content should link to a blob
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()
# The contents of the file should be 'File 0'.
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"
# Only reference should be `file-2`.
self.assertListEqual(refs, [expected_reference])
with open(os.path.join(expected_path, "refs", expected_reference)) as f:
snapshot_name = f.read().strip()
# The `main` reference should point to the only snapshot we have downloaded
self.assertListEqual(snapshots, [snapshot_name])
snapshot_path = os.path.join(expected_path, "snapshots", snapshot_name)
snapshot_content = os.listdir(snapshot_path)
# Only a single file in the snapshot
self.assertEqual(len(snapshot_content), 1)
snapshot_content_path = os.path.join(snapshot_path, snapshot_content[0])
# The snapshot content should link to a blob
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()
# The contents of the file should be 'File 0'.
self.assertEqual(blob_contents, "File 0")
def test_file_download_happens_once(self):
# Tests that a file is only downloaded once if it's not updated.
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):
# Tests that a file is only downloaded once if it's not updated, even across different revisions.
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()
# Directory should contain two revisions
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()
# snapshots directory should contain two snapshots
self.assertListEqual(refs_contents, snapshots)
snapshot_links = [
os.readlink(
os.path.join(expected_path, "snapshots", filename, "file_0.txt")
)
for filename in snapshots
]
# All snapshot links should point to the same file.
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()
# Directory should contain two revisions
self.assertListEqual(refs, ["main"])
ref_content = get_file_contents(
os.path.join(expected_path, "refs", refs[0])
)
# snapshots directory should contain two snapshots
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()
# Directory should contain two revisions
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()
# snapshots directory should contain two snapshots
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)
# [ 100] .
# βββ [ 140] blobs
# β βββ [ 7] 4475433e279a71203927cbe80125208a3b5db560
# β βββ [ 7] 50fcd26d6ce3000f9d5f12904e80eccdc5685dd1
# β βββ [ 7] 80146afc836c60e70ba67933fec439ab05b478f6
# β βββ [ 7] 8cf9e18f080becb674b31c21642538269fe886a4
# β βββ [1.1K] ac481c8eb05e4d2496fbe076a38a7b4835dd733d
# βββ [ 80] refs
# β βββ [ 40] file-2
# β βββ [ 40] file-3
# βββ [ 80] snapshots
# βββ [ 120] 5e23cb3ae7f904919a442e1b27dcddae6c6bc292
# β βββ [ 52] file_0.txt -> ../../blobs/80146afc836c60e70ba67933fec439ab05b478f6
# β βββ [ 52] file_1.txt -> ../../blobs/50fcd26d6ce3000f9d5f12904e80eccdc5685dd1
# β βββ [ 52] file_2.txt -> ../../blobs/4475433e279a71203927cbe80125208a3b5db560
# β βββ [ 52] .gitattributes -> ../../blobs/ac481c8eb05e4d2496fbe076a38a7b4835dd733d
# βββ [ 120] 78aa2ebdb60bba086496a8792ba506e58e587b4c
# βββ [ 52] file_0.txt -> ../../blobs/80146afc836c60e70ba67933fec439ab05b478f6
# βββ [ 52] file_1.txt -> ../../blobs/50fcd26d6ce3000f9d5f12904e80eccdc5685dd1
# βββ [ 52] file_3.txt -> ../../blobs/8cf9e18f080becb674b31c21642538269fe886a4
# βββ [ 52] .gitattributes -> ../../blobs/ac481c8eb05e4d2496fbe076a38a7b4835dd733d
# Across the two revisions, there should be 8 total links
self.assertEqual(len(all_links), 8)
# Across the two revisions, there should only be 5 unique files.
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"))
# Directory should contain two revisions
self.assertListEqual(refs, ["main"])
initial_ref_content = get_file_contents(
os.path.join(expected_path, "refs", refs[0])
)
# Upload a new file on the same branch
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])
)
# The `main` reference should point to two different, but existing snapshots which contain
# a 'file.txt'
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)
|