[Diffusion] Add native paged KV backend for diffusion workers (#6102)
Signed-off-by: Acerak01-fy <wfy2003324@163.com> Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
1 parent
c2daa471c3
commit
72ee535fcd
34 files changed
+4595
-71
No files matched your search
@@ -15,6 +15,7 @@ from vllm_omni.diffusion.attention.backends.abstract import (
|
||||
from vllm_omni.diffusion.attention.backends.sdpa import _maybe_reshape_attn_mask
|
||||
from vllm_omni.diffusion.attention.backends.utils.piecewise_attn import (
|
||||
piecewise_attn,
|
||||
run_paged_piecewise_plan,
|
||||
)
|
||||
from vllm_omni.diffusion.config import get_current_diffusion_config_or_none
|
||||
from vllm_omni.platforms import current_omni_platform
|
||||
@@ -25,6 +26,7 @@ logger = init_logger(__name__)
|
||||
class FlashAttentionBackend(AttentionBackend):
|
||||
accept_output_buffer: bool = True
|
||||
supports_piecewise_spans: bool = True
|
||||
supports_paged_kv: bool = True
|
||||
|
||||
@classmethod
|
||||
def supports_packed_mask_free(cls) -> bool:
|
||||
@@ -246,6 +248,73 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
# (b s) h d -> b s h d
|
||||
return out.reshape(batch_size, q_len, *out.shape[1:])
|
||||
|
||||
def forward_paged(self, paged_kv_context) -> torch.Tensor:
|
||||
"""Run platform-native paged attention through the Omni backend.
|
||||
|
||||
``DiffusionPagedAttentionAdapter`` owns BlockTables and prepares the
|
||||
rank-local native cache context. The adapter no longer performs the
|
||||
attention call itself; this method is the backend-owned execution
|
||||
boundary. Its native layer wrapper keeps vLLM version-specific cache
|
||||
and kernel details out of Omni's common ``Attention`` layer.
|
||||
"""
|
||||
|
||||
layer = getattr(paged_kv_context, "layer", None)
|
||||
if layer is None:
|
||||
raise TypeError("paged_kv_context must expose a native diffusion attention layer")
|
||||
kv_cache = getattr(layer, "kv_cache", None)
|
||||
if kv_cache is None:
|
||||
raise RuntimeError(f"Native KV cache is not bound for diffusion layer {layer.layer_name!r}")
|
||||
native_impl = getattr(layer, "impl", None)
|
||||
if native_impl is None:
|
||||
raise RuntimeError(f"Native attention implementation is not bound for diffusion layer {layer.layer_name!r}")
|
||||
if not layer.attn_backend.forward_includes_kv_cache_update:
|
||||
native_impl.do_kv_cache_update(
|
||||
layer,
|
||||
paged_kv_context.key_write,
|
||||
paged_kv_context.value_write,
|
||||
kv_cache,
|
||||
paged_kv_context.slot_mapping,
|
||||
)
|
||||
|
||||
def run_native_attention(
|
||||
query: torch.Tensor,
|
||||
key: torch.Tensor,
|
||||
value: torch.Tensor,
|
||||
native_metadata,
|
||||
) -> torch.Tensor:
|
||||
output = torch.empty(
|
||||
(query.shape[0], layer.num_heads, layer.head_size_v),
|
||||
dtype=query.dtype,
|
||||
device=query.device,
|
||||
)
|
||||
return native_impl.forward(
|
||||
layer,
|
||||
query,
|
||||
key,
|
||||
value,
|
||||
kv_cache,
|
||||
native_metadata,
|
||||
output,
|
||||
)
|
||||
|
||||
if paged_kv_context.piecewise_plan is not None:
|
||||
output = run_paged_piecewise_plan(
|
||||
paged_kv_context.query,
|
||||
paged_kv_context.key_write,
|
||||
paged_kv_context.value_write,
|
||||
paged_kv_context.piecewise_plan,
|
||||
paged_kv_context.piecewise_native_metadata,
|
||||
run_native_attention,
|
||||
)
|
||||
else:
|
||||
output = run_native_attention(
|
||||
paged_kv_context.query,
|
||||
paged_kv_context.key_write,
|
||||
paged_kv_context.value_write,
|
||||
paged_kv_context.native_metadata,
|
||||
)
|
||||
return paged_kv_context.restore_output(output)
|
||||
|
||||
def forward_cuda(
|
||||
self,
|
||||
query: torch.Tensor,
|
||||
|
||||
Reference in new issue
Block a user