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.

Scope WavLM's 4-bit attention skip to WavLM's own layers (#16060)

Daniel Hanf842c04a6965
diff --git a/tests/python/test_wavlm_4bit_attention_skip.py b/tests/python/test_wavlm_4bit_attention_skip.py
index 9f4fbe991bc..1474b424fb4 100644
--- a/tests/python/test_wavlm_4bit_attention_skip.py
+++ b/tests/python/test_wavlm_4bit_attention_skip.py
@@ -92,3 +92,82 @@ def test_transformers_4_matcher_keeps_wavlm_attention_and_a_bart_decoder_quantiz
"decoder.model.decoder.layers.0.fc1",
):
assert not _transformers4_skips(name, skip), name
+
+
+def _zoo_transformers5_skips(name, keys):
+ """unsloth_zoo's patched transformers 5 `should_convert_module`: regex prefix, suffix, or a path component."""
+ import re
+ return any(
+ re.match(f"{k}\\.", name)
+ or re.match(k, name)
+ or name.endswith(k)
+ or f".{k}." in f".{name}."
+ for k in keys
+ )
+
+
+def _wavlm_bert_speech_encoder_decoder_linears(num_layers = 24):
+ """Linear names of SpeechEncoderDecoderModel(WavLM encoder, BERT decoder with cross-attention),
+ the same on transformers 4.57 and 5.x."""
+ names = [
+ "encoder.feature_projection.projection",
+ "decoder.cls.predictions.transform.dense",
+ "decoder.cls.predictions.decoder",
+ ]
+ for i in range(num_layers):
+ enc = f"encoder.encoder.layers.{i}."
+ names += [
+ enc + "attention." + p
+ for p in ("q_proj", "k_proj", "v_proj", "out_proj", "gru_rel_pos_linear")
+ ]
+ names += [enc + "feed_forward.intermediate_dense", enc + "feed_forward.output_dense"]
+ dec = f"decoder.bert.encoder.layer.{i}."
+ for attn in ("attention", "crossattention"):
+ names += [dec + attn + ".self." + p for p in ("query", "key", "value")] + [
+ dec + attn + ".output.dense"
+ ]
+ names += [dec + "intermediate.dense", dec + "output.dense"]
+ return names
+
+
+def test_the_composite_names_match_transformers():
+ torch = pytest.importorskip("torch")
+ transformers = pytest.importorskip("transformers")
+ encoder = transformers.WavLMConfig(
+ hidden_size = 32,
+ num_hidden_layers = 2,
+ num_attention_heads = 2,
+ intermediate_size = 64,
+ conv_dim = (8, 8),
+ conv_stride = (5, 2),
+ conv_kernel = (10, 3),
+ num_conv_pos_embeddings = 4,
+ )
+ decoder = transformers.BertConfig(
+ hidden_size = 32,
+ num_hidden_layers = 2,
+ num_attention_heads = 2,
+ intermediate_size = 64,
+ vocab_size = 32,
+ is_decoder = True,
+ add_cross_attention = True,
+ )
+ config = transformers.SpeechEncoderDecoderConfig.from_encoder_decoder_configs(encoder, decoder)
+ with torch.device("meta"):
+ model = transformers.SpeechEncoderDecoderModel(config)
+ linears = {n for n, m in model.named_modules() if isinstance(m, torch.nn.Linear)}
+ assert linears == set(_wavlm_bert_speech_encoder_decoder_linears(2))
+
+
[email protected]("matcher", [_transformers4_skips, _zoo_transformers5_skips])
+def test_a_bert_decoder_attention_is_still_quantized(matcher):
+ skip = _architecture_skip_modules()(["bert", "speech-encoder-decoder", "wavlm"])
+ linears = _wavlm_bert_speech_encoder_decoder_linears()
+ kept = {n for n in linears if matcher(n, skip)}
+ decoder_kept = sorted(n for n in kept if n.startswith("decoder."))
+ assert not decoder_kept, decoder_kept
+ encoder_attention = {
+ n for n in linears if n.startswith("encoder.encoder.layers.") and ".attention." in n
+ }
+ assert {n for n in encoder_attention if not n.endswith("gru_rel_pos_linear")} <= kept
+ assert kept <= encoder_attention
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 1b50b868606..f264f8fa027 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -2831,17 +2831,17 @@ def _architecture_skip_modules(model_types):
skip.append("proj_out")
# WavLM's attention hands q/k/v/out_proj.weight to F.multi_head_attention_forward, which a packed Params4bit cannot be.
if any(mt == "wavlm" for mt in model_types):
- # Qualified so a composite's decoder (SpeechEncoderDecoderModel) stays quantized: transformers 5 matches
- # "attention.q_proj" as a suffix; 4.x matches only bare leaf names or ancestors, hence "attention".
+ # Qualified so a composite's decoder stays quantized: 5.x matches "attention.q_proj" as a suffix; a bare
+ # "attention" on 4.x also caught a BERT decoder's (cross)attention, so name WavLM's own layers instead.
skip.extend(
(
- "attention",
"attention.q_proj",
"attention.k_proj",
"attention.v_proj",
"attention.out_proj",
)
)
+ skip.extend(f"encoder.layers.{i}.attention" for i in range(48))
# 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.