|
| 1 | +#include "flash_decode_attn.hpp" |
| 2 | + |
| 3 | +#include "../../../utils.hpp" |
| 4 | +#include "infinicore/ops.hpp" |
| 5 | +#include "infinicore/ops/mha_kvcache.hpp" |
| 6 | + |
| 7 | +namespace infinilm::layers::attention::backends { |
| 8 | + |
| 9 | +FlashDecodeAttentionImpl::FlashDecodeAttentionImpl(size_t num_heads, |
| 10 | + size_t head_size, |
| 11 | + float scale, |
| 12 | + size_t num_kv_heads, |
| 13 | + size_t layer_idx) |
| 14 | + : num_heads_(num_heads), |
| 15 | + head_size_(head_size), |
| 16 | + scale_(scale), |
| 17 | + num_kv_heads_(num_kv_heads), |
| 18 | + layer_idx_(layer_idx), |
| 19 | + head_dim_(head_size) { |
| 20 | + |
| 21 | + const infinilm::global_state::InfinilmConfig &infinilm_config = infinilm::global_state::get_infinilm_config(); |
| 22 | + if (!infinilm_config.model_config) { |
| 23 | + throw std::runtime_error("infinilm::layers::attention::backends::FlashDecodeAttentionImpl: model_config is null"); |
| 24 | + } |
| 25 | + max_position_embeddings_ = infinilm_config.model_config->get<size_t>("max_position_embeddings"); |
| 26 | +} |
| 27 | + |
| 28 | +infinicore::Tensor FlashDecodeAttentionImpl::forward(const AttentionLayer &layer, |
| 29 | + const infinicore::Tensor &query, |
| 30 | + const infinicore::Tensor &key, |
| 31 | + const infinicore::Tensor &value, |
| 32 | + infinicore::Tensor &kv_cache, |
| 33 | + const infinilm::global_state::AttentionMetadata &attn_metadata) const { |
| 34 | + auto total_sequence_lengths = attn_metadata.total_sequence_lengths; |
| 35 | + auto input_offsets = attn_metadata.input_offsets; |
| 36 | + auto block_tables = attn_metadata.block_tables; |
| 37 | + auto slot_mapping = attn_metadata.slot_mapping; |
| 38 | + auto cu_seqlens = attn_metadata.cu_seqlens; |
| 39 | + |
| 40 | + ASSERT(block_tables.has_value()); |
| 41 | + ASSERT(slot_mapping.has_value()); |
| 42 | + |
| 43 | + // 1. update paged kv cache |
| 44 | + auto [k_total, v_total] = do_kv_cache_update(layer, key, value, kv_cache, slot_mapping.value()); |
| 45 | + |
| 46 | + size_t seq_len = query->shape()[0]; |
| 47 | + bool is_prefill = (seq_len != total_sequence_lengths.value()->shape()[0]); |
| 48 | + |
| 49 | + // 2. Compute attention |
| 50 | + infinicore::Tensor attn_output = infinicore::Tensor::empty({seq_len, num_heads_, head_dim_}, query->dtype(), query->device()); |
| 51 | + if (is_prefill) { |
| 52 | + infinicore::op::paged_attention_prefill_( |
| 53 | + attn_output, |
| 54 | + query, |
| 55 | + k_total->permute({0, 2, 1, 3}), |
| 56 | + v_total->permute({0, 2, 1, 3}), |
| 57 | + block_tables.value(), |
| 58 | + total_sequence_lengths.value(), |
| 59 | + input_offsets.value(), |
| 60 | + std::nullopt, |
| 61 | + scale_); |
| 62 | + } else { |
| 63 | + auto q_for_fa = query->view({seq_len, 1, num_heads_, head_dim_}); |
| 64 | + auto attn_out_4d = infinicore::op::mha_kvcache( |
| 65 | + q_for_fa, |
| 66 | + k_total, // [num_blocks, block_size, num_kv_heads, head_dim] |
| 67 | + v_total, |
| 68 | + total_sequence_lengths.value(), // [seq_len] int32 (one entry per sequence) |
| 69 | + block_tables.value(), // [seq_len, max_num_blocks_per_seq] int32 |
| 70 | + std::nullopt, |
| 71 | + scale_); |
| 72 | + attn_output = attn_out_4d->view({seq_len, num_heads_, head_dim_}); |
| 73 | + } |
| 74 | + attn_output = attn_output->view({1, seq_len, num_heads_ * head_dim_}); |
| 75 | + return attn_output; |
| 76 | +} |
| 77 | + |
| 78 | +std::tuple<infinicore::Tensor, infinicore::Tensor> FlashDecodeAttentionImpl::do_kv_cache_update(const AttentionLayer &layer, |
| 79 | + const infinicore::Tensor key, |
| 80 | + const infinicore::Tensor value, |
| 81 | + infinicore::Tensor &kv_cache, |
| 82 | + const infinicore::Tensor slot_mapping) const { |
| 83 | + auto k_cache_layer = kv_cache->narrow({{0, 0, 1}})->squeeze(0); |
| 84 | + auto v_cache_layer = kv_cache->narrow({{0, 1, 1}})->squeeze(0); |
| 85 | + infinicore::op::paged_caching_( |
| 86 | + k_cache_layer->permute({0, 2, 1, 3}), |
| 87 | + v_cache_layer->permute({0, 2, 1, 3}), |
| 88 | + key, |
| 89 | + value, |
| 90 | + slot_mapping); |
| 91 | + |
| 92 | + return {k_cache_layer, v_cache_layer}; |
| 93 | +} |
| 94 | +} // namespace infinilm::layers::attention::backends |
0 commit comments