|
| 1 | +// Copyright (c) MLLM Team. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include <cmath> |
| 8 | +#include <cstdint> |
| 9 | +#include <limits> |
| 10 | + |
| 11 | +#include "mllm/backends/cpu/kernels/common/fa2_1/arch.hpp" |
| 12 | +#include "mllm/backends/cpu/kernels/common/fa2_1/impl-any.hpp" |
| 13 | +#include "mllm/core/DataTypes.hpp" |
| 14 | +#include "mllm/utils/CPUArchHelper.hpp" |
| 15 | + |
| 16 | +#if defined(MLLM_HOST_ARCH_ARM64) || defined(MLLM_HOST_ARCH_ARM) |
| 17 | +#include "mllm/backends/cpu/kernels/common/fa2_1/impl-arm.hpp" |
| 18 | +#elif defined(MLLM_HOST_ARCH_X86_64) || defined(MLLM_HOST_ARCH_X86) |
| 19 | +#include "mllm/backends/cpu/kernels/common/fa2_1/impl-any-simd.hpp" |
| 20 | +#endif |
| 21 | + |
| 22 | +namespace mllm::cpu::gqa_decode { |
| 23 | + |
| 24 | +struct BhsdStrides { |
| 25 | + int32_t batch; |
| 26 | + int32_t head; |
| 27 | + int32_t sequence; |
| 28 | + int32_t dimension; |
| 29 | +}; |
| 30 | + |
| 31 | +namespace detail { |
| 32 | + |
| 33 | +#if defined(MLLM_HOST_ARCH_ARM64) || defined(MLLM_HOST_ARCH_ARM) |
| 34 | +using attention_arch_tag = flash_attn2::details::arm_arch_tag; |
| 35 | +#elif defined(MLLM_HOST_ARCH_X86_64) || defined(MLLM_HOST_ARCH_X86) |
| 36 | +using attention_arch_tag = flash_attn2::details::x86_arch_tag; |
| 37 | +#else |
| 38 | +using attention_arch_tag = flash_attn2::details::any_arch_tag; |
| 39 | +#endif |
| 40 | + |
| 41 | +inline bool validStrides(const BhsdStrides& strides) { |
| 42 | + return strides.batch > 0 && strides.head > 0 && strides.sequence > 0 && strides.dimension == 1; |
| 43 | +} |
| 44 | + |
| 45 | +} // namespace detail |
| 46 | + |
| 47 | +// Single-token float32 GQA for native KV-head [B, H, S, D] cache views. |
| 48 | +// QK and softmax preserve C10's complete per-query-head traversal. P@V groups |
| 49 | +// the query heads sharing one KV head so each V token is reused while hot, but |
| 50 | +// every output head still accumulates in increasing key-index order. |
| 51 | +// Scratch: group_size * kv_sequence floats. |
| 52 | +inline bool fwdBhsdFp32(int32_t batch_size, int32_t query_heads, int32_t kv_heads, int32_t kv_sequence, int32_t qk_dim, |
| 53 | + int32_t value_dim, const mllm_fp32_t* query, BhsdStrides query_strides, const mllm_fp32_t* key, |
| 54 | + BhsdStrides key_strides, const mllm_fp32_t* value, BhsdStrides value_strides, mllm_fp32_t* output, |
| 55 | + BhsdStrides output_strides, mllm_fp32_t* grouped_scratch) { |
| 56 | + if (batch_size <= 0 || query_heads <= 0 || kv_heads <= 0 || kv_sequence <= 0 || qk_dim <= 0 || value_dim <= 0 |
| 57 | + || query_heads % kv_heads != 0 || query == nullptr || key == nullptr || value == nullptr || output == nullptr |
| 58 | + || grouped_scratch == nullptr || !detail::validStrides(query_strides) || !detail::validStrides(key_strides) |
| 59 | + || !detail::validStrides(value_strides) || !detail::validStrides(output_strides)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + using ArchTag = detail::attention_arch_tag; |
| 64 | + const int32_t group_size = query_heads / kv_heads; |
| 65 | + const float scale = 1.0F / std::sqrt(static_cast<float>(qk_dim)); |
| 66 | + |
| 67 | + for (int32_t batch = 0; batch < batch_size; ++batch) { |
| 68 | + for (int32_t kv_head = 0; kv_head < kv_heads; ++kv_head) { |
| 69 | + const int32_t first_query_head = kv_head * group_size; |
| 70 | + const auto* query_group = query + static_cast<size_t>(batch) * query_strides.batch |
| 71 | + + static_cast<size_t>(first_query_head) * query_strides.head; |
| 72 | + const auto* key_head = key + static_cast<size_t>(batch) * key_strides.batch |
| 73 | + + static_cast<size_t>(kv_head) * key_strides.head; |
| 74 | + const auto* value_head = value + static_cast<size_t>(batch) * value_strides.batch |
| 75 | + + static_cast<size_t>(kv_head) * value_strides.head; |
| 76 | + auto* output_group = output + static_cast<size_t>(batch) * output_strides.batch |
| 77 | + + static_cast<size_t>(first_query_head) * output_strides.head; |
| 78 | + |
| 79 | + for (int32_t group_index = 0; group_index < group_size; ++group_index) { |
| 80 | + const auto* query_token = query_group + static_cast<size_t>(group_index) * query_strides.head; |
| 81 | + auto* probabilities = grouped_scratch + static_cast<size_t>(group_index) * kv_sequence; |
| 82 | + float maximum = std::numeric_limits<float>::lowest(); |
| 83 | + for (int32_t key_index = 0; key_index < kv_sequence; ++key_index) { |
| 84 | + const auto* key_token = key_head + static_cast<size_t>(key_index) * key_strides.sequence; |
| 85 | + float score = 0.0F; |
| 86 | + flash_attn2::details::VectorDotProduct<ArchTag, mllm_fp32_t, mllm_fp32_t, mllm_fp32_t>::run( |
| 87 | + query_token, key_token, &score, static_cast<size_t>(qk_dim)); |
| 88 | + probabilities[key_index] = score * scale; |
| 89 | + maximum = std::max(maximum, probabilities[key_index]); |
| 90 | + } |
| 91 | + |
| 92 | + float denominator = 0.0F; |
| 93 | + for (int32_t key_index = 0; key_index < kv_sequence; ++key_index) { |
| 94 | + probabilities[key_index] = std::exp(probabilities[key_index] - maximum); |
| 95 | + denominator += probabilities[key_index]; |
| 96 | + } |
| 97 | + const float inverse_denominator = 1.0F / denominator; |
| 98 | + for (int32_t key_index = 0; key_index < kv_sequence; ++key_index) { |
| 99 | + probabilities[key_index] *= inverse_denominator; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + for (int32_t group_index = 0; group_index < group_size; ++group_index) { |
| 104 | + auto* output_token = output_group + static_cast<size_t>(group_index) * output_strides.head; |
| 105 | + flash_attn2::details::FilledWithConst<ArchTag, mllm_fp32_t>::run(output_token, 0.0F, |
| 106 | + static_cast<size_t>(value_dim)); |
| 107 | + } |
| 108 | + for (int32_t key_index = 0; key_index < kv_sequence; ++key_index) { |
| 109 | + const auto* value_token = value_head + static_cast<size_t>(key_index) * value_strides.sequence; |
| 110 | + auto* probability = grouped_scratch + key_index; |
| 111 | + auto* output_token = output_group; |
| 112 | + for (int32_t group_index = 0; group_index < group_size; ++group_index) { |
| 113 | + flash_attn2::details::FMAConstArray<ArchTag, mllm_fp32_t, mllm_fp32_t, mllm_fp32_t>::run( |
| 114 | + output_token, *probability, value_token, static_cast<size_t>(value_dim)); |
| 115 | + probability += kv_sequence; |
| 116 | + output_token += output_strides.head; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return true; |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace mllm::cpu::gqa_decode |
0 commit comments