Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cuda/moe-weighted-reduction.cu # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / moe-weighted-reduction.cu` 65 lines ยท 3440 bytes Raw Blame History 1 #include "moe-weighted-reduction.cuh" 2 3 static __global__ void moe_weighted_reduction_f32(const float * __restrict__ experts, 4 const float * __restrict__ expert_scale, 5 const float * __restrict__ weights, 6 float * __restrict__ dst, 7 const int64_t n_embd, 8 const int n_expert_used) { 9 const int64_t token = blockIdx.x; 10 const int64_t col = (int64_t) blockIdx.y * blockDim.x + threadIdx.x; 11 if (col >= n_embd) { 12 return; 13 } 14 15 const uint64_t first_row = (uint64_t) token * n_expert_used; 16 const float first_scale = expert_scale != nullptr ? expert_scale[first_row] : 1.0f; 17 float sum = (experts[first_row * n_embd + col] * first_scale) * weights[first_row]; 18 19 for (int expert = 1; expert < n_expert_used; ++expert) { 20 const uint64_t row = first_row + expert; 21 const float scale = expert_scale != nullptr ? expert_scale[row] : 1.0f; 22 sum += (experts[row * n_embd + col] * scale) * weights[row]; 23 } 24 dst[token * n_embd + col] = sum; 25 } 26 27 static void launch_moe_weighted_reduction(const float * experts, 28 const float * expert_scale, 29 const float * weights, 30 float * dst, 31 int64_t n_embd, 32 int64_t n_tokens, 33 int n_expert_used, 34 cudaStream_t stream) { 35 constexpr int threads = 256; 36 const dim3 blocks(n_tokens, (n_embd + threads - 1) / threads, 1); 37 moe_weighted_reduction_f32 38 <<>>(experts, expert_scale, weights, dst, n_embd, n_expert_used); 39 } 40 41 void ggml_cuda_op_moe_weighted_reduction(ggml_backend_cuda_context & ctx, 42 const ggml_tensor * experts, 43 const ggml_tensor * expert_scale, 44 const ggml_tensor * weights, 45 ggml_tensor * dst) { 46 GGML_ASSERT(experts->type == GGML_TYPE_F32); 47 GGML_ASSERT(weights->type == GGML_TYPE_F32); 48 GGML_ASSERT(expert_scale == nullptr || expert_scale->type == GGML_TYPE_F32); 49 GGML_ASSERT(dst->type == GGML_TYPE_F32); 50 GGML_ASSERT(ggml_is_contiguous(experts)); 51 GGML_ASSERT(ggml_is_contiguous(weights)); 52 GGML_ASSERT(expert_scale == nullptr || ggml_is_contiguous(expert_scale)); 53 GGML_ASSERT(ggml_is_contiguous(dst)); 54 55 const int64_t n_embd = experts->ne[0]; 56 const int64_t n_expert_used = experts->ne[1]; 57 const int64_t n_tokens = experts->ne[2] * experts->ne[3]; 58 cudaStream_t stream = ctx.stream(); 59 60 launch_moe_weighted_reduction((const float *) experts->data, 61 expert_scale ? (const float *) expert_scale->data : nullptr, 62 (const float *) weights->data, 63 (float *) dst->data, n_embd, n_tokens, (int) n_expert_used, stream); 64 CUDA_CHECK(cudaGetLastError()); 65 }