char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
1cbfd1988
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_mimo_audio::build () { 4ggml_tensor * inp = build_inp_raw (1 );// [n_frames, n_mel, 1] 5 6ggml_tensor * cur = ggml_conv_1d_ph (ctx0 ,model .conv1d_1_w ,inp ,1 ,1 ); 7cur = ggml_add (ctx0 ,cur ,model .conv1d_1_b ); 8cur = ggml_gelu_erf (ctx0 ,cur ); 9 10cur = ggml_conv_1d_ph (ctx0 ,model .conv1d_2_w ,cur ,2 ,1 ); 11cur = ggml_add (ctx0 ,cur ,model .conv1d_2_b ); 12cur = ggml_gelu_erf (ctx0 ,cur ); 13 14ggml_tensor * inpL = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,cur ));// [n_embd, n_pos] 15const int64_t n_pos = inpL -> ne [1 ]; 16cb (inpL ,"after_conv1d" ,-1 ); 17 18GGML_ASSERT ((int )hparams .wa_pattern_mode .size ()== n_layer ); 19 20ggml_tensor * inp_pos = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos ); 21ggml_set_name (inp_pos ,"mimo_audio_positions" ); 22ggml_set_input (inp_pos ); 23 24ggml_tensor * full_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_pos ,n_pos ); 25ggml_set_name (full_mask ,"mimo_audio_full_mask" ); 26ggml_set_input (full_mask ); 27 28ggml_tensor * window_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_pos ,n_pos ); 29ggml_set_name (window_mask ,"mimo_audio_window_mask" ); 30ggml_set_input (window_mask ); 31 32build_vit_opts opts ; 33opts .attn_mask_layers .resize (n_layer ); 34for (int il = 0 ;il < n_layer ;il ++ ) { 35opts .attn_mask_layers [il ]= hparams .wa_pattern_mode [il ]== -1 ?full_mask :window_mask ; 36 } 37// the skip connection below must be added before the post-transformer norm, 38// so build_vit must not apply that norm itself 39opts .skip_post_ln = true; 40 41// encoder_skip_layer_id=3 (1-indexed) -> capture output of layer index 2 42const int skip_capture_il = 2 ; 43GGML_ASSERT (n_layer > skip_capture_il ); 44ggml_tensor * skip_hidden = nullptr ; 45opts .callback_layer_out = [& ](ggml_tensor * layer_cur ,int il ) { 46if (il == skip_capture_il ) { 47skip_hidden = layer_cur ; 48 } 49 }; 50 51auto add_pos = [& ](ggml_tensor * x ,const clip_layer & ) { 52return ggml_rope_ext (ctx0 ,x ,inp_pos ,nullptr ,d_head , 53GGML_ROPE_TYPE_NEOX ,0 ,hparams .rope_theta ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 54 }; 55 56inpL = build_vit (inpL ,n_pos ,NORM_TYPE_NORMAL ,hparams .ffn_op ,nullptr ,add_pos ,opts ); 57inpL = ggml_reshape_2d (ctx0 ,inpL ,n_embd ,n_pos );// build_vit restores a (size-1) batch dim 58 59GGML_ASSERT (skip_hidden != nullptr ); 60inpL = ggml_add (ctx0 ,inpL ,skip_hidden ); 61 62inpL = build_norm (inpL ,model .post_ln_w ,model .post_ln_b ,NORM_TYPE_NORMAL ,eps ,-1 ); 63cb (inpL ,"after_transformer" ,-1 ); 64 65// downsample: strided conv (no bias) + gelu + layernorm 66 { 67ggml_tensor * ds = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,inpL ));// [n_pos, n_embd] 68ds = ggml_conv_1d (ctx0 ,model .downsample_conv_w ,ds ,2 ,0 ,1 ); 69ds = ggml_gelu_erf (ctx0 ,ds ); 70ds = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,ds ));// [n_embd, n_pos/2] 71ds = build_norm (ds ,model .downsample_norm_w ,model .downsample_norm_b ,NORM_TYPE_NORMAL ,eps ,-1 ); 72inpL = ds ; 73 } 74cb (inpL ,"after_downsample" ,-1 ); 75 76// RVQ quantize: codebook ne=[dim, max_bins, n_q] 77// quantize input vector to codes (type=I32) 78 std::vector < ggml_tensor *> codes ; 79 { 80GGML_ASSERT (model .rvq_codebook != nullptr ); 81const int64_t dim = model .rvq_codebook -> ne [0 ]; 82GGML_ASSERT (dim == inpL -> ne [0 ]); 83GGML_ASSERT ((int64_t )hparams .rvq_codebook_size .size ()== model .rvq_codebook -> ne [2 ]); 84 85ggml_tensor * residual = inpL ;// [dim, n_pos_ds] 86 87for (size_t q = 0 ;q < hparams .rvq_codebook_size .size ();q ++ ) { 88const int64_t bins = hparams .rvq_codebook_size [q ]; 89ggml_tensor * codebook_q = ggml_view_2d (ctx0 ,model .rvq_codebook ,dim ,bins , 90model .rvq_codebook -> nb [1 ],q * model .rvq_codebook -> nb [2 ]); 91codebook_q = ggml_cont (ctx0 ,codebook_q ); 92 93ggml_tensor * codebook_norm = ggml_sum_rows (ctx0 ,ggml_sqr (ctx0 ,codebook_q ));// [1, bins] 94codebook_norm = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,codebook_norm ));// [bins, 1] 95 96ggml_tensor * dot = ggml_mul_mat (ctx0 ,codebook_q ,residual );// [bins, n_pos_ds] 97ggml_tensor * scores = ggml_sub (ctx0 ,ggml_scale (ctx0 ,dot ,2.0f ),codebook_norm ); 98 99ggml_tensor * idx = ggml_argmax (ctx0 ,scores );// [n_pos_ds] 100codes .push_back (idx ); 101 102ggml_tensor * quant = ggml_get_rows (ctx0 ,codebook_q ,idx );// [dim, n_pos_ds] 103residual = ggml_sub (ctx0 ,residual ,quant ); 104cb (idx ,"rvq_code" , (int )q ); 105 } 106 } 107 108// convert codes to LLM embeddings 109ggml_tensor * code_embd_sum = nullptr ; 110 { 111GGML_ASSERT (model .mm_a_code_embd != nullptr ); 112const int64_t dim = model .mm_a_code_embd -> ne [0 ]; 113const int64_t vocab = model .mm_a_code_embd -> ne [1 ]; 114GGML_ASSERT ((int64_t )codes .size ()== model .mm_a_code_embd -> ne [2 ]); 115GGML_ASSERT (dim == inpL -> ne [0 ]); 116 117for (size_t i = 0 ;i < codes .size ();i ++ ) { 118ggml_tensor * table_i = ggml_view_2d (ctx0 ,model .mm_a_code_embd ,dim ,vocab , 119model .mm_a_code_embd -> nb [1 ],i * model .mm_a_code_embd -> nb [2 ]); 120table_i = ggml_cont (ctx0 ,table_i ); 121 122ggml_tensor * embd_i = ggml_get_rows (ctx0 ,table_i ,codes [i ]);// [dim, n_pos_ds] 123code_embd_sum = code_embd_sum ?ggml_add (ctx0 ,code_embd_sum ,embd_i ) :embd_i ; 124 } 125cb (code_embd_sum ,"code_embd_sum" ,-1 ); 126 } 127 128// input_local_transformer 129// groups of `group_size` consecutive downsampled frames are processed together, attending only within their own group. 130// Implemented as a block-diagonal mask + in-group-repeating positions 131// (rather than a real batch dim) - same technique as the encoder's masks above, and as gemma4a's / deepseekocr2's chunked attention. 132 133// note: hand-rolled here instead of build_vit() because this is a second, independent layer stack 134// (own layer array/count, RMSNorm instead of LN, SiLU FFN, own RoPE theta) 135 136ggml_tensor * projected ; 137 { 138const int group_size = hparams .audio_local_group_size ; 139GGML_ASSERT (group_size > 0 ); 140const int64_t n_pos_ds = code_embd_sum -> ne [1 ]; 141const int64_t n_groups = (n_pos_ds + group_size - 1 ) /group_size ; 142const int64_t n_padded = n_groups * group_size ; 143 144ggml_tensor * cur_local = code_embd_sum ; 145if (n_padded != n_pos_ds ) { 146cur_local = ggml_pad (ctx0 ,cur_local ,0 , (int ) (n_padded - n_pos_ds ),0 ,0 ); 147 } 148 149ggml_tensor * local_pos = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_padded ); 150ggml_set_name (local_pos ,"mimo_audio_local_positions" ); 151ggml_set_input (local_pos ); 152 153ggml_tensor * local_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_padded ,n_padded ); 154ggml_set_name (local_mask ,"mimo_audio_local_mask" ); 155ggml_set_input (local_mask ); 156 157const float local_rope_theta = 640000.0f ;// audio_config.rope_theta (differs from the encoder's) 158auto apply_local_rope = [& ](ggml_tensor * x ) { 159return ggml_rope_ext (ctx0 ,x ,local_pos ,nullptr ,d_head , 160GGML_ROPE_TYPE_NEOX ,0 ,local_rope_theta ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 161 }; 162 163for (int il = 0 ;il < hparams .audio_local_n_layer ;il ++ ) { 164auto & layer = model .mm_a_local_layers [il ]; 165 166ggml_tensor * attn_in = build_norm (cur_local ,layer .ln_1_w ,nullptr ,NORM_TYPE_RMS ,eps ,il ); 167 168ggml_tensor * Qcur = build_mm (layer .q_w ,attn_in ); 169if (layer .q_b ) { 170Qcur = ggml_add (ctx0 ,Qcur ,layer .q_b ); 171 } 172ggml_tensor * Kcur = build_mm (layer .k_w ,attn_in ); 173if (layer .k_b ) { 174Kcur = ggml_add (ctx0 ,Kcur ,layer .k_b ); 175 } 176ggml_tensor * Vcur = build_mm (layer .v_w ,attn_in ); 177if (layer .v_b ) { 178Vcur = ggml_add (ctx0 ,Vcur ,layer .v_b ); 179 } 180 181Qcur = ggml_reshape_3d (ctx0 ,Qcur ,d_head ,n_head ,n_padded ); 182Kcur = ggml_reshape_3d (ctx0 ,Kcur ,d_head ,n_head ,n_padded ); 183Vcur = ggml_reshape_3d (ctx0 ,Vcur ,d_head ,n_head ,n_padded ); 184 185Qcur = apply_local_rope (Qcur ); 186Kcur = apply_local_rope (Kcur ); 187 188ggml_tensor * attn_out = build_attn (layer .o_w ,nullptr ,Qcur ,Kcur ,Vcur ,local_mask ,kq_scale ,il ); 189cur_local = ggml_add (ctx0 ,cur_local ,attn_out ); 190 191ggml_tensor * ffn_in = build_norm (cur_local ,layer .ln_2_w ,nullptr ,NORM_TYPE_RMS ,eps ,il ); 192ggml_tensor * ffn_out = build_ffn (ffn_in , 193layer .ff_up_w ,nullptr , 194layer .ff_gate_w ,nullptr , 195layer .ff_down_w ,nullptr , 196FFN_SILU ,il ); 197cur_local = ggml_add (ctx0 ,cur_local ,ffn_out ); 198 } 199 200cur_local = build_norm (cur_local ,model .mm_a_local_norm_w ,nullptr ,NORM_TYPE_RMS ,eps ,-1 ); 201cb (cur_local ,"after_local_transformer" ,-1 ); 202 203// flatten each group of `group_size` frames into one (group_size*n_embd)-dim vector 204// (matching AudioProjection's flattened input) 205ggml_tensor * grouped = ggml_reshape_2d (ctx0 ,cur_local ,n_embd * group_size ,n_groups ); 206 207// AudioProjection: Linear (no bias) -> GELU -> Linear (no bias) 208projected = build_ffn (grouped , 209model .mm_1_w ,nullptr , 210nullptr ,nullptr , 211model .mm_2_w ,nullptr , 212FFN_GELU_ERF ,-1 ); 213cb (projected ,"after_projection" ,-1 ); 214 } 215 216ggml_build_forward_expand (gf ,projected ); 217return gf ; 218}