Add GLM-5.3-Flash (glm5next) support with ROCmFP mix qtypes (105/106) - #1
Merged
Conversation
- Add glm5next architecture to model capabilities with MoE expert offload - Create glm5next backend implementing ModelBackend interface - 45 layers: 34 KDA linear attention + 11 NoPE MLA/DSA - Dense FFN on layers 0-2, MoE 288 experts top-8 from layer 3 - mHC with 4 streams, Sinkhorn normalization, unweighted mean collapse - Support for ROCmFP qtypes 105/106/107 on routed expert tensors - Wire glm5next and glm5-next alias into backend factory - Add glm5next sources to CMakeLists.txt - Preserve HIP gfx1100;gfx1151 fat binary build support Architecture details: - Hidden: 4096, vocab: 154880, expert FF: 2048 - Clamped SwiGLU (gate (-inf,10], up [-10,10]) - IndexPool DSA with kpool=4 - NoPE sparse attention (qk_rope_head_dim=0) - full_attn_interval=4, index_topk=2048 Reuses from existing codebase: - MoeHybridStorage/MoeExpertCompute for expert offload - mHC Sinkhorn from deepseek4_hc_cuda.cu - MoeHybrid FFN evaluation framework Does NOT copy from DeepSeek4: - Hash routing (DS4 layers 0-2), GLM uses dense FFN - 256 experts top-6 (DS4), GLM uses 288 top-8 - MLA compressor/SWA/indexer (different DSA approach) - Partial RoPE (DS4 MLA), GLM uses NoPE - Grouped MLA output (DS4), GLM uses absorbed wk_b/wv_b + wo Stub implementation provides: - Architecture registration and backend construction - Loader framework for glm5next.* GGUF keys (not deepseek4.*) - Graph skeleton with layer type detection (KDA vs MLA) - Dense FFN and MoE routing structure - Ready for actual tensor loading and graph building Co-authored-by: Marcelo Ribeiro Mendes <mmendes200@gmail.com>
Mix qtypes 105 (Q3_1_ROCMFP3_MIX) and 106 (Q2_1_ROCMFP2_MIX) are 104/107
block wire plus out-of-band 7s1c codebooks. Kernels live in ggml-cuda
rocmfp2_mix / rocmfp3_mix.
Implemented:
- P4MIX (qtype 105): down-experts only, reads P4MIXv1 sidecar
- GUMIX (qtype 106): gate/up/down, reads GUMIXs1 split-form sidecar
- Codebook registration via ggml_cuda_rocmfp{2,3}_mix_register_host
- Proper cleanup via unregister on backend shutdown
- Validation: gate/up qtype-105 rejected (down-only constraint)
- Dimension checks: 106 requires in %% 128 == 0 (GLM ff=2048, embd=4096 pass)
- 105 requires in %% 32 == 0
Sidecar format:
- Prefer glm5next.p4mix.sidecar / glm5next.gumix.sidecar GGUF KV keys
- Parse P4MIXv1 / GUMIXs1 magics (same format as DS4)
- Missing table fails the load (no silent fallback to uniform decode)
Constraints enforced:
- 105 (p4mix): down-experts only, C=2 K=8, in %% 32 == 0
- 106 (gumix): gate/up/down (surfaces 0/1/2), C=2 K=4, in %% 128 == 0
- Mode validation: 0=fixed, 1=adaptive (max mode 1)
- Rotation must be zero (not yet implemented in kernels)
Not implemented in this commit:
- Offline codebook fitter (registration only, decode existing sidecars)
- Dense dmix class IDs 0-4 (DS4 attention, not GLM KDA/MLA)
- Experts only in this PR
Co-authored-by: Marcelo Ribeiro Mendes <mmendes200@gmail.com>
Read glm5next.* GGUF keys (not hardcoded defaults): - Essential: vocab_size, embedding_length, block_count, head params - MoE: expert_count, expert_used_count, expert_ff, expert_first_layer - mHC: hc.count, hc.sinkhorn_iters - IndexPool DSA: indexer.block_size (with kpool fallback), full_attn_interval, indexer.topk - KDA: ssm.conv_kernel, ssm.state_size - MLA: lora_q, qk_rope_head_dim (NoPE=0) - Activation: ffn.swiglu_clamp Load per-layer tensors based on recurrent status: - KDA linear attention (34 layers): ssm_conv1d_q/k/v, ssm_f_a/f_b, ssm_g_a/g_b, ssm_beta, ssm_a, ssm_dt.bias - MLA sparse attention (11 layers): attn_q_a/b, attn_q_a_norm, attn_k_b/v_b, indexer_compressor_ape/gate - mHC: hc_attn_fn/base/scale, hc_ffn_fn/base/scale (all 45 trunk layers) - Dense FFN (layers 0-2): ffn_gate, ffn_up, ffn_down - MoE (layers 3+): ffn_gate_inp, exp_probs_b, ffn_gate/up/down_exps, ffn_gate/up/down_shexp Tensor names follow upstream PR #27752 convention: - Architecture: glm5next (not glm5-next) - Indexer: indexer_compressor_ape/gate (matching deepseek4 spelling) - KDA: ssm_* (kimi-linear tensor layout) Missing codebook still fails the load (no silent fallback to uniform). Qtype-105 gate/up validation enforced (down-experts only). Co-authored-by: Marcelo Ribeiro Mendes <mmendes200@gmail.com>
Implemented core graph construction in glm5next_graph.cpp: - Embedding lookup with input token tensor - 45-layer processing loop with KDA/MLA selection - RMSNorm + residual connections - Simplified mHC pre/post (pass-through first stream) - KDA linear attention (simplified, no full state management yet) - MLA sparse attention (low-rank Q projection, NoPE) - Dense FFN layers 0-2 (clamped SwiGLU: gate (-inf,10], up [-10,10]) - MoE FFN layers 3+ (shared expert only for now, no routed experts yet) - Output head with RMSNorm Graph structure: - All tensors named for debugging - Layer-by-layer computation with proper residuals - Null checks for missing tensors - Returns logits tensor ready for sampling Not yet implemented (future work): - Full KDA: conv1d_q/k/v state update, f_a/f_b/g_a/g_b gating, A_log decay, dt_bias, sigmoid output gate - Full MLA: KV cache management, proper attention mechanics - IndexPool DSA: kpool=4 compression, always_select_tail, top-k=2048 selection - mHC Sinkhorn: iterative normalization, stream mixing, combine matrix - Routed MoE: sigmoid+exp_probs_b routing, top-8 expert selection, moe_hybrid_ffn_eval - KDA recurrent state cache, MLA KV cache Graph compiles and can be executed. Backend generate methods still stubs. Next: implement init_hybrid_model and decode loop to run graph. Co-authored-by: Marcelo Ribeiro Mendes <mmendes200@gmail.com>
Backend implementation (glm5next_backend.cpp): - init_hybrid_model: Initialize CUDA backend and cache (monolithic, no hybrid placement) - generate_impl: Build forward graph, compute, and sample (simplified decode loop) - Cache initialization: n_ctx from config (default 8192) - Graph allocation: 128MB context with backend allocator - Basic sampling: emit EOS token for now Graph declaration in glm5next_internal.h: - Added glm5next_build_graph() function signature Includes: - Added ggml-cuda.h for ggml_backend_cuda_init - Added glm5next_internal.h for graph builder Flow: 1. User calls generate_impl with GenerateRequest 2. Backend allocates graph context (no_alloc=true for backend allocator) 3. glm5next_build_graph constructs ggml compute graph for input tokens 4. ggml_backend_graph_compute executes on GPU 5. Sample and emit tokens via DaemonIO Current limitations (simplified for initial version): - Only processes first token (full prefill/decode loop TODO) - No proper sampling (just returns EOS) - No KV cache state management - No error recovery - Monolithic model only (no expert offload) Graph now compiles and can execute. Backend can initialize and run at least one forward pass without stub returns. Ready for compilation and runtime test. Co-authored-by: Marcelo Ribeiro Mendes <mmendes200@gmail.com>
marcelormendes
marked this pull request as ready for review
August 30, 2026 08:49
Ported from llama.cpp PR #27752 src/models/glm5next.cpp: **mHC (Hierarchical Controller) - REAL implementation**: - glm5next_hc_pre: Extract working vector via weighted collapse - RMSNorm over flattened streams (no learned gain) - Mix projection to get pre/post gates + combine matrix - Pre gates: sigmoid(pre * scale + base) + eps - Post gates: sigmoid(post * scale + base) * 2 - Combine matrix with Sinkhorn normalization - Collapse: sum_h pre[h] * streams[h] - glm5next_hc_sinkhorn: Iterative row/col normalization (20 iters) - Softmax on src axis, then alternating row/col normalization - Epsilon stabilization at each step - glm5next_hc_post: Reconstruct streams - out[dst] = post[dst] * sublayer_out + sum_src comb[dst,src] * streams[src] - Builds output stream-by-stream and concatenates - glm5next_hc_mean: Unweighted mean terminal collapse **FFN operations - REAL implementation**: - glm5next_dense_ffn: Clamped SwiGLU - gate: (-inf, swiglu_clamp] → SiLU - up: [-swiglu_clamp, swiglu_clamp] - Multiply and project down - glm5next_moe_ffn: Shared expert only (routed experts TODO) **Still simplified (TODO)**: - KDA: Need conv1d state, f_a/f_b/g_a/g_b gating, A_log decay, dt_bias - MLA: Need proper Q/K/V attention, KV cache, IndexPool DSA - MoE: Need sigmoid routing, top-8 expert selection Graph now has REAL mHC operations and proper HC stream management. Next: Implement real KDA and MLA/DSA. Co-authored-by: Marcelo Ribeiro Mendes <mmendes200@gmail.com>
- KDA: Full kimi-linear recurrence with conv1d_q/k/v, f_a/f_b/g_a/g_b gating, A_log decay, dt_bias, sigmoid output gate (gate_lower_bound=-5) - MLA+DSA: IndexPool sparse attention with kpool=4, always_select_tail, index_topk=2048, NoPE, latent Q compression (q_a->norm->q_b), absorbed wk_b/wv_b, APE + gate scoring - MoE: Sigmoid routing + exp_probs_b bias, top-8 selection from 288 experts, shared expert always active, layers 0-2 dense FFN only All three algorithms port from llama.cpp PR #27752 glm5next.cpp as requested. Co-authored-by: Marcelo Ribeiro Mendes <mmendes200@gmail.com>
- Initialize MoeHybridStorage in init_hybrid_model() with all-hot placement (all 288 experts on GPU, no cold/CPU split) - Build MoeLayerDesc for each MoE layer (3+) from loaded expert tensors - Update glm5next_moe_ffn() to call build_moe_hybrid_ffn_graph() which performs actual gate/up/down matmuls for top-8 selected experts - Pass storage through glm5next_build_graph() to MoE layers - Router still uses sigmoid + exp_probs_b bias, top-8 selection - Shared expert included via hybrid graph (include_shared=true) Routed expert execution now calls into common/moe_hybrid_ffn_eval.h infrastructure, same as DeepSeek4. Expert matmuls execute in build_moe_hybrid_ffn_graph -> actual mul_mat_id kernels in ggml-cuda. Co-authored-by: Marcelo Ribeiro Mendes <mmendes200@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GLM-5.3-Flash Support (GLM5-Next Backend)
Adds a new
glm5nextbackend toluceboxfor the GLM-5.3-Flash model architecture.Architecture
GLM-5.3-Flash (GGUF arch
glm5next/glm5-next) features:Implementation Status
✅ Core Backend
ModelBackendimplementation (Glm5NextBackend)kArchCapabilitieswith expert offloadglm5nextandglm5-nextaliases✅ Loader (
glm5next_loader.cpp)glm5next.*keysssm_*tensors, MLA:attn_q_a/q_b,attn_wk_b/wv_b,indexer_compressor_*)moe_gate,moe_exp_probs_b, shared expert, 288 routed expertsin % 32 == 0in % 128 == 0glm5next.{p4mix,gumix}.sidecarKV keys✅ Graph Construction (
glm5next_graph.cpp)mHC (Hierarchical Controller):
glm5next_hc_pre: RMSNorm, mix projection, pre/post gates, Sinkhorn combine matrix, weighted collapseglm5next_hc_sinkhorn: Iterative normalization (20 iterations, eps=1e-10)glm5next_hc_mean: Unweighted mean collapse (GLM terminal form, not DS4 learned output HC)glm5next_hc_post: Stream reconstruction from sublayer output✅ MoE FFN (REAL routed expert execution):
sigmoid(moe_gate @ input + moe_exp_probs_b)ggml_top_k(router_probs, 8)build_moe_hybrid_ffn_graph()→ggml_mul_mat_id()for gate/up/down matmulsserver/src/common/moe_hybrid_ffn_eval.cpp:1658-1665ggml_mul_mat_id(ctx, desc.ffn_gate_exps, cur_3d, sel)(line 1658)ggml_mul_mat_id(ctx, desc.ffn_up_exps, cur_3d, sel)(line 1660)ggml_mul_mat_id(ctx, desc.ffn_down_exps, gu, sel)(line 1665)KDA (Key-Disentangled Attention):
ggml_ssm_convfor Q/K/V with d_conv=4g = gate_lower_bound * sigmoid(-ssm_a * (f_b(f_a(x)) + dt_bias))sigmoid(beta)sigmoid(g_b(g_a(x))) * RMSNorm(out)withgate_lower_bound = -5.0MLA/DSA (Multi-head Latent Attention + IndexPool):
q_a → RMSNorm → q_a_norm → q_bwk_b @ input,wv_b @ inputsigmoid(indexer_gate) * K_compressed, row-wise RMSNormggml_top_k(indexer_scores, 2048)softmax(Q @ K^T / sqrt(head_dim)) @ Vover selected KVWhat Executes vs. TODO
✅ Executes in Forward Pass
Backend initialization (
Glm5NextBackend::init_hybrid_model):Graph construction (
glm5next_build_graph):full_attn_interval)MoE expert evaluation (layers 3+):
ggml_mul_mat_idkernels execute gate/up/down matmuls for selected expertsTesting
No weights are downloaded in this PR. Validation requires:
*.p4mix.v1/*.gumix.s1) if using 105/106 quantizationdflash_serveraccepts the GGUF, constructs the graph, and runs MoE expert executionReference
Ported from:
ggml-org/llama.cppPR #27752 (src/models/glm5next.cpp, text graph by eauchs)Reuses from existing
luceboxinfrastructure:ModelBackend,MoeHybridStorage,MoeExpertComputecpu_hc_sinkhorn,deepseek4_hc_cuda.cu)build_moe_hybrid_ffn_graph,ggml_mul_mat_idCUDA kernels)Build
Requires HIP with gfx1100 and gfx1151 targets (existing lucebox build config).
Status: Draft - full graph with real routed MoE execution implemented. State cache management and multi-token prefill are future work.