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
74 changes: 68 additions & 6 deletions ggml/src/ggml-spacemit/ime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ static gemm_path_f16 resolve_gemm_f16() {
o.reducesum = mlir::speir::spekernel::rvv::get_spe_reducesum_mxn_function(
march.value, typeid(_spe_fp16_), typeid(float), 16, 32, 1, 1);
o.quantize_a_gemv = &spacemit_kernels::rvv::quantize_a_i8_r1;
o.dot_gemv = &spacemit_kernels::ime2::gemm_kernel_i8i4_m1_q41;
if constexpr (Mode == q41_zero_point_mode::fixed_8) {
o.dot_gemv = &spacemit_kernels::ime2::gemm_kernel_i8i4_m1_q40;
} else {
o.dot_gemv = &spacemit_kernels::ime2::gemm_kernel_i8i4_m1_q41;
}
o.dot_f16acc = &spacemit_kernels::ime2::gemm_kernel_f16f16f16_m16_vfwmadot;
o.dot_f32acc = &spacemit_kernels::ime2::gemm_kernel_f16f16f32_m16_vfwmadot;
if (!o.castpack_a || !o.dequantpack_b || !o.reducesum) {
Expand Down Expand Up @@ -1827,14 +1831,27 @@ class tensor_traits_f16 : public ggml::spacemit::tensor_traits_base {
return slices * m_pad * n_pad * sizeof(_Float16) + metadata_size;
}

// MTP verification uses a fused four-row Q4_0 kernel without M16 padding or
// split-K F16 partials. Quantizing activations changes the M2-4 arithmetic,
// so restrict this path to the validated fixed-zero Q4_0 mode.
static bool small_m_fused_supported(const size_t M, const size_t K) {
return Mode == q41_zero_point_mode::fixed_8 && M >= 2 && M <= 4 && K % KB == 0;
}

bool work_size(int n_threads, const ggml_tensor * op, size_t & size) const override {
if (op->op != GGML_OP_MUL_MAT) {
return false;
}
const size_t N = static_cast<size_t>(op->src[0]->ne[1]);
const size_t M = static_cast<size_t>(op->src[1]->ne[1] * op->src[1]->ne[2] * op->src[1]->ne[3]);
const size_t K = static_cast<size_t>(op->src[1]->ne[0]);
const size_t gemv_size = ((K + KB - 1) / KB) * 38;
const size_t gemv_rows = small_m_fused_supported(M, K) && op->src[1]->ne[2] == 1 && op->src[1]->ne[3] == 1 ? M : 1;
size_t gemv_size = gemv_rows * ((K + KB - 1) / KB) * 38;
if (gemv_rows > 1) {
// Keep the interleaved four-row tile separate from the original
// quantized rows and align its FP32 scale fields.
gemv_size = GGML_PAD(gemv_size, 64) + 4 * (K / KB) * 38;
}
size = std::max(partial_workspace_size(M, N, n_threads), GGML_PAD(gemv_size, sizeof(int64_t)));
return true;
}
Expand All @@ -1860,10 +1877,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 All @@ -1889,6 +1902,55 @@ class tensor_traits_f16 : public ggml::spacemit::tensor_traits_base {
const auto * b_src = reinterpret_cast<const uint8_t *>(src0->data);
auto * out = reinterpret_cast<uint8_t *>(dst->data);

if (small_m_fused_supported(M, K) && src1->ne[2] == 1 && src1->ne[3] == 1) {
constexpr size_t a_blk_len = 32;
const size_t a_blk_stride = spacemit_kernels::q8_blk_size(a_blk_len, true);
const size_t k_blks = K / KB;
const size_t quant_row_stride = k_blks * a_blk_stride;
GGML_ASSERT(ctx.workspace_size >= M * quant_row_stride);
auto * quant_a = reinterpret_cast<uint8_t *>(ctx.workspace);

// Distribute all (row, K-block) pairs, but end each quantizer call at
// its source row boundary so non-contiguous row strides are honored.
const size_t tasks = M * k_blks;
const size_t tasks_per_thread = (tasks + static_cast<size_t>(nth) - 1) / nth;
const size_t task_end = std::min((static_cast<size_t>(ith) + 1) * tasks_per_thread, tasks);
for (size_t task = static_cast<size_t>(ith) * tasks_per_thread; task < task_end;) {
const size_t row = task / k_blks;
const size_t kb0 = task % k_blks;
const size_t count = std::min(task_end - task, k_blks - kb0);
ops.quantize_a_gemv(
a_blk_len, reinterpret_cast<const float *>(a_src + row * a_src_stride) + kb0 * a_blk_len,
count * a_blk_len, quant_a + row * quant_row_stride + kb0 * a_blk_stride);
task += count;
}
ctx.sync();

const size_t packed_offset = GGML_PAD(M * quant_row_stride, 64);
GGML_ASSERT(ctx.workspace_size >= packed_offset + 4 * quant_row_stride);
uint8_t * quant_a_m4 = quant_a + packed_offset;
const size_t kb0 = k_blks * static_cast<size_t>(ith) / nth;
const size_t kb1 = k_blks * static_cast<size_t>(ith + 1) / nth;
if (kb1 > kb0) {
spacemit_kernels::ime2::pack_q8_rows_m4(quant_a + kb0 * a_blk_stride, quant_row_stride, M,
kb1 - kb0, quant_a_m4 + kb0 * 4 * a_blk_stride);
}
ctx.sync();

const size_t b_row_stride = k_blks * ROW_PITCH;
constexpr size_t tile_cols = 32;
for (int64_t ni = static_cast<int64_t>(ith) * tile_cols; ni < static_cast<int64_t>(N);
ni += static_cast<int64_t>(tile_cols) * nth) {
const size_t nb_real = std::min(N - static_cast<size_t>(ni), tile_cols);
const uint8_t * b_row = b_src + ni * b_row_stride;
GGML_ASSERT(out_row_stride % sizeof(float) == 0);
spacemit_kernels::ime2::gemm_kernel_i8i4_m4_q40(
quant_a_m4, b_row, reinterpret_cast<float *>(out) + ni,
M, nb_real, k_blks, out_row_stride / sizeof(float));
}
return true;
}

// Decode quantizes A once; workers compute separate N32 tiles without TCM staging.
if (M == 1 && K % KB == 0) {
constexpr size_t a_blk_len = 32;
Expand Down
171 changes: 162 additions & 9 deletions ggml/src/ggml-spacemit/ime2_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1948,21 +1948,16 @@ void gemm_kernel_i8i4_m1(const uint8_t * quant_a_ptr,

// A stores (dA, -sum(qA), qA); B has a uint8 zero point per row and K32 block.
// Compute dot(qA,qB) - zpB*sum(qA) in INT32 before applying FP32 scales.
void gemm_kernel_i8i4_m1_q41(const uint8_t * quant_a_ptr,
template <bool fixed_8>
static void gemm_kernel_i8i4_m1_q4(const uint8_t * quant_a_ptr,
const uint8_t * quant_b_data,
float * c_ptr,
size_t count_n,
size_t k_blks) {
using block_type = ggml::cpu::riscv64_spacemit::Q41Block<32, 32, 2>;
constexpr size_t b_data_bytes = 32 * 32 / 2 * sizeof(uint8_t);
constexpr size_t b_zp_bytes = 32 * sizeof(uint8_t);
constexpr size_t b_scale_bytes = 32 * sizeof(_Float16);
constexpr size_t a_sum_off = sizeof(float);
constexpr size_t a_data_off = a_sum_off + sizeof(int16_t);
constexpr size_t a_block_bytes = a_data_off + 32 * sizeof(int8_t);
static_assert(sizeof(block_type) == b_data_bytes + b_zp_bytes + b_scale_bytes &&
offsetof(block_type, zp) == b_data_bytes &&
offsetof(block_type, scale) == b_data_bytes + b_zp_bytes);
for (size_t n = 0; n < count_n; n += 32) {
const size_t nblks = std::min<size_t>(count_n - n, 32);
const uint8_t * b = quant_b_data + (n / 32) * k_blks * sizeof(block_type);
Expand Down Expand Up @@ -2013,9 +2008,13 @@ void gemm_kernel_i8i4_m1_q41(const uint8_t * quant_a_ptr,
"vpack.vv v26, v20, v22, 2\n\t"
"vpack.vv v16, v24, v26, 3\n\t"
"vsetvli t0, x0, e16, mf2\n\t"
"addi s4, s5, %[b_zp_off]\n\t"
".if %[fixed_8]\n\t"
"vmv.v.i v26, 8\n\t"
".else\n\t"
"addi s4, s5, 512\n\t"
"vle8.v v28, (s4)\n\t"
"vzext.vf2 v26, v28\n\t"
".endif\n\t"
"vwmacc.vx v16, t2, v26\n\t"
"addi s4, s5, %[b_scale_off]\n\t"
"vle16.v v30, (s4)\n\t"
Expand All @@ -2031,14 +2030,168 @@ void gemm_kernel_i8i4_m1_q41(const uint8_t * quant_a_ptr,
"vse32.v v2, (s6)\n\t"
:
: [BCK] "r"(k_blks), [NBLKS] "r"(nblks), [pA] "r"(quant_a_ptr), [pB] "r"(b), [pC] "r"(c),
[block_bytes] "i"(sizeof(block_type)), [b_zp_off] "i"(offsetof(block_type, zp)),
[block_bytes] "i"(sizeof(block_type)), [fixed_8] "i"(fixed_8),
[b_scale_off] "i"(offsetof(block_type, scale)), [a_sum_off] "i"(a_sum_off),
[a_data_off] "i"(a_data_off), [a_block_bytes] "i"(a_block_bytes)
: "cc", "memory", "t0", "t2", "t3", "t4", "f0", "s2", "s3", "s4", "s5", "s6");
// clang-format on
}
}

void gemm_kernel_i8i4_m1_q41(const uint8_t * a, const uint8_t * b, float * c, size_t n, size_t k) {
gemm_kernel_i8i4_m1_q4<false>(a, b, c, n, k);
}

void gemm_kernel_i8i4_m1_q40(const uint8_t * a, const uint8_t * b, float * c, size_t n, size_t k) {
gemm_kernel_i8i4_m1_q4<true>(a, b, c, n, k);
}

// Interleave existing GEMV blocks rather than quantizing a second time. Each
// K32 block is [4 x f32 scale][4 x i16 negative sum][4 x 32 i8].
void pack_q8_rows_m4(const uint8_t * quant_a_rows,
size_t quant_row_stride,
size_t count_m,
size_t k_blks,
uint8_t * quant_a_m4) {
GGML_ASSERT(count_m >= 1 && count_m <= 4);
constexpr size_t row_block_bytes = sizeof(float) + sizeof(int16_t) + 32;
constexpr size_t tile_block_bytes = 4 * row_block_bytes;
for (size_t kb = 0; kb < k_blks; ++kb) {
auto * dst = quant_a_m4 + kb * tile_block_bytes;
// Zero scales, sums and values in all padded rows.
memset(dst, 0, tile_block_bytes);
for (size_t row = 0; row < count_m; ++row) {
const auto * src = quant_a_rows + row * quant_row_stride + kb * row_block_bytes;
memcpy(dst + row * sizeof(float), src, sizeof(float));
memcpy(dst + 4 * sizeof(float) + row * sizeof(int16_t), src + sizeof(float), sizeof(int16_t));
memcpy(dst + 4 * (sizeof(float) + sizeof(int16_t)) + row * 32,
src + sizeof(float) + sizeof(int16_t), 32);
}
}
}

// Four A rows share each B load and IME2 dot. Apply the fixed Q4_0 zero point
// to the INT32 dot before converting to FP32 and applying the scales.
void gemm_kernel_i8i4_m4_q40(const uint8_t * quant_a_ptr,
const uint8_t * quant_b_data,
float * c_ptr,
size_t count_m,
size_t count_n,
size_t k_blks,
size_t ldc) {
using block_type = ggml::cpu::riscv64_spacemit::Q41Block<32, 32, 2>;
GGML_ASSERT(count_m >= 1 && count_m <= 4);
GGML_ASSERT(k_blks > 0);
for (size_t ni = 0; ni < count_n; ni += 32) {
const auto * b = quant_b_data + (ni / 32) * k_blks * sizeof(block_type);
auto * c = c_ptr + ni;
const size_t nb_real = std::min<size_t>(32, count_n - ni);
// The payload nibble order is identical to the legacy integer M4 B
// layout: adjacent K values, with each group of eight N rows contiguous.
// clang-format off
__asm__ volatile(
"mv s1, %[BK]\n\t"
"mv s2, %[A]\n\t"
"mv s5, %[B]\n\t"
"vsetvli t0, x0, e32, m1\n\t"
"vxor.vv v28, v28, v28\n\t"
"vxor.vv v29, v29, v29\n\t"
"vxor.vv v30, v30, v30\n\t"
"vxor.vv v31, v31, v31\n\t"
".align 4\n\t"
"Q40_M4_K%=:\n\t"
"flw fa0, 0(s2)\n\t"
"flw fa1, 4(s2)\n\t"
"flw fa2, 8(s2)\n\t"
"flw fa3, 12(s2)\n\t"
"lh t1, 16(s2)\n\t"
"lh t2, 18(s2)\n\t"
"lh t3, 20(s2)\n\t"
"lh t4, 22(s2)\n\t"
"addi s3, s2, 24\n\t"
"addi s2, s2, 152\n\t"
"vsetvli t0, x0, e8, m1\n\t"
"vl1r.v v0, (s3)\n\t"
"vl4r.v v4, (s5)\n\t"
"vsrl.vi v1, v0, 4\n\t"
"vnpack4.vv v12, v0, v1, 3\n\t"
"vupack.vv v2, v12, v12, 2\n\t"
"vsetvli t0, x0, e32, m1\n\t"
"vxor.vv v16, v16, v16\n\t"
"vxor.vv v18, v18, v18\n\t"
"vxor.vv v20, v20, v20\n\t"
"vxor.vv v22, v22, v22\n\t"
"vmadotsu v16, v3, v4, i4\n\t"
"vmadotsu v18, v3, v5, i4\n\t"
"vmadotsu v20, v3, v6, i4\n\t"
"vmadotsu v22, v3, v7, i4\n\t"
"vsll.vi v16, v16, 4\n\t"
"vsll.vi v18, v18, 4\n\t"
"vsll.vi v20, v20, 4\n\t"
"vsll.vi v22, v22, 4\n\t"
"vmadotu v16, v2, v4, i4\n\t"
"vmadotu v18, v2, v5, i4\n\t"
"vmadotu v20, v2, v6, i4\n\t"
"vmadotu v22, v2, v7, i4\n\t"
"vpack.vv v0, v16, v18, 2\n\t"
"vpack.vv v2, v20, v22, 2\n\t"
"vpack.vv v16, v0, v2, 3\n\t"
"vpack.vv v18, v1, v3, 3\n\t"
"vsetvli t0, x0, e16, mf2\n\t"
"vmv.v.i v26, 8\n\t"
"vwmacc.vx v16, t1, v26\n\t"
"vwmacc.vx v17, t2, v26\n\t"
"vwmacc.vx v18, t3, v26\n\t"
"vwmacc.vx v19, t4, v26\n\t"
"addi s4, s5, %[scale_off]\n\t"
"vle16.v v12, (s4)\n\t"
"vfwcvt.f.f.v v14, v12\n\t"
"vsetvli t0, x0, e32, m1\n\t"
"vfcvt.f.x.v v16, v16\n\t"
"vfcvt.f.x.v v17, v17\n\t"
"vfcvt.f.x.v v18, v18\n\t"
"vfcvt.f.x.v v19, v19\n\t"
"vfmul.vv v16, v16, v14\n\t"
"vfmul.vv v17, v17, v14\n\t"
"vfmul.vv v18, v18, v14\n\t"
"vfmul.vv v19, v19, v14\n\t"
"vfmacc.vf v28, fa0, v16\n\t"
"vfmacc.vf v29, fa1, v17\n\t"
"vfmacc.vf v30, fa2, v18\n\t"
"vfmacc.vf v31, fa3, v19\n\t"
"addi s5, s5, %[block_bytes]\n\t"
"addi s1, s1, -1\n\t"
"bgtz s1, Q40_M4_K%=\n\t"
"mv s6, %[C]\n\t"
"mv t5, %[M]\n\t"
"vsetvli t0, %[N], e32, m1\n\t"
"vse32.v v28, (s6)\n\t"
"addi t5, t5, -1\n\t"
"beqz t5, Q40_M4_END%=\n\t"
"add s6, s6, %[LDC]\n\t"
"vse32.v v29, (s6)\n\t"
"addi t5, t5, -1\n\t"
"beqz t5, Q40_M4_END%=\n\t"
"add s6, s6, %[LDC]\n\t"
"vse32.v v30, (s6)\n\t"
"addi t5, t5, -1\n\t"
"beqz t5, Q40_M4_END%=\n\t"
"add s6, s6, %[LDC]\n\t"
"vse32.v v31, (s6)\n\t"
"Q40_M4_END%=:\n\t"
:
: [A] "r"(quant_a_ptr), [B] "r"(b), [C] "r"(c), [BK] "r"(k_blks),
[M] "r"(count_m), [N] "r"(nb_real), [LDC] "r"(ldc * sizeof(float)),
[scale_off] "i"(offsetof(block_type, scale)), [block_bytes] "i"(sizeof(block_type))
: "cc", "memory", "t0", "t1", "t2", "t3", "t4", "t5", "s1", "s2", "s3", "s4", "s5", "s6",
"fa0", "fa1", "fa2", "fa3",
"v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11",
"v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
"v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31");
// clang-format on
}
}

void gemm_kernel_i8i4_m4(const uint8_t * quant_a_ptr,
const uint8_t * quant_b_data,
const uint8_t * quant_b_zp,
Expand Down
28 changes: 28 additions & 0 deletions ggml/src/ggml-spacemit/ime_kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,34 @@ void gemm_kernel_i8i4_m1_q41(const uint8_t * quant_a_ptr,
size_t count_n,
size_t k_blks);

// Interleave GEMV A rows: input K32 block = [f32 scale][i16 -sum][32 i8].
// Output = k_blks * 152 bytes, each [4 f32 scales][4 i16 -sum][4x32 i8].
// count_m is 1..4; missing rows are zero padded. Source/destination cannot overlap.
// quant_row_stride is in bytes. The function may be called on a K-block subrange.
void pack_q8_rows_m4(const uint8_t * quant_a_rows,
size_t quant_row_stride,
size_t count_m,
size_t k_blks,
uint8_t * quant_a_m4);

// Q4_0 INT32 GEMM for 1..4 A rows packed by pack_q8_rows_m4.
// B = [N32 tiles][K32 blocks] of Q41Block<32,32,2> with fixed zp=8; K must be nonzero.
// All four padded A rows are read, only count_m rows are stored. ldc is in F32
// elements; count_n may have an N32 tail, with B physically padded to N32.
void gemm_kernel_i8i4_m4_q40(const uint8_t * quant_a_ptr,
const uint8_t * quant_b_data,
float * c_ptr,
size_t count_m,
size_t count_n,
size_t k_blks,
size_t ldc);

void gemm_kernel_i8i4_m1_q40(const uint8_t * quant_a_ptr,
const uint8_t * quant_b_data,
float * c_ptr,
size_t count_n,
size_t k_blks);

// A/B layouts: [K/8][M16|N32][K8]; gemm_k is the K extent.
void gemm_kernel_f16f16f16_m16_vfwmadot(size_t gemm_k,
const _Float16 * b_f16_k8,
Expand Down
Loading