okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17). 2// Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file. 3// src/dsv41/vendor/moe_win.cu - the window's expert grouping kernel. 4// Extracted from Project Maya's src/kernels/cuda/glm_fast.cu (MIT, see third_party/project-maya/NOTICE.md). 5#include "strata/dsv41/moe_win.hpp" 6#include <cstdio> 7#include <cstdlib> 8 9namespace strata::dsv41k::glmf { 10namespace { 11constexpr int kWinEnt = kMaxWinTokens * 8; 12 13// the entries grouped by expert slot (in order of first appearance), each group's members in entry order (t * 8 + i) 14// (the tables in shared memory: as locals they were a 2.6 KB stack a thread, which the driver reserves for every thread 15// the device can hold - ~420 MB on a V100 - and a launch failed "out of memory" once the expert pool had the rest) 16__global__ void moe_win_group_kernel(const __grid_constant__ MoeWin w) { 17 __shared__ unsigned long long gp[kWinEnt]; 18 __shared__ int cnt[kWinEnt], gof[kWinEnt], fill[kWinEnt + 1]; 19 if (threadIdx.x != 0) return; 20 int ng = 0; 21 for (int t = 0; t < w.T; ++t) 22 for (int i = 0; i < 8; ++i) { 23 const int e = t * 8 + i; 24 gof[e] = -1; 25 const unsigned long long p = i < w.k ? w.plan_ptr[e] : 0ull; 26 if (p == 0ull) continue; 27 int g = 0; 28 while (g < ng && gp[g] != p) ++g; 29 if (g == ng) { 30 gp[ng] = p; 31 cnt[ng++] = 0; 32 } 33 ++cnt[g]; 34 gof[e] = g; 35 } 36 fill[0] = 0; 37 for (int g = 0; g < ng; ++g) { 38 w.grp_ptr[g] = gp[g]; 39 w.grp_start[g] = fill[g]; 40 fill[g + 1] = fill[g] + cnt[g]; 41 } 42 w.grp_start[ng] = fill[ng]; 43 for (int e = 0; e < w.T * 8; ++e) 44 if (gof[e] >= 0) w.grp_ent[fill[gof[e]]++] = e; 45 *w.n_groups = ng; 46} 47} // namespace 48 49void moe_win_group(const MoeWin& w, cudaStream_t s) { 50 moe_win_group_kernel<<<1, 32, 0, s>>>(w); 51 const cudaError_t e = cudaGetLastError(); 52 if (e != cudaSuccess) { 53 std::fprintf(stderr, "dsv41: moe_win_group: %s\n", cudaGetErrorString(e)); 54 std::abort(); 55 } 56} 57} // namespace strata::dsv41k::glmf