diff --git a/ggml/src/ggml-spacemit/ime.cpp b/ggml/src/ggml-spacemit/ime.cpp index 8b3b3871cc48..dc58e901f8e8 100644 --- a/ggml/src/ggml-spacemit/ime.cpp +++ b/ggml/src/ggml-spacemit/ime.cpp @@ -416,10 +416,11 @@ template class tensor_ size = GGML_PAD(size, sizeof(int64_t)); const int64_t ne02 = op->src[0]->ne[2]; // n_as, n_expert - const int64_t ne12 = op->src[1]->ne[2]; // n_tokens + // An expert may appear more than once in a token's IDs. + const int64_t row_capacity = ggml_nelements(op->src[2]); const size_t sizeof_mmid_row_mapping = sizeof(int64_t); - size += sizeof_mmid_row_mapping * ne02 * (ne12 + 1) + (ne02 + 1) * sizeof(int64_t); + size += sizeof_mmid_row_mapping * ne02 * (row_capacity + 1) + (ne02 + 1) * sizeof(int64_t); size = GGML_PAD(size, sizeof(int64_t)); @@ -859,7 +860,8 @@ template class tensor_ } } -#define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id) *ne12 + (i1)] + const int64_t row_capacity = (int64_t) n_ids * ids->ne[1]; +#define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id) * row_capacity + (i1)] int64_t * matrix_row_counts = (int64_t *) (ws_ptr + gemm_workspace_size); int32_t * valid_ep_count = (int32_t *) (matrix_row_counts + n_as); @@ -876,7 +878,7 @@ template class tensor_ const int64_t n_rows_all = (int64_t) n_ids * ids->ne[1]; auto align64 = [](uintptr_t p) { return (p + 63) & ~(uintptr_t) 63; }; const size_t nbw1_m4 = hp_aligned() ? a_k_blks * spacemit_kernels::q8_hp_al_row_blk_size : nbw1; - uintptr_t m4_ptr = align64(reinterpret_cast(matrix_rows + n_as * ne12)); + uintptr_t m4_ptr = align64(reinterpret_cast(matrix_rows + n_as * row_capacity)); if (hp_aligned()) { m4_ptr = (m4_ptr + spacemit_kernels::q8_hp_al_align - 1) & ~(uintptr_t) (spacemit_kernels::q8_hp_al_align - 1); }