From 481e637189cf2bb5525ab8bc83963cda0aa95b18 Mon Sep 17 00:00:00 2001 From: co-seven Date: Fri, 27 Nov 2026 03:20:24 +0000 Subject: [PATCH 1/2] spacemit: skip MoE M8 padding for narrow small-token projections Assisted-by: Codex --- ggml/src/ggml-spacemit/ime.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ggml/src/ggml-spacemit/ime.cpp b/ggml/src/ggml-spacemit/ime.cpp index 493c989d4ccf..95709290378b 100644 --- a/ggml/src/ggml-spacemit/ime.cpp +++ b/ggml/src/ggml-spacemit/ime.cpp @@ -829,7 +829,15 @@ template class tensor_ bool pad8_all = false; if constexpr (std::is_same_v && INTER_SIZE == 256) { - pad8_all = ids->ne[1] > 1 && quantize_a_i8_r4 != nullptr && ne01 % 32 == 0 && hp_aligned(); + // At T2-4, top-k routing commonly gives each expert only 1-4 real + // rows. Select the validated narrow expert projection shape: + // broadcast activations, K2048/N512. Wider projections retain m8 + // padding because no-pad regressed for some routing. + const bool tiny_batch = ids->ne[1] >= 2 && ids->ne[1] <= 4; + const bool gate_up_shape = ne00 == 2048 && ne01 == 512 && ne11 == 1; + const bool small_t_nopad = tiny_batch && gate_up_shape; + pad8_all = ids->ne[1] > 1 && quantize_a_i8_r4 != nullptr && ne01 % 32 == 0 && hp_aligned() && + !small_t_nopad; } if (!pad8_all && ne11 == 1) { @@ -1860,10 +1868,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; From e612eb23ddb32531baf92dcf24bbc11e5e700734 Mon Sep 17 00:00:00 2001 From: co-seven Date: Mon, 30 Nov 2026 13:38:05 +0000 Subject: [PATCH 2/2] ggml-spacemit: Clarify the MoE M8 tail-padding exception Assisted-by: Codex --- ggml/src/ggml-spacemit/ime.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/ggml/src/ggml-spacemit/ime.cpp b/ggml/src/ggml-spacemit/ime.cpp index 95709290378b..bb51f229f3bc 100644 --- a/ggml/src/ggml-spacemit/ime.cpp +++ b/ggml/src/ggml-spacemit/ime.cpp @@ -829,15 +829,14 @@ template class tensor_ bool pad8_all = false; if constexpr (std::is_same_v && INTER_SIZE == 256) { - // At T2-4, top-k routing commonly gives each expert only 1-4 real - // rows. Select the validated narrow expert projection shape: - // broadcast activations, K2048/N512. Wider projections retain m8 - // padding because no-pad regressed for some routing. - const bool tiny_batch = ids->ne[1] >= 2 && ids->ne[1] <= 4; - const bool gate_up_shape = ne00 == 2048 && ne01 == 512 && ne11 == 1; - const bool small_t_nopad = tiny_batch && gate_up_shape; + // Skip M8 tail padding for the validated K=2048, N=512 projection + // with broadcast activations and 2-4 tokens. Full M8 groups remain + // eligible. Wider projections retain their existing padding policy. + const bool small_token_batch = ids->ne[1] >= 2 && ids->ne[1] <= 4; + const bool narrow_broadcast_shape = ne00 == 2048 && ne01 == 512 && ne11 == 1; + const bool skip_m8_tail_padding = small_token_batch && narrow_broadcast_shape; pad8_all = ids->ne[1] > 1 && quantize_a_i8_r4 != nullptr && ne01 % 32 == 0 && hp_aligned() && - !small_t_nopad; + !skip_m8_tail_padding; } if (!pad8_all && ne11 == 1) {