[Perf] Minimax-H3 support fused RMSNorm and RoPE opt (#5801)
Signed-off-by: Fan <fan@FandeMacBook-Air.local> Co-authored-by: Fan <fan@FandeMacBook-Air.local>
This commit is contained in:
1 parent
240e6655be
commit
f2e3d2e25b
6 files changed
+145
-39
No files matched your search
@@ -65,9 +65,9 @@ class LayerNorm(nn.LayerNorm, CustomOp):
|
||||
|
||||
|
||||
class RMSNorm(CustomOp):
|
||||
def __init__(self, hidden_size: int, eps: float = 1e-6) -> None:
|
||||
def __init__(self, hidden_size: int, eps: float = 1e-6, dtype: torch.dtype = torch.float32) -> None:
|
||||
super().__init__()
|
||||
self.weight = nn.Parameter(torch.ones(hidden_size))
|
||||
self.weight = nn.Parameter(torch.ones(hidden_size, dtype=dtype))
|
||||
self.variance_epsilon = eps
|
||||
|
||||
def _forward_fused(self, x: torch.Tensor) -> torch.Tensor:
|
||||
|
||||
@@ -89,10 +89,11 @@ class RotaryEmbedding(CustomOp):
|
||||
of 1st half and 2nd half (GPT-NeoX style).
|
||||
"""
|
||||
|
||||
def __init__(self, is_neox_style: bool = False) -> None:
|
||||
def __init__(self, is_neox_style: bool = False, half_head_dim: bool = True) -> None:
|
||||
super().__init__()
|
||||
self.is_neox_style = is_neox_style
|
||||
self.interleaved = not is_neox_style
|
||||
self.half_head_dim = half_head_dim
|
||||
self.apply_rotary_emb_flash_attn = None
|
||||
self.has_mindie = False
|
||||
# ``find_spec("flash_attn")`` is True as long as *any* package publishes
|
||||
@@ -110,6 +111,21 @@ class RotaryEmbedding(CustomOp):
|
||||
if find_spec("mindiesd") is not None:
|
||||
self.has_mindie = True
|
||||
|
||||
def _prepare_half_head_dim_cos_sin(
|
||||
self,
|
||||
cos: torch.Tensor,
|
||||
sin: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Normalize shared cos/sin to the half-dim layout used by CUDA/native kernels."""
|
||||
if cos.dim() == 3:
|
||||
# All batch elements share the same rotary position encoding.
|
||||
cos = cos[0]
|
||||
sin = sin[0]
|
||||
if not self.half_head_dim:
|
||||
cos = cos[..., ::2] if self.interleaved else cos[..., : cos.shape[-1] // 2]
|
||||
sin = sin[..., ::2] if self.interleaved else sin[..., : sin.shape[-1] // 2]
|
||||
return cos, sin
|
||||
|
||||
def forward_cuda(
|
||||
self,
|
||||
x: torch.Tensor,
|
||||
@@ -118,10 +134,7 @@ class RotaryEmbedding(CustomOp):
|
||||
) -> torch.Tensor:
|
||||
from vllm.vllm_flash_attn.layers.rotary import apply_rotary_emb
|
||||
|
||||
if cos.dim() == 3:
|
||||
# (B, S, D/2) -> (S, D/2)
|
||||
cos = cos[0]
|
||||
sin = sin[0]
|
||||
cos, sin = self._prepare_half_head_dim_cos_sin(cos, sin)
|
||||
|
||||
x, squeezed = _ensure_batch_dim(x)
|
||||
output = apply_rotary_emb(
|
||||
@@ -141,10 +154,7 @@ class RotaryEmbedding(CustomOp):
|
||||
if self.apply_rotary_emb_flash_attn is None:
|
||||
return self.forward_cuda(x, cos, sin)
|
||||
|
||||
if cos.dim() == 3:
|
||||
# (B, S, D/2) -> (S, D/2)
|
||||
cos = cos[0]
|
||||
sin = sin[0]
|
||||
cos, sin = self._prepare_half_head_dim_cos_sin(cos, sin)
|
||||
|
||||
x, squeezed = _ensure_batch_dim(x)
|
||||
output = self.apply_rotary_emb_flash_attn(
|
||||
@@ -162,7 +172,7 @@ class RotaryEmbedding(CustomOp):
|
||||
sin: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
if self.has_mindie:
|
||||
return apply_rotary_emb_mindiesd(x, cos, sin, self.interleaved)
|
||||
return apply_rotary_emb_mindiesd(x, cos, sin, self.interleaved, self.half_head_dim)
|
||||
else:
|
||||
return self.forward_native(x, cos, sin)
|
||||
|
||||
@@ -191,9 +201,7 @@ class RotaryEmbedding(CustomOp):
|
||||
# All batch elements share the same rotary position encoding.
|
||||
# Strip the batch dim so the underlying op broadcasts over the batch,
|
||||
# consistent with forward_cuda / forward_hip / apply_rotary_emb_mindiesd.
|
||||
if cos.dim() == 3:
|
||||
cos = cos[0]
|
||||
sin = sin[0]
|
||||
cos, sin = self._prepare_half_head_dim_cos_sin(cos, sin)
|
||||
return apply_rotary_emb_torch(
|
||||
x,
|
||||
cos,
|
||||
|
||||
Reference in new issue
Block a user