diff --git a/engine/src/ling32/sycl/ling32_sy_k_misc.cpp b/engine/src/ling32/sycl/ling32_sy_k_misc.cpp index 66364cda1c..2883e3d30e 100644 --- a/engine/src/ling32/sycl/ling32_sy_k_misc.cpp +++ b/engine/src/ling32/sycl/ling32_sy_k_misc.cpp @@ -31,4 +31,34 @@ void argmax_rows(const float* logits, int N, int V, int* out, cudaStream_t s) { }); } +// Dynamic expert skip (--skip-tau, opt-in, NOT exact; port of prune_experts_kernel of ling32_layers.cu, one token per thread, the same arithmetic): a routed expert whose share of the token's routing +// weight is below tau is dropped (its slot gets the top expert's id with weight 0), the others are rescaled so that the weight sum stays the same. +void prune_experts(int* ridx, float* rw, int N, int top_k, float tau, cudaStream_t s) { + sy::launch(s, (N + 31) / 32, 32, 0, [=](sy::Blk b, char*) { + const int t = b.bid() * b.bdim() + b.tid(); + if (t >= N) return; + int* ix = ridx + static_cast(t) * top_k; + float* w = rw + static_cast(t) * top_k; + float sum = 0.0f, wmax = -1.0f; + int imax = 0; + for (int j = 0; j < top_k; ++j) { + sum += w[j]; + if (w[j] > wmax) { wmax = w[j]; imax = j; } + } + if (!(sum > 0.0f)) return; + float kept = 0.0f; + int drops = 0; + for (int j = 0; j < top_k; ++j) { + if (j != imax && w[j] < tau * sum) ++drops; else kept += w[j]; + } + if (drops == 0 || !(kept > 0.0f)) return; + const float scale = sum / kept; + const int top_id = ix[imax]; + for (int j = 0; j < top_k; ++j) { + if (j != imax && w[j] < tau * sum) { ix[j] = top_id; w[j] = 0.0f; } + else w[j] *= scale; + } + }); +} + } // namespace strata::ling32::gpu