[Core] Support fp32 lm_head for generation models via head_dtype (RFC #48305 §3.6) (#48390)

Signed-off-by: Karthik Kothuri <karthikkothuri2009@gmail.com>
Signed-off-by: wang.yuqi <yuqi.wang@daocloud.io>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: wang.yuqi <yuqi.wang@daocloud.io>
This commit is contained in:
Karthik Kothuri
2026-07-13 16:43:34 +08:00
committed by GitHub
co-authored by Claude wang.yuqi
parent 56a357ed33
commit 107a03ba63
6 changed files with 249 additions and 15 deletions
+5 -11
View File
@@ -1709,24 +1709,18 @@ class ModelConfig:
such as the lm_head in a generation model,
or the score or classifier in a classification model.
`head_dtype` currently only supports pooling models.
- The pooling model defaults to using fp32 head, you can use
- Pooling models default to an fp32 head; use
--hf-overrides '{"head_dtype": "model"}' to disable it.
- Generation models default to the model dtype; set
--hf-overrides '{"head_dtype": "float32"}' to run the lm_head in
fp32, which is required for RL training-inference consistency
(the trainer computes logits in fp32).
"""
head_dtype = _get_head_dtype(
config=self.hf_config, dtype=self.dtype, runner_type=self.runner_type
)
if self.runner_type != "pooling" and head_dtype != self.dtype:
logger.warning_once(
"`head_dtype` currently only supports pooling models, "
"fallback to model dtype [%s].",
self.dtype,
)
return self.dtype
if head_dtype not in current_platform.supported_dtypes:
logger.warning_once(
"The current platform does not support [%s] head dtype, "
+9
View File
@@ -45,6 +45,15 @@ class LogitsProcessorWithLoRA(BaseLayerWithLoRA):
self.hidden_size = hidden_size
self.dtype = dtype
self.device = device
# The fp32 lm_head path lives in the base LogitsProcessor._get_logits,
# which this wrapper bypasses. Rather than silently emit model-dtype
# logits, reject the combination until the LoRA path supports it.
head_dtype = getattr(base_layer, "head_dtype", None)
if head_dtype is not None and head_dtype != dtype:
raise ValueError(
"A head_dtype different from the model dtype (e.g. an fp32 "
"lm_head) is not yet supported with LoRA."
)
self.tp_size = get_tensor_model_parallel_world_size()
self.tp_rank = get_tensor_model_parallel_rank()
self.sharded_to_full_mapping = sharded_to_full_mapping
+51 -3
View File
@@ -3,14 +3,19 @@
"""A layer that compute logits from hidden_stats."""
import torch
import torch.nn.functional as F
from vllm.config import get_current_vllm_config
from vllm.distributed import (
get_tensor_model_parallel_world_size,
tensor_model_parallel_all_gather,
tensor_model_parallel_gather,
)
from vllm.model_executor.custom_op import PluggableLayer
from vllm.model_executor.layers.vocab_parallel_embedding import VocabParallelEmbedding
from vllm.model_executor.layers.vocab_parallel_embedding import (
UnquantizedEmbeddingMethod,
VocabParallelEmbedding,
)
from vllm.platforms import current_platform
@@ -50,6 +55,11 @@ class LogitsProcessor(PluggableLayer):
self.soft_cap = soft_cap
# Whether to use gather or all-gather to gather the logits.
self.use_all_gather = current_platform.use_all_gather()
# Dtype of the lm_head projection. Defaults to the model dtype; an
# fp32 head (via `--hf-overrides '{"head_dtype": "float32"}'`) is
# required for RL training-inference consistency.
model_config = get_current_vllm_config().model_config
self.head_dtype = model_config.head_dtype if model_config is not None else None
def forward(
self,
@@ -86,6 +96,44 @@ class LogitsProcessor(PluggableLayer):
logits = tensor_model_parallel_gather(logits)
return logits
def _apply_head(
self,
lm_head: VocabParallelEmbedding,
hidden_states: torch.Tensor,
embedding_bias: torch.Tensor | None,
) -> torch.Tensor:
"""Project hidden states through the lm_head, honoring head_dtype."""
if self.head_dtype is None or self.head_dtype == hidden_states.dtype:
return lm_head.quant_method.apply(
lm_head, hidden_states, bias=embedding_bias
)
if not isinstance(lm_head.quant_method, UnquantizedEmbeddingMethod):
raise ValueError(
"A head_dtype different from the model dtype is only "
"supported for an unquantized lm_head."
)
if (
self.head_dtype == torch.float32
and current_platform.is_cuda()
and hidden_states.is_cuda
):
# Accumulate the projection directly into fp32. This avoids
# materializing an fp32 copy of the lm_head weight on every step,
# unlike casting both operands. `torch.mm(out_dtype=...)` is
# CUDA-only and only supports fp32 output for fp16/bf16 inputs, so
# other cases fall back to the cast path below.
flat = hidden_states.reshape(-1, hidden_states.shape[-1])
logits = torch.mm(flat, lm_head.weight.t(), out_dtype=self.head_dtype)
if embedding_bias is not None:
logits = logits + embedding_bias.to(self.head_dtype)
return logits.reshape(*hidden_states.shape[:-1], -1)
return F.linear(
hidden_states.to(self.head_dtype),
lm_head.weight.to(self.head_dtype),
embedding_bias.to(self.head_dtype) if embedding_bias is not None else None,
)
def _get_logits(
self,
hidden_states: torch.Tensor,
@@ -93,7 +141,7 @@ class LogitsProcessor(PluggableLayer):
embedding_bias: torch.Tensor | None,
) -> torch.Tensor | None:
# Get the logits for the next tokens.
logits = lm_head.quant_method.apply(lm_head, hidden_states, bias=embedding_bias)
logits = self._apply_head(lm_head, hidden_states, embedding_bias)
# Gather logits for TP
logits = self._gather_logits(logits)
@@ -122,7 +170,7 @@ class LogitsProcessor(PluggableLayer):
)
tp_size = get_tensor_model_parallel_world_size()
logits = lm_head.quant_method.apply(lm_head, hidden_states, bias=embedding_bias)
logits = self._apply_head(lm_head, hidden_states, embedding_bias)
if self.soft_cap is not None:
logits = torch.tanh(logits / self.soft_cap) * self.soft_cap
if self.scale != 1.0: