char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b21e4de74
char/aliceai-t5-moe
1#include "models.h" 2#include <cmath> 3 4ggml_cgraph * clip_graph_gemma4v::build () { 5ggml_tensor * inp_raw = build_inp_raw (); 6 7// patches = 2 * (patches - 0.5) 8// equivalent to: patches * 2 - 1 9inp_raw = ggml_scale_bias (ctx0 ,inp_raw ,2.0f ,-1.0f ); 10ggml_set_name (inp_raw ,"inp_raw_scaled" ); 11 12ggml_tensor * inp = ggml_conv_2d (ctx0 ,model .patch_embeddings_0 ,inp_raw ,patch_size ,patch_size ,0 ,0 ,1 ,1 ); 13inp = ggml_reshape_3d (ctx0 ,inp ,n_patches ,n_embd ,n_batch ); 14inp = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,inp )); 15ggml_set_name (inp ,"inp" ); 16// note: no patch bias 17 18ggml_tensor * pos_x = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_patches ); 19ggml_set_name (pos_x ,"pos_x" ); 20ggml_set_input (pos_x ); 21 22ggml_tensor * pos_y = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_patches ); 23ggml_set_name (pos_y ,"pos_y" ); 24ggml_set_input (pos_y ); 25 26 { 27const int64_t pos_size = model .position_embeddings -> ne [1 ]; 28const size_t nb1 = ggml_row_size (model .position_embeddings -> type ,n_embd ); 29 30// positional embeddings are stored as lookup tables (one for x, one for y) 31ggml_tensor * tbl_x = ggml_view_2d (ctx0 ,model .position_embeddings , 32n_embd ,pos_size ,nb1 ,0 ); 33ggml_tensor * tbl_y = ggml_view_2d (ctx0 ,model .position_embeddings , 34n_embd ,pos_size ,nb1 ,pos_size * nb1 ); 35 36// ggml_get_rows: [n_embd, n_patches] 37ggml_tensor * emb_x = ggml_get_rows (ctx0 ,tbl_x ,pos_x ); 38ggml_tensor * emb_y = ggml_get_rows (ctx0 ,tbl_y ,pos_y ); 39 40inp = ggml_add (ctx0 ,inp ,emb_x ); 41inp = ggml_add (ctx0 ,inp ,emb_y ); 42cb (inp ,"pos_embd" ,-1 ); 43 } 44 45// similar to build_rope_2d, but use neox ordering 46auto add_pos = [& ](ggml_tensor * cur ,const clip_layer & ) { 47const int64_t n_dim = cur -> ne [0 ]; 48 49// first half, dims [0, n_dim/2) 50cur = ggml_rope_ext ( 51ctx0 , 52cur , 53pos_x ,// positions 54nullptr ,// freq factors 55n_dim /2 ,// n_dims 56GGML_ROPE_TYPE_NEOX ,0 ,hparams .rope_theta , 571.0f ,0.0f ,1.0f ,0.0f ,0.0f 58 ); 59 60// second half, dims [n_dim/2, n_dim) 61cur = ggml_rope_ext ( 62ctx0 , 63cur , 64pos_y ,// positions 65nullptr ,// freq factors 66n_dim /2 ,// n_dims 67GGML_ROPE_TYPE_NEOX ,0 ,hparams .rope_theta , 681.0f ,0.0f ,1.0f ,0.0f ,0.0f 69 ); 70cur = ggml_rope_set_offset (cur ,n_dim /2 ); 71 72return cur ; 73 }; 74 75kq_scale = 1.0f ; 76ggml_tensor * cur = build_vit ( 77inp ,n_patches , 78NORM_TYPE_RMS , 79hparams .ffn_op , 80nullptr ,// pos embd is already handled above 81add_pos ); 82 83// Gemma4VisionPooler 84 { 85const int kernel_size = hparams .n_merge ; 86GGML_ASSERT (kernel_size > 0 ); 87 88// [n_embd, n_patches] -> [n_patches_x, n_patches_y, n_embd, n_batch] 89cur = ggml_cont_4d (ctx0 ,ggml_transpose (ctx0 ,cur ),n_patches_x ,n_patches_y ,n_embd ,n_batch ); 90cur = ggml_pool_2d (ctx0 ,cur ,GGML_OP_POOL_AVG , 91kernel_size ,kernel_size ,kernel_size ,kernel_size ,0 ,0 ); 92const int out_x = n_patches_x /kernel_size ; 93const int out_y = n_patches_y /kernel_size ; 94// [out_x, out_y, n_embd, n_batch] -> [n_embd, out_x * out_y, n_batch] 95cur = ggml_reshape_3d (ctx0 ,cur ,out_x * out_y ,n_embd ,n_batch ); 96cur = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,cur )); 97cur = ggml_scale (ctx0 ,cur ,sqrtf ((float )n_embd )); 98cb (cur ,"pooled" ,-1 ); 99 } 100 101// hidden_states = (hidden_states - self.std_bias) * self.std_scale 102if (model .std_bias && model .std_scale ) { 103cur = ggml_sub (ctx0 ,cur ,model .std_bias ); 104cur = ggml_mul (ctx0 ,cur ,model .std_scale ); 105cb (cur ,"std_scaled" ,-1 ); 106 } 107 108// Gemma4MultimodalEmbedder 109 { 110// embedding_pre_projection_norm 111cur = ggml_rms_norm (ctx0 ,cur ,hparams .eps ); 112cur = build_mm (model .mm_input_proj_w ,cur ); 113cb (cur ,"projected" ,-1 ); 114 } 115 116ggml_build_forward_expand (gf ,cur ); 117return gf ; 118} 119 120ggml_tensor * clip_graph_gemma4v::build_mm (ggml_tensor * w ,ggml_tensor * x )const { 121// Gemma4ClippableLinear 122 123auto it = model .clamp_info_map .find (w -> name ); 124if (it == model .clamp_info_map .end ()) { 125return ggml_mul_mat (ctx0 ,w ,x ); 126 }else { 127const auto & clamp_info = it -> second ; 128ggml_tensor * clamped = ggml_clamp (ctx0 ,x ,clamp_info .inp_min ,clamp_info .inp_max ); 129ggml_tensor * out = ggml_mul_mat (ctx0 ,w ,clamped ); 130out = ggml_clamp (ctx0 ,out ,clamp_info .out_min ,clamp_info .out_max ); 131return out ; 132 } 133}