Skip to content
Merged
Prev Previous commit
Next Next commit
int8 LoRA base: the environment opt-in falls back to 16 bit where the…
… int8 base cannot run
  • Loading branch information
danielhanchen committed Nov 27, 2026
commit 4baa0591d863a09e79523e4a1371efcb59c4bc3c
10 changes: 10 additions & 0 deletions tests/test_int8_lora_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_registry_entry():
)
def test_env_request(monkeypatch, value, on):
monkeypatch.setenv("UNSLOTH_LORA_BASE_INT8", value)
monkeypatch.setattr(f8t, "int8_lora_unavailable", lambda gpu = "current": None)
assert f8t.int8_lora_requested() is on
assert f8t.float8_training_request(None) == ("int8_lora" if on else None)
assert f8t.float8_training_request(False) == ("int8_lora" if on else False)
Expand All @@ -34,6 +35,15 @@ def test_env_request(monkeypatch, value, on):
assert f8t.float8_training_request(None, full_finetuning = True) is None


@pytest.mark.parametrize("gpu", ["sm80", "sm89", "sm75", None])
def test_env_opt_in_on_unsupported_gpu_stays_16bit(monkeypatch, capsys, gpu):
monkeypatch.setenv("UNSLOTH_LORA_BASE_INT8", "1")
monkeypatch.setattr(P, "current_gpu_class", lambda: gpu)
assert f8t.int8_lora_unavailable() is not None
assert f8t.float8_training_request(None) is None
assert "ignored here" in capsys.readouterr().out


def test_env_unset_changes_nothing(monkeypatch):
monkeypatch.delenv("UNSLOTH_LORA_BASE_INT8", raising = False)
assert f8t.float8_training_request(None) is None
Expand Down
25 changes: 24 additions & 1 deletion unsloth/models/float8_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,33 @@ def int8_lora_requested() -> bool:
)


def int8_lora_unavailable(gpu = "current"):
"""Why the int8 LoRA base cannot run on this GPU class (registry gate, Triton, device), else None."""
if gpu == "current":
gpu = _precision.current_gpu_class()
if gpu is None:
return "no CUDA GPU is visible"
gate = _precision.get_recipe(INT8_LORA_RECIPE).gate(gpu)
if gate.status == "refused":
return f"{gpu}: {gate.reason}"
try:
from ..kernels import int8_lora
except ImportError as exc:
return f"the int8 kernels do not import ({exc})"
return int8_lora.int8_lora_supported(torch.device("cuda"))


def float8_training_request(float8_training, full_finetuning = False):
"""The LoRA frozen-base request: the float8_training kwarg, or 'int8_lora' when UNSLOTH_LORA_BASE_INT8=1
and the kwarg is unset."""
and the kwarg is unset. The environment opt-in never fails a load: where the int8 base cannot run it says
why and training stays 16 bit (float8_training = 'int8_lora' refuses instead)."""
if float8_training in (None, False) and not full_finetuning and int8_lora_requested():
why = int8_lora_unavailable()
if why is not None:
print(
f"Unsloth: UNSLOTH_LORA_BASE_INT8=1 is ignored here ({why}); training stays in 16 bit."
)
return float8_training
return "int8_lora"
return float8_training

Expand Down
Loading