okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3// HRM-Text: alternating low/high transformer stacks over the same token stream. 4// Reference: HrmTextModel in transformers, DFM Mimir 1B. 5 6void llama_model_hrm_text::load_arch_hparams (llama_model_loader & ml ) { 7ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 8ml .get_key (LLM_KV_EMBEDDING_SCALE ,hparams .f_embedding_scale , false); 9 10ml .get_key (LLM_KV_HRM_LAYERS_PER_STACK ,hparams .n_hrm_layers_per_stack ); 11ml .get_key (LLM_KV_HRM_H_CYCLES ,hparams .n_hrm_h_cycles ); 12ml .get_key (LLM_KV_HRM_L_CYCLES ,hparams .n_hrm_l_cycles ); 13 14// prefix-LM prefill is not implemented (causal attention only); kept for round-trip 15ml .get_key (LLM_KV_HRM_PREFIX_LM ,hparams .hrm_prefix_lm , false); 16 17GGML_ASSERT (hparams .n_hrm_layers_per_stack > 0 ); 18GGML_ASSERT (hparams .n_hrm_h_cycles > 0 ); 19GGML_ASSERT (hparams .n_hrm_l_cycles > 0 ); 20 21// the GGUF block count is the expanded cache-slot count 22const uint32_t n_slot = hparams .n_hrm_layers_per_stack * hparams .n_hrm_h_cycles * (hparams .n_hrm_l_cycles + 1 ); 23GGML_ASSERT (hparams .n_layer ()== n_slot ); 24 25switch (hparams .n_embd ) { 26case 1536 : 27type = LLM_TYPE_1B ; 28break ; 29default : 30type = LLM_TYPE_UNKNOWN ; 31 } 32} 33 34void llama_model_hrm_text::load_arch_tensors (llama_model_loader & ) { 35LLAMA_LOAD_LOCALS ; 36 37tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 38 39// output 40output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 41// if output is NULL, init from the input tok embed 42if (output == NULL ) { 43output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 44 } 45 46hrm_z_l_init = create_tensor (tn (LLM_TENSOR_HRM_Z_L_INIT ), {n_embd },0 ); 47 48const int lps = hparams .n_hrm_layers_per_stack ; 49 50// blocks [0, lps) hold the low stack, blocks [lps, 2*lps) hold the high stack. 51// the first low and high passes create the layers; later passes alias them. 52const int l_first = 0 ; 53const int h_first = hparams .n_hrm_l_cycles * lps ; 54 55for (int h = 0 ;h < (int )hparams .n_hrm_h_cycles ;++ h ) { 56for (int l = 0 ;l < (int )hparams .n_hrm_l_cycles + 1 ;++ l ) { 57const int slot_base = (h * (hparams .n_hrm_l_cycles + 1 )+ l )* lps ; 58const int blk_base = l == (int )hparams .n_hrm_l_cycles ?lps :0 ; 59 60if (h > 0 || (l > 0 && l < (int )hparams .n_hrm_l_cycles )) { 61// alias pass: these cache slots hold the same layers as the first passes 62const int src_base = l == (int )hparams .n_hrm_l_cycles ?h_first :l_first ; 63for (int il = 0 ;il < lps ;++ il ) { 64layers [slot_base + il ]= layers [src_base + il ]; 65 } 66continue ; 67 } 68 69for (int il = 0 ;il < lps ;++ il ) { 70auto & layer = layers [slot_base + il ]; 71const int bid = blk_base + il ; 72 73create_tensor_qkv (layer ,bid ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 74 75// sigmoid attention gate, applied to the attention output before o_proj 76layer .wqkv_gate = 77create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,bid ), {n_embd ,n_embd_head_k * n_head },0 ); 78layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,bid ), {n_embd_head_k * n_head ,n_embd },0 ); 79 80layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,bid ), {n_embd ,n_ff },0 ); 81layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,bid ), {n_ff ,n_embd },0 ); 82layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,bid ), {n_embd ,n_ff },0 ); 83 } 84 } 85 } 86} 87 88std::unique_ptr < llm_graph_context > llama_model_hrm_text::build_arch_graph (const llm_graph_params & params )const { 89return std::make_unique < graph > (* this ,params ); 90} 91 92// one stack invocation: lps pre-norm decoder layers, then the parameterless final norm 93ggml_tensor * llama_model_hrm_text::graph::build_stack (llm_graph_input_attn_kv * inp_attn , 94ggml_tensor * inp_pos , 95ggml_tensor * cur , 96int slot_base )const { 97const float kq_scale = 1.0f /sqrtf (float (n_embd_head_k )); 98 99const int lps = model .hparams .n_hrm_layers_per_stack ; 100 101for (int il = 0 ;il < lps ;++ il ) { 102const int s = slot_base + il ; 103const auto & layer = model .layers [s ]; 104 105ggml_tensor * inpSA = cur ; 106 107cur = build_norm (cur ,nullptr ,nullptr ,LLM_NORM_RMS ,s ); 108cb (cur ,"attn_norm" ,s ); 109 110// sigmoid-gated self-attention (same shape as qwen3next attention layers) 111 { 112ggml_tensor * gate = build_lora_mm (layer .wqkv_gate ,cur ); 113cb (gate ,"attn_gate_proj" ,s ); 114 115auto [Qcur ,Kcur ,Vcur ]= build_qkv (layer ,cur ,n_embd_head_k ,n_head ,n_head_kv ,s ); 116 117Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 118ext_factor ,attn_factor ,beta_fast ,beta_slow ); 119cb (Qcur ,"Qcur" ,s ); 120 121Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 122ext_factor ,attn_factor ,beta_fast ,beta_slow ); 123cb (Kcur ,"Kcur" ,s ); 124 125cur = build_attn (inp_attn , 126nullptr ,nullptr ,nullptr , 127Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,s ); 128cb (cur ,"attn_pregate" ,s ); 129 130gate = ggml_sigmoid (ctx0 ,gate ); 131cb (gate ,"attn_gate_sigmoid" ,s ); 132 133cur = ggml_mul (ctx0 ,cur ,gate ); 134cb (cur ,"attn_gated" ,s ); 135 136cur = build_lora_mm (layer .wo ,cur ,layer .wo_s ); 137cb (cur ,"attn_out" ,s ); 138 } 139 140cur = ggml_add (ctx0 ,cur ,inpSA ); 141cb (cur ,"attn_add" ,s ); 142 143inpSA = cur ; 144cur = build_norm (cur ,nullptr ,nullptr ,LLM_NORM_RMS ,s ); 145cb (cur ,"ffn_norm" ,s ); 146 147cur = build_ffn (cur , 148layer .ffn_up ,nullptr ,nullptr , 149layer .ffn_gate ,nullptr ,nullptr , 150layer .ffn_down ,nullptr ,nullptr , 151nullptr , 152LLM_FFN_SILU ,LLM_FFN_PAR ,s ); 153cb (cur ,"ffn_out" ,s ); 154 155cur = ggml_add (ctx0 ,cur ,inpSA ); 156cb (cur ,"ffn_add" ,s ); 157 158cur = build_cvec (cur ,s ); 159cb (cur ,"l_out" ,s ); 160 } 161 162cur = build_norm (cur ,nullptr ,nullptr ,LLM_NORM_RMS ,slot_base ); 163cb (cur ,"stack_norm" ,slot_base ); 164 165return cur ; 166} 167 168llama_model_hrm_text::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 169llm_graph_context (params ), 170model (model ) { 171ggml_tensor * cur ; 172 173// {n_embd, n_tokens}, scaled by hparams.f_embedding_scale inside build_inp_embd 174ggml_tensor * zH = build_inp_embd (model .tok_embd ); 175 176ggml_tensor * inp_pos = build_inp_pos (); 177 178auto * inp_attn = build_attn_inp_kv (); 179 180ggml_tensor * inp_out_ids = build_inp_out_ids (); 181 182// the learned low-cycle state is [n_embd]; binary ops broadcast it over [n_embd, n_tokens] 183ggml_tensor * zL = model .hrm_z_l_init ; 184 185for (uint32_t h = 0 ;h < model .hparams .n_hrm_h_cycles ;++ h ) { 186for (uint32_t l = 0 ;l < model .hparams .n_hrm_l_cycles ;++ l ) { 187const int slot_base = (h * (model .hparams .n_hrm_l_cycles + 1 )+ l )* model .hparams .n_hrm_layers_per_stack ; 188 189zL = build_stack (inp_attn ,inp_pos ,ggml_add (ctx0 ,zH ,zL ),slot_base ); 190 } 191 192const int slot_base = (h * (model .hparams .n_hrm_l_cycles + 1 )+ model .hparams .n_hrm_l_cycles )* 193model .hparams .n_hrm_layers_per_stack ; 194 195zH = build_stack (inp_attn ,inp_pos ,ggml_add (ctx0 ,zH ,zL ),slot_base ); 196 } 197 198cur = zH ; 199 200if (inp_out_ids ) { 201cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 202 } 203 204cb (cur ,"result_norm" ,-1 ); 205res -> t_embd = cur ; 206 207cur = build_lora_mm (model .output ,cur ,model .output_s ); 208 209cb (cur ,"result_output" ,-1 ); 210res -> t_logits = cur ; 211 212ggml_build_forward_expand (gf ,cur ); 213}