Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions unsloth/chat_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ def get_chat_template(
chat_template, stop_word = chat_template
assert(type(chat_template) is str)
assert(type(stop_word) is str)
ollama_modelfile = None

elif type(chat_template) is str:

Expand Down
33 changes: 31 additions & 2 deletions unsloth/models/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,9 +1423,38 @@ def get_peft_model(
transformers_set_seed(random_state)

if isinstance(model, PeftModelForCausalLM):
raise TypeError(
"Unsloth: Your model already has LoRA adapters. No need to run this again!"
# Check if exactly the same and then pass through!
assert(hasattr(model, "peft_config"))

peft_config = model.peft_config["default"].to_dict()
check_parameters = [
"r", "lora_alpha", "lora_dropout",
"bias", "layers_to_transform", "layers_pattern",
"use_rslora", "modules_to_save", "init_lora_weights",
]
check_all = True
for param in check_parameters:
check_all = check_all and (peft_config[param] == eval(param))
pass
check_all = check_all and (
len(set(peft_config["target_modules"]) ^ set(target_modules)) == 0
)
check_all = check_all and (
(loftq_config == {} or loftq_config is None) and \
(peft_config["loftq_config"] == {} or peft_config["loftq_config"] is None)
)

if check_all:
# Simply pass through!
logger.warning(
"Unsloth: Already have LoRA adapters! We shall skip this step."
)
return model
else:
raise TypeError(
"Unsloth: Your model already has LoRA adapters. Your new parameters are different."
)
pass
pass

if loftq_config is None: loftq_config = {}
Expand Down
36 changes: 26 additions & 10 deletions unsloth/models/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,37 @@ def from_pretrained(
model_name = _get_model_name(model_name, load_in_4bit)

# First check if it's a normal model via AutoConfig
is_peft = False
try:
model_config = AutoConfig.from_pretrained(model_name, token = token, revision = revision)
is_peft = False
is_model = True
except:
is_model = False
try:
peft_config = PeftConfig .from_pretrained(model_name, token = token, revision = revision)
is_peft = True
except:
try:
# Most likely a PEFT model
peft_config = PeftConfig.from_pretrained(model_name, token = token, revision = revision)
except:
raise RuntimeError(f"Unsloth: `{model_name}` is not a full model or a PEFT model.")

is_peft = False

# Cannot be both!
if is_model and is_peft:
raise RuntimeError(
"Unsloth: You repo has a LoRA adapter and a base model.\n"\
"You have 2 files `config.json` and `adapter_config.json`.\n"\
"We must only allow one config file.\n"\
"Please separate the LoRA and base models to 2 repos."
)
elif not is_model and not is_peft:
raise RuntimeError(
f"Unsloth: `{model_name}` is not a base model or a PEFT model.\n"\
"We could not locate a `config.json` or `adapter_config.json` file"
)
pass

# Get base model for PEFT:
if is_peft:
# Check base model again for PEFT
model_name = _get_model_name(peft_config.base_model_name_or_path, load_in_4bit)
model_config = AutoConfig.from_pretrained(model_name, token = token)
is_peft = True
model_config = AutoConfig.from_pretrained(model_name, token = token, revision = revision)
pass

model_type = model_config.model_type
Expand Down
10 changes: 7 additions & 3 deletions unsloth/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,13 @@ def save_to_gguf(
model_dtype = "f16" if model_dtype == "float16" else "bf16"

# Convert quantization_method to list
quantization_method = \
quantization_method if type(quantization_method) is list else list(quantization_method)

if isinstance(quantization_method, list): pass
elif isinstance(quantization_method, str): quantization_method = [ quantization_method, ]
elif isinstance(quantization_method, tuple): quantization_method = list(quantization_method)
else:
raise TypeError("Unsloth: quantization_method can only be a string or a list of strings")
pass

# Check if bfloat16 is supported
if model_dtype == "bf16" and not torch.cuda.is_bf16_supported():
logger.warning(
Expand Down