|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | +import torch |
| 7 | + |
| 8 | +from vllm.v1.attention.backends.flash_attn import ( |
| 9 | + FlashAttentionBackend, FlashAttentionMetadataBuilder) |
| 10 | +from vllm.v1.attention.backends.flex_attention import ( |
| 11 | + FlexAttentionBackend, FlexAttentionMetadataBuilder) |
| 12 | +from vllm.v1.kv_cache_interface import FullAttentionSpec, KVCacheGroupSpec |
| 13 | +from vllm.v1.worker.utils import (AttentionGroup, |
| 14 | + initialize_kv_cache_for_kv_sharing) |
| 15 | + |
| 16 | + |
| 17 | +def new_kv_cache_spec(): |
| 18 | + return FullAttentionSpec(16, 1, 1, torch.float32, False) |
| 19 | + |
| 20 | + |
| 21 | +def test_initialize_kv_cache_for_kv_sharing_different_attn_groups(): |
| 22 | + """ |
| 23 | + Test initializing KV cache sharing with different attention groups. |
| 24 | + Layers in the same KV cache group might be placed in different attn groups |
| 25 | + if they have different attention backends. |
| 26 | + """ |
| 27 | + shared_kv_cache_layers = { |
| 28 | + "model.layers.2": "model.layers.0", |
| 29 | + "model.layers.3": "model.layers.1", |
| 30 | + } |
| 31 | + |
| 32 | + # Layers 0 and 1 both belong in KV cache group 0 |
| 33 | + # However, if they have have different attention backends, they will be |
| 34 | + # placed in different attention groups for KV cache group 0 |
| 35 | + kv_cache_groups = [ |
| 36 | + KVCacheGroupSpec(["model.layers.0", "model.layers.1"], |
| 37 | + new_kv_cache_spec()), |
| 38 | + ] |
| 39 | + |
| 40 | + attn_groups = [ |
| 41 | + # KV cache group 0 has two attention groups |
| 42 | + [ |
| 43 | + AttentionGroup( |
| 44 | + backend=FlashAttentionBackend, |
| 45 | + metadata_builder=Mock(spec=FlashAttentionMetadataBuilder), |
| 46 | + layer_names=["model.layers.0"], |
| 47 | + ), |
| 48 | + AttentionGroup( |
| 49 | + backend=FlexAttentionBackend, |
| 50 | + metadata_builder=Mock(spec=FlexAttentionMetadataBuilder), |
| 51 | + layer_names=["model.layers.1"], |
| 52 | + ), |
| 53 | + ], |
| 54 | + ] |
| 55 | + |
| 56 | + # Only layers 0 and 1 will have KV caches allocated |
| 57 | + kv_caches = { |
| 58 | + "model.layers.0": torch.zeros(1, 2, 3), |
| 59 | + "model.layers.1": torch.ones(1, 2, 3), |
| 60 | + } |
| 61 | + |
| 62 | + initialize_kv_cache_for_kv_sharing( |
| 63 | + shared_kv_cache_layers=shared_kv_cache_layers, |
| 64 | + kv_cache_groups=kv_cache_groups, |
| 65 | + kv_caches=kv_caches, |
| 66 | + attn_groups=attn_groups, |
| 67 | + ) |
| 68 | + |
| 69 | + # Check that the KV caches were shared correctly |
| 70 | + assert kv_caches["model.layers.2"].data_ptr( |
| 71 | + ) == kv_caches["model.layers.0"].data_ptr() |
| 72 | + assert kv_caches["model.layers.3"].data_ptr( |
| 73 | + ) == kv_caches["model.layers.1"].data_ptr() |
| 74 | + |
| 75 | + # Check that the layers were added to the correct KV cache group |
| 76 | + assert len(kv_cache_groups) == 1 |
| 77 | + assert kv_cache_groups[0].layer_names == [ |
| 78 | + "model.layers.0", "model.layers.1", "model.layers.2", "model.layers.3" |
| 79 | + ] |
| 80 | + |
| 81 | + # Check that the layers were added to the attention groups |
| 82 | + assert len(attn_groups) == 1 and len(attn_groups[0]) == 2 |
| 83 | + assert attn_groups[0][0].layer_names == [ |
| 84 | + "model.layers.0", "model.layers.2" |
| 85 | + ] |
| 86 | + assert attn_groups[0][1].layer_names == [ |
| 87 | + "model.layers.1", "model.layers.3" |
| 88 | + ] |
| 89 | + |
| 90 | + |
| 91 | +def test_initialize_kv_cache_for_kv_sharing_same_attn_groups(): |
| 92 | + """ |
| 93 | + Test case assuming that all layers in the same KV cache group have the same |
| 94 | + attention backends. This is true for most models. |
| 95 | + """ |
| 96 | + shared_kv_cache_layers = { |
| 97 | + "model.layers.2": "model.layers.0", |
| 98 | + "model.layers.3": "model.layers.1", |
| 99 | + } |
| 100 | + |
| 101 | + kv_cache_groups = [ |
| 102 | + KVCacheGroupSpec(["model.layers.0", "model.layers.1"], |
| 103 | + new_kv_cache_spec()), |
| 104 | + ] |
| 105 | + |
| 106 | + attn_groups = [ |
| 107 | + # KV cache group 0 has a single attention group |
| 108 | + # as all layers have the same flash attention backend |
| 109 | + [ |
| 110 | + AttentionGroup( |
| 111 | + backend=FlashAttentionBackend, |
| 112 | + metadata_builder=Mock(spec=FlashAttentionMetadataBuilder), |
| 113 | + layer_names=["model.layers.0", "model.layers.1"], |
| 114 | + ), |
| 115 | + ], |
| 116 | + ] |
| 117 | + |
| 118 | + kv_caches = { |
| 119 | + "model.layers.0": torch.zeros(1, 2, 3), |
| 120 | + "model.layers.1": torch.ones(1, 2, 3), |
| 121 | + } |
| 122 | + |
| 123 | + initialize_kv_cache_for_kv_sharing( |
| 124 | + shared_kv_cache_layers=shared_kv_cache_layers, |
| 125 | + kv_cache_groups=kv_cache_groups, |
| 126 | + kv_caches=kv_caches, |
| 127 | + attn_groups=attn_groups, |
| 128 | + ) |
| 129 | + |
| 130 | + # Check that the KV caches were shared correctly |
| 131 | + assert kv_caches["model.layers.2"].data_ptr( |
| 132 | + ) == kv_caches["model.layers.0"].data_ptr() |
| 133 | + assert kv_caches["model.layers.3"].data_ptr( |
| 134 | + ) == kv_caches["model.layers.1"].data_ptr() |
| 135 | + |
| 136 | + # Check that the layers were added to the correct KV cache group |
| 137 | + assert len(kv_cache_groups) == 1 |
| 138 | + assert kv_cache_groups[0].layer_names == [ |
| 139 | + "model.layers.0", "model.layers.1", "model.layers.2", "model.layers.3" |
| 140 | + ] |
| 141 | + |
| 142 | + # Check that the layers were added to the attention groups |
| 143 | + assert len(attn_groups) == 1 and len(attn_groups[0]) == 1 |
| 144 | + assert attn_groups[0][0].layer_names == [ |
| 145 | + "model.layers.0", "model.layers.1", "model.layers.2", "model.layers.3" |
| 146 | + ] |
| 147 | + |
| 148 | + |
| 149 | +def test_initialize_kv_cache_for_kv_sharing_no_attn_groups(): |
| 150 | + """ |
| 151 | + Test KV sharing set up when no attention groups are provided. |
| 152 | + This is the case for the TPU model runner, which doesn't have |
| 153 | + support for attention groups yet. |
| 154 | + """ |
| 155 | + shared_kv_cache_layers = { |
| 156 | + "model.layers.2": "model.layers.0", |
| 157 | + "model.layers.3": "model.layers.1", |
| 158 | + } |
| 159 | + |
| 160 | + kv_cache_groups = [ |
| 161 | + KVCacheGroupSpec(["model.layers.0"], new_kv_cache_spec()), |
| 162 | + KVCacheGroupSpec(["model.layers.1"], new_kv_cache_spec()), |
| 163 | + ] |
| 164 | + |
| 165 | + kv_caches = { |
| 166 | + "model.layers.0": torch.zeros(1, 2, 3), |
| 167 | + "model.layers.1": torch.ones(1, 2, 3), |
| 168 | + } |
| 169 | + |
| 170 | + initialize_kv_cache_for_kv_sharing( |
| 171 | + shared_kv_cache_layers=shared_kv_cache_layers, |
| 172 | + kv_cache_groups=kv_cache_groups, |
| 173 | + kv_caches=kv_caches, |
| 174 | + ) |
| 175 | + |
| 176 | + # Check that the KV caches were shared correctly |
| 177 | + assert kv_caches["model.layers.2"].data_ptr( |
| 178 | + ) == kv_caches["model.layers.0"].data_ptr() |
| 179 | + assert kv_caches["model.layers.3"].data_ptr( |
| 180 | + ) == kv_caches["model.layers.1"].data_ptr() |
| 181 | + |
| 182 | + # Check that the layers were added to the correct KV cache group |
| 183 | + assert len(kv_cache_groups) == 2 |
| 184 | + assert kv_cache_groups[0].layer_names == [ |
| 185 | + "model.layers.0", "model.layers.2" |
| 186 | + ] |
| 187 | + assert kv_cache_groups[1].layer_names == [ |
| 188 | + "model.layers.1", "model.layers.3" |
| 189 | + ] |
0 commit comments