char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b21e4de74
char/aliceai-t5-moe
1#pragma once 2 3#include "ggml.h" 4#include "ggml-cpp.h" 5#include "clip.h" 6#include "clip-impl.h" 7#include "clip-model.h" 8 9#include <vector> 10#include <functional> 11 12#define DEFAULT_INTERPOLATION_MODE (GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ANTIALIAS) 13 14struct build_vit_opts { 15ggml_tensor * attn_mask = nullptr; 16// TODO @ngxson : merge attn_mask and attn_mask_layers into one call 17std ::vector < ggml_tensor *> attn_mask_layers ;// one per layer 18 19// hook at layer output embeddings 20std ::function < void (ggml_tensor * cur ,int il )> callback_layer_out = nullptr; 21 22// whether to skip the automatic post-layernorm (model.post_ln_w) applied at the end 23bool skip_post_ln = false; 24}; 25 26struct clip_graph { 27const clip_model & model ; 28const clip_hparams & hparams ; 29projector_type proj_type ; 30 31const clip_image_f32 & img ;// for backward compat 32const clip_image_f32_batch * img_batch = nullptr; 33 34const int patch_size ; 35const int n_patches_x ; 36const int n_patches_y ; 37const int n_patches ; 38const int n_embd ; 39const int n_head ; 40const int n_head_kv ; 41const int d_head ; 42const int n_layer ; 43const int n_mmproj_embd ; 44const float eps ; 45float kq_scale ;// TODO: maybe move this to hparams 46const clip_flash_attn_type flash_attn_type ; 47 48// TODO [QWEN_VIDEO]: improve this in the future 49int n_batch = 1 ; 50 51ggml_context_ptr ctx0_ptr ; 52ggml_context * ctx0 ; 53ggml_cgraph * gf ; 54 55clip_graph (clip_ctx * ctx ,const clip_image_f32 & img ); 56 57// build sub-graph, reuse buf from parent 58clip_graph (const clip_graph & parent ); 59 60virtual ~clip_graph ()= default ; 61virtual ggml_cgraph * build ()= 0 ; 62 63// wrapper around ggml_mul_mat, allow hooking (e.g. LoRA, clamping) depending on the model 64// tensor w should be the weight matrix, and tensor x should be the input 65virtual ggml_tensor * build_mm (ggml_tensor * w ,ggml_tensor * x )const ; 66// TODO: build_mm(w, b, x) to support bias 67 68virtual bool support_batch ()const { 69return false; 70 } 71 72// 73// utility functions 74// 75void cb (ggml_tensor * cur0 ,const char * name ,int il )const ; 76 77const clip_image_f32 & get_img (size_t idx )const { 78GGML_ASSERT (img_batch ); 79GGML_ASSERT (idx < img_batch -> entries .size ()); 80return img_batch -> entries [idx ]; 81 } 82 83// siglip2 naflex 84ggml_tensor * resize_position_embeddings (uint32_t interpolation_mode = DEFAULT_INTERPOLATION_MODE ); 85 86// build vision transformer (ViT) cgraph 87// this function should cover most of the models 88// if your model has specific features, you should probably duplicate this function 89ggml_tensor * build_vit ( 90ggml_tensor * inp , 91int64_t n_pos , 92norm_type norm_t , 93ffn_op_type ffn_t , 94ggml_tensor * learned_pos_embd , 95std ::function < ggml_tensor * (ggml_tensor * ,const clip_layer & )> add_pos , 96const build_vit_opts & opts = {}); 97 98// build the input after conv2d (inp_raw --> patches) 99// returns tensor with shape [n_embd, n_patches] 100ggml_tensor * build_inp (); 101 102ggml_tensor * build_inp_raw (int channels = 3 ); 103 104ggml_tensor * build_norm ( 105ggml_tensor * cur , 106ggml_tensor * mw , 107ggml_tensor * mb , 108norm_type type , 109float norm_eps , 110int il )const ; 111 112ggml_tensor * build_ffn ( 113ggml_tensor * cur , 114ggml_tensor * up , 115ggml_tensor * up_b , 116ggml_tensor * gate , 117ggml_tensor * gate_b , 118ggml_tensor * down , 119ggml_tensor * down_b , 120ffn_op_type type_op , 121int il )const ; 122 123ggml_tensor * build_moe_ffn ( 124ggml_tensor * cur , 125const clip_layer & layer , 126ffn_op_type type_op , 127int il )const ; 128 129ggml_tensor * build_attn ( 130ggml_tensor * wo , 131ggml_tensor * wo_b , 132ggml_tensor * q_cur , 133ggml_tensor * k_cur , 134ggml_tensor * v_cur , 135ggml_tensor * kq_mask , 136float kq_scale , 137int il , 138ggml_tensor * sinks = nullptr )const ; 139 140// implementation of the 2D RoPE using two ggml_rope_ext calls 141// 142// unlike GGML_ROPE_TYPE_VISION which forces NEOX ordering, this rotates adjacent pairs (normal ordering) 143// 144// example: 145// given a single head with size = 8 --> [00000000] 146// dims [0, 4) rotate with pos_a, dims [4, 8) rotate with pos_b --> [aaaabbbb] 147// interleave_freq = false --> both halves use the same inv_freq set (like GGML_ROPE_TYPE_VISION) 148// interleave_freq = true --> first half uses even inv_freq, second half uses odd inv_freq (used by pixtral) 149ggml_tensor * build_rope_2d ( 150ggml_context * ctx0 , 151ggml_tensor * cur , 152ggml_tensor * pos_a ,// first half 153ggml_tensor * pos_b ,// second half 154const float freq_base , 155const bool interleave_freq 156 ); 157 158// aka pixel_shuffle / pixel_unshuffle / patch_merger (Kimi-VL) 159// support dynamic resolution 160ggml_tensor * build_patch_merge_permute (ggml_tensor * cur ,int scale_factor ); 161 162// Generic function to stack frames for audio processing 163// Abstracts out the StackAudioFrames logic used by ultravox 164ggml_tensor * build_stack (ggml_tensor * cur ,int32_t stack_factor ,int32_t n_embed ); 165};