char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
54ee5ee64
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_dots3note_a::build () { 4// inp_raw: [n_frames, n_mel, 1], one 60s chunk, mel frames not padded 5// the reference impl zero-masks conv inputs beyond the valid length at each stage; 6// running on exactly the valid frames with the convs' zero padding is equivalent 7ggml_tensor * inp = build_inp_raw (1 ); 8GGML_ASSERT (inp -> type == GGML_TYPE_F32 ); 9 10// 3x conv2d (k=3, s=2, p=1) + gelu 11 { 12auto conv_block = [& ](ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ) { 13x = ggml_conv_2d (ctx0 ,w ,x ,2 ,2 ,1 ,1 ,1 ,1 ); 14x = ggml_add (ctx0 ,x ,ggml_reshape_4d (ctx0 ,b ,1 ,1 ,x -> ne [2 ],1 )); 15return ggml_gelu_erf (ctx0 ,x ); 16 }; 17 18inp = conv_block (inp ,model .conv2d_1_w ,model .conv2d_1_b ); 19inp = conv_block (inp ,model .conv2d_2_w ,model .conv2d_2_b ); 20inp = conv_block (inp ,model .conv2d_3_w ,model .conv2d_3_b ); 21// inp: [OW=n_frames/8, OH=n_mel/8, OC=480, 1] 22cb (inp ,"after_conv_stem" ,-1 ); 23 } 24 25// [OW, OH, OC, 1] -> [OH*OC, OW], feature index f + OH*c (matches the reference permute+reshape) 26inp = ggml_cont (ctx0 ,ggml_permute (ctx0 ,inp ,2 ,0 ,1 ,3 )); 27inp = ggml_reshape_2d (ctx0 ,inp ,inp -> ne [0 ]* inp -> ne [1 ],inp -> ne [2 ]); 28 29// project to d_model (no bias) 30inp = ggml_mul_mat (ctx0 ,model .conv_out_w ,inp ); 31cb (inp ,"after_conv_out" ,-1 ); 32 33const int64_t n_pos = inp -> ne [1 ]; 34 35ggml_tensor * positions = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos ); 36ggml_set_name (positions ,"positions" ); 37ggml_set_input (positions ); 38 39// partial rotary: first half of each head, NEOX style 40auto add_pos = [& ](ggml_tensor * cur ,const clip_layer & ) { 41return ggml_rope_ext (ctx0 ,cur ,positions ,nullptr ,d_head /2 , 42GGML_ROPE_TYPE_NEOX ,0 ,hparams .rope_theta ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 43 }; 44 45ggml_tensor * cur = build_vit (inp ,n_pos , 46NORM_TYPE_RMS ,hparams .ffn_op , 47nullptr ,add_pos ); 48cb (cur ,"after_transformer" ,-1 ); 49 50// adapter: LayerNorm -> Linear -> GELU -> Linear 51cur = build_norm (cur ,model .mm_norm_pre_w ,model .mm_norm_pre_b ,NORM_TYPE_NORMAL ,1e-5 ,-1 ); 52cur = build_ffn (cur , 53model .mm_1_w ,model .mm_1_b , 54nullptr ,nullptr , 55model .mm_2_w ,model .mm_2_b , 56FFN_GELU_ERF ,-1 ); 57cb (cur ,"projected" ,-1 ); 58 59ggml_build_forward_expand (gf ,cur ); 60return gf ; 61}