Query compression

Query latent

×

The compressed query features before normalization.

shape
cQc^{Q} [B batch, T positions, 1536 q latent]
Sources

Contextual evidence from Query down-projection

MLA owns a fused down-projection of q_lora_rank + kv_lora_rank + qk_rope_head_dim outputs, the query RMSNorm and up-projection, the latent RMSNorm, the key–value up-projection and the output projection, all without bias; the latent norms take the configuration’s ε; the softmax scale is qk_head_dim^−0.5; with attention_factor 1 the rope type becomes deepseek_llama_scaling, the rotation is not NeoX style, and the YaRN mscale is folded into the softmax scale only for deepseek_yarn.

deepseek_v2.py 1033–1136 ↗ · vllm-project/vllm @ 5df63a7203cd

Excerpt
        self.qk_head_dim = qk_nope_head_dim + qk_rope_head_dim
        self.v_head_dim = v_head_dim

        self.q_lora_rank = q_lora_rank
        self.kv_lora_rank = kv_lora_rank

        self.num_heads = num_heads
        tp_size = get_tensor_model_parallel_world_size()
        assert num_heads % tp_size == 0
        self.num_local_heads = num_heads // tp_size

        self.scaling = self.qk_head_dim**-0.5

… lines 1045–1050 omitted …

        if self.q_lora_rank is not None:
            self.fused_qkv_a_proj = DeepSeekV2FusedQkvAProjLinear(
                proj_input_size,
                [self.q_lora_rank, self.kv_lora_rank + self.qk_rope_head_dim],
                quant_config=quant_config,
                prefix=f"{prefix}.fused_qkv_a_proj",
            )

… lines 1058–1080 omitted …

        if self.q_lora_rank is not None:
            self.q_a_layernorm = RMSNorm(self.q_lora_rank, eps=config.rms_norm_eps)
            self.q_b_proj = q_proj_cls(
                self.q_lora_rank,
                self.num_heads * self.qk_head_dim,
                bias=False,
                quant_config=quant_config,
                prefix=f"{prefix}.q_b_proj",
            )
        else:
            self.q_proj = q_proj_cls(
                proj_input_size,
                self.num_heads * self.qk_head_dim,
                bias=False,
                quant_config=quant_config,
                prefix=f"{prefix}.q_proj",
            )
        self.kv_a_layernorm = RMSNorm(self.kv_lora_rank, eps=config.rms_norm_eps)
        self.kv_b_proj = ColumnParallelLinear(
            self.kv_lora_rank,
            self.num_heads * (self.qk_nope_head_dim + self.v_head_dim),
            bias=False,
            quant_config=quant_config,
            prefix=f"{prefix}.kv_b_proj",
        )
        self.o_proj = RowParallelLinear(
            self.num_heads * self.v_head_dim,
            self.hidden_size,
            bias=False,
            reduce_results=reduce_results,
            quant_config=quant_config,
            prefix=f"{prefix}.o_proj",

… lines 1113–1114 omitted …

        if config.rope_parameters["rope_type"] != "default":
            config.rope_parameters["rope_type"] = (
                "deepseek_llama_scaling"
                if config.rope_parameters.get("attention_factor") == 1.0
                else "deepseek_yarn"
            )

        self.rotary_emb = get_rope(
            qk_rope_head_dim,
            max_position=max_position_embeddings,
            rope_parameters=config.rope_parameters,
            is_neox_style=False,
        )

        if (
            config.rope_parameters["rope_type"] != "default"
            and config.rope_parameters["rope_type"] == "deepseek_yarn"
        ):
            mscale_all_dim = config.rope_parameters.get("mscale_all_dim", False)
            scaling_factor = config.rope_parameters["factor"]
            mscale = yarn_get_mscale(scaling_factor, float(mscale_all_dim))
            self.scaling = self.scaling * mscale * mscale

The fused down-projection’s output is split into the query latent and the key–value part; the query latent is normalized, projected up by q_b_proj and viewed as heads of qk_head_dim features, and RoPE rotates each head’s last qk_rope_head_dim features.

mla.py 183–230 ↗ · vllm-project/vllm @ 5df63a7203cd

Excerpt
            qkv_lora = self.fused_qkv_a_proj(hidden_states)[0]
            q_c, kv_lora = qkv_lora.split(
                [self.q_lora_rank, self.kv_lora_rank + self.qk_rope_head_dim],
                dim=-1,
            )
            q_proj_layer = self.q_b_proj
            if not self.fuse_qkv_rmsnorm:
                q_c = self.q_a_layernorm(q_c)
            q_proj_input = q_c

… lines 192–220 omitted …

        q = q_proj_layer(q_proj_input)[0]
        heads = self.num_heads
        if self.dcp_q_replicate:
            heads *= q_proj_layer.group_size
        q = q.view(-1, heads, self.qk_head_dim)

        if self.rotary_emb is not None:
            q[..., self.qk_nope_head_dim :], k_pe = self.rotary_emb(
                positions, q[..., self.qk_nope_head_dim :], k_pe
            )

The checkpoint’s quantization: compressed-tensors FP8 for every Linear, weights in 128 × 128 blocks with one scale each, activations quantized dynamically in groups of 128; the token embedding, the MLA down-projections (q_a_proj and kv_a_proj_with_mqa), the routers (gate), the output head and the vision parts are left out.

params.json 31–86 ↗ · mistralai/Mistral-Large-3-675B-Instruct-2512 @ 383ffea2c7d6

Excerpt
  "quantization_config": {
    "config_groups": {
      "FP8_BLOCK": {
        "format": "float-quantized",
        "input_activations": {
          "actorder": null,
          "block_structure": null,
          "dynamic": true,
          "group_size": 128,
          "num_bits": 8,
          "observer": null,
          "observer_kwargs": {},
          "strategy": "group",
          "symmetric": true,
          "type": "float"
        },
        "output_activations": null,
        "targets": [
          "Linear"
        ],
        "weights": {
          "actorder": null,
          "block_structure": [
            128,
            128
          ],
          "dynamic": false,
          "group_size": null,
          "num_bits": 8,
          "observer": "static_minmax",
          "observer_kwargs": {},
          "strategy": "block",
          "symmetric": true,
          "type": "float"
        }
      }
    },
    "format": "float-quantized",
    "global_compression_ratio": null,
    "ignore": [
      "model.embed_tokens",
      "re:patch_merger.*",
      "re:vision_encoder.*",
      "re:vision_language_adapter.*",
      "re:.*kv_a_proj_with_mqa$",
      "re:.*q_a_proj$",
      "re:.*gate$",
      "lm_head"
    ],
    "kv_cache_scheme": null,
    "quant_method": "compressed-tensors",
    "quantization_status": "compressed",
    "sparsity_config": {},
    "transform_config": {},
    "version": "0.12.3.dev29+g73c2cf9.d20251119"
  },

Contextual evidence from Query latent RMSNorm

RMSNorm normalizes in float32, casts to the gain’s type, multiplies by the gain and casts back; the fused form first adds the residual in float32 and returns the normalized sum and the sum, cast back to the input type.

layernorm.py 9–62 ↗ · vllm-project/vllm @ 5df63a7203cd

Excerpt
@register_op
def rms_norm(
    x: Tensor, weight: Tensor | None, epsilon: float, variance_size: int | None = None
) -> Tensor:
    """Weighted root-mean-square layer normalization."""
    orig_dtype = x.dtype
    x = x.to(torch.float32)
    x_var = x if variance_size is None else x[..., :variance_size]
    variance = x_var.pow(2).mean(dim=-1, keepdim=True)
    x = x * torch.rsqrt(variance + epsilon)
    if weight is not None:
        x = x.to(weight.dtype) * weight
    return x.to(orig_dtype)

… lines 22–42 omitted …

@register_op(allow_inplace=True)
def fused_add_rms_norm(
    x: Tensor,
    x_residual: Tensor,
    weight: Tensor | None,
    epsilon: float,
    variance_size: int | None = None,
) -> tuple[Tensor, Tensor]:
    """Fused add and weighted root-mean-square layer normalization."""
    orig_dtype = x.dtype
    x = x.to(torch.float32)
    x = x + x_residual.to(torch.float32)
    x_residual = x.to(orig_dtype)

    x_var = x if variance_size is None else x[..., :variance_size]
    variance = x_var.pow(2).mean(dim=-1, keepdim=True)
    x = x * torch.rsqrt(variance + epsilon)
    if weight is not None:
        x = x.to(weight.dtype) * weight
    return x.to(orig_dtype), x_residual