char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
1e6453457
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_llama4::build () { 4GGML_ASSERT (model .class_embedding != nullptr ); 5GGML_ASSERT (model .position_embeddings != nullptr ); 6 7const int n_pos = n_patches + 1 ;// +1 for [CLS] 8 9// 2D input positions 10ggml_tensor * pos_h = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos ); 11ggml_set_name (pos_h ,"pos_h" ); 12ggml_set_input (pos_h ); 13 14ggml_tensor * pos_w = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos ); 15ggml_set_name (pos_w ,"pos_w" ); 16ggml_set_input (pos_w ); 17 18ggml_tensor * inp = build_inp_raw (); 19 20// Llama4UnfoldConvolution 21 { 22ggml_tensor * kernel = ggml_reshape_4d (ctx0 ,model .patch_embeddings_0 , 23patch_size ,patch_size ,3 ,n_embd ); 24inp = ggml_im2col (ctx0 ,kernel ,inp ,patch_size ,patch_size ,0 ,0 ,1 ,1 , true,inp -> type ); 25inp = build_mm (model .patch_embeddings_0 ,inp ); 26inp = ggml_reshape_2d (ctx0 ,inp ,n_embd ,n_patches ); 27cb (inp ,"patch_conv" ,-1 ); 28 } 29 30// add CLS token 31inp = ggml_concat (ctx0 ,inp ,model .class_embedding ,1 ); 32 33// build ViT with 2D position embeddings 34auto add_pos = [& ](ggml_tensor * cur ,const clip_layer & ) { 35// first half is X axis and second half is Y axis 36// ref: https://github.com/huggingface/transformers/blob/40a493c7ed4f19f08eadb0639cf26d49bfa5e180/src/transformers/models/llama4/modeling_llama4.py#L1312 37// ref: https://github.com/Blaizzy/mlx-vlm/blob/a57156aa87b33cca6e5ee6cfc14dd4ef8f611be6/mlx_vlm/models/llama4/vision.py#L441 38return build_rope_2d (ctx0 ,cur ,pos_w ,pos_h ,hparams .rope_theta , false); 39 }; 40ggml_tensor * cur = build_vit ( 41inp ,n_pos , 42NORM_TYPE_NORMAL , 43hparams .ffn_op , 44model .position_embeddings , 45add_pos ); 46 47// remove CLS token 48cur = ggml_view_2d (ctx0 ,cur , 49n_embd ,n_patches , 50ggml_row_size (cur -> type ,n_embd ),0 ); 51 52// pixel shuffle 53// based on Llama4VisionPixelShuffleMLP 54// https://github.com/huggingface/transformers/blob/2932f318a20d9e54cc7aea052e040164d85de7d6/src/transformers/models/llama4/modeling_llama4.py#L1151 55 { 56const int scale_factor = model .hparams .n_merge ; 57const int bsz = 1 ;// batch size, always 1 for now since we don't support batching 58GGML_ASSERT (scale_factor > 0 ); 59GGML_ASSERT (n_patches_x == n_patches_y );// llama4 only supports square images 60cur = ggml_reshape_4d (ctx0 ,cur , 61n_embd * scale_factor , 62n_patches_x /scale_factor , 63n_patches_y , 64bsz ); 65cur = ggml_permute (ctx0 ,cur ,0 ,2 ,1 ,3 ); 66cur = ggml_cont_4d (ctx0 ,cur , 67n_embd * scale_factor * scale_factor , 68n_patches_x /scale_factor , 69n_patches_y /scale_factor , 70bsz ); 71//cur = ggml_permute(ctx0, cur, 0, 2, 1, 3); 72// flatten to 2D 73cur = ggml_cont_2d (ctx0 ,cur , 74n_embd * scale_factor * scale_factor , 75n_patches /scale_factor /scale_factor ); 76cb (cur ,"pixel_shuffle" ,-1 ); 77 } 78 79// based on Llama4VisionMLP2 (always uses GELU activation, no bias) 80 { 81cur = build_mm (model .mm_model_mlp_1_w ,cur ); 82cur = ggml_gelu (ctx0 ,cur ); 83cur = build_mm (model .mm_model_mlp_2_w ,cur ); 84cur = ggml_gelu (ctx0 ,cur ); 85cb (cur ,"adapter_mlp" ,-1 ); 86 } 87 88// Llama4MultiModalProjector 89cur = build_mm (model .mm_model_proj ,cur ); 90cb (cur ,"projected" ,-1 ); 91 92// build the graph 93ggml_build_forward_expand (gf ,cur ); 94 95return gf ; 96}