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
3 changes: 3 additions & 0 deletions ggml/src/ggml-spacemit/ggml-spacemit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,9 @@ static void * ggml_backend_spacemit_buffer_get_base(ggml_backend_buffer_t buffer

static enum ggml_status ggml_backend_spacemit_buffer_init_tensor(ggml_backend_buffer_t buffer,
ggml_tensor * tensor) {
if (!ggml_spacemit_q6_compact_view_supported(tensor)) {
GGML_ABORT("SPACEMIT: unsupported view into compact Q6_K tensor %s", tensor->name);
}
tensor->extra =
(void *) const_cast<ggml::spacemit::tensor_traits_base *>(ggml_spacemit_get_optimal_repack_type(tensor));

Expand Down
178 changes: 178 additions & 0 deletions ggml/src/ggml-spacemit/ime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,128 @@ class tensor_traits_f16 : public ggml::spacemit::tensor_traits_base {
}
};

#if defined(RISCV64_SPACEMIT_IME2)
class tensor_traits_q6_compact : public ggml::spacemit::tensor_traits_base {
static constexpr int64_t k = 2048;
static constexpr int64_t n = 248320;
static constexpr size_t row_bytes = (k / 32) * 38;
static constexpr size_t tile_bytes = (k / QK_K) * q6_compact_layout::tile_bytes;

bool work_size(int, const ggml_tensor * op, size_t & size) const override {
if (op->op != GGML_OP_MUL_MAT || !op->src[1] || op->src[1]->ne[0] != k ||
op->src[1]->type != GGML_TYPE_F32 || op->type != GGML_TYPE_F32) {
return false;
}
size = ggml_nrows(op->src[1]) * row_bytes;
return true;
}

static size_t row_offset(const ggml_tensor * t, size_t row) {
const size_t i1 = row % t->ne[1];
row /= t->ne[1];
return i1 * t->nb[1] + row % t->ne[2] * t->nb[2] + row / t->ne[2] * t->nb[3];
}

bool compute_forward(ggml::spacemit::context & ctx, ggml_tensor * out) const override {
size_t workspace_size;
if (!work_size(ctx.nth, out, workspace_size)) {
return false;
}
GGML_ASSERT(ctx.workspace_size >= workspace_size && out->nb[0] == sizeof(float));
const auto * input = out->src[1];
const auto * source = static_cast<const uint8_t *>(input->data);
const auto * weights = static_cast<const uint8_t *>(out->src[0]->data);
const size_t rows = ggml_nrows(input);
auto * a = static_cast<uint8_t *>(ctx.workspace);
const size_t blocks = k / 32;
const size_t begin = rows * blocks * ctx.ith / ctx.nth;
const size_t end = rows * blocks * (ctx.ith + 1) / ctx.nth;
for (size_t i = begin; i < end;) {
const size_t row = i / blocks;
const size_t block = i % blocks;
const size_t count = std::min(blocks - block, end - i);
const auto * src = source + row_offset(input, row) + block * 32 * input->nb[0];
if (input->nb[0] == sizeof(float)) {
spacemit_kernels::rvv::quantize_a_i8_r1(32, reinterpret_cast<const float *>(src), count * 32, a + i * 38);
} else {
for (size_t b = 0; b < count; ++b) {
float values[32];
for (size_t d = 0; d < 32; ++d) {
memcpy(values + d, src + (b * 32 + d) * input->nb[0], sizeof(float));
}
spacemit_kernels::rvv::quantize_a_i8_r1(32, values, 32, a + (i + b) * 38);
}
}
i += count;
}
ctx.sync();
auto * stage = static_cast<uint8_t *>(ctx.shared.data);
size_t stage_size = ctx.shared.size;
static_assert(row_bytes % 128 == 0 && tile_bytes % 128 == 0);
if (stage) {
const size_t padding = (128 - (reinterpret_cast<uintptr_t>(stage) % 128)) % 128;
if (padding <= stage_size) {
stage += padding;
stage_size -= padding;
} else {
stage_size = 0;
}
}
const bool staged = stage && tile_bytes <= stage_size;
if (staged && workspace_size <= stage_size - tile_bytes) {
spacemit_kernels::rvv::memcpy1d(stage, a, workspace_size);
a = stage;
stage += workspace_size;
}
const size_t tiles = n / 32;
const size_t lane = ctx.ith % 2;
const bool paired = lane != 0 || ctx.ith + 1 < ctx.nth;
GGML_ASSERT(!paired || (global_spine_env_info.init_barrier && ctx.ith / 2 < spine_init_barrier_count));
auto * barrier = paired ? &global_spine_env_info.init_barrier[ctx.ith / 2] : nullptr;
auto copy = [&](size_t tile) {
if (staged && tile < tiles) {
spacemit_kernels::rvv::memcpy1d(stage, weights + tile * tile_bytes, tile_bytes);
}
};
if (!lane) {
copy(ctx.ith);
}
if (paired) {
spine_barrier_wait(barrier);
}
if (lane) {
copy(ctx.ith);
}
for (size_t base = ctx.ith - lane; base < tiles; base += ctx.nth) {
const size_t tile = base + lane;
if (paired && lane) {
spine_barrier_wait(barrier);
}
if (tile < tiles) {
const uint8_t * b = staged ? stage : weights + tile * tile_bytes;
for (size_t row = 0; row < rows; ++row) {
auto * dst = reinterpret_cast<float *>(static_cast<uint8_t *>(out->data) + row_offset(out, row));
spacemit_kernels::ime2::q6_compact_dot_tile(a + row * row_bytes, b, dst + tile * 32, k / QK_K);
}
}
if (paired && !lane) {
spine_barrier_wait(barrier);
}
copy(tile + ctx.nth);
}
return true;
}

int repack(ggml_tensor * t, const void * data, size_t size) const override {
return repack_q6_head_compact(t, data, size);
}

void unpack(const ggml_tensor * t, void * data, size_t size) const override {
unpack_q6_head_compact(t, data, size);
}
};
#endif

// Q5_0 weights converted exactly to Q8_0 at load time and run on the Q8_0 IME path
class tensor_traits_q5_0_as_q8_0 : public tensor_traits<block_q8_0, 32, 32> {
int repack(ggml_tensor * t, const void * data, size_t data_size) const override {
Expand Down Expand Up @@ -2572,7 +2694,52 @@ static const tensor_traits_common rvv_impl;

} // namespace ggml::cpu::riscv64_spacemit

#if defined(RISCV64_SPACEMIT_IME2)
static const ggml_tensor * q6_compact_view_root(const ggml_tensor * tensor) {
const ggml_tensor * root = tensor->view_src;
if (root == nullptr) {
return nullptr;
}
while (root->view_src != nullptr) {
root = root->view_src;
}
return root->extra == &ggml::cpu::riscv64_spacemit::traits_instance<ggml::cpu::riscv64_spacemit::tensor_traits_q6_compact>()
? root : nullptr;
}
#endif

bool ggml_spacemit_q6_compact_view_supported(const ggml_tensor * tensor) {
#if defined(RISCV64_SPACEMIT_IME2)
const ggml_tensor * root = q6_compact_view_root(tensor);
if (root != nullptr) {
if (tensor->type != root->type || !ggml_are_same_shape(tensor, root)) {
return false;
}
for (int i = 0; i < GGML_MAX_DIMS; ++i) {
if (tensor->nb[i] != root->nb[i]) {
return false;
}
}
for (const ggml_tensor * view = tensor; view->view_src != nullptr; view = view->view_src) {
if (view->view_offs != 0) {
return false;
}
}
}
#else
GGML_UNUSED(tensor);
#endif
return true;
}

const ggml::spacemit::tensor_traits_base * ggml_spacemit_get_optimal_repack_type(const ggml_tensor * cur) {
#if defined(RISCV64_SPACEMIT_IME2)
const ggml_tensor * root = q6_compact_view_root(cur);
if (root != nullptr) {
return ggml_spacemit_q6_compact_view_supported(cur)
? static_cast<const ggml::spacemit::tensor_traits_base *>(root->extra) : nullptr;
}
#endif
using ggml::cpu::riscv64_spacemit::f16_traits;
using ggml::cpu::riscv64_spacemit::i8_traits;
using ggml::cpu::riscv64_spacemit::q41_zero_point_mode;
Expand Down Expand Up @@ -2673,6 +2840,10 @@ const ggml::spacemit::tensor_traits_base * ggml_spacemit_get_optimal_repack_type
{
#if defined(RISCV64_SPACEMIT_IME2)
if ((ggml::cpu::riscv64_spacemit::global_spine_env_info.use_ime2)) {
if (!cur->view_src && cur->op == GGML_OP_NONE && ggml_is_contiguous(cur) &&
cur->ne[0] == 2048 && cur->ne[1] == 248320 && cur->ne[2] == 1 && cur->ne[3] == 1) {
return &traits_instance<ggml::cpu::riscv64_spacemit::tensor_traits_q6_compact>();
}
return &i8_traits<block_q6_K, 32, 32>();
}
#endif
Expand Down Expand Up @@ -2794,6 +2965,13 @@ size_t ggml_spacemit_nbytes(ggml_backend_buffer_type_t buft, const ggml_tensor *
}
break;
case GGML_TYPE_Q6_K:
#if defined(RISCV64_SPACEMIT_IME2)
if (ggml_spacemit_get_optimal_repack_type(tensor) ==
&ggml::cpu::riscv64_spacemit::traits_instance<ggml::cpu::riscv64_spacemit::tensor_traits_q6_compact>()) {
return (ggml_nbytes(tensor) / sizeof(block_q6_K)) *
(ggml::cpu::riscv64_spacemit::q6_compact_layout::tile_bytes / 32);
}
#endif
nbytes = remap_block_nbytes(sizeof(block_q6_K), sizeof(block_q8_0) * 8, 32);
break;
case GGML_TYPE_Q8_0:
Expand Down
1 change: 1 addition & 0 deletions ggml/src/ggml-spacemit/ime.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "spacemit-context.h"

const ggml::spacemit::tensor_traits_base * ggml_spacemit_get_optimal_repack_type(const ggml_tensor * cur);
bool ggml_spacemit_q6_compact_view_supported(const ggml_tensor * tensor);
const ggml::spacemit::tensor_traits_base * ggml_spacemit_get_tensor_traits(const ggml_tensor * op);
bool ggml_spacemit_get_work_size(int n_threads, const ggml_tensor * op, size_t * size);
bool ggml_spacemit_compute_forward(ggml::spacemit::context & ctx, ggml_tensor * op);
Expand Down
79 changes: 79 additions & 0 deletions ggml/src/ggml-spacemit/ime2_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4125,6 +4125,85 @@ static void gemm_kernel_i8i8_m4(const uint8_t * quant_a_ptr,
}
}

#if defined(RISCV64_SPACEMIT_IME2)
#define Q6_DOT_HALF(A) \
"vl1r.v v12, (t3)\n\t" \
"addi t3, t3, 128\n\t" \
"vl1r.v v13, (t3)\n\t" \
"addi t3, t3, 128\n\t" \
"vl1r.v v14, (t3)\n\t" \
"addi t3, t3, 128\n\t" \
"vsetvli t0, zero, e8, m1, ta, ma\n\t" \
"vsra.vi v8, v12, 2\n\t" \
"vsra.vi v9, v13, 2\n\t" \
"vsra.vi v10, v14, 2\n\t" \
"vsll.vi v11, v14, 6\n\t" \
"vand.vi v15, v13, 3\n\t" \
"vsll.vi v15, v15, 4\n\t" \
"vor.vv v11, v11, v15\n\t" \
"vand.vi v15, v12, 3\n\t" \
"vsll.vi v15, v15, 2\n\t" \
"vor.vv v11, v11, v15\n\t" \
"vsra.vi v11, v11, 2\n\t" \
"vsetvli t0, zero, e8, mf4, ta, ma\n\t" \
"vle8.v v5, (%[b])\n\t" \
"addi %[b], %[b], 32\n\t" \
"vsetvli t0, zero, e32, m1, ta, ma\n\t" \
"vxor.vv v16, v16, v16\n\t" \
"vxor.vv v18, v16, v16\n\t" \
"vxor.vv v20, v16, v16\n\t" \
"vxor.vv v22, v16, v16\n\t" \
"vmadot v16, " A ", v8, i8\n\t" \
"vmadot v18, " A ", v9, i8\n\t" \
"vmadot v20, " A ", v10, i8\n\t" \
"vmadot v22, " A ", v11, i8\n\t" \
"vpack.vv v24, v16, v18, 2\n\t" \
"vpack.vv v26, v20, v22, 2\n\t" \
"vpack.vv v16, v24, v26, 3\n\t" \
"vsext.vf4 v6, v5\n\t"

void q6_compact_dot_tile(const uint8_t * quant_a, const uint8_t * packed, float * output, size_t k256_blocks) {
assert(k256_blocks > 0);
asm volatile(
"vsetvli t0, zero, e32, m1, ta, ma\n\t"
"vmv.v.i v2, 0\n\t"
"1:\n\t"
"vsetvli t0, zero, e16, mf2, ta, ma\n\t"
"vle16.v v28, (%[b])\n\t"
"vfwcvt.f.f.v v30, v28\n\t"
"addi %[b], %[b], 64\n\t"
"addi t3, %[b], 576\n\t"
"li t2, 8\n\t"
"2:\n\t"
"flw ft0, (%[a])\n\t"
"addi t1, %[a], 6\n\t"
"vsetvli t0, zero, e8, mf4, ta, ma\n\t"
"vle8.v v3, (t1)\n\t"
"addi %[a], %[a], 38\n\t"
"vsetvli t0, zero, e32, m1, ta, ma\n\t"
"vslidedown.vi v4, v3, 4\n\t"
Q6_DOT_HALF("v3")
"vmul.vv v7, v16, v6\n\t"
Q6_DOT_HALF("v4")
"vmacc.vv v7, v16, v6\n\t"
"vfcvt.f.x.v v7, v7\n\t"
"vfmul.vf v1, v30, ft0\n\t"
"vfmacc.vv v2, v1, v7\n\t"
"addi t2, t2, -1\n\t"
"bnez t2, 2b\n\t"
"mv %[b], t3\n\t"
"addi %[n], %[n], -1\n\t"
"bnez %[n], 1b\n\t"
"vse32.v v2, (%[out])\n\t"
: [a] "+&r"(quant_a), [b] "+&r"(packed), [n] "+&r"(k256_blocks)
: [out] "r"(output)
: "memory", "t0", "t1", "t2", "t3", "ft0", "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", "v30");
}
#undef Q6_DOT_HALF
#endif

static void gemm_kernel_i8i8_m1(const uint8_t * quant_a_ptr,
const uint8_t * quant_b_data,
float * c_ptr,
Expand Down
4 changes: 4 additions & 0 deletions ggml/src/ggml-spacemit/ime_kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ size_t gemm_i8i4_k256(a_pack ap,
size_t k_blks,
size_t ldc);

#if defined(RISCV64_SPACEMIT_IME2)
void q6_compact_dot_tile(const uint8_t * quant_a, const uint8_t * packed, float * output, size_t k256_blocks);
#endif

size_t gemm_i8i8_k32(a_pack ap,
size_t blk_len,
const uint8_t * quant_a_ptr,
Expand Down
Loading
Loading