char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
f29551215
char/aliceai-t5-moe
1#pragma once 2 3#include "server-http.h" 4#include "server-task.h" 5#include "server-queue.h" 6 7#include "json.h" 8 9#include <cstddef> 10#include <memory> 11#include <mutex> 12#include <set> 13 14struct server_context_impl ;// private implementation 15 16struct server_context_meta { 17std ::string build_info ; 18std ::string model_name ; 19std ::set < std ::string > model_aliases ; 20std ::set < std ::string > model_tags ; 21std ::string model_path ; 22bool has_mtmd ; 23bool has_inp_image ; 24bool has_inp_audio ; 25bool has_inp_video ; 26json json_ui_settings ; 27int slot_n_ctx ; 28enum llama_pooling_type pooling_type ; 29 30// chat params 31server_chat_params & chat_params ; 32std ::map < std ::string ,bool > chat_template_caps ; 33 34// tokens 35std ::string bos_token_str ; 36std ::string eos_token_str ; 37llama_token fim_pre_token ; 38llama_token fim_sub_token ; 39llama_token fim_mid_token ; 40llama_token fim_pad_token ; 41llama_token fim_rep_token ; 42llama_token fim_sep_token ; 43 44// sampling 45std ::vector < llama_logit_bias > logit_bias_eog ; 46 47// model meta 48enum llama_vocab_type model_vocab_type ; 49int32_t model_vocab_n_tokens ; 50int32_t model_n_ctx_train ; 51int32_t model_n_embd_inp ; 52uint64_t model_n_params ; 53uint64_t model_size ; 54std ::string model_ftype ; 55}; 56 57enum server_state { 58SERVER_STATE_DOWNLOADING , 59SERVER_STATE_LOADING , 60SERVER_STATE_READY , 61SERVER_STATE_SLEEPING , 62}; 63 64static std ::string server_state_to_str (server_state state ) { 65switch (state ) { 66case SERVER_STATE_DOWNLOADING :return "downloading" ; 67case SERVER_STATE_LOADING :return "loading" ; 68case SERVER_STATE_READY :return "ready" ; 69case SERVER_STATE_SLEEPING :return "sleeping" ; 70default :GGML_ASSERT (false&& "invalid server_state" ); 71 } 72} 73 74static server_state server_state_from_str (const std ::string & str ) { 75if (str == "downloading" )return SERVER_STATE_DOWNLOADING ; 76if (str == "loading" )return SERVER_STATE_LOADING ; 77if (str == "ready" )return SERVER_STATE_READY ; 78if (str == "sleeping" )return SERVER_STATE_SLEEPING ; 79GGML_ASSERT (false&& "invalid server_state string" ); 80} 81 82using server_state_callback_t = std ::function < void (server_state ,json /* payload */ )> ; 83 84struct server_context { 85std ::unique_ptr < server_context_impl > impl ; 86 87server_context (); 88~ server_context (); 89 90// load the model and initialize llama_context 91// returns true on success 92bool load_model (common_params & params ); 93 94// this function will block main thread until termination 95void start_loop (); 96 97// terminate main loop (will unblock start_loop) 98void terminate (); 99 100// get the underlaying llama_context, can return nullptr if sleeping 101// not thread-safe, should only be used from the main thread 102llama_context * get_llama_context () const ; 103 104// get a new response reader, used by CLI application 105server_response_reader get_response_reader (); 106 107// get server metadata (read-only), can only be called after load_model() 108// not thread-safe, should only be used from the main thread 109server_context_meta get_meta () const ; 110 111// note: must be set before load_model() is called 112void set_state_callback (server_state_callback_t callback ); 113}; 114 115 116// forward declarations 117struct server_res_generator; 118 119struct server_routes { 120server_routes ( const common_params & params , server_context & ctx_server ); 121 122void init_routes (); 123 124// note: this is not thread-safe and can only when ctx_http.is_ready is false 125void update_meta ( const server_context & ctx_server ) { 126this -> meta = std :: make_unique < server_context_meta > ( ctx_server . get_meta ()); 127} 128 129// handlers using lambda function, so that they can capture `this` without `std::bind` 130// they won't be called until ctx_http.is_ready is set to true 131server_http_context :: handler_t get_health ; 132server_http_context ::handler_t get_metrics ; 133server_http_context ::handler_t get_slots ; 134server_http_context ::handler_t post_slots ; 135server_http_context ::handler_t get_props ; 136server_http_context ::handler_t post_props ; 137server_http_context ::handler_t post_infill ; 138server_http_context ::handler_t post_completions ; 139server_http_context ::handler_t post_completions_oai ; 140server_http_context ::handler_t post_chat_completions ; 141server_http_context ::handler_t post_chat_completions_tok ; 142server_http_context ::handler_t post_control ; 143server_http_context ::handler_t post_responses_oai ; 144server_http_context ::handler_t post_responses_tok_oai ; 145server_http_context ::handler_t post_transcriptions_oai ; 146server_http_context ::handler_t post_anthropic_messages ; 147server_http_context ::handler_t post_anthropic_count_tokens ; 148server_http_context ::handler_t post_apply_template ; 149server_http_context ::handler_t get_models ; 150server_http_context ::handler_t post_tokenize ; 151server_http_context ::handler_t post_detokenize ; 152server_http_context ::handler_t post_embeddings ; 153server_http_context ::handler_t post_embeddings_oai ; 154server_http_context ::handler_t post_rerank ; 155server_http_context ::handler_t get_lora_adapters ; 156server_http_context ::handler_t post_lora_adapters ; 157 158// to be used in router mode 159json get_model_info () const ; 160 161private : 162std :: unique_ptr < server_res_generator > handle_completions_impl ( 163const server_http_req & req , 164server_task_type type , 165const json & data , 166const std :: vector < raw_buffer > & files , 167task_response_type res_type ); 168std :: unique_ptr < server_res_generator > handle_slots_save ( const server_http_req & req , int id_slot ); 169std :: unique_ptr < server_res_generator > handle_slots_restore ( const server_http_req & req , int id_slot ); 170std :: unique_ptr < server_res_generator > handle_slots_erase ( const server_http_req & , int id_slot ); 171std :: unique_ptr < server_res_generator > handle_embeddings_impl ( const server_http_req & req , task_response_type res_type ); 172std :: unique_ptr < server_res_generator > handle_count_tokens ( const llama_vocab * vocab , mtmd_context * mctx , const mtmd_helper_init_opt & init_opt , const server_http_req & req , task_response_type res_type ); 173 174// using unique_ptr to allow late initialization of const 175std :: unique_ptr < const server_context_meta > meta ; 176 177const common_params & params ; 178server_context_impl & ctx_server ; 179 180server_queue & queue_tasks ; 181server_response & queue_results ; 182std :: unique_ptr < server_res_generator > create_response ( bool bypass_sleep = false); 183 184// cached responses, to be used during sleep 185std :: mutex mutex_cache ; 186json cached_models = nullptr ; 187json cached_props = nullptr ; 188server_metrics cached_metrics ; 189// set when a scrape during sleep already reported the throughput buckets 190bool should_reset_buckets = false; 191// call right before sleep to update the cached responses 192void update_cached_responses ( bool is_sleeping ); 193};