From dac1b60cd76344be654ea1ed114e3a148d581d96 Mon Sep 17 00:00:00 2001 From: Roland Tannous Date: Sun, 5 Apr 2026 01:59:22 +0000 Subject: [PATCH 1/7] 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. --- tests/test_loader_glob_skip.py | 126 +++++++++++++++++++++++++++++++++ unsloth/models/loader.py | 4 +- 2 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 tests/test_loader_glob_skip.py diff --git a/tests/test_loader_glob_skip.py b/tests/test_loader_glob_skip.py new file mode 100644 index 0000000000..9ca77bd9ee --- /dev/null +++ b/tests/test_loader_glob_skip.py @@ -0,0 +1,126 @@ +"""Tests that HfFileSystem().glob() is skipped when is_model or is_peft is False. + +The glob calls in FastLanguageModel.from_pretrained and FastModel.from_pretrained +exist solely to detect repos with both config.json and adapter_config.json. When +either AutoConfig or PeftConfig fails to load, the glob cannot find both files, +so calling it is redundant and risks hanging on slow networks. +""" + +import os +import unittest +from unittest.mock import MagicMock, patch + + +class TestGlobSkippedWhenNotBothConfigs(unittest.TestCase): + """Verify HfFileSystem.glob is not called when is_model or is_peft is False.""" + + def _run_both_exist_block(self, is_model, is_peft, supports_llama32, model_name, is_local_dir=False): + """Simulate the both_exist detection block from loader.py. + + This mirrors the exact logic at lines 500-517 / 1276-1292 of loader.py. + Returns (both_exist, glob_called). + """ + from unittest.mock import MagicMock + + both_exist = (is_model and is_peft) and not supports_llama32 + glob_mock = MagicMock(return_value=[ + f"{model_name}/config.json", + f"{model_name}/adapter_config.json", + ]) + + # This mirrors the guarded block in loader.py + if supports_llama32 and is_model and is_peft: + if is_local_dir: + # Local path branch — would use os.path.exists in real code + both_exist = True # simulate both files present locally + else: + files = glob_mock(f"{model_name}/*.json") + files = list(os.path.split(x)[-1] for x in files) + if sum(x == "adapter_config.json" or x == "config.json" for x in files) >= 2: + both_exist = True + + return both_exist, glob_mock.called + + # --- Cases where glob should NOT be called --- + + def test_glob_skipped_when_is_model_false(self): + both_exist, glob_called = self._run_both_exist_block( + is_model=False, is_peft=True, supports_llama32=True, + model_name="org/some-adapter", + ) + self.assertFalse(glob_called, "glob should not be called when is_model=False") + self.assertFalse(both_exist) + + def test_glob_skipped_when_is_peft_false(self): + both_exist, glob_called = self._run_both_exist_block( + is_model=True, is_peft=False, supports_llama32=True, + model_name="org/some-model", + ) + self.assertFalse(glob_called, "glob should not be called when is_peft=False") + self.assertFalse(both_exist) + + def test_glob_skipped_when_both_false(self): + both_exist, glob_called = self._run_both_exist_block( + is_model=False, is_peft=False, supports_llama32=True, + model_name="org/bad-repo", + ) + self.assertFalse(glob_called, "glob should not be called when both are False") + self.assertFalse(both_exist) + + def test_glob_skipped_when_supports_llama32_false(self): + both_exist, glob_called = self._run_both_exist_block( + is_model=True, is_peft=True, supports_llama32=False, + model_name="org/some-model", + ) + self.assertFalse(glob_called, "glob should not be called when SUPPORTS_LLAMA32=False") + # both_exist is set by the old-style check: (is_model and is_peft) and not SUPPORTS_LLAMA32 + self.assertTrue(both_exist) + + # --- Cases where glob SHOULD be called --- + + def test_glob_called_when_both_true_and_supports_llama32(self): + both_exist, glob_called = self._run_both_exist_block( + is_model=True, is_peft=True, supports_llama32=True, + model_name="org/mixed-repo", + ) + self.assertTrue(glob_called, "glob should be called when is_model and is_peft are both True") + self.assertTrue(both_exist) + + def test_local_dir_skips_glob(self): + both_exist, glob_called = self._run_both_exist_block( + is_model=True, is_peft=True, supports_llama32=True, + model_name="/local/path/to/model", is_local_dir=True, + ) + self.assertFalse(glob_called, "glob should not be called for local directories") + self.assertTrue(both_exist) + + +class TestLoaderSourceHasGuard(unittest.TestCase): + """Verify the actual loader.py source code has the is_model/is_peft guard.""" + + def test_loader_source_has_guard(self): + """Check that both SUPPORTS_LLAMA32 checks in loader.py include is_model and is_peft.""" + loader_path = os.path.join( + os.path.dirname(__file__), os.pardir, "unsloth", "models", "loader.py" + ) + with open(loader_path) as f: + source = f.read() + + # Find all lines with the SUPPORTS_LLAMA32 check near glob usage + lines = source.splitlines() + guard_lines = [ + line.strip() for line in lines + if "SUPPORTS_LLAMA32" in line and "if " in line and "is_model" in line + ] + # There should be exactly 2 guarded checks (one per from_pretrained method) + self.assertEqual( + len(guard_lines), 2, + f"Expected 2 guarded SUPPORTS_LLAMA32 checks with is_model/is_peft, found {len(guard_lines)}: {guard_lines}" + ) + for line in guard_lines: + self.assertIn("is_model", line) + self.assertIn("is_peft", line) + + +if __name__ == "__main__": + unittest.main() diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index a6cb3eb529..b7bbf14085 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -508,7 +508,7 @@ def from_pretrained( model_type = model_types # New transformers need to check manually. - if SUPPORTS_LLAMA32: + if SUPPORTS_LLAMA32 and is_model and is_peft: # Check if folder exists locally if os.path.isdir(model_name): exist_adapter_config = os.path.exists( @@ -1282,7 +1282,7 @@ def from_pretrained( os.environ["UNSLOTH_DISABLE_STATIC_GENERATION"] = "1" # New transformers need to check manually. - if SUPPORTS_LLAMA32: + if SUPPORTS_LLAMA32 and is_model and is_peft: # Check if folder exists locally if os.path.isdir(model_name): exist_adapter_config = os.path.exists( From c50591b1a2766c4e458d5e24ec0d1e80c01b0c5b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Apr 2026 02:00:35 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_loader_glob_skip.py | 72 +++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/tests/test_loader_glob_skip.py b/tests/test_loader_glob_skip.py index 9ca77bd9ee..d461e23cf8 100644 --- a/tests/test_loader_glob_skip.py +++ b/tests/test_loader_glob_skip.py @@ -14,7 +14,9 @@ class TestGlobSkippedWhenNotBothConfigs(unittest.TestCase): """Verify HfFileSystem.glob is not called when is_model or is_peft is False.""" - def _run_both_exist_block(self, is_model, is_peft, supports_llama32, model_name, is_local_dir=False): + def _run_both_exist_block( + self, is_model, is_peft, supports_llama32, model_name, is_local_dir = False + ): """Simulate the both_exist detection block from loader.py. This mirrors the exact logic at lines 500-517 / 1276-1292 of loader.py. @@ -23,10 +25,12 @@ def _run_both_exist_block(self, is_model, is_peft, supports_llama32, model_name, from unittest.mock import MagicMock both_exist = (is_model and is_peft) and not supports_llama32 - glob_mock = MagicMock(return_value=[ - f"{model_name}/config.json", - f"{model_name}/adapter_config.json", - ]) + glob_mock = MagicMock( + return_value = [ + f"{model_name}/config.json", + f"{model_name}/adapter_config.json", + ] + ) # This mirrors the guarded block in loader.py if supports_llama32 and is_model and is_peft: @@ -36,7 +40,10 @@ def _run_both_exist_block(self, is_model, is_peft, supports_llama32, model_name, else: files = glob_mock(f"{model_name}/*.json") files = list(os.path.split(x)[-1] for x in files) - if sum(x == "adapter_config.json" or x == "config.json" for x in files) >= 2: + if ( + sum(x == "adapter_config.json" or x == "config.json" for x in files) + >= 2 + ): both_exist = True return both_exist, glob_mock.called @@ -45,34 +52,44 @@ def _run_both_exist_block(self, is_model, is_peft, supports_llama32, model_name, def test_glob_skipped_when_is_model_false(self): both_exist, glob_called = self._run_both_exist_block( - is_model=False, is_peft=True, supports_llama32=True, - model_name="org/some-adapter", + is_model = False, + is_peft = True, + supports_llama32 = True, + model_name = "org/some-adapter", ) self.assertFalse(glob_called, "glob should not be called when is_model=False") self.assertFalse(both_exist) def test_glob_skipped_when_is_peft_false(self): both_exist, glob_called = self._run_both_exist_block( - is_model=True, is_peft=False, supports_llama32=True, - model_name="org/some-model", + is_model = True, + is_peft = False, + supports_llama32 = True, + model_name = "org/some-model", ) self.assertFalse(glob_called, "glob should not be called when is_peft=False") self.assertFalse(both_exist) def test_glob_skipped_when_both_false(self): both_exist, glob_called = self._run_both_exist_block( - is_model=False, is_peft=False, supports_llama32=True, - model_name="org/bad-repo", + is_model = False, + is_peft = False, + supports_llama32 = True, + model_name = "org/bad-repo", ) self.assertFalse(glob_called, "glob should not be called when both are False") self.assertFalse(both_exist) def test_glob_skipped_when_supports_llama32_false(self): both_exist, glob_called = self._run_both_exist_block( - is_model=True, is_peft=True, supports_llama32=False, - model_name="org/some-model", + is_model = True, + is_peft = True, + supports_llama32 = False, + model_name = "org/some-model", + ) + self.assertFalse( + glob_called, "glob should not be called when SUPPORTS_LLAMA32=False" ) - self.assertFalse(glob_called, "glob should not be called when SUPPORTS_LLAMA32=False") # both_exist is set by the old-style check: (is_model and is_peft) and not SUPPORTS_LLAMA32 self.assertTrue(both_exist) @@ -80,16 +97,23 @@ def test_glob_skipped_when_supports_llama32_false(self): def test_glob_called_when_both_true_and_supports_llama32(self): both_exist, glob_called = self._run_both_exist_block( - is_model=True, is_peft=True, supports_llama32=True, - model_name="org/mixed-repo", + is_model = True, + is_peft = True, + supports_llama32 = True, + model_name = "org/mixed-repo", + ) + self.assertTrue( + glob_called, "glob should be called when is_model and is_peft are both True" ) - self.assertTrue(glob_called, "glob should be called when is_model and is_peft are both True") self.assertTrue(both_exist) def test_local_dir_skips_glob(self): both_exist, glob_called = self._run_both_exist_block( - is_model=True, is_peft=True, supports_llama32=True, - model_name="/local/path/to/model", is_local_dir=True, + is_model = True, + is_peft = True, + supports_llama32 = True, + model_name = "/local/path/to/model", + is_local_dir = True, ) self.assertFalse(glob_called, "glob should not be called for local directories") self.assertTrue(both_exist) @@ -109,13 +133,15 @@ def test_loader_source_has_guard(self): # Find all lines with the SUPPORTS_LLAMA32 check near glob usage lines = source.splitlines() guard_lines = [ - line.strip() for line in lines + line.strip() + for line in lines if "SUPPORTS_LLAMA32" in line and "if " in line and "is_model" in line ] # There should be exactly 2 guarded checks (one per from_pretrained method) self.assertEqual( - len(guard_lines), 2, - f"Expected 2 guarded SUPPORTS_LLAMA32 checks with is_model/is_peft, found {len(guard_lines)}: {guard_lines}" + len(guard_lines), + 2, + f"Expected 2 guarded SUPPORTS_LLAMA32 checks with is_model/is_peft, found {len(guard_lines)}: {guard_lines}", ) for line in guard_lines: self.assertIn("is_model", line) From 30db8f3935e8a09a08c983412cef5ffff8338508 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Sun, 5 Apr 2026 02:33:13 +0000 Subject: [PATCH 3/7] 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. --- tests/test_loader_glob_skip.py | 152 --------------------------------- 1 file changed, 152 deletions(-) delete mode 100644 tests/test_loader_glob_skip.py diff --git a/tests/test_loader_glob_skip.py b/tests/test_loader_glob_skip.py deleted file mode 100644 index d461e23cf8..0000000000 --- a/tests/test_loader_glob_skip.py +++ /dev/null @@ -1,152 +0,0 @@ -"""Tests that HfFileSystem().glob() is skipped when is_model or is_peft is False. - -The glob calls in FastLanguageModel.from_pretrained and FastModel.from_pretrained -exist solely to detect repos with both config.json and adapter_config.json. When -either AutoConfig or PeftConfig fails to load, the glob cannot find both files, -so calling it is redundant and risks hanging on slow networks. -""" - -import os -import unittest -from unittest.mock import MagicMock, patch - - -class TestGlobSkippedWhenNotBothConfigs(unittest.TestCase): - """Verify HfFileSystem.glob is not called when is_model or is_peft is False.""" - - def _run_both_exist_block( - self, is_model, is_peft, supports_llama32, model_name, is_local_dir = False - ): - """Simulate the both_exist detection block from loader.py. - - This mirrors the exact logic at lines 500-517 / 1276-1292 of loader.py. - Returns (both_exist, glob_called). - """ - from unittest.mock import MagicMock - - both_exist = (is_model and is_peft) and not supports_llama32 - glob_mock = MagicMock( - return_value = [ - f"{model_name}/config.json", - f"{model_name}/adapter_config.json", - ] - ) - - # This mirrors the guarded block in loader.py - if supports_llama32 and is_model and is_peft: - if is_local_dir: - # Local path branch — would use os.path.exists in real code - both_exist = True # simulate both files present locally - else: - files = glob_mock(f"{model_name}/*.json") - files = list(os.path.split(x)[-1] for x in files) - if ( - sum(x == "adapter_config.json" or x == "config.json" for x in files) - >= 2 - ): - both_exist = True - - return both_exist, glob_mock.called - - # --- Cases where glob should NOT be called --- - - def test_glob_skipped_when_is_model_false(self): - both_exist, glob_called = self._run_both_exist_block( - is_model = False, - is_peft = True, - supports_llama32 = True, - model_name = "org/some-adapter", - ) - self.assertFalse(glob_called, "glob should not be called when is_model=False") - self.assertFalse(both_exist) - - def test_glob_skipped_when_is_peft_false(self): - both_exist, glob_called = self._run_both_exist_block( - is_model = True, - is_peft = False, - supports_llama32 = True, - model_name = "org/some-model", - ) - self.assertFalse(glob_called, "glob should not be called when is_peft=False") - self.assertFalse(both_exist) - - def test_glob_skipped_when_both_false(self): - both_exist, glob_called = self._run_both_exist_block( - is_model = False, - is_peft = False, - supports_llama32 = True, - model_name = "org/bad-repo", - ) - self.assertFalse(glob_called, "glob should not be called when both are False") - self.assertFalse(both_exist) - - def test_glob_skipped_when_supports_llama32_false(self): - both_exist, glob_called = self._run_both_exist_block( - is_model = True, - is_peft = True, - supports_llama32 = False, - model_name = "org/some-model", - ) - self.assertFalse( - glob_called, "glob should not be called when SUPPORTS_LLAMA32=False" - ) - # both_exist is set by the old-style check: (is_model and is_peft) and not SUPPORTS_LLAMA32 - self.assertTrue(both_exist) - - # --- Cases where glob SHOULD be called --- - - def test_glob_called_when_both_true_and_supports_llama32(self): - both_exist, glob_called = self._run_both_exist_block( - is_model = True, - is_peft = True, - supports_llama32 = True, - model_name = "org/mixed-repo", - ) - self.assertTrue( - glob_called, "glob should be called when is_model and is_peft are both True" - ) - self.assertTrue(both_exist) - - def test_local_dir_skips_glob(self): - both_exist, glob_called = self._run_both_exist_block( - is_model = True, - is_peft = True, - supports_llama32 = True, - model_name = "/local/path/to/model", - is_local_dir = True, - ) - self.assertFalse(glob_called, "glob should not be called for local directories") - self.assertTrue(both_exist) - - -class TestLoaderSourceHasGuard(unittest.TestCase): - """Verify the actual loader.py source code has the is_model/is_peft guard.""" - - def test_loader_source_has_guard(self): - """Check that both SUPPORTS_LLAMA32 checks in loader.py include is_model and is_peft.""" - loader_path = os.path.join( - os.path.dirname(__file__), os.pardir, "unsloth", "models", "loader.py" - ) - with open(loader_path) as f: - source = f.read() - - # Find all lines with the SUPPORTS_LLAMA32 check near glob usage - lines = source.splitlines() - guard_lines = [ - line.strip() - for line in lines - if "SUPPORTS_LLAMA32" in line and "if " in line and "is_model" in line - ] - # There should be exactly 2 guarded checks (one per from_pretrained method) - self.assertEqual( - len(guard_lines), - 2, - f"Expected 2 guarded SUPPORTS_LLAMA32 checks with is_model/is_peft, found {len(guard_lines)}: {guard_lines}", - ) - for line in guard_lines: - self.assertIn("is_model", line) - self.assertIn("is_peft", line) - - -if __name__ == "__main__": - unittest.main() From 1d46c655b4de53e5f291043ee86c155dd32b6087 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 6 Apr 2026 12:07:57 +0000 Subject: [PATCH 4/7] 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. --- unsloth/models/loader.py | 41 +++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index b7bbf14085..c6164e326f 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -517,14 +517,19 @@ def from_pretrained( exist_config = os.path.exists(os.path.join(model_name, "config.json")) both_exist = exist_adapter_config and exist_config else: - # Because HfFileSystem assumes linux paths, we need to set the path with forward slashes, even on Windows. - files = HfFileSystem(token = token).glob(f"{model_name}/*.json") - files = list(os.path.split(x)[-1] for x in files) - if ( - sum(x == "adapter_config.json" or x == "config.json" for x in files) - >= 2 - ): - both_exist = True + # HfFileSystem returns POSIX paths; use str.rsplit to extract + # filenames correctly on all platforms (os.path.split uses the + # OS separator which breaks on Windows). + try: + files = HfFileSystem(token = token).glob(f"{model_name}/*.json") + files = [x.rsplit("/", 1)[-1] for x in files] + if ( + sum(x == "adapter_config.json" or x == "config.json" for x in files) + >= 2 + ): + both_exist = True + except Exception: + pass if not is_model and not is_peft: error = autoconfig_error if autoconfig_error is not None else peft_error @@ -1291,13 +1296,19 @@ def from_pretrained( exist_config = os.path.exists(os.path.join(model_name, "config.json")) both_exist = exist_adapter_config and exist_config else: - files = HfFileSystem(token = token).glob(f"{model_name}/*.json") - files = list(os.path.split(x)[-1] for x in files) - if ( - sum(x == "adapter_config.json" or x == "config.json" for x in files) - >= 2 - ): - both_exist = True + # HfFileSystem returns POSIX paths; use str.rsplit to extract + # filenames correctly on all platforms (os.path.split uses the + # OS separator which breaks on Windows). + try: + files = HfFileSystem(token = token).glob(f"{model_name}/*.json") + files = [x.rsplit("/", 1)[-1] for x in files] + if ( + sum(x == "adapter_config.json" or x == "config.json" for x in files) + >= 2 + ): + both_exist = True + except Exception: + pass if not is_model and not is_peft: error = autoconfig_error if autoconfig_error is not None else peft_error From 54ffa99e20c8a7b326d587bce3d086cc7965cece Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Apr 2026 12:08:21 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- unsloth/models/loader.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index c6164e326f..3f8c7d767b 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -524,7 +524,10 @@ def from_pretrained( files = HfFileSystem(token = token).glob(f"{model_name}/*.json") files = [x.rsplit("/", 1)[-1] for x in files] if ( - sum(x == "adapter_config.json" or x == "config.json" for x in files) + sum( + x == "adapter_config.json" or x == "config.json" + for x in files + ) >= 2 ): both_exist = True @@ -1303,7 +1306,10 @@ def from_pretrained( files = HfFileSystem(token = token).glob(f"{model_name}/*.json") files = [x.rsplit("/", 1)[-1] for x in files] if ( - sum(x == "adapter_config.json" or x == "config.json" for x in files) + sum( + x == "adapter_config.json" or x == "config.json" + for x in files + ) >= 2 ): both_exist = True From 6025e9b66008e4a90df57d297610aba75a54b1e1 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 6 Apr 2026 14:12:59 +0000 Subject: [PATCH 6/7] 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. --- unsloth/models/loader.py | 40 ++++++++-------------------------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index 3f8c7d767b..b5ecc4eb1e 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -517,22 +517,10 @@ def from_pretrained( exist_config = os.path.exists(os.path.join(model_name, "config.json")) both_exist = exist_adapter_config and exist_config else: - # HfFileSystem returns POSIX paths; use str.rsplit to extract - # filenames correctly on all platforms (os.path.split uses the - # OS separator which breaks on Windows). - try: - files = HfFileSystem(token = token).glob(f"{model_name}/*.json") - files = [x.rsplit("/", 1)[-1] for x in files] - if ( - sum( - x == "adapter_config.json" or x == "config.json" - for x in files - ) - >= 2 - ): - both_exist = True - except Exception: - pass + # Both AutoConfig and PeftConfig loaded successfully from this + # remote repo, so both config.json and adapter_config.json + # definitely exist -- no need for an extra HfFileSystem network call. + both_exist = True if not is_model and not is_peft: error = autoconfig_error if autoconfig_error is not None else peft_error @@ -1299,22 +1287,10 @@ def from_pretrained( exist_config = os.path.exists(os.path.join(model_name, "config.json")) both_exist = exist_adapter_config and exist_config else: - # HfFileSystem returns POSIX paths; use str.rsplit to extract - # filenames correctly on all platforms (os.path.split uses the - # OS separator which breaks on Windows). - try: - files = HfFileSystem(token = token).glob(f"{model_name}/*.json") - files = [x.rsplit("/", 1)[-1] for x in files] - if ( - sum( - x == "adapter_config.json" or x == "config.json" - for x in files - ) - >= 2 - ): - both_exist = True - except Exception: - pass + # Both AutoConfig and PeftConfig loaded successfully from this + # remote repo, so both config.json and adapter_config.json + # definitely exist -- no need for an extra HfFileSystem network call. + both_exist = True if not is_model and not is_peft: error = autoconfig_error if autoconfig_error is not None else peft_error From a21efffc8379c620c55257453b10883ac948b7c6 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 6 Apr 2026 14:23:48 +0000 Subject: [PATCH 7/7] 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. --- unsloth/models/loader.py | 1 - 1 file changed, 1 deletion(-) diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index b5ecc4eb1e..94e76f2db0 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -49,7 +49,6 @@ except: # For older versions of huggingface_hub from huggingface_hub.utils._token import get_token -from huggingface_hub import HfFileSystem import importlib.util from ..device_type import ( is_hip,