char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
312cf0332
char/aliceai-t5-moe
1#pragma once 2 3#include "llama.h" 4 5#include "ggml-cpp.h" 6 7#include <string> 8#include <unordered_map> 9#include <vector> 10 11// TODO: pimpl 12 13// 14// llama_adapter_cvec 15// 16 17struct llama_adapter_cvec { 18ggml_tensor * tensor_for (int il )const ; 19 20ggml_tensor * apply_to (ggml_context * ctx ,ggml_tensor * cur ,int il )const ; 21 22bool apply ( 23const llama_model & model , 24const float * data , 25size_t len , 26int32_t n_embd , 27int32_t il_start , 28int32_t il_end ); 29 30private : 31bool init (const llama_model & model ); 32 33int32_t layer_start = -1 ; 34int32_t layer_end = -1 ; 35 36std ::vector < ggml_context_ptr > ctxs ; 37std ::vector < ggml_backend_buffer_ptr > bufs ; 38 39std ::vector < ggml_tensor *> tensors ;// per layer 40}; 41 42using llama_adapter_cvec_ptr = std ::shared_ptr < llama_adapter_cvec > ; 43 44// 45// llama_adapter_lora 46// 47 48struct llama_adapter_lora_weight { 49ggml_tensor * a = nullptr; 50ggml_tensor * b = nullptr; 51 52// get actual scale based on rank and alpha 53float get_scale (float alpha ,float adapter_scale )const { 54const float rank = (float )b -> ne [0 ]; 55const float scale = alpha ?adapter_scale * alpha /rank :adapter_scale ; 56return scale ; 57 } 58 59llama_adapter_lora_weight ()= default ; 60llama_adapter_lora_weight (ggml_tensor * a ,ggml_tensor * b ) :a (a ),b (b ) {} 61}; 62 63struct llama_adapter_lora { 64llama_model * model = nullptr; 65 66// map tensor name to lora_a_b 67std ::unordered_map < std ::string ,llama_adapter_lora_weight > ab_map ; 68 69std ::vector < ggml_context_ptr > ctxs ; 70std ::vector < ggml_backend_buffer_ptr > bufs ; 71 72float alpha ; 73 74// gguf metadata 75std ::unordered_map < std ::string ,std ::string > gguf_kv ; 76 77// activated lora (aLoRA) 78std ::vector < llama_token > alora_invocation_tokens ; 79 80explicit llama_adapter_lora (llama_model * model ) :model (model ) {} 81 ~llama_adapter_lora ()= default ; 82 83llama_adapter_lora_weight * get_weight (ggml_tensor * w ); 84 85uint32_t get_n_nodes ()const { 86return ab_map .size ()* 6u ;// a, b, scale, add, 2 x mul_mat 87 } 88}; 89 90using llama_adapter_loras = std ::unordered_map < llama_adapter_lora * ,float > ; 91using llama_adapter_loras_ptr = std ::unique_ptr < llama_adapter_loras > ;