Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 28 additions & 33 deletions ggml/src/ggml-spacemit/ime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,12 @@ template <typename BLOC_TYPE, int64_t INTER_SIZE, int64_t NB_COLS> class tensor_
}
};

#if defined(RISCV64_SPACEMIT_IME2)
static bool flash_attn_ext_f16_tiled_use_ime2(int64_t dk, int64_t dv) {
return global_spine_env_info.use_ime2 && dk % 8 == 0 && dv % 32 == 0;
}
#endif

class tensor_traits_common : public ggml::spacemit::tensor_traits_base {
bool work_size(int n_threads, const ggml_tensor * op, size_t & size) const override {
switch (op->op) {
Expand All @@ -1302,11 +1308,12 @@ class tensor_traits_common : public ggml::spacemit::tensor_traits_base {

// Tiled flash attention scratch (tile sizes defined in common.h)
// Per-thread: Q_q + KQ + mask + VKQ32 + V32 + K_f32 + padding
size_t prefill = sizeof(float) *
(GGML_FA_TILE_Q * DK + 2 * GGML_FA_TILE_Q * GGML_FA_TILE_KV + GGML_FA_TILE_Q * DV +
GGML_FA_TILE_KV * DV + GGML_FA_TILE_KV * DK +
ggml::spacemit::cache_line_size_f32) *
n_tasks;
size_t prefill = spacemit_kernels::rvv::flash_attn_ext_f16_tiled_workspace_size(op, n_tasks);
#if defined(RISCV64_SPACEMIT_IME2)
if (flash_attn_ext_f16_tiled_use_ime2(DK, DV)) {
prefill = spacemit_kernels::ime2::flash_attn_ext_f16_tiled_workspace_size(op, n_tasks);
}
#endif

// Decode path: n_kv_chunks = n_tasks (one chunk per thread)
// Per-thread: VKQ accmulator (DV), partial M, partial S + intra-thread scratch for V, Q and VKQ
Expand Down Expand Up @@ -1702,8 +1709,10 @@ class tensor_traits_common : public ggml::spacemit::tensor_traits_base {

const bool supported_prec = (dst->op_params[3] == GGML_PREC_F32 || dst->op_params[3] == GGML_PREC_UNDEFINED);
const bool supported_types = (q->type == GGML_TYPE_F32 && k->type == GGML_TYPE_F16 && v->type == GGML_TYPE_F16);
const bool supported_shape = (DK > 0 && DK <= 256 && DV > 0 && DV <= 256);
const bool supported_vlen = (__riscv_vlenb() == 128);
const bool supported_shape = (DK > 0 && DK <= 512 && DV > 0 && DV <= 512);
const size_t vlenb = __riscv_vlenb();
const bool use_tiled = neq1 >= ggml_fa_tile_config::Q;
const bool supported_vlen = vlenb == 128 || (vlenb == 32 && use_tiled);

if (!(supported_prec && supported_types && supported_shape && supported_vlen)) {
return false;
Expand All @@ -1716,9 +1725,6 @@ class tensor_traits_common : public ggml::spacemit::tensor_traits_base {
const int ith = ctx.ith;
const int nth = ctx.nth;

static constexpr int64_t Q_TILE_SZ = ggml_fa_tile_config::Q;
const bool use_tiled = neq1 >= Q_TILE_SZ;

// 4x chunks per thread
// int nth_scaled = nth * 4;
// int64_t chunk_size = (nr + nth_scaled - 1) / nth_scaled;
Expand All @@ -1742,27 +1748,20 @@ class tensor_traits_common : public ggml::spacemit::tensor_traits_base {
return true;
}

int64_t nchunk = nth;

// The number of elements in each chunk
const int64_t dr = (nr + nchunk - 1) / nchunk;

// The first chunk comes from our thread_id, the rest will get auto-assigned.
int current_chunk = ith;
const int64_t dr = (nr + nth - 1) / nth;
const int64_t ir0 = dr * ith;
const int64_t ir1 = std::min(ir0 + dr, nr);

while (current_chunk < nchunk) {
const int64_t ir0 = dr * current_chunk;
const int64_t ir1 = MIN(ir0 + dr, nr);

if (use_tiled) {
spacemit_kernels::rvv::forward_flash_attn_ext_f16_tiled_vlen1024_vf16(
ctx, dst, ir0, ir1);
} else {
spacemit_kernels::rvv::forward_flash_attn_ext_f16_one_chunk_vlen1024_vf16(
ctx, dst, ir0, ir1);
if (use_tiled) {
#if defined(RISCV64_SPACEMIT_IME2)
if (vlenb == 128 && flash_attn_ext_f16_tiled_use_ime2(DK, DV)) {
spacemit_kernels::ime2::forward_flash_attn_ext_f16_tiled_ime2(ctx, dst, ir0, ir1);
return true;
}

current_chunk += nth;
#endif
spacemit_kernels::rvv::forward_flash_attn_ext_f16_tiled_rvv(ctx, dst, ir0, ir1);
} else {
spacemit_kernels::rvv::forward_flash_attn_ext_f16_one_chunk_vlen1024_vf16(ctx, dst, ir0, ir1);
}

return true;
Expand Down Expand Up @@ -1860,10 +1859,6 @@ class tensor_traits_f16 : public ggml::spacemit::tensor_traits_base {
const int ith = ctx.ith;
const int nth = ctx.nth;

if (ith == 0 && getenv("GGML_SPACEMIT_Q41_TRACE") != nullptr) {
fprintf(stderr, "q41_mul_mat: N=%zu M=%zu K=%zu (%s)\n", N, M, K, M == 1 ? "decode" : "prefill");
}

const size_t out_row_stride = dst->nb[1]; // bytes

constexpr size_t m_tile = F16_M_TILE;
Expand Down
Loading
Loading