diff --git a/ggml/src/ggml-spacemit/ime2_kernels.cpp b/ggml/src/ggml-spacemit/ime2_kernels.cpp index 7450109acc82..06d055b418fa 100644 --- a/ggml/src/ggml-spacemit/ime2_kernels.cpp +++ b/ggml/src/ggml-spacemit/ime2_kernels.cpp @@ -5342,6 +5342,110 @@ void gemm_kernel_f16f16f32_m16_vfwmadot(size_t gemm_k, count_m, count_n, ldc, accumulate); } +// Decode QK: A is [K/8][8][8], B is [K/8][32][8]. +// Only the eight useful query heads are evaluated. Paired K8 panels keep +// the same ascending depth order as the M16 kernel, without unused rows. +void fa_qk_f16f16f32_m8n32(size_t depth, const _Float16 * b, const _Float16 * a, + float * out, size_t ldc) { + GGML_ASSERT(depth > 0 && depth % 8 == 0 && ldc >= 32); + const _Float16 * b0 = b; + const _Float16 * b1 = b + 64; + const _Float16 * b2 = b + 128; + const _Float16 * b3 = b + 192; + size_t kc = depth / 8; + const size_t stride = ldc * sizeof(float); + __asm__ volatile( + "vsetvli t0, zero, e32, m8, tu, mu\n\t" + "vxor.vv v16, v16, v16\n\t" + "vsetvli t0, zero, e16, m1, tu, mu\n\t" + "vle16.v v0, (%[a])\n\t" + "addi %[a], %[a], 128\n\t" + "vle16.v v2, (%[b0])\n\t" + "addi %[b0], %[b0], 512\n\t" + "vle16.v v3, (%[b1])\n\t" + "addi %[b1], %[b1], 512\n\t" + "vle16.v v4, (%[b2])\n\t" + "addi %[b2], %[b2], 512\n\t" + "vle16.v v5, (%[b3])\n\t" + "addi %[b3], %[b3], 512\n\t" + "addi %[kc], %[kc], -1\n\t" + "beqz %[kc], .L_qk8_cur_%=\n\t" + ".L_qk8_loop_%=:\n\t" + "vle16.v v1, (%[a])\n\t" + "addi %[a], %[a], 128\n\t" + "vle16.v v6, (%[b0])\n\t" + "addi %[b0], %[b0], 512\n\t" + "vfwmadot v16, v0, v2\n\t" + "vle16.v v7, (%[b1])\n\t" + "addi %[b1], %[b1], 512\n\t" + "vfwmadot v18, v0, v3\n\t" + "vle16.v v8, (%[b2])\n\t" + "addi %[b2], %[b2], 512\n\t" + "vfwmadot v20, v0, v4\n\t" + "vle16.v v9, (%[b3])\n\t" + "addi %[b3], %[b3], 512\n\t" + "vfwmadot v22, v0, v5\n\t" + "addi %[kc], %[kc], -1\n\t" + "beqz %[kc], .L_qk8_next_%=\n\t" + "vle16.v v0, (%[a])\n\t" + "addi %[a], %[a], 128\n\t" + "vle16.v v2, (%[b0])\n\t" + "addi %[b0], %[b0], 512\n\t" + "vfwmadot v16, v1, v6\n\t" + "vle16.v v3, (%[b1])\n\t" + "addi %[b1], %[b1], 512\n\t" + "vfwmadot v18, v1, v7\n\t" + "vle16.v v4, (%[b2])\n\t" + "addi %[b2], %[b2], 512\n\t" + "vfwmadot v20, v1, v8\n\t" + "vle16.v v5, (%[b3])\n\t" + "addi %[b3], %[b3], 512\n\t" + "vfwmadot v22, v1, v9\n\t" + "addi %[kc], %[kc], -1\n\t" + "bnez %[kc], .L_qk8_loop_%=\n\t" + ".L_qk8_cur_%=:\n\t" + "vfwmadot v16, v0, v2\n\t" + "vfwmadot v18, v0, v3\n\t" + "vfwmadot v20, v0, v4\n\t" + "vfwmadot v22, v0, v5\n\t" + "j .L_qk8_pack_%=\n\t" + ".L_qk8_next_%=:\n\t" + "vfwmadot v16, v1, v6\n\t" + "vfwmadot v18, v1, v7\n\t" + "vfwmadot v20, v1, v8\n\t" + "vfwmadot v22, v1, v9\n\t" + ".L_qk8_pack_%=:\n\t" + "vsetvli t0, zero, e32, m1, tu, mu\n\t" + "vpack.vv v0, v16, v18, 2\n\t" + "vpack.vv v2, v20, v22, 2\n\t" + "vpack.vv v4, v17, v19, 2\n\t" + "vpack.vv v6, v21, v23, 2\n\t" + "vpack.vv v16, v0, v2, 3\n\t" + "vpack.vv v18, v1, v3, 3\n\t" + "vpack.vv v20, v4, v6, 3\n\t" + "vpack.vv v22, v5, v7, 3\n\t" + "vse32.v v16, (%[out])\n\t" + "add %[out], %[out], %[stride]\n\t" + "vse32.v v17, (%[out])\n\t" + "add %[out], %[out], %[stride]\n\t" + "vse32.v v18, (%[out])\n\t" + "add %[out], %[out], %[stride]\n\t" + "vse32.v v19, (%[out])\n\t" + "add %[out], %[out], %[stride]\n\t" + "vse32.v v20, (%[out])\n\t" + "add %[out], %[out], %[stride]\n\t" + "vse32.v v21, (%[out])\n\t" + "add %[out], %[out], %[stride]\n\t" + "vse32.v v22, (%[out])\n\t" + "add %[out], %[out], %[stride]\n\t" + "vse32.v v23, (%[out])\n\t" + : [a] "+&r"(a), [b0] "+&r"(b0), [b1] "+&r"(b1), [b2] "+&r"(b2), + [b3] "+&r"(b3), [kc] "+&r"(kc), [out] "+&r"(out) + : [stride] "r"(stride) + : "memory", "t0", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", + "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23"); +} + void fa_pv_f16f16f32_m16n32_k64_accumulate(const _Float16 * b_f16_k8, _Float16 * a_f16_k8, float * c_ptr, @@ -5355,7 +5459,7 @@ void fa_pv_f16f16f32_m16n32_k64_accumulate(const _Float16 * b_f16_k8, // Pack row-major F16 rows into IME2 [K/8][panel rows][8] panels. // Callers zero the destination for padding rows before packing. -static void fa_pack_f16_k8(_Float16 * dst, const char * src, size_t row_bytes, +void fa_pack_f16_k8(_Float16 * dst, const char * src, size_t row_bytes, int rows, int depth, int panel_rows) { if (depth >= 128) { // Treat K8 as two 64-bit words. Strided segmented loads gather one diff --git a/ggml/src/ggml-spacemit/ime_kernels.h b/ggml/src/ggml-spacemit/ime_kernels.h index 3f9094dfa1fa..648c068d0b2c 100644 --- a/ggml/src/ggml-spacemit/ime_kernels.h +++ b/ggml/src/ggml-spacemit/ime_kernels.h @@ -243,6 +243,14 @@ void gemm_kernel_f16f16f32_m16_vfwmadot(size_t gemm_k, size_t ldc, bool accumulate = false); +// Pack row-major F16 rows into IME2 [K/8][panel rows][8] panels. +void fa_pack_f16_k8(_Float16 * dst, const char * src, size_t row_bytes, + int rows, int depth, int panel_rows); + +// Decode QK: full M8/N32, packed K8 panels, row-major F32 output. +void fa_qk_f16f16f32_m8n32(size_t depth, const _Float16 * b, const _Float16 * a, + float * out, size_t ldc); + // Flash-attention PV: K64/N32 packed operands, direct F32 accumulation into valid M rows. void fa_pv_f16f16f32_m16n32_k64_accumulate(const _Float16 * b_f16_k8, _Float16 * a_f16_k8, diff --git a/ggml/src/ggml-spacemit/rvv_kernels.cpp b/ggml/src/ggml-spacemit/rvv_kernels.cpp index cf48465ad067..501f1abbd5b1 100644 --- a/ggml/src/ggml-spacemit/rvv_kernels.cpp +++ b/ggml/src/ggml-spacemit/rvv_kernels.cpp @@ -1,4 +1,6 @@ #include "rvv_kernels.h" +#include "ime_kernels.h" +#include "spacemit-env.h" #include "common.h" #include "ggml.h" @@ -35,6 +37,21 @@ namespace { using namespace detail; +enum class spacemit_fa_decode_kernel { generic, packed_pv4, packed_pv8 }; + +// Split-KV GQA8 decode uses the same QK layout for both head geometries. +// The two-KV-head layout also benefits from sharing V loads across eight rows. +constexpr spacemit_fa_decode_kernel spacemit_select_fa_decode( + int64_t q_heads, int64_t kv_heads, int64_t dk, int64_t dv) { + if (dk == dv && q_heads == 8 && kv_heads == 1 && (dk == 256 || dk == 512)) { + return spacemit_fa_decode_kernel::packed_pv4; + } + if (dk == 256 && dv == 256 && q_heads == 16 && kv_heads == 2) { + return spacemit_fa_decode_kernel::packed_pv8; + } + return spacemit_fa_decode_kernel::generic; +} + auto align_up(size_t value, size_t alignment) { return (value + alignment - 1) / alignment * alignment; } @@ -505,6 +522,358 @@ static void permute_transpose_impl(const ggml_tensor * src0, } } +// Software pipeline for four Q heads: load the next depth before consuming +// the current one. Each FP32 accumulator retains the ascending depth/FMA order. +static __attribute__((noinline)) void fa_decode_qk4_pipeline( + float * out, const _Float16 * q, const _Float16 * k, + int64_t depth, int64_t stride, int64_t count, float scale) { + GGML_ASSERT(depth >= 2 && depth % 2 == 0 && count > 0 && count <= 64); + const _Float16 * q0 = q; + const _Float16 * q1 = q + depth; + const _Float16 * q2 = q + 2 * depth; + const _Float16 * q3 = q + 3 * depth; + int64_t pairs = depth / 2 - 1; + const int64_t k_stride = stride * sizeof(_Float16); + const int64_t out_stride = stride * sizeof(float); + // clang-format off + __asm__ volatile( + "vsetvli t0, zero, e32, m8\n\t" + "vxor.vv v8, v8, v8\n\t" + "vsetvli t0, %[count], e16, m1\n\t" + "vle16.v v1, (%[k])\n\t" + "add %[k], %[k], %[ks]\n\t" + "flh f0, 0(%[q0])\n\t" + "flh f1, 0(%[q1])\n\t" + "flh f2, 0(%[q2])\n\t" + "flh f3, 0(%[q3])\n\t" + "beqz %[pairs], QK4_PIPE_TAIL%=\n\t" + ".align 4\n\t" + "QK4_PIPE_LOOP%=:\n\t" + "vle16.v v2, (%[k])\n\t" + "add %[k], %[k], %[ks]\n\t" + "flh f4, 2(%[q0])\n\t" + "flh f5, 2(%[q1])\n\t" + "flh f6, 2(%[q2])\n\t" + "flh f7, 2(%[q3])\n\t" + "vfwmacc.vf v8, f0, v1\n\t" + "vfwmacc.vf v10, f1, v1\n\t" + "vfwmacc.vf v12, f2, v1\n\t" + "vfwmacc.vf v14, f3, v1\n\t" + "vle16.v v1, (%[k])\n\t" + "add %[k], %[k], %[ks]\n\t" + "flh f0, 4(%[q0])\n\t" + "flh f1, 4(%[q1])\n\t" + "flh f2, 4(%[q2])\n\t" + "flh f3, 4(%[q3])\n\t" + "vfwmacc.vf v8, f4, v2\n\t" + "vfwmacc.vf v10, f5, v2\n\t" + "vfwmacc.vf v12, f6, v2\n\t" + "vfwmacc.vf v14, f7, v2\n\t" + "addi %[q0], %[q0], 4\n\t" + "addi %[q1], %[q1], 4\n\t" + "addi %[q2], %[q2], 4\n\t" + "addi %[q3], %[q3], 4\n\t" + "addi %[pairs], %[pairs], -1\n\t" + "bnez %[pairs], QK4_PIPE_LOOP%=\n\t" + "QK4_PIPE_TAIL%=:\n\t" + "vle16.v v2, (%[k])\n\t" + "flh f4, 2(%[q0])\n\t" + "flh f5, 2(%[q1])\n\t" + "flh f6, 2(%[q2])\n\t" + "flh f7, 2(%[q3])\n\t" + "vfwmacc.vf v8, f0, v1\n\t" + "vfwmacc.vf v10, f1, v1\n\t" + "vfwmacc.vf v12, f2, v1\n\t" + "vfwmacc.vf v14, f3, v1\n\t" + "vfwmacc.vf v8, f4, v2\n\t" + "vfwmacc.vf v10, f5, v2\n\t" + "vfwmacc.vf v12, f6, v2\n\t" + "vfwmacc.vf v14, f7, v2\n\t" + "vsetvli t0, %[count], e32, m2\n\t" + "vfmul.vf v8, v8, %[scale]\n\t" + "vse32.v v8, (%[out])\n\t" + "add %[out], %[out], %[os]\n\t" + "vfmul.vf v10, v10, %[scale]\n\t" + "vse32.v v10, (%[out])\n\t" + "add %[out], %[out], %[os]\n\t" + "vfmul.vf v12, v12, %[scale]\n\t" + "vse32.v v12, (%[out])\n\t" + "add %[out], %[out], %[os]\n\t" + "vfmul.vf v14, v14, %[scale]\n\t" + "vse32.v v14, (%[out])\n\t" + : [q0] "+&r"(q0), [q1] "+&r"(q1), [q2] "+&r"(q2), [q3] "+&r"(q3), + [k] "+&r"(k), [out] "+&r"(out), [pairs] "+&r"(pairs) + : [count] "r"(count), [ks] "r"(k_stride), [os] "r"(out_stride), [scale] "f"(scale) + : "memory", "t0", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", + "v1", "v2", "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15"); + // clang-format on +} + +// The decode PV path already rounds each probability to FP16. Convert a tile +// once with RVV, then reuse the same rounded values across all V segments. +static void fa_pack_decode_prob_f16(_Float16 * out, const float * prob, + int64_t stride, int64_t count) { + for (int64_t row = 0; row < 8; ++row) { + for (int64_t i = 0; i < count;) { + const size_t vl = __riscv_vsetvl_e32m4(count - i); + const auto values = __riscv_vle32_v_f32m4(prob + row * stride + i, vl); + __riscv_vse16_v_f16m2(out + row * stride + i, + __riscv_vfncvt_f_f_w_f16m2(values, vl), vl); + i += vl; + } + } +} + +template +static void fa_decode_pv4_prob(_Float16 * out, const P * prob, const char * v, + int64_t row_bytes, int64_t width, int64_t stride, int64_t count) { + for (int64_t d = 0; d < width;) { + const size_t vl = __riscv_vsetvl_e16m4(width - d); + for (int64_t g = 0; g < 8; g += 4) { + auto a0 = __riscv_vle16_v_f16m4(out + (g + 0) * width + d, vl); + auto a1 = __riscv_vle16_v_f16m4(out + (g + 1) * width + d, vl); + auto a2 = __riscv_vle16_v_f16m4(out + (g + 2) * width + d, vl); + auto a3 = __riscv_vle16_v_f16m4(out + (g + 3) * width + d, vl); + for (int64_t t = 0; t < count; ++t) { + const auto vv = __riscv_vle16_v_f16m4( + reinterpret_cast(v + t * row_bytes) + d, vl); + a0 = __riscv_vfmacc_vf_f16m4(a0, (_Float16) prob[(g + 0) * stride + t], vv, vl); + a1 = __riscv_vfmacc_vf_f16m4(a1, (_Float16) prob[(g + 1) * stride + t], vv, vl); + a2 = __riscv_vfmacc_vf_f16m4(a2, (_Float16) prob[(g + 2) * stride + t], vv, vl); + a3 = __riscv_vfmacc_vf_f16m4(a3, (_Float16) prob[(g + 3) * stride + t], vv, vl); + } + __riscv_vse16_v_f16m4(out + (g + 0) * width + d, a0, vl); + __riscv_vse16_v_f16m4(out + (g + 1) * width + d, a1, vl); + __riscv_vse16_v_f16m4(out + (g + 2) * width + d, a2, vl); + __riscv_vse16_v_f16m4(out + (g + 3) * width + d, a3, vl); + } + d += vl; + } +} + +// Eight query heads reuse each V segment once. Two V registers preserve +// ascending token/FMA order while loading the next row before current FMAs. +template +static __attribute__((noinline)) void fa_decode_pv8_pipeline( + _Float16 * out, const P * prob, const char * v, + int64_t row_bytes, int64_t width, int64_t stride, int64_t count) { + GGML_ASSERT(count >= 2 && count % 2 == 0 && stride >= count); + for (int64_t d = 0; d < width;) { + const size_t vl = __riscv_vsetvl_e16m2(width - d); + const P * p0 = prob + 0 * stride; + const P * p1 = prob + 1 * stride; + const P * p2 = prob + 2 * stride; + const P * p3 = prob + 3 * stride; + const P * p4 = prob + 4 * stride; + const P * p5 = prob + 5 * stride; + const P * p6 = prob + 6 * stride; + const P * p7 = prob + 7 * stride; + const char * vp = v + d * sizeof(_Float16); + int64_t pairs = count / 2 - 1; + __asm__ volatile( + "vsetvli zero, %[vl], e16, m2\n\t" + "mv t0, %[out]\n\t" + "vle16.v v0, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vle16.v v2, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vle16.v v4, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vle16.v v6, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vle16.v v8, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vle16.v v10, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vle16.v v12, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vle16.v v14, (t0)\n\t" + "vle16.v v16, (%[vp])\n\t" + "add %[vp], %[vp], %[row_bytes]\n\t" + ".if %[psize] == 4\n\t" + "flw f0, 0(%[p0])\n\t" + "fcvt.h.s f0, f0\n\t" + "flw f1, 0(%[p1])\n\t" + "fcvt.h.s f1, f1\n\t" + "flw f2, 0(%[p2])\n\t" + "fcvt.h.s f2, f2\n\t" + "flw f3, 0(%[p3])\n\t" + "fcvt.h.s f3, f3\n\t" + "flw f4, 0(%[p4])\n\t" + "fcvt.h.s f4, f4\n\t" + "flw f5, 0(%[p5])\n\t" + "fcvt.h.s f5, f5\n\t" + "flw f6, 0(%[p6])\n\t" + "fcvt.h.s f6, f6\n\t" + "flw f7, 0(%[p7])\n\t" + "fcvt.h.s f7, f7\n\t" + ".else\n\t" + "flh f0, 0(%[p0])\n\t" + "flh f1, 0(%[p1])\n\t" + "flh f2, 0(%[p2])\n\t" + "flh f3, 0(%[p3])\n\t" + "flh f4, 0(%[p4])\n\t" + "flh f5, 0(%[p5])\n\t" + "flh f6, 0(%[p6])\n\t" + "flh f7, 0(%[p7])\n\t" + ".endif\n\t" + "beqz %[pairs], PV8_PIPE_TAIL%=\n\t" + ".align 4\n\t" + "PV8_PIPE_LOOP%=:\n\t" + "vle16.v v18, (%[vp])\n\t" + "add %[vp], %[vp], %[row_bytes]\n\t" + ".if %[psize] == 4\n\t" + "flw f8, 4(%[p0])\n\t" + "fcvt.h.s f8, f8\n\t" + "flw f9, 4(%[p1])\n\t" + "fcvt.h.s f9, f9\n\t" + "flw f10, 4(%[p2])\n\t" + "fcvt.h.s f10, f10\n\t" + "flw f11, 4(%[p3])\n\t" + "fcvt.h.s f11, f11\n\t" + "flw f12, 4(%[p4])\n\t" + "fcvt.h.s f12, f12\n\t" + "flw f13, 4(%[p5])\n\t" + "fcvt.h.s f13, f13\n\t" + "flw f14, 4(%[p6])\n\t" + "fcvt.h.s f14, f14\n\t" + "flw f15, 4(%[p7])\n\t" + "fcvt.h.s f15, f15\n\t" + ".else\n\t" + "flh f8, 2(%[p0])\n\t" + "flh f9, 2(%[p1])\n\t" + "flh f10, 2(%[p2])\n\t" + "flh f11, 2(%[p3])\n\t" + "flh f12, 2(%[p4])\n\t" + "flh f13, 2(%[p5])\n\t" + "flh f14, 2(%[p6])\n\t" + "flh f15, 2(%[p7])\n\t" + ".endif\n\t" + "vfmacc.vf v0, f0, v16\n\t" + "vfmacc.vf v2, f1, v16\n\t" + "vfmacc.vf v4, f2, v16\n\t" + "vfmacc.vf v6, f3, v16\n\t" + "vfmacc.vf v8, f4, v16\n\t" + "vfmacc.vf v10, f5, v16\n\t" + "vfmacc.vf v12, f6, v16\n\t" + "vfmacc.vf v14, f7, v16\n\t" + "vle16.v v16, (%[vp])\n\t" + "add %[vp], %[vp], %[row_bytes]\n\t" + ".if %[psize] == 4\n\t" + "flw f0, 8(%[p0])\n\t" + "fcvt.h.s f0, f0\n\t" + "flw f1, 8(%[p1])\n\t" + "fcvt.h.s f1, f1\n\t" + "flw f2, 8(%[p2])\n\t" + "fcvt.h.s f2, f2\n\t" + "flw f3, 8(%[p3])\n\t" + "fcvt.h.s f3, f3\n\t" + "flw f4, 8(%[p4])\n\t" + "fcvt.h.s f4, f4\n\t" + "flw f5, 8(%[p5])\n\t" + "fcvt.h.s f5, f5\n\t" + "flw f6, 8(%[p6])\n\t" + "fcvt.h.s f6, f6\n\t" + "flw f7, 8(%[p7])\n\t" + "fcvt.h.s f7, f7\n\t" + ".else\n\t" + "flh f0, 4(%[p0])\n\t" + "flh f1, 4(%[p1])\n\t" + "flh f2, 4(%[p2])\n\t" + "flh f3, 4(%[p3])\n\t" + "flh f4, 4(%[p4])\n\t" + "flh f5, 4(%[p5])\n\t" + "flh f6, 4(%[p6])\n\t" + "flh f7, 4(%[p7])\n\t" + ".endif\n\t" + "vfmacc.vf v0, f8, v18\n\t" + "vfmacc.vf v2, f9, v18\n\t" + "vfmacc.vf v4, f10, v18\n\t" + "vfmacc.vf v6, f11, v18\n\t" + "vfmacc.vf v8, f12, v18\n\t" + "vfmacc.vf v10, f13, v18\n\t" + "vfmacc.vf v12, f14, v18\n\t" + "vfmacc.vf v14, f15, v18\n\t" + "addi %[p0], %[p0], %[pstep]\n\t" + "addi %[p1], %[p1], %[pstep]\n\t" + "addi %[p2], %[p2], %[pstep]\n\t" + "addi %[p3], %[p3], %[pstep]\n\t" + "addi %[p4], %[p4], %[pstep]\n\t" + "addi %[p5], %[p5], %[pstep]\n\t" + "addi %[p6], %[p6], %[pstep]\n\t" + "addi %[p7], %[p7], %[pstep]\n\t" + "addi %[pairs], %[pairs], -1\n\t" + "bnez %[pairs], PV8_PIPE_LOOP%=\n\t" + "PV8_PIPE_TAIL%=:\n\t" + "vle16.v v18, (%[vp])\n\t" + ".if %[psize] == 4\n\t" + "flw f8, 4(%[p0])\n\t" + "fcvt.h.s f8, f8\n\t" + "flw f9, 4(%[p1])\n\t" + "fcvt.h.s f9, f9\n\t" + "flw f10, 4(%[p2])\n\t" + "fcvt.h.s f10, f10\n\t" + "flw f11, 4(%[p3])\n\t" + "fcvt.h.s f11, f11\n\t" + "flw f12, 4(%[p4])\n\t" + "fcvt.h.s f12, f12\n\t" + "flw f13, 4(%[p5])\n\t" + "fcvt.h.s f13, f13\n\t" + "flw f14, 4(%[p6])\n\t" + "fcvt.h.s f14, f14\n\t" + "flw f15, 4(%[p7])\n\t" + "fcvt.h.s f15, f15\n\t" + ".else\n\t" + "flh f8, 2(%[p0])\n\t" + "flh f9, 2(%[p1])\n\t" + "flh f10, 2(%[p2])\n\t" + "flh f11, 2(%[p3])\n\t" + "flh f12, 2(%[p4])\n\t" + "flh f13, 2(%[p5])\n\t" + "flh f14, 2(%[p6])\n\t" + "flh f15, 2(%[p7])\n\t" + ".endif\n\t" + "vfmacc.vf v0, f0, v16\n\t" + "vfmacc.vf v2, f1, v16\n\t" + "vfmacc.vf v4, f2, v16\n\t" + "vfmacc.vf v6, f3, v16\n\t" + "vfmacc.vf v8, f4, v16\n\t" + "vfmacc.vf v10, f5, v16\n\t" + "vfmacc.vf v12, f6, v16\n\t" + "vfmacc.vf v14, f7, v16\n\t" + "vfmacc.vf v0, f8, v18\n\t" + "vfmacc.vf v2, f9, v18\n\t" + "vfmacc.vf v4, f10, v18\n\t" + "vfmacc.vf v6, f11, v18\n\t" + "vfmacc.vf v8, f12, v18\n\t" + "vfmacc.vf v10, f13, v18\n\t" + "vfmacc.vf v12, f14, v18\n\t" + "vfmacc.vf v14, f15, v18\n\t" + "mv t0, %[out]\n\t" + "vse16.v v0, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vse16.v v2, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vse16.v v4, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vse16.v v6, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vse16.v v8, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vse16.v v10, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vse16.v v12, (t0)\n\t" + "add t0, t0, %[out_step]\n\t" + "vse16.v v14, (t0)\n\t" + : [p0] "+&r"(p0), [p1] "+&r"(p1), [p2] "+&r"(p2), [p3] "+&r"(p3), [p4] "+&r"(p4), [p5] "+&r"(p5), [p6] "+&r"(p6), [p7] "+&r"(p7), [vp] "+&r"(vp), [pairs] "+&r"(pairs) + : [out] "r"(out + d), [out_step] "r"(width * sizeof(_Float16)), + [vl] "r"(vl), [row_bytes] "r"(row_bytes), + [psize] "i"(sizeof(P)), [pstep] "i"(2 * sizeof(P)) + : "memory", "t0", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19"); + d += vl; + } +} + template static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** pq, const char * k_data_row, @@ -524,7 +893,8 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** size_t tcm_buffer_size, float ** partial = nullptr, int64_t n_mask_rows = 1, - size_t mask_row_stride = 0) { + size_t mask_row_stride = 0, + spacemit_fa_decode_kernel kernel = spacemit_fa_decode_kernel::generic) { // QLEN rows share K/V; groups of 4 rows are computed from the packed K/V tile in TCM. // Rows may come from n_mask_rows query tokens (QLEN / n_mask_rows consecutive rows per token), each with // its own mask row at mp + m * mask_row_stride bytes; a KV position is packed if any token can see it. @@ -576,6 +946,31 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** const uintptr_t k_tile_addr = kq_tile_addr + QLEN * ic_tile_step * sizeof(float); const uintptr_t v_tile_addr = k_tile_addr + DK * ic_tile_step * sizeof(_Float16); const uintptr_t mv_tile_addr = v_tile_addr + ic_tile_step * DV * sizeof(_Float16); + const uintptr_t q_m8_addr = align_up(mv_tile_addr + n_mask_rows * ic_tile_step * sizeof(float), 128); + bool decode_m8 = false; + char * k_stage = nullptr; + size_t k_stage_bytes = 0; +#if defined(RISCV64_SPACEMIT_IME2) + if constexpr (QLEN == 8) { + const uintptr_t scratch_end = reinterpret_cast(tcm_buffer) + tcm_buffer_size; + decode_m8 = kernel != spacemit_fa_decode_kernel::generic && ggml::cpu::riscv64_spacemit::global_spine_env_info.use_ime2 && + partial && n_mask_rows == 1 && DK == DV && (DK == 256 || DK == 512) && + ic_tile_step == 64 && q_m8_addr <= scratch_end && + 8 * DK * sizeof(_Float16) <= scratch_end - q_m8_addr; + if (decode_m8) { + ime2::fa_pack_f16_k8(reinterpret_cast<_Float16 *>(q_m8_addr), (const char *) qv_buffer, + DK * sizeof(_Float16), 8, DK, 8); + const uintptr_t stage_addr = align_up(q_m8_addr + 8 * DK * sizeof(_Float16), 128); + const size_t row_bytes = DK * sizeof(_Float16); + // Preserve the physical span, including interleaved KV heads. + if (stage_addr <= scratch_end && scratch_end - stage_addr >= row_bytes && + nbk1 >= row_bytes && nbk1 <= (scratch_end - stage_addr - row_bytes) / (ic_tile_step - 1)) { + k_stage = reinterpret_cast(stage_addr); + k_stage_bytes = (ic_tile_step - 1) * nbk1 + row_bytes; + } + } + } +#endif if (ic_tile_step >= 8) { float * kq_tile_buffer = reinterpret_cast(kq_tile_addr); @@ -587,6 +982,12 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** int64_t ic_step = 0; for (int64_t ic = 0; ic < nek1; ++ic) { + // No pending tile needs flushing when all preceding rows were masked. + if constexpr (QLEN == 8) { + if (partial && n_mask_rows == 1 && ic_step == 0 && mask_at(0, ic) == -INFINITY) { + continue; + } + } const char * direct_v = nullptr; bool packed_tile = false; if constexpr (QLEN == 4) { @@ -610,6 +1011,54 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** } } + // Pack full visible K tiles; masked and partial tiles retain the per-row path. + if constexpr (QLEN == 8) { + if (partial && n_mask_rows == 1 && + DK == DV && (DK == 256 || DK == 512) && ic_step == 0 && + ic + ic_tile_step <= nek1) { + bool all_visible = true; + for (int64_t tk = 0; tk < ic_tile_step; ++tk) { + mv_tile_pack[tk] = mask_at(0, ic + tk); + if (mv_tile_pack[tk] == -INFINITY) { + all_visible = false; + break; + } + } + if (all_visible) { +#if defined(RISCV64_SPACEMIT_IME2) + if (decode_m8) { + const char * tile_src = k_data_row + ic * nbk1; + if (k_stage) { + memcpy(k_stage, tile_src, k_stage_bytes); + tile_src = k_stage; + } + ime2::fa_pack_f16_k8(k_tile_pack, tile_src, nbk1, ic_tile_step, DK, 32); + } else +#endif + { + rvv_transposed_s16_mn_to_nm((int8_t *) k_tile_pack, k_tile_byte_stride, + (int8_t *) k_data_row + ic * nbk1, nbk1, ic_tile_step, DK); + } + if (kernel != spacemit_fa_decode_kernel::generic) { + // Full visible tiles already have consecutive KV + // positions; PV can read the original physical rows. + direct_v = v_data_row + ic * nbv1; + } else if (nbv1 == DV * (int64_t) sizeof(_Float16)) { + memcpy(v_tile_pack, v_data_row + ic * nbv1, + ic_tile_step * DV * sizeof(_Float16)); + } else { + for (int64_t tk = 0; tk < ic_tile_step; ++tk) { + memcpy(v_tile_pack + tk * DV, v_data_row + (ic + tk) * nbv1, + DV * sizeof(_Float16)); + } + } + ic += ic_tile_step - 1; + ic_step = ic_tile_step; + packed_tile = true; + } + } + } + float mvs[16]; bool visible = false; for (int64_t m = 0; !packed_tile && m < n_mask_rows; ++m) { @@ -641,7 +1090,31 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** if (ic_step > 0 && (ic_step == ic_tile_step || ic == (nek1 - 1))) { const size_t qk_vl = __riscv_vsetvl_e16m1(ic_step); - for (size_t g = 0; g < QLEN; g += 4) { + bool qk8 = false; +#if defined(RISCV64_SPACEMIT_IME2) + if (decode_m8 && packed_tile) { + for (int64_t tk = 0; tk < ic_step; tk += 32) { + spacemit_kernels::ime2::fa_qk_f16f16f32_m8n32( + DK, k_tile_pack + tk * DK, reinterpret_cast(q_m8_addr), + kq_tile_buffer + tk, ic_tile_step); + } + for (size_t row = 0; row < QLEN; ++row) { + rvv_scale_f32(kq_tile_buffer + row * ic_tile_step, scale, ic_step); + } + qk8 = true; + } +#endif + if constexpr (QLEN == 8) { + if (!qk8 && kernel != spacemit_fa_decode_kernel::generic && partial && n_mask_rows == 1 && + (DK == 256 || DK == 512) && ic_step <= 64) { + for (size_t g = 0; g < QLEN; g += 4) { + fa_decode_qk4_pipeline(kq_tile_buffer + g * ic_tile_step, qv_buffer + g * DK, + k_tile_pack, DK, ic_tile_step, ic_step, scale); + } + qk8 = true; + } + } + for (size_t g = 0; !qk8 && g < QLEN; g += 4) { if constexpr (QLEN >= 4) { const _Float16 * q0 = qv_buffer + (g + 0) * DK; const _Float16 * q1 = qv_buffer + (g + 1) * DK; @@ -709,7 +1182,25 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** S[i] += rvv_softmax_exp_inplace_f32(row_ptr, ic_step, M[i]); } + const char * pv_data = direct_v ? direct_v : reinterpret_cast(v_tile_pack); + const int64_t pv_row_bytes = direct_v ? nbv1 : DV * sizeof(_Float16); bool pv_m4 = false; + if constexpr (QLEN == 8) { + if (kernel != spacemit_fa_decode_kernel::generic && partial && n_mask_rows == 1 && DK == DV && + (DK == 256 || DK == 512) && ic_tile_step <= 64) { + // QK is complete and this K scratch is unused until the + // next tile. + auto * p16 = k_tile_pack; + fa_pack_decode_prob_f16(p16, kq_tile_buffer, ic_tile_step, ic_step); + if (kernel == spacemit_fa_decode_kernel::packed_pv8 && ic_tile_step == 64 && ic_step == 64) { + fa_decode_pv8_pipeline(kq16_buffer, p16, pv_data, pv_row_bytes, DV, ic_tile_step, ic_step); + } else { + fa_decode_pv4_prob(kq16_buffer, p16, pv_data, pv_row_bytes, DV, ic_tile_step, ic_step); + } + pv_m4 = true; + } + } + if constexpr (QLEN == 4) { if (partial && n_mask_rows == 1 && DK == 256 && DV == 256) { const size_t vl = __riscv_vsetvl_e16m4(DV); @@ -740,6 +1231,44 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** } } + if constexpr (QLEN == 8) { + if (!pv_m4 && partial && n_mask_rows == 1 && + DK == DV && (DK == 256 || DK == 512)) { + // Four m4 accumulators plus one m4 V row fit in registers. + // Each output element retains the original token/FMA order. + for (int64_t d0 = 0; d0 < DV;) { + const size_t vl = __riscv_vsetvl_e16m4(DV - d0); + for (size_t g = 0; g < QLEN; g += 4) { + _Float16 * o0 = kq16_buffer + (g + 0) * DV + d0; + _Float16 * o1 = kq16_buffer + (g + 1) * DV + d0; + _Float16 * o2 = kq16_buffer + (g + 2) * DV + d0; + _Float16 * o3 = kq16_buffer + (g + 3) * DV + d0; + const float * p0 = kq_tile_buffer + (g + 0) * ic_tile_step; + const float * p1 = kq_tile_buffer + (g + 1) * ic_tile_step; + const float * p2 = kq_tile_buffer + (g + 2) * ic_tile_step; + const float * p3 = kq_tile_buffer + (g + 3) * ic_tile_step; + vfloat16m4_t a0 = __riscv_vle16_v_f16m4(o0, vl); + vfloat16m4_t a1 = __riscv_vle16_v_f16m4(o1, vl); + vfloat16m4_t a2 = __riscv_vle16_v_f16m4(o2, vl); + vfloat16m4_t a3 = __riscv_vle16_v_f16m4(o3, vl); + for (int64_t tk = 0; tk < ic_step; ++tk) { + const vfloat16m4_t vv = __riscv_vle16_v_f16m4(reinterpret_cast(pv_data + tk * pv_row_bytes) + d0, vl); + a0 = __riscv_vfmacc_vf_f16m4(a0, (_Float16) p0[tk], vv, vl); + a1 = __riscv_vfmacc_vf_f16m4(a1, (_Float16) p1[tk], vv, vl); + a2 = __riscv_vfmacc_vf_f16m4(a2, (_Float16) p2[tk], vv, vl); + a3 = __riscv_vfmacc_vf_f16m4(a3, (_Float16) p3[tk], vv, vl); + } + __riscv_vse16_v_f16m4(o0, a0, vl); + __riscv_vse16_v_f16m4(o1, a1, vl); + __riscv_vse16_v_f16m4(o2, a2, vl); + __riscv_vse16_v_f16m4(o3, a3, vl); + } + d0 += vl; + } + pv_m4 = true; + } + } + for (int64_t d0 = 0; !pv_m4 && d0 < DV; d0 += seg) { const size_t vl = __riscv_vsetvl_e16m2(DV - d0); for (size_t g = 0; g < QLEN; g += 4) { @@ -759,7 +1288,7 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** vfloat16m2_t pv_acc3 = __riscv_vle16_v_f16m2(o3, vl); for (int64_t tk = 0; tk < ic_step; ++tk) { - const vfloat16m2_t v16 = __riscv_vle16_v_f16m2(v_tile_pack + tk * DV + d0, vl); + const vfloat16m2_t v16 = __riscv_vle16_v_f16m2(reinterpret_cast(pv_data + tk * pv_row_bytes) + d0, vl); pv_acc0 = __riscv_vfmacc_vf_f16m2(pv_acc0, (_Float16) p0[tk], v16, vl); pv_acc1 = __riscv_vfmacc_vf_f16m2(pv_acc1, (_Float16) p1[tk], v16, vl); pv_acc2 = __riscv_vfmacc_vf_f16m2(pv_acc2, (_Float16) p2[tk], v16, vl); @@ -775,7 +1304,7 @@ static void flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(float ** vfloat16m2_t pv_acc1 = __riscv_vle16_v_f16m2(kq16_buffer + 1 * DV + d0, vl); for (int64_t tk = 0; tk < ic_step; ++tk) { - const vfloat16m2_t v16 = __riscv_vle16_v_f16m2(v_tile_pack + tk * DV + d0, vl); + const vfloat16m2_t v16 = __riscv_vle16_v_f16m2(reinterpret_cast(pv_data + tk * pv_row_bytes) + d0, vl); pv_acc0 = __riscv_vfmacc_vf_f16m2( pv_acc0, (_Float16) kq_tile_buffer[0 * ic_tile_step + tk], v16, vl); pv_acc1 = __riscv_vfmacc_vf_f16m2( @@ -1254,6 +1783,7 @@ void forward_flash_attn_ext_f16_split_kv_partial_vlen1024_vf16(ggml::spacemit::c const int64_t nh = q->ne[2]; const int64_t nq = q->ne[1]; const int64_t rk2 = nh / n_kvh; + const auto kernel = spacemit_select_fa_decode(nh, n_kvh, DK, DV); float scale = ((const float *) dst->op_params)[0]; float logit_softcap = ((const float *) dst->op_params)[2]; @@ -1303,7 +1833,7 @@ void forward_flash_attn_ext_f16_split_kv_partial_vlen1024_vf16(ggml::spacemit::c flash_attn_ext_f16_one_chunk_inner_vlen1024_vf16_mrow(pq.data(), kd, vd, mp, unused.data(), unused.data(), \ scale, logit_softcap, 1.0f, c1 - c0, k->nb[1], \ v->nb[1], DV, DK, tcm_buffer, tcm_buffer_size, pr.data(), \ - n_mask_rows, mstride) + n_mask_rows, mstride, kernel) switch (prows) { case 2: FA_SPLIT_MROW(2); break; case 4: FA_SPLIT_MROW(4); break;