Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

1.6 KB
Raw Blame
// SPDX-License-Identifier: MIT
// Kyojin: routed (MoE) F16-WMMA prefill torch op (rocm/routed_f16/routed_f16.cu, from Gufo; see THIRD_PARTY_NOTICES.md). HIP gfx1151 only.
#pragma once
#include <torch/extension.h>

// out[t * top_k + s, :K] (half) = down[e] @ (silu(gate[e] @ x[t]) * (up[e] @ x[t])), e = ids[t, s]. counts int32 [E], tiles int32 (expert | 128-row
// tile << 16); *_ptrs int64 [E] device pointers to each expert's raw GGUF rows. Formats: gate/up Q4_K 12 / Q5_K 13, down Q5_1 7 / Q8_0 8.
void fq_routed_f16_moe(torch::Tensor x, torch::Tensor ids, torch::Tensor counts, torch::Tensor tiles,
                       torch::Tensor gate_ptrs, torch::Tensor up_ptrs, torch::Tensor down_ptrs, int64_t gate_fmt,
                       int64_t down_fmt, int64_t I, torch::Tensor out);

// out[t, :] (float) = sum_s weights[t, s] * rows[t * top_k + s, :], s in order (rows half [T * top_k, K], weights float [T, top_k]); the weighted
// top-k reduction after fq_routed_f16_moe (Gufo MoeEpilogueVec4Kernel<__half> without the shared expert).
void fq_routed_f16_combine(torch::Tensor rows, torch::Tensor weights, torch::Tensor out);

// fq_routed_f16_combine plus the gated shared expert in the same pass: out[t, :] = sum_s weights[t, s] * rows[t * top_k + s, :] + shared[t, :] * sigmoid(f16(x[t, :] . gate_w)).
// shared half [T, K]; x half [T, K] (unit column stride, any row pitch); gate_w half [K]. Gufo MoeEpilogueVec4Kernel<__half> with the shared-expert term.
void fq_routed_f16_combine_shared(torch::Tensor rows, torch::Tensor weights, torch::Tensor shared, torch::Tensor x, torch::Tensor gate_w,
                                  torch::Tensor out);