Merge branch 'fix-issue-5344-quantization-guardrail' into feat-gemma4-moe-4bit-swap

This commit is contained in:
Daniel Han 2026-05-15 00:12:07 -07:00 committed by GitHub
commit cfdda21620
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 15 deletions

View file

@ -5,6 +5,7 @@ Covers two failure modes the helper detects:
2. partial bypass: bnb quantized nn.Linear but a large fraction of weight
bytes live in non-nn.Linear Parameters (e.g. Gemma-4 MoE fused experts).
"""
import warnings
import torch
@ -116,9 +117,7 @@ class _MoEFusedExpertWrapper(nn.Module):
def __init__(self, num_experts = 128, intermediate = 1408, hidden = 2816):
super().__init__()
self.gate_up_proj = nn.Parameter(
torch.zeros(
(num_experts, intermediate, hidden), dtype = torch.bfloat16
),
torch.zeros((num_experts, intermediate, hidden), dtype = torch.bfloat16),
requires_grad = False,
)

View file

@ -106,15 +106,25 @@ _BNB_QUANT_CLASS_NAMES = ("Linear4bit", "Linear8bitLt", "LinearNF4", "LinearFP4"
# norms, biases, routers/gates that need fp16/fp32 precision, vision/audio
# towers, classification heads, rotary tables.
_GUARDRAIL_SKIP_PATTERNS = (
"embed", "embedding",
"norm", "ln_", "rms",
"embed",
"embedding",
"norm",
"ln_",
"rms",
".bias",
"lm_head",
"multi_modal_projector", "merger", "modality_projection",
"router", "mlp.gate", "block_sparse_moe.gate",
"multi_modal_projector",
"merger",
"modality_projection",
"router",
"mlp.gate",
"block_sparse_moe.gate",
"mamba",
"audio_tower", "vision_tower",
"score", "classifier", "qa_outputs",
"audio_tower",
"vision_tower",
"score",
"classifier",
"qa_outputs",
"rotary",
)
@ -152,9 +162,7 @@ def _warn_if_quantization_silently_dropped(
if not (load_in_4bit or load_in_8bit):
return
has_bnb = any(
type(m).__name__ in _BNB_QUANT_CLASS_NAMES for m in model.modules()
)
has_bnb = any(type(m).__name__ in _BNB_QUANT_CLASS_NAMES for m in model.modules())
# Failure mode 1: total bypass.
if not has_bnb:
@ -199,9 +207,7 @@ def _warn_if_quantization_silently_dropped(
if suspect_bytes > 0 and suspect_bytes >= 2 * quantized_bytes:
kind = "4bit" if load_in_4bit else "8bit"
suspect_human = ", ".join(
f"{n} ({d}, {s})" for n, d, s in suspect_samples
)
suspect_human = ", ".join(f"{n} ({d}, {s})" for n, d, s in suspect_samples)
warnings.warn(
f"Unsloth: load_in_{kind}=True is partially applied. "
f"bitsandbytes quantized ~{quantized_bytes/1024**3:.2f} GB of "