[Attention] Add UltraQuant 4-bit KV cache backend (FlyDSL D=256) (#57057)
Signed-off-by: Aditi Rana <aditi.rana@amd.com>
This commit is contained in:
+24
-10
@@ -906,15 +906,29 @@ class Platform:
|
||||
cache_dtype_str=cache_config.cache_dtype,
|
||||
kv_quant_mode=kv_quant_mode,
|
||||
).page_size_bytes
|
||||
elif cache_config.cache_dtype.startswith("turboquant_"):
|
||||
# TQ has a packed K|V layout; the standard FullAttentionSpec
|
||||
# formula over-sizes it and trips unify_kv_cache_spec_page_size
|
||||
# when all attention layers are TQ. With mixed skip+TQ the skip
|
||||
# layers still use the standard layout — take max so mamba
|
||||
# padding covers the largest actual page.
|
||||
from vllm.v1.attention.backends.turboquant_attn import (
|
||||
TurboQuantAttentionBackend,
|
||||
)
|
||||
elif (
|
||||
cache_config.cache_dtype.startswith("turboquant_")
|
||||
or cache_config.cache_dtype == "ultraquant_4bit"
|
||||
):
|
||||
# TurboQuant and UltraQuant have packed K|V layouts; the standard
|
||||
# FullAttentionSpec formula over-sizes them and trips
|
||||
# unify_kv_cache_spec_page_size when all attention layers are
|
||||
# packed. With mixed skip+packed the skip layers still use the
|
||||
# standard layout — take max so mamba padding covers the largest
|
||||
# actual page. Each dtype packs via its own backend.
|
||||
_pack_backend: type[AttentionBackend]
|
||||
if cache_config.cache_dtype == "ultraquant_4bit":
|
||||
from vllm.v1.attention.backends.ultraquant_attn import (
|
||||
UltraQuantAttentionBackend,
|
||||
)
|
||||
|
||||
_pack_backend = UltraQuantAttentionBackend
|
||||
else:
|
||||
from vllm.v1.attention.backends.turboquant_attn import (
|
||||
TurboQuantAttentionBackend,
|
||||
)
|
||||
|
||||
_pack_backend = TurboQuantAttentionBackend
|
||||
|
||||
tq_spec = FullAttentionSpec(
|
||||
block_size=1,
|
||||
@@ -923,7 +937,7 @@ class Platform:
|
||||
dtype=kv_cache_dtype,
|
||||
kv_quant_mode=kv_quant_mode,
|
||||
)
|
||||
tq_page = TurboQuantAttentionBackend.customize_spec(tq_spec).page_size_bytes
|
||||
tq_page = _pack_backend.customize_spec(tq_spec).page_size_bytes
|
||||
if cache_config.kv_cache_dtype_skip_layers:
|
||||
skip_page = FullAttentionSpec(
|
||||
block_size=1,
|
||||
|
||||
+49
-10
@@ -30,6 +30,7 @@ logger = init_logger(__name__)
|
||||
|
||||
_KV_CACHE_DTYPE_REASON = "kv_cache_dtype not supported"
|
||||
_TURBOQUANT_LAYOUT_REASON = "no KV cache layout in common with TURBOQUANT"
|
||||
_ULTRAQUANT_LAYOUT_REASON = "no KV cache layout in common with ULTRAQUANT"
|
||||
|
||||
try:
|
||||
from amdsmi import (
|
||||
@@ -505,6 +506,7 @@ def _get_backend_priorities(
|
||||
backends.insert(0, AttentionBackendEnum.ROCM_AITER_UNIFIED_ATTN)
|
||||
backends.append(AttentionBackendEnum.TRITON_ATTN)
|
||||
backends.append(AttentionBackendEnum.TURBOQUANT)
|
||||
backends.append(AttentionBackendEnum.ULTRAQUANT)
|
||||
|
||||
return backends
|
||||
|
||||
@@ -517,6 +519,14 @@ def _uses_turboquant(vllm_config: "VllmConfig | None") -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _uses_ultraquant(vllm_config: "VllmConfig | None") -> bool:
|
||||
"""Whether the run's KV cache dtype is the UltraQuant 4-bit format."""
|
||||
cache_config = getattr(vllm_config, "cache_config", None)
|
||||
return (
|
||||
cache_config is not None and str(cache_config.cache_dtype) == "ultraquant_4bit"
|
||||
)
|
||||
|
||||
|
||||
def _shares_layout_with_turboquant(backend_class: type["AttentionBackend"]) -> bool:
|
||||
"""Whether this backend reads a KV cache layout TURBOQUANT reads too.
|
||||
|
||||
@@ -535,12 +545,29 @@ def _shares_layout_with_turboquant(backend_class: type["AttentionBackend"]) -> b
|
||||
return not set(layouts).isdisjoint(turboquant_layouts)
|
||||
|
||||
|
||||
def _shares_layout_with_ultraquant(backend_class: type["AttentionBackend"]) -> bool:
|
||||
"""Whether this backend reads a KV cache layout ULTRAQUANT reads too.
|
||||
|
||||
Same constraint as _shares_layout_with_turboquant: an ultraquant_4bit run
|
||||
keeps its boundary layers at the native dtype, and one layout has to serve
|
||||
the whole worker.
|
||||
"""
|
||||
layouts = backend_class.supported_kv_cache_layouts()
|
||||
ultraquant_layouts = (
|
||||
AttentionBackendEnum.ULTRAQUANT.get_class().supported_kv_cache_layouts()
|
||||
)
|
||||
if layouts is None or ultraquant_layouts is None:
|
||||
return True
|
||||
return not set(layouts).isdisjoint(ultraquant_layouts)
|
||||
|
||||
|
||||
def _get_invalid_reasons(
|
||||
backend_class: type["AttentionBackend"],
|
||||
device_capability: DeviceCapability,
|
||||
attn_selector_config: "AttentionSelectorConfig",
|
||||
*,
|
||||
is_turboquant_run: bool,
|
||||
is_ultraquant_run: bool = False,
|
||||
) -> list[str]:
|
||||
"""Why this backend cannot serve the layer, empty when it can."""
|
||||
invalid_reasons = backend_class.validate_configuration(
|
||||
@@ -553,6 +580,12 @@ def _get_invalid_reasons(
|
||||
and not _shares_layout_with_turboquant(backend_class)
|
||||
):
|
||||
invalid_reasons = [_TURBOQUANT_LAYOUT_REASON]
|
||||
if (
|
||||
not invalid_reasons
|
||||
and is_ultraquant_run
|
||||
and not _shares_layout_with_ultraquant(backend_class)
|
||||
):
|
||||
invalid_reasons = [_ULTRAQUANT_LAYOUT_REASON]
|
||||
return invalid_reasons
|
||||
|
||||
|
||||
@@ -683,7 +716,7 @@ class RocmPlatform(Platform):
|
||||
@classmethod
|
||||
def get_attn_backend_cls(
|
||||
cls,
|
||||
selected_backend: "AttentionBackendEnum",
|
||||
selected_backend: "AttentionBackendEnum | None",
|
||||
attn_selector_config: "AttentionSelectorConfig",
|
||||
num_heads: int | None = None,
|
||||
) -> str:
|
||||
@@ -695,13 +728,16 @@ class RocmPlatform(Platform):
|
||||
# Keep lazy: vllm.config imports current_platform during initialization.
|
||||
from vllm.config import get_current_vllm_config_or_none
|
||||
|
||||
is_turboquant_run = _uses_turboquant(get_current_vllm_config_or_none())
|
||||
vllm_config = get_current_vllm_config_or_none()
|
||||
is_turboquant_run = _uses_turboquant(vllm_config)
|
||||
is_ultraquant_run = _uses_ultraquant(vllm_config)
|
||||
try:
|
||||
sel_invalid_reasons = _get_invalid_reasons(
|
||||
selected_backend.get_class(),
|
||||
device_capability,
|
||||
attn_selector_config,
|
||||
is_turboquant_run=is_turboquant_run,
|
||||
is_ultraquant_run=is_ultraquant_run,
|
||||
)
|
||||
except ImportError:
|
||||
sel_invalid_reasons = ["ImportError"]
|
||||
@@ -711,21 +747,23 @@ class RocmPlatform(Platform):
|
||||
selected_backend.name,
|
||||
)
|
||||
return selected_backend.get_path()
|
||||
# Only tolerate the mismatch when turboquant is in play: boundary
|
||||
# layers keep the native dtype while every other layer needs
|
||||
# TURBOQUANT, so no single --attention-backend can serve every layer.
|
||||
# For any other dtype the selection is genuinely invalid -> fail loud.
|
||||
# Only tolerate the mismatch when turboquant or ultraquant is in play:
|
||||
# boundary layers keep the native dtype while every other layer needs
|
||||
# the packed backend, so no single --attention-backend can serve every
|
||||
# layer. For any other dtype the selection is genuinely invalid -> fail
|
||||
# loud.
|
||||
kv_dtype = attn_selector_config.kv_cache_dtype
|
||||
layer_is_turboquant = kv_dtype is not None and str(kv_dtype).startswith(
|
||||
"turboquant"
|
||||
)
|
||||
is_turboquant_fallback = (
|
||||
is_turboquant_run or layer_is_turboquant
|
||||
is_packed_kv_fallback = (
|
||||
is_turboquant_run or layer_is_turboquant or is_ultraquant_run
|
||||
) and sel_invalid_reasons in (
|
||||
[_KV_CACHE_DTYPE_REASON],
|
||||
[_TURBOQUANT_LAYOUT_REASON],
|
||||
[_ULTRAQUANT_LAYOUT_REASON],
|
||||
)
|
||||
if not is_turboquant_fallback:
|
||||
if not is_packed_kv_fallback:
|
||||
raise ValueError(
|
||||
f"Selected backend {selected_backend} is not valid for "
|
||||
f"this configuration. Reason: {sel_invalid_reasons}"
|
||||
@@ -733,10 +771,11 @@ class RocmPlatform(Platform):
|
||||
# NOTE: pass a str (not the list) -- info_once hashes its args.
|
||||
logger.info_once(
|
||||
"Selected backend %s is incompatible with this layer (%s) of "
|
||||
"the turboquant run; using the auto-selected per-layer backend. "
|
||||
"the %s run; using the auto-selected per-layer backend. "
|
||||
"Reason: %s",
|
||||
selected_backend.name,
|
||||
attn_selector_config.attn_type,
|
||||
"ultraquant" if is_ultraquant_run else "turboquant",
|
||||
str(sel_invalid_reasons),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user