+26









Konrad Zawora
Kunshang Ji
Sanju C Sudhakaran
Michal Adamczyk
Marceli Fylcek
Himangshu Lahkar
Vivek Goel
yuwenzho
Dominika Olszewska
barak goldberg
Michal Szutenberg
Jan Kaniecki
Agata Dobrzyniewicz
Krzysztof Wisniewski
Dudi Lester
Ilia Taraban
Chendi.Xue
Michał Kuligowski
Jakub Maksymczuk
Tomasz Zielinski
Sun Choi
Iryna Boiko
Bob Zhu
hlin99
Zehao Huang
Andrzej Kotłowski
Yan Tomsinsky
Nir David
Yu-Zhou
Ruheena Suhani Shaik
Karol Damaszke
Marcin Swiniarski
Woosuk Kwon
Jacek Czaja
Jacek Czaja
Yuan
a02a50e6e5
Signed-off-by: yuwenzho <yuwen.zhou@intel.com> Signed-off-by: Chendi.Xue <chendi.xue@intel.com> Signed-off-by: Bob Zhu <bob.zhu@intel.com> Signed-off-by: zehao-intel <zehao.huang@intel.com> Signed-off-by: Konrad Zawora <kzawora@habana.ai> Co-authored-by: Kunshang Ji <kunshang.ji@intel.com> Co-authored-by: Sanju C Sudhakaran <scsudhakaran@habana.ai> Co-authored-by: Michal Adamczyk <madamczyk@habana.ai> Co-authored-by: Marceli Fylcek <mfylcek@habana.ai> Co-authored-by: Himangshu Lahkar <49579433+hlahkar@users.noreply.github.com> Co-authored-by: Vivek Goel <vgoel@habana.ai> Co-authored-by: yuwenzho <yuwen.zhou@intel.com> Co-authored-by: Dominika Olszewska <dolszewska@habana.ai> Co-authored-by: barak goldberg <149692267+bgoldberg-habana@users.noreply.github.com> Co-authored-by: Michal Szutenberg <37601244+szutenberg@users.noreply.github.com> Co-authored-by: Jan Kaniecki <jkaniecki@habana.ai> Co-authored-by: Agata Dobrzyniewicz <160237065+adobrzyniewicz-habana@users.noreply.github.com> Co-authored-by: Krzysztof Wisniewski <kwisniewski@habana.ai> Co-authored-by: Dudi Lester <160421192+dudilester@users.noreply.github.com> Co-authored-by: Ilia Taraban <tarabanil@gmail.com> Co-authored-by: Chendi.Xue <chendi.xue@intel.com> Co-authored-by: Michał Kuligowski <mkuligowski@habana.ai> Co-authored-by: Jakub Maksymczuk <jmaksymczuk@habana.ai> Co-authored-by: Tomasz Zielinski <85164140+tzielinski-habana@users.noreply.github.com> Co-authored-by: Sun Choi <schoi@habana.ai> Co-authored-by: Iryna Boiko <iboiko@habana.ai> Co-authored-by: Bob Zhu <41610754+czhu15@users.noreply.github.com> Co-authored-by: hlin99 <73271530+hlin99@users.noreply.github.com> Co-authored-by: Zehao Huang <zehao.huang@intel.com> Co-authored-by: Andrzej Kotłowski <Andrzej.Kotlowski@intel.com> Co-authored-by: Yan Tomsinsky <73292515+Yantom1@users.noreply.github.com> Co-authored-by: Nir David <ndavid@habana.ai> Co-authored-by: Yu-Zhou <yu.zhou@intel.com> Co-authored-by: Ruheena Suhani Shaik <rsshaik@habana.ai> Co-authored-by: Karol Damaszke <kdamaszke@habana.ai> Co-authored-by: Marcin Swiniarski <mswiniarski@habana.ai> Co-authored-by: Woosuk Kwon <woosuk.kwon@berkeley.edu> Co-authored-by: Jacek Czaja <jacek.czaja@intel.com> Co-authored-by: Jacek Czaja <jczaja@habana.ai> Co-authored-by: Yuan <yuan.zhou@outlook.com>
206 lines
6.5 KiB
Python
206 lines
6.5 KiB
Python
"""Custom normalization layers."""
|
|
from typing import Optional, Tuple, Union
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
|
|
from vllm.model_executor.custom_op import CustomOp
|
|
|
|
|
|
@CustomOp.register("rms_norm")
|
|
class RMSNorm(CustomOp):
|
|
"""Root mean square normalization.
|
|
|
|
Computes x -> w * x / sqrt(E[x^2] + eps) where w is the learned weight.
|
|
Refer to https://arxiv.org/abs/1910.07467
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
hidden_size: int,
|
|
eps: float = 1e-6,
|
|
var_hidden_size: Optional[int] = None,
|
|
) -> None:
|
|
super().__init__()
|
|
|
|
self.hidden_size = hidden_size
|
|
self.variance_epsilon = eps
|
|
self.variance_size_override = (None if var_hidden_size == hidden_size
|
|
else var_hidden_size)
|
|
self.weight = nn.Parameter(torch.ones(hidden_size))
|
|
|
|
def forward_native(
|
|
self,
|
|
x: torch.Tensor,
|
|
residual: Optional[torch.Tensor] = None,
|
|
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
|
"""PyTorch-native implementation equivalent to forward()."""
|
|
orig_dtype = x.dtype
|
|
x = x.to(torch.float32)
|
|
if residual is not None:
|
|
x = x + residual.to(torch.float32)
|
|
residual = x.to(orig_dtype)
|
|
|
|
hidden_size = x.shape[-1]
|
|
if hidden_size != self.hidden_size:
|
|
raise ValueError("Expected hidden_size to be "
|
|
f"{self.hidden_size}, but found: {hidden_size}")
|
|
|
|
if self.variance_size_override is None:
|
|
x_var = x
|
|
else:
|
|
if hidden_size < self.variance_size_override:
|
|
raise ValueError(
|
|
"Expected hidden_size to be at least "
|
|
f"{self.variance_size_override}, but found: {hidden_size}")
|
|
|
|
x_var = x[:, :, :self.variance_size_override]
|
|
|
|
variance = x_var.pow(2).mean(dim=-1, keepdim=True)
|
|
|
|
x = x * torch.rsqrt(variance + self.variance_epsilon)
|
|
x = x.to(orig_dtype) * self.weight
|
|
if residual is None:
|
|
return x
|
|
else:
|
|
return x, residual
|
|
|
|
def forward_cuda(
|
|
self,
|
|
x: torch.Tensor,
|
|
residual: Optional[torch.Tensor] = None,
|
|
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
|
if self.variance_size_override is not None:
|
|
return self.forward_native(x, residual)
|
|
|
|
from vllm import _custom_ops as ops
|
|
|
|
if residual is not None:
|
|
ops.fused_add_rms_norm(
|
|
x,
|
|
residual,
|
|
self.weight.data,
|
|
self.variance_epsilon,
|
|
)
|
|
return x, residual
|
|
out = torch.empty_like(x)
|
|
ops.rms_norm(
|
|
out,
|
|
x,
|
|
self.weight.data,
|
|
self.variance_epsilon,
|
|
)
|
|
return out
|
|
|
|
def forward_hpu(
|
|
self,
|
|
x: torch.Tensor,
|
|
residual: Optional[torch.Tensor] = None,
|
|
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
|
from vllm_hpu_extension.ops import HPUFusedRMSNorm
|
|
if HPUFusedRMSNorm is None:
|
|
return self.forward_native(x, residual)
|
|
if residual is not None:
|
|
orig_shape = x.shape
|
|
residual += x.view(residual.shape)
|
|
# Note: HPUFusedRMSNorm requires 3D tensors as inputs
|
|
x = HPUFusedRMSNorm.apply(residual, self.weight,
|
|
self.variance_epsilon)
|
|
return x.view(orig_shape), residual
|
|
|
|
x = HPUFusedRMSNorm.apply(x, self.weight, self.variance_epsilon)
|
|
return x
|
|
|
|
def forward_xpu(
|
|
self,
|
|
x: torch.Tensor,
|
|
residual: Optional[torch.Tensor] = None,
|
|
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
|
if self.variance_size_override is not None:
|
|
return self.forward_native(x, residual)
|
|
|
|
from vllm._ipex_ops import ipex_ops as ops
|
|
|
|
if residual is not None:
|
|
ops.fused_add_rms_norm(
|
|
x,
|
|
residual,
|
|
self.weight.data,
|
|
self.variance_epsilon,
|
|
)
|
|
return x, residual
|
|
return ops.rms_norm(
|
|
x,
|
|
self.weight.data,
|
|
self.variance_epsilon,
|
|
)
|
|
|
|
def extra_repr(self) -> str:
|
|
s = f"hidden_size={self.weight.data.size(0)}"
|
|
s += f", eps={self.variance_epsilon}"
|
|
return s
|
|
|
|
|
|
@CustomOp.register("gemma_rms_norm")
|
|
class GemmaRMSNorm(CustomOp):
|
|
"""RMS normalization for Gemma.
|
|
|
|
Two differences from the above RMSNorm:
|
|
1. x * (1 + w) instead of x * w.
|
|
2. (x * w).to(orig_dtype) instead of x.to(orig_dtype) * w.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
hidden_size: int,
|
|
eps: float = 1e-6,
|
|
) -> None:
|
|
super().__init__()
|
|
self.weight = nn.Parameter(torch.zeros(hidden_size))
|
|
self.variance_epsilon = eps
|
|
|
|
@staticmethod
|
|
def forward_static(
|
|
weight: torch.Tensor,
|
|
variance_epsilon: float,
|
|
x: torch.Tensor,
|
|
residual: Optional[torch.Tensor],
|
|
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
|
"""PyTorch-native implementation equivalent to forward()."""
|
|
orig_dtype = x.dtype
|
|
if residual is not None:
|
|
x = x + residual
|
|
residual = x
|
|
|
|
x = x.float()
|
|
variance = x.pow(2).mean(dim=-1, keepdim=True)
|
|
x = x * torch.rsqrt(variance + variance_epsilon)
|
|
# Llama does x.to(float16) * w whilst Gemma is (x * w).to(float16)
|
|
# See https://github.com/huggingface/transformers/pull/29402
|
|
x = x * (1.0 + weight.float())
|
|
x = x.to(orig_dtype)
|
|
return x if residual is None else (x, residual)
|
|
|
|
def forward_native(
|
|
self,
|
|
x: torch.Tensor,
|
|
residual: Optional[torch.Tensor] = None,
|
|
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
|
"""PyTorch-native implementation equivalent to forward()."""
|
|
return self.forward_static(self.weight.data, self.variance_epsilon, x,
|
|
residual)
|
|
|
|
def forward_cuda(
|
|
self,
|
|
x: torch.Tensor,
|
|
residual: Optional[torch.Tensor] = None,
|
|
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
|
if torch.compiler.is_compiling():
|
|
return self.forward_native(x, residual)
|
|
|
|
if not getattr(self, "_is_compiled", False):
|
|
self.forward_static = torch.compile( # type: ignore
|
|
self.forward_static)
|
|
self._is_compiled = True
|
|
return self.forward_native(x, residual)
|