diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index d1df57ad5c..ab2694fde1 100644 --- a/unsloth/models/_utils.py +++ b/unsloth/models/_utils.py @@ -68,6 +68,7 @@ __all__ = [ "patch_fast_lora", "validate_loftq_config", "RaiseUninitialized", + "dequantize_module_weight", ] import torch @@ -724,6 +725,7 @@ pass # Weirdly LoraLayer.update_layer downcasts PEFT layers to float16?? # For mixed precision, we need it to be in float32 not float16. from peft import __version__ as peft_version +from peft.utils.integrations import dequantize_module_weight if Version(peft_version) < Version("0.12.0"): from peft.tuners.lora.layer import LoraLayer try: diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index 7ac27158a2..d0b7d4dc4c 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -618,9 +618,18 @@ class FastModel(FastBaseModel): os.environ["UNSLOTH_FORCE_CUSTOM_DTYPE"] = \ "all;None;None;"\ "x = 'gate_up_proj_bias'\n"\ - "if hasattr(module, x): setattr(module, x, torch.nn.Parameter(getattr(module, x).to(torch.float32)) if isinstance(getattr(module, x), torch.nn.Parameter) else getattr(module, x).to(torch.float32))\n"\ + "if hasattr(module, x): "\ + "setattr(module, x, torch.nn.Parameter(getattr(module, x).to(torch.float32)) if isinstance(getattr(module, x), torch.nn.Parameter) else getattr(module, x).to(torch.float32))\n"\ "x = 'down_proj_bias'\n"\ - "if hasattr(module, x): setattr(module, x, torch.nn.Parameter(getattr(module, x).to(torch.float32)) if isinstance(getattr(module, x), torch.nn.Parameter) else getattr(module, x).to(torch.float32))\n;" + ";" + else: + # Set down projection compute dtype to be float32 for float16 machines + os.environ["UNSLOTH_FORCE_CUSTOM_DTYPE"] = \ + "all;None;None;"\ + "if 'down_projs' in name and hasattr(module, 'compute_dtype') and "\ + "torch.amax(dequantize_module_weight(module)) >= 1024:"\ + "module._pre_set_compute_dtype = torch.float32\n"\ + ";" else: for check_model_name in DISABLE_COMPILE_MODEL_NAMES: if check_model_name in lowered_model_name: