Repository navigation
Fetch a sentence-transformers model's subfolder weights instead of pruning them - #7855
Conversation
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e673ecbbf8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if not _repo_has_weighted_st_subfolders( | ||
| model_name, token = token, revision = revision | ||
| ): | ||
| ignore_patterns = [*(ignore_patterns or []), *_SUBDIR_WEIGHT_IGNORE_PATTERNS] |
There was a problem hiding this comment.
Preserve per-module weight formats for mixed ST repos
When an ST repo has root model.safetensors but a weighted module such as 2_Dense/pytorch_model.bin, _prefetch_ignore_patterns has already added the global *.bin pattern after inspecting only the root load scope. Detecting the weighted subfolder here merely avoids adding _SUBDIR_WEIGHT_IGNORE_PATTERNS; it does not remove that existing pattern, which also matches files below subdirectories. The snapshot therefore still omits the Dense weight and remains unsatisfiable for mixed-format ST repositories. Once this helper returns true, weight-format pruning needs multi-component semantics rather than root-only semantics.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed: _utils.py:1246 adds a bare ".bin" after inspecting only the root scope, and huggingface_hub matches ignore_patterns with fnmatch (utils/_paths.py:132), where "" spans "/", so 2_Dense/pytorch_model.bin was still stripped. That is the ordinary pre-v3 sentence-transformers head, since the Hub's auto-conversion only ever added a root safetensors. Fixed in ab71e4e: when modules.json declares weight-bearing modules the glob is replaced by the concrete files outside them, taken from the listing that branch already fetches, so the redundant root .bin is still pruned and nothing extra is downloaded. Explicit use_safetensors and from_tf/from_flax have no listing to scope against, so they keep both formats like the multi-component branch.
| path = hf_hub_download( | ||
| model_name, "modules.json", token = token, revision = revision, | ||
| ) |
There was a problem hiding this comment.
Probe modules.json in the requested cache
When the caller supplies a writable cache_dir because the default Hugging Face cache is unavailable, this probe still downloads modules.json into the default cache. A read-only home directory or exhausted default-cache filesystem therefore makes hf_hub_download fail; the broad exception converts that into False, and the subsequent snapshot—despite correctly using the supplied cache—again prunes the required ST subfolder weights. Thread cache_dir through this helper and into hf_hub_download so detection uses the same cache as the snapshot.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
cache_dir is now threaded through in ab71e4e, but not for this reason: _gpu_init.py:62 runs redirect_hf_cache_if_readonly() before any load, so an unwritable default cache is already redirected rather than left to fail, and the probe swallowing an error only falls back to today's behaviour. The one caller that resolves cache_folder, sentence_transformer.py:1500, never sets weights_at_root and so never reaches the probe.
| if not _repo_has_weighted_st_subfolders( | ||
| model_name, token = token, revision = revision | ||
| ): |
There was a problem hiding this comment.
Stub the metadata probe in the existing offline tests
With a current unsloth_zoo that exports _ST_WEIGHTED_MODULE_TYPES, every weights_at_root=True call in tests/test_prefetch_snapshot_scope.py now reaches this helper, but that suite's capture fixture stubs only HfApi and snapshot_download_with_xet_fallback, not hf_hub_download. Its nominally no-network tests consequently issue live requests for the fake some-org/some-repo repository (the fixture even clears offline environment flags), causing offline CI to wait on repeated network timeouts and making the suite network-dependent. The fixture should stub this new probe as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Measured and worse than described: with the endpoint black holed the file took 415s and still reported 43 passed, and it was shipping the runner's HF_TOKEN to the Hub for a repo that does not exist. Fixed in ab71e4e by stubbing hf_hub_download in the capture fixture, and the mirror image in test_st_subfolder_weights_are_fetched.py, which stubbed hf_hub_download but not HfApi. The two files now run in 0.31s with no egress.
| if not _repo_has_weighted_st_subfolders( | ||
| model_name, token = token, revision = revision | ||
| ): | ||
| ignore_patterns = [*(ignore_patterns or []), *_SUBDIR_WEIGHT_IGNORE_PATTERNS] |
There was a problem hiding this comment.
Keep pruning weights outside declared ST module directories
For a hybrid ST repository that also publishes an unrelated alternate checkpoint such as fp16/model.safetensors or experimental/model.safetensors, finding one declared Dense/CNN/LSTM module makes this branch suppress _SUBDIR_WEIGHT_IGNORE_PATTERNS for every directory. The prefetch then downloads those potentially full-size alternate weights along with the required 2_Dense/3_Dense files, defeating the existing storage and bandwidth guard and potentially doubling a large model download. Preserve only weights beneath the module paths declared by modules.json while continuing to prune unrelated subdirectories.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Scanned the top 600 sentence-transformers repos by downloads: 52 declare a Dense/CNN/LSTM module dir, 7 pull anything extra, and every one of those is a sub-MB 3_LayerNorm/model.safetensors, which is an ST module weight the load reads. No repo had the hypothesised fp16/ alongside a declared module, and the large offenders (onnx/, coreml/, checkpoint-*, openvino .bin) stay pruned by the static list regardless of this branch.
…uning them
`weights_at_root` is a two-way split, root weights or per-subfolder weights, and a
sentence-transformers model can be both. `unsloth/embeddinggemma-300m` ships a root
`model.safetensors` (so `weights_at_root` is True) plus `2_Dense/model.safetensors`
and `3_Dense/model.safetensors`, which the ST load reads as part of the model.
`_SUBDIR_WEIGHT_IGNORE_PATTERNS` applies in the `weights_at_root` branch, and its
`*/*.safetensors` excluded both Dense weights from the download. The post-download
gate in unsloth_zoo then did exactly its job, since a known weight-bearing ST module
with no weight is real breakage, so the download was retried over HTTP, excluded the
same two files for the same reason, and raised
DownloadStallError: Download for 'unsloth/embeddinggemma-300m' returned an
incomplete snapshot even with HF_HUB_DISABLE_XET=1 -- missing files, check
your network connection
The retry could never have succeeded. The request itself guaranteed the files would
be absent, and the message named the network for a fault that was local.
The subdir patterns are correct for what they were written for, an fp16/ or
experimental/ directory a root load never reads, so this narrows where they apply
rather than retiring them: a repo whose modules.json declares a weight-bearing module
in a subfolder keeps those weights. The module taxonomy is imported from unsloth_zoo
rather than restated, so the two cannot drift apart and start disagreeing about what
must be fetched. Best-effort throughout: a missing or unreadable modules.json, an
unsloth_zoo predating the taxonomy, or any hub failure all keep today's behaviour.
23 offline tests cover detection, the malformed-input fallbacks, the older-zoo
fallback, both directions of the behaviour change, and the two `weights_at_root`
call sites, so the patterns cannot be quietly dropped for everyone.
e673ecb to
2d827d8
Compare
…aching the network Detecting the weighted subfolder only suppressed _SUBDIR_WEIGHT_IGNORE_PATTERNS. It did nothing about the bare "*.bin" that _prefetch_ignore_patterns had already added after inspecting the ROOT load scope, and huggingface_hub matches ignore patterns with fnmatch, where "*" spans "/". So a repo with a root model.safetensors and a legacy 2_Dense/pytorch_model.bin still lost the Dense weight, and unsloth_zoo's post-download gate still reported the unsatisfiable request as a network fault. That shape is what sentence-transformers produced before v3 defaulted to safe_serialization, and the Hub's auto-conversion only ever added a root safetensors, so the mixed repo is the normal legacy case rather than a corner one. The redundant-format prune is now scoped. When modules.json declares weight-bearing module subfolders, the spanning glob is replaced by the concrete repo files outside those modules, taken from the listing the branch already fetches: the redundant root .bin is still pruned and nothing new is downloaded. Filenames go through glob.escape, since a "[" in a name would otherwise become a character class and stop matching itself. On an explicit use_safetensors or from_tf/from_flax request there is no listing to scope against, so both formats are kept, which is the trade the whole-multi-component branch already makes. The probe is also hoisted and shared rather than run twice, and now honours cache_dir like the snapshot it precedes. test_prefetch_snapshot_scope.py calls itself a no-network file and stubs HfApi, but not hf_hub_download, so every weights_at_root case reached out for the fake repo some-org/some-repo, with the runner's HF_TOKEN attached. Against an unroutable HF_ENDPOINT the file took 415s and still passed; stubbed, 0.24s. test_st_subfolder_weights_are_fetched.py had the mirror image, stubbing hf_hub_download but not HfApi, so its model_info calls were live for real repos. Both are stubbed now, and the two files run in 0.31s with the network black holed. Tests: 70 pass. 4 new, three of which fail without the fix.
unsloth is Apache-2.0 (LICENSE, and pyproject license = "Apache-2.0"), and 303 of its 314 test files carry no header at all while 10 carry the Apache one. The new files here went in with an AGPL-3.0 notice attributed to Unsloth Zoo, which is the wrong licence, the wrong project and against the convention here.
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
The failure
weights_at_rootis treated as a two-way split, root weights or per-subfolder weights, and a sentence-transformers model can be both.unsloth/embeddinggemma-300mships a rootmodel.safetensorsplus2_Dense/model.safetensorsand3_Dense/model.safetensors, all of which the ST load reads.The
weights_at_rootbranch pruned the subfolder weights from the prefetch, which turned a satisfiable download into one that could never succeed.unsloth_zoo's post-download gate then reported the result as a network fault, so the error the user saw pointed at their connection rather than at the missing files.The fix
_repo_has_weighted_st_subfoldersreadsmodules.json(a few hundred bytes) and checks whether the repo declares a weight-bearing module in a subfolder. If it does,_SUBDIR_WEIGHT_IGNORE_PATTERNSis not applied.The module taxonomy is imported from
unsloth_zoo.hf_cache_state._ST_WEIGHTED_MODULE_TYPESrather than restated, so the two cannot drift apart. Every failure path returnsFalse, which keeps the previous behaviour exactly.Cost: one small extra fetch on the prefetch path for repos that do not ship
modules.json, against a snapshot download that is otherwise gigabytes.Verification
grep "weights_at_root = True"overunsloth/returns exactlyllama.py:2486andvision.py:1115, so both call sites are covered by the change.unsloth_zoo.hf_cache_state._ST_WEIGHTED_MODULE_TYPESresolves tofrozenset({'lstm', 'dense', 'cnn'}).Tests
tests/test_st_subfolder_weights_are_fetched.py, 23 tests, all passing, and again underHF_HUB_OFFLINE=1. The neighbouringtests/test_prefetch_snapshot_scope.pystill passes, 43 tests, no regression.Everything is stubbed:
hf_hub_downloadandsnapshot_download_with_xet_fallbackare both replaced, so the suite touches no network and needs no gated model.