[AR-Diffusion] Allocate K and V separately so the compiled path stops cloning the pool (#6463)
Signed-off-by: linzhenpl07 <linzhenpl07@gmail.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2df95de421
commit
38abc0ba28
5 files changed
+365
-19
No files matched your search
@@ -350,7 +350,9 @@ class ARDiffusionKVCache:
|
||||
self.null_block_id = self.manager.block_pool.null_block.block_id
|
||||
|
||||
# Allocate the per-layer paged K/V pools on the given device.
|
||||
self._kv_pools: list[torch.Tensor] = []
|
||||
# Per layer, ``[k_cache, v_cache]`` -- separate allocations; see
|
||||
# allocate_kv_pool_with_views for why they are not one tensor.
|
||||
self._kv_pools: list[list[torch.Tensor]] = []
|
||||
self._k_pools: list[torch.Tensor] = []
|
||||
self._v_pools: list[torch.Tensor] = []
|
||||
if device is not None:
|
||||
|
||||
@@ -91,22 +91,35 @@ def allocate_kv_pool_with_views(
|
||||
head_dim: int,
|
||||
dtype: torch.dtype,
|
||||
device: torch.device,
|
||||
) -> tuple[list[torch.Tensor], list[torch.Tensor], list[torch.Tensor]]:
|
||||
) -> tuple[list[list[torch.Tensor]], list[torch.Tensor], list[torch.Tensor]]:
|
||||
"""Allocate vLLM-style paged KV pools plus flat slot-write views.
|
||||
|
||||
The owning tensor follows FlashAttention's block-table cache layout:
|
||||
``(2, num_blocks, block_size, num_kv_heads, head_dim)``, where dim-0 is
|
||||
``[K, V]``. The flat K/V views keep ``slot = block_id * block_size +
|
||||
offset`` writes simple without changing the kernel-facing cache layout.
|
||||
Each cache keeps FlashAttention's block-table layout,
|
||||
``(num_blocks, block_size, num_kv_heads, head_dim)``. The flat K/V views
|
||||
keep ``slot = block_id * block_size + offset`` writes simple without
|
||||
changing the kernel-facing layout, and ``kv_pools[layer][0]`` /
|
||||
``[1]`` still address K and V.
|
||||
|
||||
K and V are allocated separately rather than as the two halves of one
|
||||
``(2, ...)`` tensor. Sharing one allocation makes them views of the same
|
||||
storage, and the paged-write custom op declares both as mutated. Inductor's
|
||||
reinplace pass can then re-inplace only the first of the two -- the second
|
||||
hits the "mutated arg aliases a graph input" case in
|
||||
``_inductor/fx_passes/reinplace.py`` -- so ``auto_functionalized_v2``'s
|
||||
clone of the whole V pool survives into the compiled graph. That clone is
|
||||
one full pool per compiled region per denoising step.
|
||||
"""
|
||||
kv_pools: list[torch.Tensor] = []
|
||||
kv_pools: list[list[torch.Tensor]] = []
|
||||
k_pools: list[torch.Tensor] = []
|
||||
v_pools: list[torch.Tensor] = []
|
||||
cache_shape = (num_blocks, block_size, num_kv_heads, head_dim)
|
||||
flat_shape = (num_blocks * block_size, num_kv_heads, head_dim)
|
||||
for _ in range(num_layers):
|
||||
kv = torch.empty(2, num_blocks, block_size, num_kv_heads, head_dim, dtype=dtype, device=device)
|
||||
kv_pools.append(kv)
|
||||
k_pools.append(kv[0].reshape(num_blocks * block_size, num_kv_heads, head_dim))
|
||||
v_pools.append(kv[1].reshape(num_blocks * block_size, num_kv_heads, head_dim))
|
||||
k = torch.empty(cache_shape, dtype=dtype, device=device)
|
||||
v = torch.empty(cache_shape, dtype=dtype, device=device)
|
||||
kv_pools.append([k, v])
|
||||
k_pools.append(k.reshape(flat_shape))
|
||||
v_pools.append(v.reshape(flat_shape))
|
||||
return kv_pools, k_pools, v_pools
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user