Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions unsloth/models/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

The logic for skipping the glob call is duplicated in two places. Consider refactoring this into a helper function to improve maintainability and reduce code duplication.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[12/12 reviewers] This guard is correct -- it avoids the HfFileSystem().glob() call when either AutoConfig or PeftConfig already failed. Since both_exist is never read after this block (the if both_exist: raise at line 493 runs before this point), skipping the block has no behavioral impact.

Note: the entire block (lines 511-527) is effectively dead code on modern transformers because both_exist is written but never consumed afterward. The guard correctly avoids wasted network I/O in this dead path.

Suggested change
if SUPPORTS_LLAMA32 and is_model and is_peft:
if SUPPORTS_LLAMA32 and is_model and is_peft:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While this change correctly avoids a redundant network call, I've noticed two related points:

  1. Potential Bug: The both_exist variable is recalculated inside this block for newer transformers versions (SUPPORTS_LLAMA32) but is not subsequently used. This means that if a repository contains both a base model and a LoRA adapter, it would not raise an error as it does for older versions. This seems like a bug.

  2. Code Duplication: This entire logic block for loading and checking configurations is duplicated in FastModel.from_pretrained (starting around line 1055).

I'd suggest refactoring this duplicated logic into a helper function. This would improve maintainability and allow fixing the potential bug in one place. The fix would involve ensuring that an error is raised if both configs exist, regardless of the transformers version.

# Check if folder exists locally
if os.path.isdir(model_name):
exist_adapter_config = os.path.exists(
Expand Down Expand Up @@ -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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

The logic for skipping the glob call is duplicated in two places. Consider refactoring this into a helper function to improve maintainability and reduce code duplication.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[12/12 reviewers] Same as the FastLanguageModel change above -- the guard is safe. both_exist written on lines 1292/1300 is never read after this block (the check at line 1105 runs before).

Suggested change
if SUPPORTS_LLAMA32 and is_model and is_peft:
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(
Expand Down
Loading