mirror of
https://github.com/deepseek-ai/DeepInfer.git
synced 2026-10-26 16:35:05 +00:00
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# Derived from vLLM; modified by DeepSeek.
|
|
|
|
from __future__ import annotations
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
|
|
from deepinfer.model_executor.layers.linear import get_fp8_cast_params
|
|
from deepinfer.model_executor.layers.tilelang_ops.norm import norm
|
|
from deepinfer.model_executor.utils import reconstruct_param
|
|
|
|
|
|
class RMSNorm(nn.Module):
|
|
"""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,
|
|
weight: torch.Tensor | None = None,
|
|
use_fp8: bool = False,
|
|
out_scale: float = 1.0,
|
|
) -> None:
|
|
super().__init__()
|
|
if weight is None:
|
|
weight = torch.ones(hidden_size)
|
|
assert weight.shape == (hidden_size,)
|
|
self.weight = reconstruct_param(weight)
|
|
self.variance_epsilon = eps
|
|
self.use_fp8 = use_fp8
|
|
self.out_scale = out_scale
|
|
|
|
def forward(
|
|
self,
|
|
x: torch.Tensor,
|
|
residual: torch.Tensor | None = None,
|
|
out: torch.Tensor | tuple[torch.Tensor, torch.Tensor] | None = None,
|
|
return_bf16: bool = False,
|
|
fp8_cast_params: dict | None = None,
|
|
extra_sf_layout: str | None = None,
|
|
extra_sf_out: torch.Tensor | None = None,
|
|
extra_sf_block_m: int = 0,
|
|
) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]:
|
|
if fp8_cast_params is None:
|
|
fp8_cast_params = get_fp8_cast_params() if self.use_fp8 else {}
|
|
return norm(
|
|
x,
|
|
self.weight,
|
|
residual=residual,
|
|
eps=self.variance_epsilon,
|
|
out=out,
|
|
**fp8_cast_params,
|
|
extra_sf_layout=extra_sf_layout,
|
|
extra_sf_out=extra_sf_out,
|
|
extra_sf_block_m=extra_sf_block_m,
|
|
return_bf16=return_bf16,
|
|
out_scale=self.out_scale,
|
|
)
|