Skip to content

Commit cf31071

Browse files
committed
ggml-cpu: test mul-mat-id short
1 parent 89f10ba commit cf31071

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

ggml/src/ggml-cpu/ggml-cpu.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,41 @@ static void ggml_compute_forward_mul_mat_id(
15861586
#endif
15871587
}
15881588

1589+
// fast path for single-token decode (n_tokens == 1)
1590+
// skips row mapping setup and atomic work-stealing overhead,
1591+
// reduces to n_ids simple mat-vec products
1592+
if (ids->ne[1] == 1) {
1593+
ggml_barrier(params->threadpool);
1594+
1595+
ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
1596+
const size_t row_size = ggml_row_size(vec_dot_type, ne10);
1597+
1598+
const int64_t ir0_start = (ith * ne01) / nth;
1599+
const int64_t ir0_end = ((ith + 1) * ne01) / nth;
1600+
1601+
for (int id = 0; id < n_ids; ++id) {
1602+
const int32_t expert_id = *(const int32_t *) ((const char *) ids->data + id * ids->nb[0]);
1603+
1604+
const char * src0_cur = (const char *) src0->data + expert_id * nb02;
1605+
1606+
const char * src1_col;
1607+
if (src1->type != vec_dot_type) {
1608+
src1_col = (const char *) params->wdata + id * row_size;
1609+
} else if (src1_cont) {
1610+
src1_col = (const char *) src1->data + id * row_size;
1611+
} else {
1612+
src1_col = (const char *) src1->data + id * nb11;
1613+
}
1614+
1615+
float * dst_col = (float *) ((char *) dst->data + id * nb1);
1616+
1617+
for (int64_t ir0 = ir0_start; ir0 < ir0_end; ++ir0) {
1618+
vec_dot(ne00, &dst_col[ir0], 0, src0_cur + ir0 * nb01, 0, src1_col, 0, 1);
1619+
}
1620+
}
1621+
return;
1622+
}
1623+
15891624
if (ith == 0) {
15901625
// initialize matrix_row_counts
15911626
memset(matrix_row_counts, 0, n_as*sizeof(int64_t));

0 commit comments

Comments
 (0)