forced precision

This commit is contained in:
Daniel Han 2025-03-13 05:46:02 -07:00
commit 33c7219dc7
2 changed files with 13 additions and 2 deletions

View file

@ -986,8 +986,8 @@ def _unsloth_pre_compute_loss(self, model, inputs, *args, **kwargs):
"Read more on gradient accumulation issues here: https://unsloth.ai/blog/gradient"
)
pass
with torch.autocast(device_type = "cuda", dtype = torch.float32):
outputs = self._old_compute_loss(model, inputs, *args, **kwargs)
# with torch.autocast(device_type = "cuda", dtype = torch.float32):
outputs = self._old_compute_loss(model, inputs, *args, **kwargs)
return outputs
pass

View file

@ -65,6 +65,9 @@ __all__ = [
"FastBaseModel",
]
global FORCE_FLOAT32
FORCE_FLOAT32 = ["gemma3"]
def unsloth_base_fast_generate(
self,
@ -178,6 +181,14 @@ class FastBaseModel:
assert(dtype == torch.float16 or dtype == torch.bfloat16 or dtype == torch.float32)
global FORCE_FLOAT32
os.environ["UNSLOTH_FORCE_FLOAT32"] = "0"
for disable_name in FORCE_FLOAT32:
if disable_name.lower() == model_type_arch.lower() and dtype == torch.float16:
print(f"Unsloth: Using float16 precision for {model_type_arch} won't work! Using float32.")
os.environ["UNSLOTH_FORCE_FLOAT32"] = "1"
break
bnb_config = None
if full_finetuning and (load_in_4bit or load_in_8bit):
print("Unsloth: You selected full finetuning support, but 4bit / 8bit is enabled - disabling LoRA / QLoRA.")