Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Linear input-cast hook: skip float8-weight Linears (#14690)
Daniel Han2faad9fd0fc1
diff --git a/tests/test_custom_dtype_linear_input_cast.py b/tests/test_custom_dtype_linear_input_cast.pyindex 367d1e2f92d..76087ef0a96 100644--- a/tests/test_custom_dtype_linear_input_cast.py+++ b/tests/test_custom_dtype_linear_input_cast.py@@ -83,6 +83,21 @@ def test_no_hook_when_nothing_moved_off_the_load_dtype():assert cast_mixed_linear_inputs(model, torch.float16) == 0+def test_float8_weight_linears_get_no_hook():+ class _Fp8Linear(torch.nn.Linear):+ pass++ model = _falcon_like()+ model.fp8 = _Fp8Linear(8, 4, bias = False)+ model.fp8.weight = torch.nn.Parameter(+ model.fp8.weight.data.to(torch.float8_e4m3fn), requires_grad = False+ )+ assert cast_mixed_linear_inputs(model, torch.float32) == 1 # only the float16 q_proj+ assert not getattr(model.fp8, "_unsloth_casts_input_dtype", False)+ assert not model.fp8._forward_pre_hooks+ assert model.q_proj._unsloth_casts_input_dtype++def test_vision_loader_hooks_after_the_custom_dtype_code():tree = ast.parse(VISION.read_text(encoding = "utf-8"))for node in ast.walk(tree):diff --git a/unsloth/models/_custom_dtype.py b/unsloth/models/_custom_dtype.pyindex f87fa15698f..17a2aae8a8b 100644--- a/unsloth/models/_custom_dtype.py+++ b/unsloth/models/_custom_dtype.py@@ -146,6 +146,9 @@ def _cast_input_to_weight_dtype(module, args):return (x.to(weight.dtype),) + tuple(args[1:])+_INPUT_CAST_DTYPES = frozenset((torch.float16, torch.bfloat16, torch.float32))++def cast_mixed_linear_inputs(model, load_dtype):"""Outside autocast, cast inputs of Linears moved off load_dtype (Falcon-H1 / Nemotron-H fp16) to the weight dtype."""hooked = 0@@ -155,9 +158,10 @@ def cast_mixed_linear_inputs(model, load_dtype):):continueweight = getattr(module, "weight", None)+ # Not is_floating_point(): FP8Linear / FbgemmFp8Linear keep float8 weights and quantize their own input.if (not isinstance(weight, torch.Tensor)- or not weight.is_floating_point()+ or weight.dtype not in _INPUT_CAST_DTYPESor weight.dtype == load_dtype):continue