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.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Hy4 integration: prefill set 2 in the served flags (CK MLA core, WMMA experts HY4_MOE_HIP=2, HY4_PF_FUSE), frozen TunableOp results for the prefill GEMMs, Triton warm-up at start, MTP head q5k + n-gram long 12
YanissAmz93f1d18327cf
diff --git a/exllamav3/loader/hc_fused.py b/exllamav3/loader/hc_fused.pyindex a7cbcb9..08680e5 100644--- a/exllamav3/loader/hc_fused.py+++ b/exllamav3/loader/hc_fused.py@@ -21,6 +21,7 @@ ENABLED = os.environ.get("HY4_HC_FUSED", "0") == "1"ONEPASS = os.environ.get("HY4_PF_FUSE", "0") == "1" # one program per row: [optional residual add], mix dots, sigmoid, collapse, norm in ONE read of the stream (replaces K1 + K2)BK = int(os.environ.get("HY4_HC_BK", "512")) # K1 chunk (elements of the flattened H*D row)EMIT16 = [os.environ.get("HY4_PF_FUSE", "0") == "1"] # the one-pass kernel also writes the saturating fp16 copy of cur (registered with to_f16)+PF_MIN_ROWS = 16 # rows <= 16 are decode / verify steps (never take the prefill-only kernels)MAX_D = 16384 # K2/K3 keep one row of D in registers@@ -166,7 +167,7 @@ def _mix(x, y, post_prev, hw, w, eps, hc_eps, mag):NOP = _p2(NO)NCH = triton.cdiv(K, BK)dev = x.device- if ONEPASS and H <= 8:+ if ONEPASS and H <= 8 and T > PF_MIN_ROWS: # prefill rows only: the decode / verify rows keep the two-kernel path of the decode graphhas_res = y is not Nones = torch.empty_like(x) if has_res else xpost = torch.empty(T, H, dtype=torch.float32, device=dev) if NO == 2 * H else None@@ -215,6 +216,7 @@ def resid(s, f, post, w_post, eps):def rmsw(x, w, eps, emit16=False):"""rms(x, eps) * w over the last dim, fp32 [R, D] (one launch instead of 5). emit16: also writes the saturating fp16 copy and registers it with to_f16"""x = x.float().contiguous()+ emit16 = emit16 and x.shape[0] > PF_MIN_ROWSo = torch.empty_like(x)o16 = torch.empty(x.shape, dtype=torch.float16, device=x.device) if emit16 else o_k4_rmsw[(x.shape[0],)](x, w.contiguous(), o, o16, x.shape[1], eps, EMIT16=bool(emit16), BD=_p2(x.shape[1]), num_warps=8)diff --git a/tools/hy4/engine.py b/tools/hy4/engine.pyindex b1dd66e..84c9918 100644--- a/tools/hy4/engine.py+++ b/tools/hy4/engine.py@@ -40,6 +40,7 @@ class Hy4Engine:self.ngram_gate = os.environ.get("HY4_NGRAM_GATE", "0") == "1"self.ngram_fill = os.environ.get("HY4_NGRAM_FILL", "0") == "1"self.ngram_long = int(os.environ.get("HY4_NGRAM_LONG", "0")) # >= 1: a match of at least this many tokens REPLACES the MTP draft chain (verify y + up to TM - 1 copied tokens; the draft cache is refilled from the trunk states); 0 = off+ self._tune = None # torch.cuda.tunable module while a frozen TunableOp results file is loaded (HY4_TUNABLEOP_FILE)self.ready = Falseself.meta = Noneself.last_stats: dict = {}@@ -52,6 +53,16 @@ class Hy4Engine:if cap < self.ctx + 64:raise RuntimeError(f"HY4_NOSYNC_CAP={cap} must be set (before import) to at least ctx + 64 = {self.ctx + 64}")_stub_package(self.tree)+ tf = os.environ.get("HY4_TUNABLEOP_FILE", "")+ if tf and os.path.exists(tf): # frozen TunableOp results for the prefill GEMMs (no tuning at run time; shapes missing from the file use the default kernel)+ import torch.cuda.tunable as tu+ tu.set_filename(tf, insert_device_ordinal=False)+ tu.tuning_enable(False)+ tu.enable(True)+ self._tune = tu+ progress(f"TunableOp results {tf}")+ elif tf:+ progress(f"TunableOp file missing: {tf}")from exllamav3.loader.gguf_collection import GGUFCollectionfrom exllamav3.loader import hy4_forward as hffrom exllamav3.loader import mla_hy4 as mh@@ -96,6 +107,8 @@ class Hy4Engine:def phase(self, name: str):"""prefill: decode kernels off, prefill kernels on (the configuration the prefill numbers were measured in); decode: decode kernels restored"""pre = name == "prefill"+ if self._tune is not None:+ self._tune.enable(pre) # prefill GEMMs only: the decode graph keeps the default kernelsfor m, a, v in self._dec:setattr(m, a, False if pre else v)hf, mh = self.hf, self.mh@@ -103,6 +116,14 @@ class Hy4Engine:hf.PF_MOE_I8 = hf.PF_MOE_REF = hf.PF_DENSE_I8 = hf.PF_ROUTER = hf.PF_DIAG = Falsemh.PF_ATTN, mh.PF_ATTN_FLASH = True, False # (rows <= 16 never take the prefill branches)+ def warmup(self, n: int):+ """one synthetic request of n prompt tokens + 8 decode tokens: compiles the Triton prefill kernels (they would land in the first user chunk, about 0.3 s) and captures the decode graphs"""+ ids = [(i * 7919 + 13) % 50000 for i in range(n)]+ t0 = time.perf_counter()+ self.generate(ids, 8, lambda toks: False)+ self.reset()+ print(f"warm-up {n} tokens: {time.perf_counter() - t0:.1f} s", flush=True)+# ------------------------------------------------------------------------------------------ request statedef reset(self):M, GR = self.M, self.GRdiff --git a/tools/hy4/serve.py b/tools/hy4/serve.pyindex 8b2c660..85e0ca9 100644--- a/tools/hy4/serve.py+++ b/tools/hy4/serve.py@@ -34,10 +34,11 @@ SHIPPED_ENV = {"HY4_NOSYNC": "1", # static-buffer step (needed by the graph)"HY4_HC_FUSED": "1", "HY4_HEAD_BF16": "1", "HY4_MLA_BG": "1", "HY4_ATTN_EAGER": "2", "HY4_KV_FUSED": "1","HY4_IDX_POOLK": "1", "HY4_REF_FUSED": "1", "HY4_REF_ROUTER": "1", "HY4_REF_MLA": "1", "HY4_ROUTER_GEMV": "1", "HY4_MOE_SORT": "1", # decode set- "HY4_MTP_FAST": "1", "HY4_MTP_HEAD": "q8", # draft head- "HY4_NGRAM_EXT": "4", "HY4_NGRAM_MIN": "8", "HY4_NGRAM_GATE": "1", # prompt-lookup rows on top of the MTP drafts: up to 4 extra verify rows when the last 8 tokens occurred earlier; paused while their acceptance is low+ "HY4_MTP_FAST": "1", "HY4_MTP_HEAD": "q5k", # draft head+ "HY4_NGRAM_EXT": "4", "HY4_NGRAM_MIN": "8", "HY4_NGRAM_GATE": "1", "HY4_NGRAM_LONG": "12", # prompt-lookup rows on top of the MTP drafts: up to 4 extra verify rows when the last 8 tokens occurred earlier; paused while their acceptance is low"HY4_PREFILL_MOE": "1", "HY4_PREFILL_DENSE": "1", "HY4_PREFILL_ATTN": "1", "HY4_PREFILL_IX": "1", "HY4_ENGRAM_PREFETCH": "1","HY4_PF_F16": "1", "HY4_MOE_NFIRST": "1", "KEEPCACHE": "1", # prefill set+ "HY4_PF_WOT": "0", "HY4_MOE_HIP": "2", "HY4_PF_CK": "1", "HY4_PF_FUSE": "1", # prefill set 2: shared gm_ext WMMA experts (HY4_GM_EXT), CK MLA core (EXL3_CKMLA_SO), one-pass hc / norm / indexer kernels}@@ -271,6 +272,10 @@ def load_in_background(st: State):st.stage = sprint("load:", s, flush=True)eng.load(progress)+ if os.environ.get("HY4_WARMUP_TOKENS", "0") != "0":+ st.stage = "warm-up"+ print("load: warm-up", flush=True)+ eng.warmup(int(os.environ["HY4_WARMUP_TOKENS"]))st.stage = "tokenizer"st.vocab = C.Vocab(eng.meta)st.engine = engdiff --git a/tools/hy4/tunableop_gfx1151.csv b/tools/hy4/tunableop_gfx1151.csvnew file mode 100644index 0000000..73aecaa--- /dev/null+++ b/tools/hy4/tunableop_gfx1151.csv@@ -0,0 +1,51 @@+Validator,PT_VERSION,2.12.0+Validator,HIP_VERSION,713+Validator,HIPBLASLT_VERSION,100202-f000f7786e+Validator,GCN_ARCH_NAME,gfx1151+Validator,ROCBLAS_VERSION,5.4.0.f000f7786e+GemmTunableOp_Half_NN,nn_1024_8192_3072_ld_1024_3072_1024,Gemm_Rocblas_-1162084899,1.20717+GemmTunableOp_Half_NN,nn_16384_8192_1024_ld_16384_1024_16384,Gemm_Rocblas_-1162084901,6.52869+GemmTunableOp_Half_NN,nn_8192_8192_1024_ld_8192_1024_8192,Gemm_Rocblas_-1162084897,2.98608+GemmTunableOp_Half_NN,nn_576_8192_3072_ld_576_3072_576,Gemm_Hipblaslt_1113,0.786625+GemmTunableOp_Half_NN,nn_16384_8192_3072_ld_16384_3072_16384,Gemm_Hipblaslt_1113,19.1288+GemmTunableOp_Half_NN,nn_8192_8192_3072_ld_8192_3072_8192,Gemm_Hipblaslt_1113,9.89774+GemmTunableOp_Half_NN,nn_3072_8192_16384_ld_3072_16384_3072,Gemm_Hipblaslt_1112,23.9962+GemmTunableOp_Half_NN,nn_3072_8192_8192_ld_3072_8192_3072,Gemm_Hipblaslt_1110,10.2804+GemmTunableOp_Half_NN,nn_1536_8192_3072_ld_1536_3072_1536,Gemm_Rocblas_-1162084900,1.84349+GemmTunableOp_Half_NN,nn_3072_8192_1536_ld_3072_1536_3072,Gemm_Rocblas_-1162084897,1.76467+GemmTunableOp_Half_NN,nn_13824_8192_3072_ld_13824_3072_13824,Gemm_Rocblas_-1162084901,15.5465+GemmTunableOp_Half_NN,nn_576_4096_3072_ld_576_3072_576,Gemm_Hipblaslt_1111,0.327129+GemmTunableOp_Half_NN,nn_1024_4096_3072_ld_1024_3072_1024,Gemm_Hipblaslt_1112,0.623512+GemmTunableOp_Half_NN,nn_128_4096_3072_ld_128_3072_128,Gemm_Hipblaslt_1110,0.101881+GemmTunableOp_Half_NN,nn_128_4096_128_ld_128_128_128,Gemm_Rocblas_-1162084880,0.0115182+GemmTunableOp_Half_NN,nn_2048_4096_1024_ld_2048_1024_2048,Gemm_Rocblas_-1162084899,0.340418+GemmTunableOp_float_TN,tn_16_4096_3072_ld_3072_3072_16,Gemm_Hipblaslt_1671,0.283764+GemmTunableOp_Half_NN,nn_1024_65536_128_ld_1024_128_1024,Gemm_Rocblas_-1162084899,0.745136+GemmStridedBatchedTunableOp_Half_NN,nn_1024_1_16_B_4096_ld_1024_16_1024,Gemm_Rocblas_-589,0.661069+GemmTunableOp_Half_NN,nn_8192_4096_1024_ld_8192_1024_8192,Gemm_Rocblas_-1162084897,1.37794+GemmTunableOp_Half_NN,nn_16384_4096_512_ld_16384_512_16384,Gemm_Rocblas_-1162084897,1.46405+GemmTunableOp_Half_NN,nn_8192_4096_3072_ld_8192_3072_8192,Gemm_Rocblas_-1162084905,4.78212+GemmTunableOp_Half_NN,nn_3072_4096_8192_ld_3072_8192_3072,Gemm_Hipblaslt_1112,5.02616+GemmTunableOp_Half_NN,nn_13824_4096_3072_ld_13824_3072_13824,Gemm_Rocblas_-1162084900,8.12347+GemmTunableOp_Half_NN,nn_3072_4096_13824_ld_3072_13824_3072,Gemm_Hipblaslt_1110,7.60922+GemmTunableOp_Half_NN,nn_15360_4096_1536_ld_15360_1536_15360,Default,4.13531+GemmTunableOp_float_TN,tn_512_4096_3072_ld_3072_3072_512,Gemm_Rocblas_-549,2.16364+GemmTunableOp_Half_NN,nn_1536_4096_3072_ld_1536_3072_1536,Gemm_Rocblas_-1162084900,0.936389+GemmTunableOp_Half_NN,nn_3072_4096_1536_ld_3072_1536_3072,Gemm_Rocblas_-1162084840,0.874172+GemmTunableOp_Half_NN,nn_16384_4096_1024_ld_16384_1024_16384,Gemm_Rocblas_-1162084901,3.33099+GemmTunableOp_Half_NN,nn_32768_4096_512_ld_32768_512_32768,Gemm_Rocblas_-1162084820,3.67423+GemmTunableOp_Half_NN,nn_16384_4096_3072_ld_16384_3072_16384,Gemm_Hipblaslt_1113,9.96537+GemmTunableOp_Half_NN,nn_3072_4096_16384_ld_3072_16384_3072,Gemm_Hipblaslt_1112,11.9001+GemmTunableOp_float_TN,tn_8192_4096_3072_ld_3072_3072_8192,Gemm_Rocblas_-549,32.831+GemmTunableOp_float_TN,tn_6144_4096_3072_ld_3072_3072_6144,Gemm_Rocblas_-548,25.019+GemmTunableOp_Half_NN,nn_128_8192_3072_ld_128_3072_128,Gemm_Hipblaslt_1073,0.32418+GemmTunableOp_Half_NN,nn_128_8192_128_ld_128_128_128,Gemm_Rocblas_-1162084866,0.0142783+GemmTunableOp_Half_NN,nn_2048_8192_1024_ld_2048_1024_2048,Gemm_Rocblas_-1162084897,0.862751+GemmTunableOp_float_TN,tn_16_8192_3072_ld_3072_3072_16,Gemm_Hipblaslt_1671,0.652292+GemmTunableOp_Half_NN,nn_2048_131072_128_ld_2048_128_2048,Gemm_Rocblas_-1162084810,3.28439+GemmStridedBatchedTunableOp_Half_NN,nn_2048_1_16_B_8192_ld_2048_16_2048,Gemm_Rocblas_-596,2.65938+GemmTunableOp_Half_NN,nn_16384_8192_512_ld_16384_512_16384,Gemm_Rocblas_-1162084897,2.97618+GemmTunableOp_Half_NN,nn_3072_8192_13824_ld_3072_13824_3072,Gemm_Hipblaslt_1112,15.6838+GemmTunableOp_Half_NN,nn_15360_8192_1536_ld_15360_1536_15360,Default,8.31557+GemmTunableOp_float_TN,tn_512_8192_3072_ld_3072_3072_512,Gemm_Rocblas_-549,4.16669+GemmTunableOp_Half_NN,nn_32768_8192_512_ld_32768_512_32768,Gemm_Rocblas_-1162084900,7.48055diff --git a/tools/strix_halo/ck_fmha/build_mla.sh b/tools/strix_halo/ck_fmha/build_mla.shnew file mode 100755index 0000000..09b3a29--- /dev/null+++ b/tools/strix_halo/ck_fmha/build_mla.sh@@ -0,0 +1,24 @@+#!/usr/bin/env bash+# Build libckmla.so: CK fmha_fwd fp16 batch, hd 256 (gfx1151 tile), causal / window mask, elementwise bias, learnable sink, behind ckmla_fwd.+# Same env as tools/strix_halo/ck_fmha/build.sh (CK_SRC checkout at the pinned commit, EXL3_ROCM_SDK, GCC_DIR). OUT=$EXL3_CKMLA_OUT.+set -euo pipefail+: "${CK_SRC:?set CK_SRC}"+HERE=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)+OUT=${EXL3_CKMLA_OUT:-${XDG_CACHE_HOME:-$HOME/.cache}/kyojin/ck-fmha/libckmla.so}+CXX=${ROCM_CLANG:-}+for c in "${EXL3_ROCM_SDK:-/nonexistent}/lib/llvm/bin/amdclang++" "${EXL3_ROCM_SDK:-/nonexistent}/llvm/bin/amdclang++" /opt/rocm/bin/amdclang++; do+ if [ -z "$CXX" ] && [ -x "$c" ]; then CXX=$c; fi+done+: "${CXX:?no amdclang++ found: set ROCM_CLANG}"+GCC_DIR=${GCC_DIR:-/usr/lib/gcc/x86_64-linux-gnu/13}+F=$CK_SRC/example/ck_tile/01_fmha+B=$(mktemp -d); trap 'rm -rf "$B"' EXIT+mkdir -p "$B/gen" "$B/obj" "$(dirname "$OUT")"+python3 "$F/generate.py" --targets gfx1151 --api fwd --optdim ${CKMLA_HD:-256} -r 0 \+ -f "*fp16*batch*_nlogits_*_nlse_ndropout_nskip_nqscale*" -o "$B/gen" >/dev/null+FLAGS="--gcc-install-dir=$GCC_DIR -std=c++20 -O3 -fPIC -x hip --offload-arch=gfx1151 -I$CK_SRC/include -I$F -I$B/gen \+ -DCK_TILE_FMHA_FWD_FAST_EXP2=1 -fgpu-flush-denormals-to-zero -DCK_TILE_FMHA_FWD_SPLITKV_API=0 -DCK_TILE_FMHA_FWD_APPENDKV_API=0 \+ -DCK_TILE_FMHA_FWD_PAGEDKV_API=0 -DCK_TILE_FMHA_FWD_BATCH_PREFILL_API=0 -w"+ls "$B"/gen/*.cpp "$HERE/ckmla_shim.cpp" | xargs -P "${CK_JOBS:-8}" -I{} sh -c "$CXX $FLAGS -c {} -o $B/obj/\$(basename {} .cpp).o"+"$CXX" --gcc-install-dir="$GCC_DIR" -shared -fPIC --offload-arch=gfx1151 "$B"/obj/*.o -o "$OUT"+echo "built $OUT ($(ls "$B"/gen/*.cpp | wc -l) instance files)"diff --git a/tools/strix_halo/ck_fmha/ckmla_shim.cpp b/tools/strix_halo/ck_fmha/ckmla_shim.cppnew file mode 100644index 0000000..01291f6--- /dev/null+++ b/tools/strix_halo/ck_fmha/ckmla_shim.cpp@@ -0,0 +1,38 @@+// ckmla: C ABI over CK fmha_fwd for un-absorbed MLA prefill (fp16, batch 1, hd 256/256 (any hd the build covers), per-tensor strides,+// optional elementwise bias [sq, sk] (head-broadcast), optional per-head learnable sink (fp32, in scaled-logit space), causal bottom-right+// with an optional left window (window_left > 0: keys in [q - window_left, q]). Separate symbol from ckpf_fwd (that one is unchanged).+#include <cmath>+#include <cstdint>+#include <hip/hip_runtime.h>+#include "bias.hpp"+#include "fmha_fwd.hpp"+#include "mask.hpp"++extern "C" float ckmla_fwd(const void* q, const void* k, const void* v, void* o, const void* bias, const void* sink,+ int sq, int sk, int hq, int hk, int dq, int dv, float scale,+ long stride_q, long nh_q, long stride_k, long nh_k, long stride_v, long nh_v, long stride_o, long nh_o,+ long stride_bias, int window_left, int causal, hipStream_t stream, int time_it)+{+ fmha_fwd_traits t{};+ t.hdim_q = dq; t.hdim_v = dv; t.data_type = "fp16"; t.is_group_mode = false;+ t.is_v_rowmajor = true; t.has_logits_soft_cap = false;+ t.mask_type = causal ? mask_enum::mask_bottom_right : mask_enum::no_mask;+ t.bias_type = bias ? bias_enum::elementwise_bias : bias_enum::no_bias;+ t.has_lse = false; t.has_dropout = false; t.has_sink = sink != nullptr;+ t.qscale_type = quant_scale_enum::no_scale;+ fmha_fwd_args a{};+ a.q_ptr = q; a.k_ptr = k; a.v_ptr = v; a.o_ptr = o; a.bias_ptr = bias; a.sink_ptr = sink;+ a.seqlen_q = sq; a.seqlen_k = sk; a.batch = 1; a.max_seqlen_q = sq;+ a.hdim_q = dq; a.hdim_v = dv; a.nhead_q = hq; a.nhead_k = hk;+ a.scale_s = scale; a.logits_soft_cap = 0.f;+ a.stride_q = stride_q; a.stride_k = stride_k; a.stride_v = stride_v; a.stride_o = stride_o; a.stride_bias = stride_bias;+ a.nhead_stride_q = nh_q; a.nhead_stride_k = nh_k; a.nhead_stride_v = nh_v; a.nhead_stride_o = nh_o; a.nhead_stride_bias = 0;+ a.batch_stride_q = 0; a.batch_stride_k = 0; a.batch_stride_v = 0; a.batch_stride_o = 0; a.batch_stride_bias = 0;+ a.window_size_left = window_left > 0 ? window_left : -1; a.window_size_right = causal ? 0 : -1;+ a.sink_size = 0;+ a.mask_type = static_cast<ck_tile::index_t>(t.mask_type);+ a.p_drop = 0.f; a.s_randval = false;+ a.drop_seed_offset = std::pair<uint64_t, uint64_t>{0, 0};+ ck_tile::stream_config cfg{stream, time_it != 0, 0, 0, 1, false};+ return fmha_fwd(t, a, cfg);+}