Skip to content
Merged
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
21 changes: 20 additions & 1 deletion unsloth/models/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@
from .gemma2 import FastGemma2Model
pass

def get_dtype_from_input(
dtype
):
'''Converts user-defined dtype input string to a usable dtype'''
TORCH_FLOAT16_SYNONYMS = {"torch.float16"}
TORCH_BFLOAT16_SYNONYMS = {"torch.bfloat16"}
TORCH_FLOAT32_SYNONYMS = {"torch.float32"}
if dtype in TORCH_FLOAT16_SYNONYMS:
return torch.float16
if dtype in TORCH_BFLOAT16_SYNONYMS:
return torch.bfloat16
if dtype in TORCH_FLOAT32_SYNONYMS:
return torch.float32
if dtype != "None":
print(f"--------------------------------------------------\n"\
f"User-specified dtype not recognised. Defaulting to dtype = None\n"\
f"--------------------------------------------------")
return None


def __get_model_name(
model_name,
Expand Down Expand Up @@ -332,7 +351,7 @@ def from_pretrained(
model, tokenizer = dispatch_model.from_pretrained(
model_name = model_name,
max_seq_length = max_seq_length,
dtype = dtype,
dtype = get_dtype_from_input(dtype),
load_in_4bit = load_in_4bit,
token = token,
device_map = device_map,
Expand Down