[Perf][Kernel][Quantization] Fuse ReLU2 with static FP8 activation quantization (#53793)
Signed-off-by: mgoin <mgoin64@gmail.com> Signed-off-by: ElizaWszola <ewszola@redhat.com> Signed-off-by: snordmann <snordmann@nvidia.com> Co-authored-by: mgoin <mgoin64@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: ElizaWszola <ewszola@redhat.com> Co-authored-by: OpenAI Codex <noreply@openai.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mgoin
Claude Opus 4.8
ElizaWszola
OpenAI Codex
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
435c96f9db
commit
03dc26e639
@@ -91,7 +91,7 @@ class MockLinearFp8Static(torch.nn.Module):
|
||||
|
||||
def __init__(self, input_scale: torch.Tensor):
|
||||
super().__init__()
|
||||
self.input_quant_key = kFp8StaticTensorSym
|
||||
self._input_quant_key = kFp8StaticTensorSym
|
||||
self.input_scale = input_scale
|
||||
|
||||
|
||||
@@ -100,15 +100,21 @@ class MockLinearFp8Dynamic128(torch.nn.Module):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.input_quant_key = kFp8Dynamic128Sym
|
||||
self._input_quant_key = kFp8Dynamic128Sym
|
||||
|
||||
|
||||
class MockLinearNoQuant(torch.nn.Module):
|
||||
"""Mock linear layer with no input_quant_key (no fusion)."""
|
||||
"""Mock linear layer with no input quantization key."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class MockLinearRequiresUnquantized(MockLinearFp8Static):
|
||||
"""Mock consumer that needs the original activation for another branch."""
|
||||
|
||||
requires_unquantized_input = True
|
||||
|
||||
|
||||
@pytest.mark.parametrize("num_tokens", [1, 16, 128])
|
||||
@pytest.mark.parametrize("hidden_size", [128, 512, 1024])
|
||||
@pytest.mark.parametrize("dtype", DTYPES)
|
||||
@@ -199,7 +205,7 @@ def test_maybe_fused_act_quant_fallback(
|
||||
hidden_size: int,
|
||||
dtype: torch.dtype,
|
||||
) -> None:
|
||||
"""Test maybe_fused_act_quant falls back when no input_quant_key."""
|
||||
"""Test maybe_fused_act_quant falls back without an input quantization key."""
|
||||
device = "cuda:0"
|
||||
torch.set_default_device(device)
|
||||
|
||||
@@ -216,3 +222,19 @@ def test_maybe_fused_act_quant_fallback(
|
||||
|
||||
ref_out = act_fn(x)
|
||||
torch.testing.assert_close(result, ref_out)
|
||||
|
||||
|
||||
@torch.inference_mode()
|
||||
def test_maybe_fused_act_quant_preserves_required_unquantized_input(
|
||||
default_vllm_config,
|
||||
) -> None:
|
||||
device = "cuda:0"
|
||||
act_fn = SiluAndMul()
|
||||
scale = torch.tensor([0.5], device=device, dtype=torch.float32)
|
||||
linear = MockLinearRequiresUnquantized(scale)
|
||||
x = torch.randn(17, 256, dtype=torch.bfloat16, device=device)
|
||||
|
||||
result = maybe_fused_act_quant(act_fn, x, linear)
|
||||
|
||||
assert isinstance(result, torch.Tensor)
|
||||
torch.testing.assert_close(result, act_fn(x))
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from vllm.config import CompilationConfig, VllmConfig, set_current_vllm_config
|
||||
from vllm.config.vllm import OptimizationLevel
|
||||
from vllm.model_executor.layers.activation import ReLUSquaredActivation
|
||||
from vllm.model_executor.layers.fusion.fused_act_quant import maybe_fused_act_quant
|
||||
from vllm.model_executor.layers.fusion.quant_activation import QuantizedActivation
|
||||
from vllm.model_executor.layers.quantization.input_quant_fp8 import QuantFP8
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
GroupShape,
|
||||
kFp8StaticTensorSym,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
requires_sm90 = pytest.mark.skipif(
|
||||
not (current_platform.is_cuda() and current_platform.has_device_capability(90)),
|
||||
reason="This test requires SM90 or newer",
|
||||
)
|
||||
|
||||
|
||||
class _StaticFp8Linear(torch.nn.Module):
|
||||
def __init__(self, input_scale: torch.Tensor) -> None:
|
||||
super().__init__()
|
||||
self._input_quant_key = kFp8StaticTensorSym
|
||||
self.input_scale = input_scale
|
||||
|
||||
|
||||
def _assert_fp8_bitwise_equal(actual: torch.Tensor, expected: torch.Tensor) -> None:
|
||||
assert actual.dtype == expected.dtype == current_platform.fp8_dtype()
|
||||
torch.testing.assert_close(
|
||||
actual.view(torch.uint8), expected.view(torch.uint8), rtol=0, atol=0
|
||||
)
|
||||
|
||||
|
||||
def _assert_quantized_activation(
|
||||
result: QuantizedActivation, x: torch.Tensor, scale: torch.Tensor
|
||||
) -> None:
|
||||
assert result.scale is scale
|
||||
assert result.orig_dtype == x.dtype
|
||||
assert result.orig_shape == x.shape
|
||||
assert result.quant_key == kFp8StaticTensorSym
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def o2_relu2_fp8_ops():
|
||||
config = VllmConfig(
|
||||
optimization_level=OptimizationLevel.O2,
|
||||
compilation_config=CompilationConfig(custom_ops=["none"]),
|
||||
)
|
||||
with set_current_vllm_config(config):
|
||||
yield (
|
||||
ReLUSquaredActivation(),
|
||||
QuantFP8(static=True, group_shape=GroupShape.PER_TENSOR),
|
||||
)
|
||||
|
||||
|
||||
@requires_sm90
|
||||
@pytest.mark.parametrize("shape", [(1, 1), (1, 16), (17, 5120), (128, 10240)])
|
||||
@torch.inference_mode()
|
||||
def test_relu2_static_fp8_quant(o2_relu2_fp8_ops, shape) -> None:
|
||||
torch.manual_seed(0)
|
||||
x = torch.randn(shape, device="cuda", dtype=torch.bfloat16)
|
||||
scale = torch.tensor(0.1495535671710968, device="cuda", dtype=torch.float32)
|
||||
|
||||
relu2, quant_fp8 = o2_relu2_fp8_ops
|
||||
expected, _ = quant_fp8(relu2(x), scale)
|
||||
result = maybe_fused_act_quant(relu2, x, _StaticFp8Linear(scale))
|
||||
|
||||
assert isinstance(result, QuantizedActivation)
|
||||
_assert_quantized_activation(result, x, scale)
|
||||
_assert_fp8_bitwise_equal(result.data, expected)
|
||||
|
||||
|
||||
@requires_sm90
|
||||
@pytest.mark.parametrize(
|
||||
"enabled_ops",
|
||||
[(), ("relu2",), ("quant_fp8",), ("relu2", "quant_fp8")],
|
||||
)
|
||||
@torch.inference_mode()
|
||||
def test_relu2_static_fp8_quant_ignores_custom_op_dispatch(enabled_ops) -> None:
|
||||
custom_ops = ["none", *(f"+{op}" for op in enabled_ops)]
|
||||
config = VllmConfig(
|
||||
optimization_level=OptimizationLevel.O2,
|
||||
compilation_config=CompilationConfig(custom_ops=custom_ops),
|
||||
)
|
||||
with set_current_vllm_config(config):
|
||||
torch.manual_seed(0)
|
||||
relu2 = ReLUSquaredActivation()
|
||||
quant_fp8 = QuantFP8(static=True, group_shape=GroupShape.PER_TENSOR)
|
||||
x = torch.randn((17, 5120), device="cuda", dtype=torch.bfloat16)
|
||||
scale = torch.tensor(0.03125, device="cuda", dtype=torch.float32)
|
||||
|
||||
expected, _ = quant_fp8(relu2(x), scale)
|
||||
result = maybe_fused_act_quant(relu2, x, _StaticFp8Linear(scale))
|
||||
|
||||
assert isinstance(result, QuantizedActivation)
|
||||
_assert_quantized_activation(result, x, scale)
|
||||
_assert_fp8_bitwise_equal(result.data, expected)
|
||||
|
||||
|
||||
@requires_sm90
|
||||
@torch.inference_mode()
|
||||
def test_relu2_static_fp8_quant_without_compilation() -> None:
|
||||
config = VllmConfig(optimization_level=OptimizationLevel.O0)
|
||||
with set_current_vllm_config(config):
|
||||
torch.manual_seed(0)
|
||||
relu2 = ReLUSquaredActivation()
|
||||
quant_fp8 = QuantFP8(static=True, group_shape=GroupShape.PER_TENSOR)
|
||||
x = torch.randn((17, 5120), device="cuda", dtype=torch.bfloat16)
|
||||
scale = torch.tensor(0.03125, device="cuda", dtype=torch.float32)
|
||||
|
||||
expected, _ = quant_fp8(relu2(x), scale)
|
||||
result = maybe_fused_act_quant(relu2, x, _StaticFp8Linear(scale))
|
||||
|
||||
assert isinstance(result, QuantizedActivation)
|
||||
_assert_quantized_activation(result, x, scale)
|
||||
_assert_fp8_bitwise_equal(result.data, expected)
|
||||
|
||||
|
||||
@requires_sm90
|
||||
@pytest.mark.parametrize(
|
||||
"scale_value",
|
||||
[
|
||||
0.00435965,
|
||||
0.007149832788854837,
|
||||
0.03125,
|
||||
0.0714285746216774,
|
||||
0.1495535671710968,
|
||||
0.1517857164144516,
|
||||
6.03571415,
|
||||
],
|
||||
)
|
||||
@torch.inference_mode()
|
||||
def test_relu2_static_fp8_quant_all_bf16_values(o2_relu2_fp8_ops, scale_value) -> None:
|
||||
bits = torch.arange(1 << 16, dtype=torch.int32).to(torch.uint16)
|
||||
x = bits.view(torch.bfloat16).reshape(1, -1).to("cuda")
|
||||
scale = torch.tensor(scale_value, device="cuda", dtype=torch.float32)
|
||||
|
||||
relu2, quant_fp8 = o2_relu2_fp8_ops
|
||||
expected, _ = quant_fp8(relu2(x), scale)
|
||||
result = maybe_fused_act_quant(relu2, x, _StaticFp8Linear(scale))
|
||||
|
||||
assert isinstance(result, QuantizedActivation)
|
||||
_assert_quantized_activation(result, x, scale)
|
||||
_assert_fp8_bitwise_equal(result.data, expected)
|
||||
|
||||
|
||||
@requires_sm90
|
||||
@torch.inference_mode()
|
||||
def test_relu2_static_fp8_quant_empty(o2_relu2_fp8_ops) -> None:
|
||||
x = torch.empty(0, device="cuda", dtype=torch.bfloat16)
|
||||
scale = torch.tensor(0.03125, device="cuda", dtype=torch.float32)
|
||||
|
||||
relu2, _ = o2_relu2_fp8_ops
|
||||
result = maybe_fused_act_quant(relu2, x, _StaticFp8Linear(scale))
|
||||
|
||||
assert isinstance(result, QuantizedActivation)
|
||||
_assert_quantized_activation(result, x, scale)
|
||||
assert result.data.shape == x.shape
|
||||
assert result.data.dtype == current_platform.fp8_dtype()
|
||||
|
||||
|
||||
@requires_sm90
|
||||
@pytest.mark.parametrize("unsupported", ["dtype", "layout", "scale"])
|
||||
@torch.inference_mode()
|
||||
def test_relu2_static_fp8_quant_falls_back(o2_relu2_fp8_ops, unsupported: str) -> None:
|
||||
x = torch.randn((17, 32), device="cuda", dtype=torch.bfloat16)
|
||||
scale = torch.tensor(0.03125, device="cuda", dtype=torch.float32)
|
||||
if unsupported == "dtype":
|
||||
x = x.to(torch.float16)
|
||||
elif unsupported == "layout":
|
||||
x = x.T
|
||||
else:
|
||||
scale = scale.to(torch.float16)
|
||||
|
||||
relu2, _ = o2_relu2_fp8_ops
|
||||
expected = relu2(x)
|
||||
result = maybe_fused_act_quant(relu2, x, _StaticFp8Linear(scale))
|
||||
|
||||
assert isinstance(result, torch.Tensor)
|
||||
assert not isinstance(result, QuantizedActivation)
|
||||
torch.testing.assert_close(result, expected, rtol=0, atol=0)
|
||||
|
||||
|
||||
@requires_sm90
|
||||
@torch.inference_mode()
|
||||
def test_relu2_static_fp8_quant_torch_compile_dynamic(
|
||||
o2_relu2_fp8_ops,
|
||||
) -> None:
|
||||
torch.manual_seed(1)
|
||||
scale = torch.tensor(0.00435965, device="cuda", dtype=torch.float32)
|
||||
relu2, quant_fp8 = o2_relu2_fp8_ops
|
||||
linear = _StaticFp8Linear(scale)
|
||||
|
||||
def fused(x: torch.Tensor) -> torch.Tensor:
|
||||
result = maybe_fused_act_quant(relu2, x, linear)
|
||||
assert isinstance(result, QuantizedActivation)
|
||||
return result.data
|
||||
|
||||
compile_count = 0
|
||||
|
||||
def counting_inductor_backend(gm, example_inputs):
|
||||
nonlocal compile_count
|
||||
compile_count += 1
|
||||
return torch._inductor.compile(gm, example_inputs)
|
||||
|
||||
torch._dynamo.reset()
|
||||
try:
|
||||
x = torch.randn((17, 5120), device="cuda", dtype=torch.bfloat16)
|
||||
torch._dynamo.mark_dynamic(x, 0)
|
||||
compiled_fused = torch.compile(
|
||||
fused, backend=counting_inductor_backend, fullgraph=True
|
||||
)
|
||||
|
||||
for candidate in (
|
||||
x,
|
||||
torch.randn((31, 5120), device="cuda", dtype=torch.bfloat16),
|
||||
torch.randn((63, 5120), device="cuda", dtype=torch.bfloat16),
|
||||
):
|
||||
expected, _ = quant_fp8(relu2(candidate), scale)
|
||||
_assert_fp8_bitwise_equal(compiled_fused(candidate), expected)
|
||||
|
||||
assert compile_count == 1
|
||||
finally:
|
||||
torch._dynamo.reset()
|
||||
Reference in New Issue
Block a user