Skip to content

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.

Fix the FSDP2 QLoRA non-float parameter guard on Python 3.9 (#14774)

Daniel Hanf0c879a9cc79
diff --git a/tests/test_fsdp2_qlora.py b/tests/test_fsdp2_qlora.py
index c60cf9f9557..ad42a04d419 100644
--- a/tests/test_fsdp2_qlora.py
+++ b/tests/test_fsdp2_qlora.py
@@ -215,6 +215,24 @@ def test_nonfloat_guard_restores_parameter_new():
torch.nn.Parameter(torch.zeros(4, dtype = torch.uint8))
+class _Py39StaticMethod(staticmethod):
+ def __call__(self, *args, **kwargs):
+ raise TypeError("'staticmethod' object is not callable")
+
+
+def test_nonfloat_guard_works_when_staticmethod_is_not_callable(monkeypatch):
+ m = _load_module()
+ original = torch.nn.Parameter.__dict__["__new__"]
+ assert isinstance(original, staticmethod)
+ py39_new = _Py39StaticMethod(original.__func__)
+ monkeypatch.setattr(torch.nn.Parameter, "__new__", py39_new)
+ with m.nonfloat_param_guard():
+ p = torch.nn.Parameter(torch.zeros(4, dtype = torch.uint8))
+ assert p.requires_grad is False
+ assert torch.nn.Parameter(torch.zeros(4)).requires_grad is True
+ assert torch.nn.Parameter.__dict__["__new__"] is py39_new
+
+
def _free_port():
from tests._rendezvous_port import free_port
return free_port()
diff --git a/unsloth/models/fsdp2_qlora.py b/unsloth/models/fsdp2_qlora.py
index 947042f9a78..584dafc1d43 100644
--- a/unsloth/models/fsdp2_qlora.py
+++ b/unsloth/models/fsdp2_qlora.py
@@ -605,6 +605,8 @@ def nonfloat_param_guard(model = None):
p.requires_grad_(False)
Parameter = torch.nn.Parameter
original_new = Parameter.__dict__.get("__new__")
+ # Called through the class: a raw staticmethod object is not callable before Python 3.10.
+ call_new = Parameter.__new__
def __new__(
cls,
@@ -613,7 +615,7 @@ def nonfloat_param_guard(model = None):
):
if data is not None and not (data.is_floating_point() or data.is_complex()):
requires_grad = False
- return original_new(cls, data, requires_grad)
+ return call_new(cls, data, requires_grad)
Parameter.__new__ = __new__
try: