move forced float32

This commit is contained in:
Daniel Han 2025-03-17 04:41:44 -07:00
commit 9a356a7f79
3 changed files with 32 additions and 18 deletions

View file

@ -121,6 +121,11 @@ from unsloth_zoo.temporary_patches import (
for temporary_patch in TEMPORARY_PATCHES:
temporary_patch()
global FORCE_FLOAT32
FORCE_FLOAT32 = [
"gemma3",
]
# =============================================
# Disable some warnings which can get annoying
warnings.filterwarnings(action = "ignore", category = UserWarning, module = "torch")
@ -1127,6 +1132,7 @@ pass
def unsloth_compile_transformers(
dtype,
model_name,
token = None,
revision = None,
@ -1176,6 +1182,20 @@ def unsloth_compile_transformers(
if disable: return
# Set forced float32 env flag
os.environ["UNSLOTH_FORCE_FLOAT32"] = "0"
do_forced_float32 = False
for disable_name in FORCE_FLOAT32:
if (disable_name.lower() == model_types[1].lower() or \
disable_name.lower() in model_name.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"
do_forced_float32 = True
break
pass
for model_type in model_types:
_unsloth_compile_transformers(
model_type,

View file

@ -460,7 +460,14 @@ class FastModel(FastBaseModel):
*args, **kwargs,
):
if token is None: token = get_token()
assert (dtype is None or dtype in (torch.float16, torch.bfloat16, torch.float32))
SUPPORTS_BFLOAT16 = is_bfloat16_supported()
if dtype is None:
dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16
elif dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16:
logger.warning_once("Device does not support bfloat16. Will change to float16.")
dtype = torch.float16
assert(dtype in (torch.float16, torch.bfloat16, torch.float32))
patch_compiled_autograd()
patch_compiling_bitsandbytes()
@ -614,6 +621,7 @@ class FastModel(FastBaseModel):
with redirector:
patch_loss_functions(torch_compile = False)
model_types = unsloth_compile_transformers(
dtype = dtype,
model_name = model_name,
sdpa_dynamic_mask = True,
sdpa_bool_masks = True,

View file

@ -65,11 +65,6 @@ __all__ = [
"FastBaseModel",
]
global FORCE_FLOAT32
FORCE_FLOAT32 = [
"gemma3",
]
global FORCE_EAGER_ATTENTION
FORCE_EAGER_ATTENTION = [
"pixtral", # Pixtral SDPA not implemented
@ -215,20 +210,11 @@ class FastBaseModel:
assert(dtype in (torch.float16, torch.bfloat16, torch.float32))
global FORCE_FLOAT32
os.environ["UNSLOTH_FORCE_FLOAT32"] = "0"
bnb_compute_dtype = dtype
do_forced_float32 = False
for disable_name in FORCE_FLOAT32:
if (disable_name.lower() == model_type_arch.lower() or \
disable_name.lower() in model_name.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"
bnb_compute_dtype = torch.float16
do_forced_float32 = True
break
if os.environ.get("UNSLOTH_FORCE_FLOAT32", "0") == "1":
bnb_compute_dtype = torch.float16
do_forced_float32 = True
pass
global FORCE_EAGER_ATTENTION