2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-09-06 08:57:38 +09:00
|
|
|
import pytest
|
2023-03-31 09:51:22 -07:00
|
|
|
import torch
|
|
|
|
|
2024-11-08 16:20:08 -05:00
|
|
|
from tests.kernels.quant_utils import FP8_DTYPE
|
2024-09-11 15:52:19 -04:00
|
|
|
from tests.kernels.utils import opcheck
|
2023-12-02 21:18:40 -08:00
|
|
|
from vllm.model_executor.layers.layernorm import RMSNorm
|
2024-10-29 22:47:44 +08:00
|
|
|
from vllm.platforms import current_platform
|
2023-03-31 09:51:22 -07:00
|
|
|
|
2023-09-06 08:57:38 +09:00
|
|
|
DTYPES = [torch.half, torch.bfloat16, torch.float]
|
|
|
|
NUM_TOKENS = [7, 83, 4096] # Arbitrary values for testing
|
2024-11-08 16:20:08 -05:00
|
|
|
HIDDEN_SIZES = [8, 768, 769, 770, 771, 5120, 5124, 5125, 5126, 8192,
|
2024-03-30 14:26:38 -07:00
|
|
|
8199] # Arbitrary values for testing
|
2023-12-02 21:18:40 -08:00
|
|
|
ADD_RESIDUAL = [False, True]
|
2023-09-06 08:57:38 +09:00
|
|
|
SEEDS = [0]
|
2024-02-02 07:46:39 +08:00
|
|
|
CUDA_DEVICES = [
|
|
|
|
f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)
|
|
|
|
]
|
2023-09-06 08:57:38 +09:00
|
|
|
|
2023-03-31 09:51:22 -07:00
|
|
|
|
2023-09-06 08:57:38 +09:00
|
|
|
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
|
|
|
|
@pytest.mark.parametrize("hidden_size", HIDDEN_SIZES)
|
2023-12-02 21:18:40 -08:00
|
|
|
@pytest.mark.parametrize("add_residual", ADD_RESIDUAL)
|
2023-09-06 08:57:38 +09:00
|
|
|
@pytest.mark.parametrize("dtype", DTYPES)
|
|
|
|
@pytest.mark.parametrize("seed", SEEDS)
|
2024-02-02 07:46:39 +08:00
|
|
|
@pytest.mark.parametrize("device", CUDA_DEVICES)
|
2023-03-31 09:51:22 -07:00
|
|
|
@torch.inference_mode()
|
2023-09-06 08:57:38 +09:00
|
|
|
def test_rms_norm(
|
2023-03-31 09:51:22 -07:00
|
|
|
num_tokens: int,
|
|
|
|
hidden_size: int,
|
2023-12-02 21:18:40 -08:00
|
|
|
add_residual: bool,
|
2023-03-31 09:51:22 -07:00
|
|
|
dtype: torch.dtype,
|
2023-09-06 08:57:38 +09:00
|
|
|
seed: int,
|
2024-02-02 07:46:39 +08:00
|
|
|
device: str,
|
2023-03-31 09:51:22 -07:00
|
|
|
) -> None:
|
2024-10-29 22:47:44 +08:00
|
|
|
current_platform.seed_everything(seed)
|
2024-02-02 07:46:39 +08:00
|
|
|
torch.set_default_device(device)
|
|
|
|
layer = RMSNorm(hidden_size).to(dtype=dtype)
|
2023-12-02 21:18:40 -08:00
|
|
|
layer.weight.data.normal_(mean=1.0, std=0.1)
|
|
|
|
scale = 1 / (2 * hidden_size)
|
2024-02-02 07:46:39 +08:00
|
|
|
x = torch.randn(num_tokens, hidden_size, dtype=dtype)
|
2023-12-02 21:18:40 -08:00
|
|
|
x *= scale
|
|
|
|
residual = torch.randn_like(x) * scale if add_residual else None
|
|
|
|
|
|
|
|
# NOTE(woosuk): The reference implementation should be executed first
|
|
|
|
# because the custom kernel is in-place.
|
2024-06-05 09:18:19 -07:00
|
|
|
ref_out = layer.forward_native(x, residual)
|
2023-12-02 21:18:40 -08:00
|
|
|
out = layer(x, residual)
|
|
|
|
# NOTE(woosuk): LayerNorm operators (including RMS) typically have larger
|
|
|
|
# numerical errors than other operators because they involve reductions.
|
|
|
|
# Therefore, we use a larger tolerance.
|
|
|
|
if add_residual:
|
2024-08-15 21:24:04 -07:00
|
|
|
torch.testing.assert_close(out[0], ref_out[0], atol=1e-2, rtol=1e-2)
|
|
|
|
torch.testing.assert_close(out[1], ref_out[1], atol=1e-2, rtol=1e-2)
|
2023-12-02 21:18:40 -08:00
|
|
|
else:
|
2024-08-15 21:24:04 -07:00
|
|
|
torch.testing.assert_close(out, ref_out, atol=1e-2, rtol=1e-2)
|
2024-09-11 15:52:19 -04:00
|
|
|
|
|
|
|
if residual is not None:
|
|
|
|
opcheck(torch.ops._C.fused_add_rms_norm,
|
|
|
|
(x, residual, layer.weight.data, layer.variance_epsilon))
|
|
|
|
else:
|
|
|
|
opcheck(torch.ops._C.rms_norm,
|
|
|
|
(out, x, layer.weight.data, layer.variance_epsilon))
|
2024-11-08 16:20:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
|
|
|
|
@pytest.mark.parametrize("hidden_size", HIDDEN_SIZES)
|
|
|
|
@pytest.mark.parametrize("add_residual", ADD_RESIDUAL)
|
|
|
|
@pytest.mark.parametrize("dtype", DTYPES)
|
|
|
|
@pytest.mark.parametrize("quant_scale", [1.0, 0.01, 10.0])
|
|
|
|
@pytest.mark.parametrize("seed", SEEDS)
|
|
|
|
@pytest.mark.parametrize("device", CUDA_DEVICES)
|
|
|
|
def test_fused_rms_norm_quant(
|
|
|
|
num_tokens: int,
|
|
|
|
hidden_size: int,
|
|
|
|
add_residual: bool,
|
|
|
|
dtype: torch.dtype,
|
|
|
|
quant_scale: float,
|
|
|
|
seed: int,
|
|
|
|
device: str,
|
|
|
|
) -> None:
|
|
|
|
current_platform.seed_everything(seed)
|
|
|
|
torch.set_default_device(device)
|
|
|
|
|
|
|
|
weight = torch.empty(hidden_size, dtype=dtype).normal_(mean=1.0, std=0.1)
|
|
|
|
scale = 1 / (2 * hidden_size)
|
|
|
|
x = torch.randn(num_tokens, hidden_size, dtype=dtype)
|
|
|
|
x *= scale
|
|
|
|
if add_residual:
|
|
|
|
residual = torch.randn_like(x) * scale
|
|
|
|
residual_fused = residual.clone()
|
|
|
|
else:
|
|
|
|
residual = residual_fused = None
|
|
|
|
|
|
|
|
out_norm = torch.empty_like(x)
|
|
|
|
out_quant = torch.empty_like(x, dtype=FP8_DTYPE)
|
|
|
|
out_quant_fused = torch.empty_like(out_quant)
|
|
|
|
|
|
|
|
quant_scale_t = torch.tensor(quant_scale, dtype=torch.float32)
|
|
|
|
|
|
|
|
if add_residual:
|
|
|
|
torch.ops._C.fused_add_rms_norm_static_fp8_quant(
|
|
|
|
out_quant_fused, x, residual_fused, weight, quant_scale_t, 1e-6)
|
|
|
|
|
|
|
|
# Unfused kernel is in-place so it goes second
|
|
|
|
# Also use a separate clone of x to avoid modifying the input
|
|
|
|
x_unfused = x.clone()
|
|
|
|
torch.ops._C.fused_add_rms_norm(x_unfused, residual, weight, 1e-6)
|
|
|
|
torch.ops._C.static_scaled_fp8_quant(out_quant, x_unfused,
|
|
|
|
quant_scale_t)
|
|
|
|
|
|
|
|
torch.cuda.synchronize()
|
|
|
|
torch.testing.assert_close(residual_fused,
|
|
|
|
residual,
|
|
|
|
atol=1e-2,
|
|
|
|
rtol=1e-2)
|
|
|
|
|
|
|
|
opcheck(
|
|
|
|
torch.ops._C.fused_add_rms_norm_static_fp8_quant,
|
|
|
|
(out_quant_fused, x, residual_fused, weight, quant_scale_t, 1e-6))
|
|
|
|
else:
|
|
|
|
torch.ops._C.rms_norm_static_fp8_quant(out_quant_fused, x, weight,
|
|
|
|
quant_scale_t, 1e-6)
|
|
|
|
|
|
|
|
torch.ops._C.rms_norm(out_norm, x, weight, 1e-6)
|
|
|
|
torch.ops._C.static_scaled_fp8_quant(out_quant, out_norm,
|
|
|
|
quant_scale_t)
|
|
|
|
|
|
|
|
opcheck(torch.ops._C.rms_norm_static_fp8_quant,
|
|
|
|
(out_quant_fused, x, weight, quant_scale_t, 1e-6))
|
|
|
|
|
|
|
|
torch.testing.assert_close(out_quant_fused.to(dtype=torch.float32),
|
|
|
|
out_quant.to(dtype=torch.float32),
|
|
|
|
atol=1e-3,
|
|
|
|
rtol=1e-3)
|