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.

Keep Gemma 3n AltUp coefficients out of on-the-fly 4-bit quantization (#14729)

Daniel Hana52ae09c9daa
diff --git a/tests/python/test_gemma3n_4bit_altup_skip.py b/tests/python/test_gemma3n_4bit_altup_skip.py
new file mode 100644
index 00000000000..acfed8f9b3e
--- /dev/null
+++ b/tests/python/test_gemma3n_4bit_altup_skip.py
@@ -0,0 +1,63 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team.
+
+"""Gemma 3n on-the-fly 4-bit keeps the AltUp coefficient Linears in 16-bit: AltUp clamps
+prediction_coefs.weight.data in place every training step, which fails on packed uint8 weights."""
+
+import ast
+from pathlib import Path
+
+VISION_PATH = Path(__file__).resolve().parents[2] / "unsloth" / "models" / "vision.py"
+ALTUP = {"correction_coefs", "prediction_coefs", "altup_projections", "altup_unembed_projections"}
+
+
+def _architecture_skip_modules():
+ tree = ast.parse(VISION_PATH.read_text(encoding = "utf-8"))
+ node = next(
+ n
+ for n in tree.body
+ if isinstance(n, ast.FunctionDef) and n.name == "_architecture_skip_modules"
+ )
+ ns = {}
+ exec(compile(ast.Module([node], []), str(VISION_PATH), "exec"), ns)
+ return ns["_architecture_skip_modules"]
+
+
+def test_gemma3n_keeps_altup_coefficients_unquantized():
+ skip = _architecture_skip_modules()
+ for model_types in (
+ ["gemma3n"],
+ ["gemma3n", "gemma3n_text", "gemma3n_audio"],
+ ["gemma3n_text"],
+ ):
+ assert ALTUP <= set(skip(model_types)), model_types
+
+
+def test_other_models_unchanged():
+ skip = _architecture_skip_modules()
+ for model_types in (["gemma3"], ["gemma4"], ["llama"], [], None):
+ assert not ALTUP & set(skip(model_types)), model_types
+
+
+def test_altup_linears_match_the_transformers_skip_matcher():
+ """transformers 5 skips a Linear only when a key matches its full name (suffix or start-anchored regex)."""
+ import pytest
+
+ utils = pytest.importorskip("transformers.quantizers.quantizers_utils")
+ should_convert = getattr(utils, "should_convert_module", None)
+ if should_convert is None:
+ pytest.skip("transformers < 5 matches skip keys per path component")
+ skip = _architecture_skip_modules()(["gemma3n"])
+ for name in (
+ "model.language_model.altup_projections.0",
+ "model.language_model.altup_projections.2",
+ "model.language_model.altup_unembed_projections.1",
+ "model.language_model.layers.0.altup.prediction_coefs",
+ "model.language_model.layers.3.altup.correction_coefs",
+ ):
+ assert not should_convert(name, skip), name
+ for name in (
+ "model.language_model.layers.0.self_attn.q_proj",
+ "model.language_model.layers.0.mlp.down_proj",
+ ):
+ assert should_convert(name, skip), name
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 3cf06446f7b..02d59db2a7b 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -2606,6 +2606,14 @@ def _architecture_skip_modules(model_types):
# Kimi-K3's attention residuals read the Linear(hidden, 1) projections' .weight as a score vector, which a packed Params4bit cannot be.
if any(mt in ("kimi_k3", "kimi_linear") for mt in model_types):
skip.extend(("self_attention_res_proj", "mlp_res_proj", "output_attn_res_proj"))
+ # Gemma 3n AltUp clamps prediction_coefs.weight.data in place every training step, which fails on packed
+ # uint8 weights; the same four modules unsloth/gemma-3n-*-unsloth-bnb-4bit keeps in 16-bit.
+ # transformers 5 matches a skip key as a suffix of each Linear's name, so the ModuleList children are listed too.
+ if any(mt.startswith("gemma3n") for mt in model_types):
+ skip.extend(("correction_coefs", "prediction_coefs"))
+ for name in ("altup_projections", "altup_unembed_projections"):
+ skip.append(name)
+ skip.extend(f"{name}.{i}" for i in range(8))
return skip