Skip to content

[BUG] rotation is accepted on Gemma2/Gemma3 (and other LlamaQModel subclasses) and silently breaks the model #4169

Closed

Description

@Qubitium

Severity: high — silent corruption
Version: 7.11.0 @ 6c3421c

Summary

models/base.py:2124-2128 guards rotation with isinstance(self, (LlamaQModel, Qwen2QModel)). The error message says "llama/qwen2 only", but Gemma2QModel, Gemma3QModel and about 17 other definitions subclass LlamaQModel, so they pass the check.

On Gemma, each of the following is wrong:

  • The tree tags input_layernorm and post_attention_layernorm with :norm, so the "two declared norms" check passes. post_attention_layernorm is then fused into gate/up, but in Gemma that norm sits on the attention output. The real MLP input norm is pre_feedforward_layernorm.
  • reset_ln sets the norm gain to 1, but Gemma's RMSNorm computes x * (1 + w), so the effective gain becomes 2.
  • The post-norms are not rotation-invariant.

Reproduce (tiny random models, CPU)

  1. Fuse norms, rotate (hadamard), and add the online Hadamard on down_proj.
  2. Compare logits with the unrotated model.
Model Relative logits error Argmax agreement
Llama 5.4e-7 1.00
Gemma2 1.10 0.00
from gptqmodel.models.module_plan import ModulePlan
from gptqmodel.quantization.rotation.rotation import fuse_layer_norms, rotate_model
# build transformers.Gemma2ForCausalLM(tiny cfg), randomize norm weights, then:
plan = ModulePlan.from_tree(Gemma2QModel.module_tree)
fuse_layer_norms(m, pre_lm_head_norm_module_name=Gemma2QModel.pre_lm_head_norm_module,
                 layers_node="model.layers", lm_head_name="lm_head", plan=plan)
rotate_model(m, "hadamard", torch.device("cpu"), lm_head_name="lm_head", layers_node="model.layers",
             plan=plan, embedding_name="model.embed_tokens")

Suggested fix

  • Check the exact model type rather than isinstance, or add an explicit supports_rotation = True opt-in on qualified definitions.
  • Add a neutral-rotation test, rotated logits ≈ original logits, for every definition that opts in.

Found during an AI-assisted review (Claude Code) of 6c3421c; repros were run on RTX 4090/5090 (CUDA 13) and Radeon AI PRO R9700 (ROCm) unless marked static/suspected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions