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.

Point -bf16 4-bit requests at quantization_config, and warn when a bitsandbytes load quantized nothing (#11586)

Daniel Han999e964270f0
diff --git a/tests/test_bf16_quant_disabled_message.py b/tests/test_bf16_quant_disabled_message.py
index 174de207638..a9a359fc8d9 100644
--- a/tests/test_bf16_quant_disabled_message.py
+++ b/tests/test_bf16_quant_disabled_message.py
@@ -161,5 +161,21 @@ def test_the_notice_on_a_real_bare_call():
assert "PROBE_OK" in proc.stdout, proc.stderr[-3000:]
[email protected]("index", [0, 1, 2, 3])
+def test_the_notice_names_a_route_to_4bit_that_exists(index):
+ # Some -bf16 repos have no 4bit sibling (CohereLabs/command-a-plus-05-2026-bf16).
+ # Unsloth's 4bit LoRA kernels read the nested quant state and need a matching compute dtype.
+ guards = _bf16_notice_guards()
+ assert len(guards) == 4, "the notice moved; update this test"
+ text = ast.get_source_segment(SRC, guards[index].body[0]) or ""
+ for needed in (
+ "quantization_config",
+ "BitsAndBytesConfig",
+ "bnb_4bit_use_double_quant = True",
+ "bnb_4bit_compute_dtype",
+ ):
+ assert needed in text, (needed, text)
+
+
if __name__ == "__main__":
raise SystemExit(pytest.main([__file__, "-q"]))
diff --git a/tests/test_bnb_quantized_nothing_warning.py b/tests/test_bnb_quantized_nothing_warning.py
new file mode 100644
index 00000000000..3702a98c720
--- /dev/null
+++ b/tests/test_bnb_quantized_nothing_warning.py
@@ -0,0 +1,91 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""A load handed a bitsandbytes config that still built a 16bit model must say so."""
+
+import ast
+from pathlib import Path
+
+import pytest
+import torch
+
+from unsloth.models.loader_utils import (
+ _bnb_bits_requested,
+ warn_if_bitsandbytes_quantized_nothing,
+)
+
+ROOT = Path(__file__).resolve().parents[1]
+
+
+class Params4bit(torch.nn.Parameter):
+ """Only the type name is read."""
+
+
+class Linear4bit(torch.nn.Linear):
+ pass
+
+
+def _bnb4():
+ from transformers import BitsAndBytesConfig
+ return BitsAndBytesConfig(load_in_4bit = True)
+
+
+def test_bits_requested_reads_objects_and_dicts():
+ from transformers import BitsAndBytesConfig
+
+ assert _bnb_bits_requested(None) is None
+ assert _bnb_bits_requested(_bnb4()) == 4
+ assert _bnb_bits_requested(BitsAndBytesConfig(load_in_8bit = True)) == 8
+ assert _bnb_bits_requested({"quant_method": "bitsandbytes", "load_in_4bit": True}) == 4
+ assert _bnb_bits_requested({"quant_method": "fp8", "load_in_4bit": True}) is None
+ assert _bnb_bits_requested({"quant_method": "gptq", "bits": 4}) is None
+ assert _bnb_bits_requested({"load_in_4bit": True}) == 4
+ assert _bnb_bits_requested({"load_in_8bit": True}) == 8
+ assert _bnb_bits_requested({}) is None
+
+
+def test_warns_when_a_4bit_load_quantized_nothing(capsys):
+ model = torch.nn.Sequential(torch.nn.Linear(4, 4), torch.nn.Linear(4, 4))
+ assert warn_if_bitsandbytes_quantized_nothing(model, _bnb4(), "org/model") is True
+ out = capsys.readouterr().out
+ assert "4bit loading was on" in out and "org/model" in out and "16bit" in out
+
+
+def test_silent_when_a_linear_or_a_packed_parameter_is_quantized(capsys):
+ assert (
+ warn_if_bitsandbytes_quantized_nothing(torch.nn.Sequential(Linear4bit(4, 4)), _bnb4(), "m")
+ is False
+ )
+ experts = torch.nn.Module() # packed 3-D experts: Params4bit without any Linear4bit
+ experts.gate_up_proj = Params4bit(torch.zeros(8, 1), requires_grad = False)
+ assert (
+ warn_if_bitsandbytes_quantized_nothing(
+ torch.nn.Sequential(torch.nn.Linear(4, 4), experts), _bnb4(), "m"
+ )
+ is False
+ )
+ assert capsys.readouterr().out == ""
+
+
+def test_silent_without_a_bitsandbytes_request(capsys):
+ model = torch.nn.Sequential(torch.nn.Linear(4, 4))
+ assert warn_if_bitsandbytes_quantized_nothing(model, None, "m") is False
+ assert warn_if_bitsandbytes_quantized_nothing(model, {"quant_method": "fp8"}, "m") is False
+ assert capsys.readouterr().out == ""
+
+
+ "relpath, expected",
+ [("unsloth/models/vision.py", 1), ("unsloth/models/llama.py", 2)],
+)
+def test_every_in_process_load_is_checked(relpath, expected):
+ src = (ROOT / relpath).read_text(encoding = "utf-8")
+ calls = [
+ node
+ for node in ast.walk(ast.parse(src))
+ if isinstance(node, ast.Call)
+ and getattr(node.func, "id", None) == "warn_if_bitsandbytes_quantized_nothing"
+ ]
+ assert len(calls) == expected, relpath
+ for call in calls:
+ assert "quantization_config" in ast.unparse(call.args[1]), ast.unparse(call)
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index bb0f77fa9f9..618cc19bc2c 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -43,6 +43,7 @@ from .loader_utils import (
planner_quantization_kwargs,
requested_device_map,
resolve_unsloth_device_map,
+ warn_if_bitsandbytes_quantized_nothing,
)
from ..utils.packing import (
get_packed_info_from_kwargs,
@@ -2750,6 +2751,9 @@ class FastLlamaModel:
and not _head.weight.is_floating_point()
):
_head.to(dtype)
+ warn_if_bitsandbytes_quantized_nothing(
+ model, kwargs.get("quantization_config", None), model_name
+ )
# Attach dispatch hooks for bnb multi-device loads. The hooks stand aside only when vLLM
# owns the weights, which it never does here: vLLM has no classification head, so this
# branch loaded the weights in-process even though the caller asked for fast_inference.
@@ -2807,6 +2811,9 @@ class FastLlamaModel:
revision = revision,
**kwargs,
)
+ warn_if_bitsandbytes_quantized_nothing(
+ model, kwargs.get("quantization_config", None), model_name
+ )
from unsloth.models.vision import _attach_bnb_multidevice_hooks
from unsloth.models._remote_code_buffers import (
restore_remote_code_non_persistent_buffers,
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 8ab047c16f5..cf7b640066b 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -718,8 +718,11 @@ class FastLanguageModel(FastLlamaModel):
f"Unsloth: `{model_name}` is a 16bit (-bf16) checkpoint, so "
f"4bit/8bit/fp8 loading is disabled and the model will "
f"load in 16bit, which needs far more VRAM. Unsloth loads "
- f"4bit by default; point at the 4bit repo instead if you "
- f"wanted that."
+ f"4bit by default. To train in 4bit, point at a 4bit repo or "
+ f"pass quantization_config = BitsAndBytesConfig(load_in_4bit = True, "
+ f"bnb_4bit_use_double_quant = True, bnb_4bit_quant_type = 'nf4', "
+ f"bnb_4bit_compute_dtype = torch.bfloat16), which quantizes this "
+ f"checkpoint while it loads."
)
load_in_4bit = False
load_in_8bit = False
@@ -907,8 +910,11 @@ class FastLanguageModel(FastLlamaModel):
f"Unsloth: `{model_name}` is a 16bit (-bf16) checkpoint, so "
f"4bit/8bit/fp8 loading is disabled and the model will "
f"load in 16bit, which needs far more VRAM. Unsloth loads "
- f"4bit by default; point at the 4bit repo instead if you "
- f"wanted that."
+ f"4bit by default. To train in 4bit, point at a 4bit repo or "
+ f"pass quantization_config = BitsAndBytesConfig(load_in_4bit = True, "
+ f"bnb_4bit_use_double_quant = True, bnb_4bit_quant_type = 'nf4', "
+ f"bnb_4bit_compute_dtype = torch.bfloat16), which quantizes this "
+ f"checkpoint while it loads."
)
load_in_4bit = False
load_in_8bit = False
@@ -1501,8 +1507,11 @@ class FastModel(FastBaseModel):
f"Unsloth: `{model_name}` is a 16bit (-bf16) checkpoint, so "
f"4bit/8bit/fp8 loading is disabled and the model will "
f"load in 16bit, which needs far more VRAM. Unsloth loads "
- f"4bit by default; point at the 4bit repo instead if you "
- f"wanted that."
+ f"4bit by default. To train in 4bit, point at a 4bit repo or "
+ f"pass quantization_config = BitsAndBytesConfig(load_in_4bit = True, "
+ f"bnb_4bit_use_double_quant = True, bnb_4bit_quant_type = 'nf4', "
+ f"bnb_4bit_compute_dtype = torch.bfloat16), which quantizes this "
+ f"checkpoint while it loads."
)
load_in_4bit = False
load_in_8bit = False
@@ -1840,8 +1849,11 @@ class FastModel(FastBaseModel):
f"Unsloth: `{model_name}` is a 16bit (-bf16) checkpoint, so "
f"4bit/8bit/fp8 loading is disabled and the model will "
f"load in 16bit, which needs far more VRAM. Unsloth loads "
- f"4bit by default; point at the 4bit repo instead if you "
- f"wanted that."
+ f"4bit by default. To train in 4bit, point at a 4bit repo or "
+ f"pass quantization_config = BitsAndBytesConfig(load_in_4bit = True, "
+ f"bnb_4bit_use_double_quant = True, bnb_4bit_quant_type = 'nf4', "
+ f"bnb_4bit_compute_dtype = torch.bfloat16), which quantizes this "
+ f"checkpoint while it loads."
)
load_in_4bit = False
load_in_8bit = False
diff --git a/unsloth/models/loader_utils.py b/unsloth/models/loader_utils.py
index b87f12a0341..fb5464c36c5 100644
--- a/unsloth/models/loader_utils.py
+++ b/unsloth/models/loader_utils.py
@@ -1547,6 +1547,56 @@ def _restore_dropped_fp8_scales(
return (0, 0)
+_BNB_QUANTIZED_TYPES = ("Params4bit", "Int8Params", "Linear4bit", "Linear8bitLt")
+
+
+def _bnb_bits_requested(quantization_config):
+ """4 or 8 if a bitsandbytes config object or dict asks to quantize, else None."""
+ if quantization_config is None:
+ return None
+ if isinstance(quantization_config, dict):
+ get = quantization_config.get
+ else:
+ get = lambda key, default = None: getattr(quantization_config, key, default)
+ method = get("quant_method", "") or ""
+ method = str(getattr(method, "value", method)).lower()
+ # The dict shorthand {"load_in_4bit": True} has no quant_method but is still bitsandbytes.
+ if not method and isinstance(quantization_config, dict):
+ method = "bitsandbytes"
+ if "bitsandbytes" not in method:
+ return None
+ if get("load_in_4bit", False):
+ return 4
+ if get("load_in_8bit", False):
+ return 8
+ return None
+
+
+def warn_if_bitsandbytes_quantized_nothing(
+ model,
+ quantization_config,
+ model_name = "",
+):
+ """Warn and return True when a bitsandbytes load quantized no weight (e.g. every Linear skipped)."""
+ bits = _bnb_bits_requested(quantization_config)
+ if bits is None or model is None:
+ return False
+ try:
+ for module in model.modules():
+ if type(module).__name__ in _BNB_QUANTIZED_TYPES:
+ return False
+ for param in module.parameters(recurse = False):
+ if type(param).__name__ in _BNB_QUANTIZED_TYPES:
+ return False
+ except Exception:
+ return False
+ print(
+ f"Unsloth: WARNING: {bits}bit loading was on, but no weight of `{model_name}` was quantized, "
+ f"so the model is in 16bit and needs far more VRAM than a {bits}bit load."
+ )
+ return True
+
+
def check_and_disable_bitsandbytes_loading(
model_config,
load_in_4bit = True,
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 9d54436ce91..b9dc33173bc 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -103,6 +103,7 @@ from .loader_utils import (
planner_quantization_kwargs,
requested_device_map,
resolve_unsloth_device_map,
+ warn_if_bitsandbytes_quantized_nothing,
)
# `unsloth.save` imports `.models.loader_utils`, so binding a name out of it here at module
# scope closes a cycle and a cold `import unsloth.save` fails on the half-built module.
@@ -1772,6 +1773,9 @@ class FastBaseModel:
trust_remote_code = trust_remote_code,
**kwargs,
)
+ warn_if_bitsandbytes_quantized_nothing(
+ model, kwargs.get("quantization_config", None), model_name
+ )
if text_only_decoder:
# Must run before offload / hooks capture the weights.
_cast_text_only_prequantized_params(model, torch_dtype)