vllm.compilation.passes.fusion.rope_kvcache_fusion ¶
RopeKVCacheFusionPass ¶
Bases: VllmPatternMatcherPass
This pass fuses the rotary embedding and KV cache update operations into a single fused kernel if available.
It uses the pattern matcher and matches each layer manually, as strings cannot be wildcarded. This also lets us check support on attention layers upon registration instead of during pattern matching.
This fusion eliminates the need for separate kernel launches and intermediate memory operations between the RoPE and cache update steps.
Source code in vllm/compilation/passes/fusion/rope_kvcache_fusion.py
RopeReshapeKVCachePattern ¶
This pattern matches the following unfused inplace ops
q, k = rotary_embedding(positions, q, k, head_size, cos_sin_cache, is_neox) kv_cache_dummy = unified_kv_cache_update(k, v, layer_name)
and replaces it with the fused inplace op
kv_cache_dummy = fused_rope_and_unified_kv_cache_update( q, k, v, positions, cos_sin_cache, is_neox, layer_name )
Source code in vllm/compilation/passes/fusion/rope_kvcache_fusion.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
fused_rope_and_unified_kv_cache_update_impl ¶
fused_rope_and_unified_kv_cache_update_impl(
query: Tensor,
key: Tensor,
value: Tensor,
positions: Tensor,
cos_sin_cache: Tensor,
is_neox: bool,
layer_name: str = "",
) -> Tensor
This impl fetches the KV cache and slot mapping from the forward context, then calls the layer impl's AttentionImpl.do_rope_and_kv_cache_update method. It also returns a dummy tensor, similar to Attention.unified_kv_cache_update, that is passed to unified_attention to signal a side effect and the data dependency between them to ensure torch.compile preserves ordering.