fix: skip redundant HfFileSystem().glob() calls in loader.py#4852
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces guards in loader.py to skip redundant HfFileSystem().glob() calls when is_model or is_peft is false, and adds a test suite to verify this behavior. Feedback indicates that the logic being modified is effectively dead code because the both_exist variable updated within these blocks is never used, suggesting that the entire manual check logic should be simplified or removed.
danielhanchen
left a comment
There was a problem hiding this comment.
Thank you for the PR! The goal of this PR is to avoid unnecessary HfFileSystem().glob() network calls in the loader when they cannot produce useful results. As a summary, this PR adds and is_model and is_peft guards to both SUPPORTS_LLAMA32 blocks in FastLanguageModel.from_pretrained and FastModel.from_pretrained, so the remote glob call is only made when both AutoConfig and PeftConfig have successfully loaded -- skipping it otherwise to prevent hangs on slow/unreliable networks.
I ran 10 independent reviews (7 automated + 3 manual). Findings:
| Reviewers | Severity | Finding |
|---|---|---|
| 10/10 | Info (pre-existing) | both_exist is set inside the SUPPORTS_LLAMA32 block but never read afterward -- the "two config files" RuntimeError never fires on transformers > 4.45.0. This is dead code that predates this PR. |
| 10/10 | None | The guard and is_model and is_peft is logically correct: when either config failed to load, the glob result would be written to both_exist which is never consumed, so skipping it is safe. |
| 3/10 | False positive | Three reviewers flagged a regression where a mixed repo with a malformed config would no longer be rejected. However, the both_exist check (lines 483/1097) runs before the SUPPORTS_LLAMA32 block, and there is no check after it. Even without this PR, the error never fires on new transformers. No behavioral change. |
Verified compatibility with transformers 4.57.6, 5.4.0, TRL 0.22.2, 0.27.1 -- SUPPORTS_LLAMA32 is True for all of these, and the guard behaves identically.
Tests moved to #4854 to keep this PR minimal.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request modifies unsloth/models/loader.py to add is_model and is_peft checks to the SUPPORTS_LLAMA32 condition. The review feedback indicates that these manual file existence checks are redundant because the successful initialization of is_model and is_peft already confirms the presence of the necessary configuration files. Additionally, the reviewer noted that the both_exist variable updated in these blocks is never used, suggesting a more unified and simplified logic to handle mixed repositories across all model versions.
|
|
||
| # New transformers need to check manually. | ||
| if SUPPORTS_LLAMA32: | ||
| if SUPPORTS_LLAMA32 and is_model and is_peft: |
There was a problem hiding this comment.
The manual check for file existence using os.path.exists or HfFileSystem().glob() is redundant when guarded by is_model and is_peft. If both AutoConfig.from_pretrained and PeftConfig.from_pretrained succeeded (setting is_model and is_peft to True), it already confirms that both configuration files exist and are reachable for the given model_name.
Additionally, the both_exist variable set within this block (lines 508 and 517) is never checked again in this function. The only check for both_exist occurs at line 484, which is before this block executes. This means that for SUPPORTS_LLAMA32, the "mixed repo" error is currently never raised, making this entire block a no-op that only serves to potentially cause the network hangs you are fixing.
A cleaner solution would be to unify the logic at line 481 by removing the and not SUPPORTS_LLAMA32 condition. This would fix the hang, remove the redundancy, and ensure the error is correctly raised for all versions.
|
|
||
| # New transformers need to check manually. | ||
| if SUPPORTS_LLAMA32: | ||
| if SUPPORTS_LLAMA32 and is_model and is_peft: |
There was a problem hiding this comment.
Similar to the check in FastLanguageModel.from_pretrained, this manual file check is redundant when is_model and is_peft are both True. Moreover, the both_exist variable updated here (lines 1284 and 1292) is not used anywhere after this block. The error check at line 1097 happens before this manual check, so the conflict is never actually flagged for Llama 3.2+ models.
Simplifying the logic at line 1095 to both_exist = is_model and is_peft (removing the version check) would be more efficient and correct.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request updates unsloth/models/loader.py to refine the conditional logic for Llama 3.2 support. The changes ensure that the manual check for local adapter configurations only occurs when both is_model and is_peft are true. I have no feedback to provide.
|
/gemini review |
Guard the SUPPORTS_LLAMA32 glob blocks with `is_model and is_peft` so the HfFileSystem HTTP call is only made when both configs could actually exist. This prevents indefinite hangs on slow/unreliable networks since the glob result is redundant when either AutoConfig or PeftConfig already failed to load.
for more information, see https://pre-commit.ci
Tests for the glob skip guard belong in their own PR to keep the loader change minimal and reviewable.
262b9d1 to
30db8f3
Compare
There was a problem hiding this comment.
Code Review
This pull request updates unsloth/models/loader.py to include is_model and is_peft guards for Llama 3.2 manual configuration checks. However, the review identifies that these checks are currently no-ops because the error-handling logic for configuration conflicts occurs before these blocks are reached. The feedback suggests either moving the logic or raising errors directly within these blocks to ensure the manual checks are functional.
I am having trouble creating individual review comments. Click here to see my feedback.
unsloth/models/loader.py (501)
The manual check for conflicting config files (both_exist) is performed here for SUPPORTS_LLAMA32, but the result is never used to raise an error. The only check for both_exist occurs at line 483, which is before this block updates the variable. This means that for Llama 3.2 and newer transformers, the conflict detection is effectively disabled.
Additionally, guarding this block with is_model and is_peft might defeat the purpose of a "manual" check if it was intended to catch cases where transformers fails to detect one of the configs. If both is_model and is_peft are already True, a conflict is already known and the expensive glob call is redundant. You should probably raise the RuntimeError if both_exist is set to True within this block, or move this logic before the check at line 483.
unsloth/models/loader.py (1277)
Similar to the issue at line 501, the result of the manual check for both_exist is ignored here because the error check at line 1097 occurs before this block updates the variable. For SUPPORTS_LLAMA32, conflict detection is currently a no-op.
Furthermore, if the manual check was meant to be more robust than the is_model/is_peft detection, guarding it with is_model and is_peft defeats that purpose. If both are True, the conflict is already evident. Consider raising the RuntimeError directly if both_exist is determined to be True inside this block.
|
Reviewed and tested this PR thoroughly. Merged the PR branch with the latest main (clean merge, no conflicts -- only unrelated Testing summary (62 tests, all pass):
Key conclusions:
The change is safe to merge. |
- Use str.rsplit("/", 1) instead of os.path.split to extract filenames
from HfFileSystem paths. HfFileSystem always returns POSIX-style paths,
but os.path.split uses the OS separator, so on Windows the entire path
was returned as the "filename" and the config name comparison always
failed.
- Wrap the HfFileSystem().glob() call in try/except to gracefully handle
network failures (offline mode, timeouts, unreachable Hub). On failure
both_exist stays False, which is the safe default.
for more information, see https://pre-commit.ci
When is_model and is_peft are both True, AutoConfig and PeftConfig have already loaded successfully, proving both config.json and adapter_config.json exist. The HfFileSystem network call to re-verify this was redundant and could cause hangs on slow networks. Replace the glob + try/except block with a direct both_exist = True assignment.
HfFileSystem was only used for the glob() calls that were replaced with direct both_exist = True assignments in the previous commit.
Tests verifying that HfFileSystem().glob() is correctly skipped when is_model or is_peft is False, matching the guard added in PR #4852.
…ai#4852) * fix: skip redundant HfFileSystem().glob() calls in loader.py Guard the SUPPORTS_LLAMA32 glob blocks with `is_model and is_peft` so the HfFileSystem HTTP call is only made when both configs could actually exist. This prevents indefinite hangs on slow/unreliable networks since the glob result is redundant when either AutoConfig or PeftConfig already failed to load. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove test file from main PR - moved to separate PR Tests for the glob skip guard belong in their own PR to keep the loader change minimal and reviewable. * Harden HfFileSystem glob: fix Windows path splitting, add try/except - Use str.rsplit("/", 1) instead of os.path.split to extract filenames from HfFileSystem paths. HfFileSystem always returns POSIX-style paths, but os.path.split uses the OS separator, so on Windows the entire path was returned as the "filename" and the config name comparison always failed. - Wrap the HfFileSystem().glob() call in try/except to gracefully handle network failures (offline mode, timeouts, unreachable Hub). On failure both_exist stays False, which is the safe default. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove redundant HfFileSystem().glob() call for remote repos When is_model and is_peft are both True, AutoConfig and PeftConfig have already loaded successfully, proving both config.json and adapter_config.json exist. The HfFileSystem network call to re-verify this was redundant and could cause hangs on slow networks. Replace the glob + try/except block with a direct both_exist = True assignment. * Remove unused HfFileSystem import HfFileSystem was only used for the glob() calls that were replaced with direct both_exist = True assignments in the previous commit. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <danielhanchen@gmail.com>
Tests verifying that HfFileSystem().glob() is correctly skipped when is_model or is_peft is False, matching the guard added in PR unslothai#4852.
…ai#4852) * fix: skip redundant HfFileSystem().glob() calls in loader.py Guard the SUPPORTS_LLAMA32 glob blocks with `is_model and is_peft` so the HfFileSystem HTTP call is only made when both configs could actually exist. This prevents indefinite hangs on slow/unreliable networks since the glob result is redundant when either AutoConfig or PeftConfig already failed to load. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove test file from main PR - moved to separate PR Tests for the glob skip guard belong in their own PR to keep the loader change minimal and reviewable. * Harden HfFileSystem glob: fix Windows path splitting, add try/except - Use str.rsplit("/", 1) instead of os.path.split to extract filenames from HfFileSystem paths. HfFileSystem always returns POSIX-style paths, but os.path.split uses the OS separator, so on Windows the entire path was returned as the "filename" and the config name comparison always failed. - Wrap the HfFileSystem().glob() call in try/except to gracefully handle network failures (offline mode, timeouts, unreachable Hub). On failure both_exist stays False, which is the safe default. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove redundant HfFileSystem().glob() call for remote repos When is_model and is_peft are both True, AutoConfig and PeftConfig have already loaded successfully, proving both config.json and adapter_config.json exist. The HfFileSystem network call to re-verify this was redundant and could cause hangs on slow networks. Replace the glob + try/except block with a direct both_exist = True assignment. * Remove unused HfFileSystem import HfFileSystem was only used for the glob() calls that were replaced with direct both_exist = True assignments in the previous commit. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <danielhanchen@gmail.com>
Tests verifying that HfFileSystem().glob() is correctly skipped when is_model or is_peft is False, matching the guard added in PR unslothai#4852.
Summary
SUPPORTS_LLAMA32glob blocks inFastLanguageModel.from_pretrainedandFastModel.from_pretrainedwithis_model and is_peftso theHfFileSystemHTTP call is only made when both configs could actually existAutoConfigorPeftConfigalready failed to loadTest plan
pytest tests/test_loader_glob_skip.py -v— all 7 tests passunsloth/Qwen3.5-2B) — should no longer hang on slow networks