Causal attention

Scale scores

×

Scale similarities for the 192-feature head width and each query’s position.

St′=St⋅τt192{\htmlData{light=scaled scale}{S'_{t}}}={\htmlData{light=similarities similarity}{S_{t}}}\cdot\frac{{\htmlData{light=temperatures}{\tau_t}}}{\sqrt{192}}
in
SS [B batch, 128 heads, T queries, T keys]τ\tau [T positions]
out
S′S' [B batch, 128 heads, T queries, T keys]

vLLM multiplies the queries by their position’s factor after RoPE (q *= llama_4_scaling, mla.py 237–238), which multiplies each query’s scores alike; the backend multiplies the scores by self.scale, 1/1921/\sqrt{192} (deepseek_v2.py 1044; cpu_sdpa.py 69).

Explanation

tt is the query’s position, so its factor τt\tau_t, made once from the positions and supplied by the layer, is the same for every key it reads. A larger factor sharpens the softmax over the keys.

The adapter turns the configuration’s apply_scale: false into an attention factor of 1, so vLLM adds no YaRN correction to the scale (mistral.py 137–139; deepseek_v2.py 1115–1136).

Sources

In code* self.scale

Open pinned code ↗

Direct evidence

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

Once per forward pass the model computes 1 + β·ln(1 + ⌊position / original_max_position_embeddings⌋) for every position from the configuration’s llama_4_scaling and hands it to every layer.

deepseek_v2.py 445–1571 ↗ · vllm-project/vllm @ 5df63a7203cd

Excerpt
def _get_llama_4_scaling(
    original_max_position_embeddings: int, scaling_beta: float, positions: torch.Tensor
) -> torch.Tensor:
    scaling = 1 + scaling_beta * torch.log(
        1 + torch.floor(positions / original_max_position_embeddings)
    )
    # Broadcast over num_heads and head_dim
    return scaling[..., None, None]

… lines 453–1530 omitted …

        # Compute llama 4 scaling once per forward pass if enabled
        llama_4_scaling_config = getattr(self.config, "llama_4_scaling", None)
        llama_4_scaling: torch.Tensor | None
        if llama_4_scaling_config is not None:
            llama_4_scaling = _get_llama_4_scaling(
                original_max_position_embeddings=llama_4_scaling_config[
                    "original_max_position_embeddings"
                ],
                scaling_beta=llama_4_scaling_config["beta"],
                positions=positions,
            )
        else:
            llama_4_scaling = None

… lines 1544–1568 omitted …

            hidden_states, residual = layer(
                positions, hidden_states, residual, llama_4_scaling
            )