[ROCm][Compile] Fuse AITER static FP8 attention output (#58099)
Signed-off-by: Andreas Karatzas <akaratza@amd.com> Co-authored-by: OpenAI Codex <noreply@openai.com>
This commit is contained in:
co-authored by
OpenAI Codex
parent
973a3be780
commit
26879f3260
@@ -0,0 +1,88 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import json
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
import regex as re
|
||||
|
||||
from tests.utils import compare_two_settings
|
||||
from vllm._aiter_ops import is_aiter_found_and_supported
|
||||
from vllm.config import CompilationMode
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.transformers_utils.repo_utils import hf_api
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not current_platform.is_rocm() or not is_aiter_found_and_supported(),
|
||||
reason="Requires ROCm AITER attention and quantization",
|
||||
)
|
||||
def test_rocm_aiter_static_attention_output_correctness(caplog_mp_spawn):
|
||||
"""Trained FP8 outputs agree when actual AITER output quantization is fused."""
|
||||
model = hf_api().snapshot_download(
|
||||
"RedHatAI/Meta-Llama-3.1-8B-Instruct-FP8",
|
||||
revision="870a1177ed082c0ba5dbefd62573ab8f91803f6a",
|
||||
allow_patterns=["*.json", "*.safetensors", "*.model", "*.txt", "*.jinja"],
|
||||
)
|
||||
common_args = [
|
||||
"--dtype",
|
||||
"bfloat16",
|
||||
"--load-format",
|
||||
"auto",
|
||||
"--max-model-len",
|
||||
"512",
|
||||
"--max-num-batched-tokens",
|
||||
"512",
|
||||
"--max-num-seqs",
|
||||
"8",
|
||||
"--kv-cache-memory-bytes",
|
||||
str(256 * 1024**2),
|
||||
"--gpu-memory-utilization",
|
||||
"0.01",
|
||||
"--attention-backend",
|
||||
"ROCM_AITER_UNIFIED_ATTN",
|
||||
]
|
||||
|
||||
def settings(enabled):
|
||||
return [
|
||||
*common_args,
|
||||
"--compilation-config",
|
||||
json.dumps(
|
||||
{
|
||||
"mode": CompilationMode.VLLM_COMPILE,
|
||||
"cudagraph_mode": "NONE",
|
||||
"splitting_ops": [],
|
||||
"inductor_compile_config": {"force_disable_caches": True},
|
||||
"custom_ops": ["+quant_fp8", "-rms_norm"],
|
||||
"pass_config": {
|
||||
"fuse_attn_quant": enabled,
|
||||
"fuse_norm_quant": False,
|
||||
"fuse_act_quant": False,
|
||||
"fuse_allreduce_rms": False,
|
||||
},
|
||||
}
|
||||
),
|
||||
]
|
||||
|
||||
env = {
|
||||
"VLLM_ROCM_USE_AITER": "1",
|
||||
"VLLM_ROCM_USE_AITER_LINEAR": "1",
|
||||
"VLLM_DISABLE_COMPILE_CACHE": "1",
|
||||
}
|
||||
with caplog_mp_spawn(logging.DEBUG) as logs:
|
||||
compare_two_settings(
|
||||
model,
|
||||
settings(False),
|
||||
settings(True),
|
||||
env1=env,
|
||||
env2=env,
|
||||
method="generate",
|
||||
force_v1_runner=True,
|
||||
)
|
||||
matches = [
|
||||
int(count) for count in re.findall(r"'attn_quant_fusion': (\d+)", logs.text)
|
||||
]
|
||||
print(f"Attention output quantization rewrite counts: {matches}")
|
||||
assert matches and all(count == 32 for count in matches), (
|
||||
f"Expected output quantization fused in all 32 Llama layers, found {matches}"
|
||||
)
|
||||
@@ -12,6 +12,7 @@ from tests.v1.attention.utils import (
|
||||
create_common_attn_metadata,
|
||||
dense_kv_cache_views,
|
||||
)
|
||||
from vllm._aiter_ops import is_aiter_found_and_supported, rocm_aiter_ops
|
||||
from vllm._custom_ops import cutlass_scaled_fp4_mm, scaled_fp4_quant
|
||||
from vllm.compilation.passes.fusion.attn_quant_fusion import (
|
||||
ATTN_OP,
|
||||
@@ -189,6 +190,17 @@ class TestAttentionFp8StaticQuantPatternModel(AttentionQuantPatternModel):
|
||||
return self.fp8_linear(attn_output)
|
||||
|
||||
|
||||
class TestAttentionAiterFp8StaticQuantPatternModel(
|
||||
TestAttentionFp8StaticQuantPatternModel
|
||||
):
|
||||
aiter_quant = True
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fp8_linear.kernel.quant_fp8.use_aiter = True
|
||||
self.fp8_linear.input_scale.fill_(0.125)
|
||||
|
||||
|
||||
class TestAttentionNvfp4QuantPatternModel(AttentionQuantPatternModel):
|
||||
"""Test model for AttentionNvfp4QuantPattern fusion."""
|
||||
|
||||
@@ -455,11 +467,12 @@ def test_attention_quant_pattern(
|
||||
)
|
||||
|
||||
# Check quantization ops in the graph before and after fusion
|
||||
quant_op = (
|
||||
torch.ops.aten.reciprocal
|
||||
if "-quant_fp8" in custom_ops_list
|
||||
else QUANT_OPS[quant_key]
|
||||
)
|
||||
if "-quant_fp8" in custom_ops_list:
|
||||
quant_op = torch.ops.aten.reciprocal
|
||||
elif getattr(model_class, "aiter_quant", False):
|
||||
quant_op = torch.ops.vllm.rocm_aiter_per_tensor_quant.default
|
||||
else:
|
||||
quant_op = QUANT_OPS[quant_key]
|
||||
|
||||
if expect_fusion:
|
||||
# Note: for fp8, fully_replaced=False because query quant ops remain in
|
||||
@@ -523,3 +536,46 @@ def test_attention_quant_pattern(
|
||||
|
||||
# Check that results are close
|
||||
torch.testing.assert_close(result_unfused, result_fused, atol=1e-2, rtol=1e-2)
|
||||
if getattr(model_class, "aiter_quant", False):
|
||||
# Query quantization remains; exactly the output quant op is removed.
|
||||
assert test_backend.op_count(quant_op, before=True) == (
|
||||
test_backend.op_count(quant_op) + 1
|
||||
)
|
||||
for model in (model_unfused, model_fused):
|
||||
torch.testing.assert_close(
|
||||
model.fp8_linear.input_scale,
|
||||
torch.full_like(model.fp8_linear.input_scale, 0.125),
|
||||
atol=0,
|
||||
rtol=0,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not current_platform.is_rocm() or not is_aiter_found_and_supported(),
|
||||
reason="Requires ROCm AITER attention and quantization",
|
||||
)
|
||||
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16])
|
||||
def test_rocm_aiter_static_quant_attention(
|
||||
dtype, dist_init, disable_vllm_compile_cache, monkeypatch
|
||||
):
|
||||
"""Fuse the actual AITER op without changing its static scale or outputs."""
|
||||
try:
|
||||
with monkeypatch.context() as patch:
|
||||
patch.setenv("VLLM_ROCM_USE_AITER", "1")
|
||||
rocm_aiter_ops.refresh_env_variables()
|
||||
test_attention_quant_pattern(
|
||||
num_qo_heads=32,
|
||||
num_kv_heads=8,
|
||||
head_size=128,
|
||||
batch_size=8,
|
||||
dtype=dtype,
|
||||
kv_cache_dtype="fp8",
|
||||
custom_ops="+quant_fp8",
|
||||
model_name="amd/Llama-3.1-8B-Instruct-FP8-KV",
|
||||
model_class=TestAttentionAiterFp8StaticQuantPatternModel,
|
||||
backend=AttentionBackendEnum.ROCM_AITER_UNIFIED_ATTN,
|
||||
dist_init=dist_init,
|
||||
disable_vllm_compile_cache=disable_vllm_compile_cache,
|
||||
)
|
||||
finally:
|
||||
rocm_aiter_ops.refresh_env_variables()
|
||||
|
||||
Reference in New Issue
Block a user