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
37 changes: 25 additions & 12 deletions ggml/src/ggml-spacemit/ime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,11 @@ template <typename BLOC_TYPE, int64_t INTER_SIZE, int64_t NB_COLS> class tensor_
}
}
} else if (tcm_buffer != nullptr && per_nb_cols_wsize <= tcm_buffer_size) {
constexpr int64_t staged_n_cols = NB_COLS;
const int64_t staged_b_bytes = staged_n_cols * row_stride_b;
uint8_t * a_row = quant_a_buffer;
uint8_t * b_col = reinterpret_cast<uint8_t *>(tcm_buffer);
if ((gemm_workspace_size + per_nb_cols_wsize) <= tcm_buffer_size) {
if ((gemm_workspace_size + staged_b_bytes) <= tcm_buffer_size) {
a_row = (uint8_t *) tcm_buffer;
b_col = reinterpret_cast<uint8_t *>(tcm_buffer) + gemm_workspace_size;
}
Expand All @@ -670,13 +672,22 @@ template <typename BLOC_TYPE, int64_t INTER_SIZE, int64_t NB_COLS> class tensor_

const bool has_pair = ((ith & 1) != 0) || (ith + 1 < nth);

const int64_t ni0 = (int64_t) ith * NB_COLS;
const auto staged_columns = [](int64_t count) {
if constexpr (std::is_same_v<BLOC_TYPE, block_q8_0> || std::is_same_v<BLOC_TYPE, block_q6_K>) {
// K blocks interleave all N32 rows. A partial output tile
// still needs every packed K block, including padded rows.
return (int64_t) GGML_PAD(count, NB_COLS);
} else {
return count;
}
};
const int64_t ni0 = (int64_t) ith * staged_n_cols;
const bool active0 = ni0 < gemm_n;
const int64_t nb0 = active0 ? std::min(gemm_n - ni0, (int64_t) NB_COLS) : 0;
const int64_t nb0 = active0 ? std::min(gemm_n - ni0, staged_n_cols) : 0;

if (ith % 2 == 0 && active0) {
spacemit_kernels::rvv::memcpy1d(b_col, reinterpret_cast<uint8_t *>(w_data) + ni0 * row_stride_b,
nb0 * row_stride_b);
staged_columns(nb0) * row_stride_b);
if (a_row != quant_a_buffer) {
spacemit_kernels::rvv::memcpy1d(a_row, quant_a_buffer, gemm_workspace_size);
}
Expand All @@ -691,15 +702,15 @@ template <typename BLOC_TYPE, int64_t INTER_SIZE, int64_t NB_COLS> class tensor_
spacemit_kernels::rvv::memcpy1d(a_row, quant_a_buffer, gemm_workspace_size);
}
spacemit_kernels::rvv::memcpy1d(b_col, reinterpret_cast<uint8_t *>(w_data) + ni0 * row_stride_b,
nb0 * row_stride_b);
staged_columns(nb0) * row_stride_b);
}

const int64_t base_start = (int64_t) (ith & ~1) * NB_COLS;
const int64_t lane_off = (int64_t) (ith & 1) * NB_COLS;
for (int64_t base = base_start; base < gemm_n; base += NB_COLS * nth) {
const int64_t base_start = (int64_t) (ith & ~1) * staged_n_cols;
const int64_t lane_off = (int64_t) (ith & 1) * staged_n_cols;
for (int64_t base = base_start; base < gemm_n; base += staged_n_cols * nth) {
const int64_t ni = base + lane_off;
const bool active = ni < gemm_n;
const int64_t nb_real = active ? std::min(gemm_n - ni, (int64_t) NB_COLS) : 0;
const int64_t nb_real = active ? std::min(gemm_n - ni, staged_n_cols) : 0;

if (has_pair && ith % 2 != 0) {
spine_barrier_wait(cur_barrier);
Expand All @@ -720,19 +731,21 @@ template <typename BLOC_TYPE, int64_t INTER_SIZE, int64_t NB_COLS> class tensor_

rows_remaining -= rows_handled;
}

}

if (has_pair && ith % 2 == 0) {
spine_barrier_wait(cur_barrier);
}

const int64_t next_ni = ni + NB_COLS * nth;
const int64_t next_ni = ni + staged_n_cols * nth;
if (next_ni < gemm_n) {
const int64_t next_nb = std::min(gemm_n - next_ni, (int64_t) NB_COLS);
const int64_t next_nb = std::min(gemm_n - next_ni, staged_n_cols);
spacemit_kernels::rvv::memcpy1d(b_col, reinterpret_cast<uint8_t *>(w_data) + next_ni * row_stride_b,
next_nb * row_stride_b);
staged_columns(next_nb) * row_stride_b);
}
}

} else {
const int64_t task_count_m = spacemit_kernels::div_round_up(gemm_m, gemm_m_stride);
const int64_t task_count_n = spacemit_kernels::div_round_up(gemm_n, gemm_n_stride);
Expand Down
5 changes: 3 additions & 2 deletions ggml/src/ggml-spacemit/ime2_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4035,6 +4035,7 @@ static void gemm_kernel_i8i8_m4(const uint8_t * quant_a_ptr,
constexpr size_t blk_len = 32;
int64_t b_data_stride = k_blks * sizeof(ggml_fp16_t) + k_blks * blk_len;
for (size_t ni = 0; ni < count_n; ni += 32) {
const size_t nb_real = std::min<size_t>(count_n - ni, 32);
uint8_t * b_data = (uint8_t *) quant_b_data + ni * b_data_stride;
int8_t * a_data = (int8_t *) quant_a_ptr;
float * dst_c = c_ptr + ni;
Expand Down Expand Up @@ -4108,7 +4109,7 @@ static void gemm_kernel_i8i8_m4(const uint8_t * quant_a_ptr,

"bgtz %[BK], BLK_LOOP%= \n\t"

"vsetvli t0, x0, e32, m1 \n\t"
"vsetvli t0, %[NBLKS], e32, m1 \n\t"
"add t2, %[LDC], %[DST] \n\t"
"vse32.v v28, (%[DST]) \n\t"
"add t3, %[LDC], t2 \n\t"
Expand All @@ -4117,7 +4118,7 @@ static void gemm_kernel_i8i8_m4(const uint8_t * quant_a_ptr,
"vse32.v v30, (t3) \n\t"
"vse32.v v31, (t2) \n\t"
: [A] "+r"(a_data), [B] "+r"(b_data), [BK] "+r"(bk)
: [DST] "r"(dst_c), [LDC] "r"(ldc * 4), [AHDR] "i"(64)
: [DST] "r"(dst_c), [LDC] "r"(ldc * 4), [AHDR] "i"(64), [NBLKS] "r"(nb_real)
: "t0", "t1", "t2", "t3", "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", "fa0", "fa1", "fa2", "fa3", "memory");
Expand Down
Loading