Commit 0d3cf78
Use HF idiom for variant probing: try-and-recover, cache-first
Replace the ``HfApi().list_repo_files`` probe in ``_variant_available``
with the ``transformers`` / ``huggingface_hub`` idiom: cache lookup via
``try_to_load_from_cache`` first, then ``hf_hub_download`` with
``EntryNotFoundError`` catch.
Resolution order (cheapest first):
1. Local directory: ``Path.exists()`` — unchanged.
2. File in the local HF cache for this repo+revision:
``try_to_load_from_cache`` returns the cached path. **Pure local,
no network.** This is the new fast path — repeat loads of cached
models pay zero API calls.
3. ``local_files_only=True``: return ``None`` (uncertain) without
touching the network.
4. ``hf_hub_download(filename=variant_file)``: success means the file
exists (and is now cached, so the subsequent ``snapshot_download``
reuses it — no double download). ``EntryNotFoundError`` means the
publisher hasn't uploaded a variant.
Net effects:
- **Warm cache** (most common case): zero Hub API round-trips.
Previously paid one ``list_repo_files`` call per load.
- **Cold cache, file present**: equivalent — one HEAD + one GET, vs
one ``list_repo_files`` + one snapshot_download fetch. Same
bytes, fewer distinct calls.
- **Cold cache, file absent**: one HEAD that returns 404 → the
soft-fallback warning fires and the standard download path runs.
Same outward behavior.
Drops the dependency on ``HfApi`` (removed from imports). Adds
defensive catches around ``try_to_load_from_cache`` so a malformed
repo_id (e.g. a non-existent local path that wasn't caught by step 1)
falls through to "uncertain" instead of raising
``HFValidationError``.
Also adds a TODO(strict-variant) comment in ``_resolve_variant``: once
half-precision variant files have been published for the flagship
GLiNER models on the Hub, flip the soft-fallback branch to raise
``EntryNotFoundError`` instead of warning. That matches
``transformers.PreTrainedModel.from_pretrained(variant=...)`` strictness.
The soft-fallback was a transitional choice — at PR-merge time, no
GLiNER repo on the Hub shipped a variant file, so a strict surface
would have been broken-on-arrival for every caller.
Tests pass (88), ruff clean. End-to-end smoke against
``urchade/gliner_medium-v2.1`` confirms ``_variant_available`` returns
``False`` for the missing bf16 variant via ``EntryNotFoundError``,
and the soft-fallback warning still fires from the load path.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 0635381 commit 0d3cf78
1 file changed
Lines changed: 52 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
19 | 24 | | |
20 | 25 | | |
| 26 | + | |
21 | 27 | | |
22 | 28 | | |
23 | 29 | | |
| |||
339 | 345 | | |
340 | 346 | | |
341 | 347 | | |
342 | | - | |
343 | | - | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
344 | 360 | | |
345 | 361 | | |
346 | 362 | | |
347 | | - | |
348 | | - | |
349 | | - | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
350 | 366 | | |
351 | 367 | | |
352 | 368 | | |
| 369 | + | |
353 | 370 | | |
354 | 371 | | |
355 | 372 | | |
356 | 373 | | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
357 | 386 | | |
358 | 387 | | |
359 | 388 | | |
| 389 | + | |
| 390 | + | |
360 | 391 | | |
361 | | - | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
362 | 396 | | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
| 397 | + | |
| 398 | + | |
367 | 399 | | |
368 | | - | |
369 | 400 | | |
370 | 401 | | |
371 | 402 | | |
| |||
392 | 423 | | |
393 | 424 | | |
394 | 425 | | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
395 | 435 | | |
396 | 436 | | |
397 | 437 | | |
| |||
0 commit comments