server-task.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #pragma once
  2. #include "common.h"
  3. #include "llama.h"
  4. #include <string>
  5. #include <unordered_set>
  6. #include <list>
  7. // TODO: prevent including the whole server-common.h as we only use server_tokens
  8. #include "server-common.h"
  9. using json = nlohmann::ordered_json;
  10. enum server_task_type {
  11. SERVER_TASK_TYPE_COMPLETION,
  12. SERVER_TASK_TYPE_EMBEDDING,
  13. SERVER_TASK_TYPE_RERANK,
  14. SERVER_TASK_TYPE_INFILL,
  15. SERVER_TASK_TYPE_CANCEL,
  16. SERVER_TASK_TYPE_NEXT_RESPONSE,
  17. SERVER_TASK_TYPE_METRICS,
  18. SERVER_TASK_TYPE_SLOT_SAVE,
  19. SERVER_TASK_TYPE_SLOT_RESTORE,
  20. SERVER_TASK_TYPE_SLOT_ERASE,
  21. SERVER_TASK_TYPE_SET_LORA,
  22. };
  23. // TODO: change this to more generic "response_format" to replace the "format_response_*" in server-common
  24. enum task_response_type {
  25. TASK_RESPONSE_TYPE_NONE, // llama.cpp native format
  26. TASK_RESPONSE_TYPE_OAI_CHAT,
  27. TASK_RESPONSE_TYPE_OAI_CMPL,
  28. TASK_RESPONSE_TYPE_OAI_EMBD,
  29. TASK_RESPONSE_TYPE_ANTHROPIC,
  30. };
  31. enum stop_type {
  32. STOP_TYPE_NONE,
  33. STOP_TYPE_EOS,
  34. STOP_TYPE_WORD,
  35. STOP_TYPE_LIMIT,
  36. };
  37. struct task_params {
  38. bool stream = true;
  39. bool include_usage = false;
  40. bool cache_prompt = true; // remember the prompt to avoid reprocessing all prompt
  41. bool return_tokens = false;
  42. bool return_progress = false;
  43. int32_t n_keep = 0; // number of tokens to keep from initial prompt
  44. int32_t n_discard = 0; // number of tokens after n_keep that may be discarded when shifting context, 0 defaults to half
  45. int32_t n_predict = -1; // new tokens to predict
  46. int32_t n_indent = 0; // minimum line indentation for the generated text in number of whitespace characters
  47. int64_t t_max_prompt_ms = -1; // TODO: implement
  48. int64_t t_max_predict_ms = -1; // if positive, limit the generation phase to this time limit
  49. std::vector<common_adapter_lora_info> lora;
  50. std::vector<std::string> antiprompt;
  51. std::vector<std::string> response_fields;
  52. bool timings_per_token = false;
  53. bool post_sampling_probs = false;
  54. struct common_params_sampling sampling;
  55. struct common_params_speculative speculative;
  56. // response formatting
  57. bool verbose = false;
  58. task_response_type res_type = TASK_RESPONSE_TYPE_NONE;
  59. std::string oaicompat_model;
  60. std::string oaicompat_cmpl_id;
  61. common_chat_syntax oaicompat_chat_syntax;
  62. // Embeddings
  63. int32_t embd_normalize = 2; // (-1=none, 0=max absolute int16, 1=taxicab, 2=Euclidean/L2, >2=p-norm)
  64. json format_logit_bias(const std::vector<llama_logit_bias> & logit_bias) const;
  65. json to_json(bool only_metrics = false) const;
  66. };
  67. struct server_task {
  68. int id = -1; // to be filled by server_queue
  69. int index = -1; // used when there are multiple prompts (batch request)
  70. // used by SERVER_TASK_TYPE_CANCEL
  71. int id_target = -1;
  72. int id_slot = -1;
  73. // used by SERVER_TASK_TYPE_INFERENCE
  74. task_params params;
  75. server_tokens tokens;
  76. server_task_type type;
  77. // used by SERVER_TASK_TYPE_SLOT_SAVE, SERVER_TASK_TYPE_SLOT_RESTORE, SERVER_TASK_TYPE_SLOT_ERASE
  78. struct slot_action {
  79. int slot_id;
  80. std::string filename;
  81. std::string filepath;
  82. };
  83. slot_action slot_action;
  84. // used by SERVER_TASK_TYPE_METRICS
  85. bool metrics_reset_bucket = false;
  86. // used by SERVER_TASK_TYPE_SET_LORA
  87. std::vector<common_adapter_lora_info> set_lora;
  88. server_task() = default;
  89. server_task(server_task_type type) : type(type) {}
  90. int32_t n_tokens() const {
  91. return tokens.size();
  92. }
  93. static task_params params_from_json_cmpl(
  94. const llama_context * ctx,
  95. const common_params & params_base,
  96. const json & data);
  97. // utility function
  98. static std::unordered_set<int> get_list_id(const std::vector<server_task> & tasks) {
  99. std::unordered_set<int> ids(tasks.size());
  100. for (size_t i = 0; i < tasks.size(); i++) {
  101. ids.insert(tasks[i].id);
  102. }
  103. return ids;
  104. }
  105. };
  106. struct result_timings {
  107. int32_t cache_n = -1;
  108. int32_t prompt_n = -1;
  109. double prompt_ms;
  110. double prompt_per_token_ms;
  111. double prompt_per_second;
  112. int32_t predicted_n = -1;
  113. double predicted_ms;
  114. double predicted_per_token_ms;
  115. double predicted_per_second;
  116. // Optional speculative metrics - only included when > 0
  117. int32_t draft_n = 0;
  118. int32_t draft_n_accepted = 0;
  119. json to_json() const;
  120. };
  121. struct result_prompt_progress {
  122. int32_t total = 0;
  123. int32_t cache = 0;
  124. int32_t processed = 0;
  125. int64_t time_ms = 0;
  126. json to_json() const;
  127. };
  128. struct server_task_result {
  129. int id = -1;
  130. int id_slot = -1;
  131. virtual bool is_error() {
  132. // only used by server_task_result_error
  133. return false;
  134. }
  135. virtual bool is_stop() {
  136. // only used by server_task_result_cmpl_*
  137. return true;
  138. }
  139. virtual int get_index() {
  140. return -1;
  141. }
  142. virtual json to_json() = 0;
  143. virtual ~server_task_result() = default;
  144. };
  145. // using shared_ptr for polymorphism of server_task_result
  146. using server_task_result_ptr = std::unique_ptr<server_task_result>;
  147. struct completion_token_output {
  148. llama_token tok;
  149. float prob;
  150. std::string text_to_send;
  151. struct prob_info {
  152. llama_token tok;
  153. std::string txt;
  154. float prob;
  155. };
  156. std::vector<prob_info> probs;
  157. json to_json(bool post_sampling_probs) const;
  158. static json probs_vector_to_json(const std::vector<completion_token_output> & probs, bool post_sampling_probs);
  159. static float logarithm(float x);
  160. static std::vector<unsigned char> str_to_bytes(const std::string & str);
  161. };
  162. struct server_task_result_cmpl_final : server_task_result {
  163. int index = 0;
  164. std::string content;
  165. llama_tokens tokens;
  166. bool stream;
  167. bool include_usage;
  168. result_timings timings;
  169. std::string prompt;
  170. bool truncated;
  171. int32_t n_decoded;
  172. int32_t n_prompt_tokens;
  173. int32_t n_tokens_cached;
  174. bool has_new_line;
  175. std::string stopping_word;
  176. stop_type stop = STOP_TYPE_NONE;
  177. bool post_sampling_probs;
  178. std::vector<completion_token_output> probs_output;
  179. std::vector<std::string> response_fields;
  180. task_params generation_params;
  181. // response formatting
  182. bool verbose = false;
  183. task_response_type res_type = TASK_RESPONSE_TYPE_NONE;
  184. std::string oaicompat_model;
  185. std::string oaicompat_cmpl_id;
  186. common_chat_msg oaicompat_msg;
  187. std::vector<common_chat_msg_diff> oaicompat_msg_diffs;
  188. virtual int get_index() override {
  189. return index;
  190. }
  191. virtual bool is_stop() override {
  192. return true; // in stream mode, final responses are considered stop
  193. }
  194. virtual json to_json() override;
  195. json to_json_non_oaicompat();
  196. json to_json_oaicompat();
  197. json to_json_oaicompat_chat();
  198. json to_json_oaicompat_chat_stream();
  199. json to_json_anthropic();
  200. json to_json_anthropic_stream();
  201. };
  202. struct server_task_result_cmpl_partial : server_task_result {
  203. int index = 0;
  204. std::string content;
  205. llama_tokens tokens;
  206. int32_t n_decoded;
  207. int32_t n_prompt_tokens;
  208. bool post_sampling_probs;
  209. bool is_progress = false;
  210. completion_token_output prob_output;
  211. result_timings timings;
  212. result_prompt_progress progress;
  213. // response formatting
  214. bool verbose = false;
  215. task_response_type res_type = TASK_RESPONSE_TYPE_NONE;
  216. std::string oaicompat_model;
  217. std::string oaicompat_cmpl_id;
  218. std::vector<common_chat_msg_diff> oaicompat_msg_diffs;
  219. virtual int get_index() override {
  220. return index;
  221. }
  222. virtual bool is_stop() override {
  223. return false; // in stream mode, partial responses are not considered stop
  224. }
  225. virtual json to_json() override;
  226. json to_json_non_oaicompat();
  227. json to_json_oaicompat();
  228. json to_json_oaicompat_chat();
  229. json to_json_anthropic();
  230. };
  231. struct server_task_result_embd : server_task_result {
  232. int index = 0;
  233. std::vector<std::vector<float>> embedding;
  234. int32_t n_tokens;
  235. // response formatting
  236. task_response_type res_type = TASK_RESPONSE_TYPE_NONE;
  237. virtual int get_index() override {
  238. return index;
  239. }
  240. virtual json to_json() override;
  241. json to_json_non_oaicompat();
  242. json to_json_oaicompat();
  243. };
  244. struct server_task_result_rerank : server_task_result {
  245. int index = 0;
  246. float score = -1e6;
  247. int32_t n_tokens;
  248. virtual int get_index() override {
  249. return index;
  250. }
  251. virtual json to_json() override;
  252. };
  253. struct server_task_result_error : server_task_result {
  254. int index = 0;
  255. error_type err_type = ERROR_TYPE_SERVER;
  256. std::string err_msg;
  257. // for ERROR_TYPE_EXCEED_CONTEXT_SIZE
  258. int32_t n_prompt_tokens = 0;
  259. int32_t n_ctx = 0;
  260. virtual bool is_error() override {
  261. return true;
  262. }
  263. virtual json to_json() override;
  264. };
  265. struct server_task_result_metrics : server_task_result {
  266. int n_idle_slots;
  267. int n_processing_slots;
  268. int n_tasks_deferred;
  269. int64_t t_start;
  270. // TODO: somehow reuse server_metrics in the future, instead of duplicating the fields
  271. uint64_t n_prompt_tokens_processed_total = 0;
  272. uint64_t t_prompt_processing_total = 0;
  273. uint64_t n_tokens_predicted_total = 0;
  274. uint64_t t_tokens_generation_total = 0;
  275. uint64_t n_tokens_max = 0;
  276. uint64_t n_prompt_tokens_processed = 0;
  277. uint64_t t_prompt_processing = 0;
  278. uint64_t n_tokens_predicted = 0;
  279. uint64_t t_tokens_generation = 0;
  280. uint64_t n_decode_total = 0;
  281. uint64_t n_busy_slots_total = 0;
  282. // while we can also use std::vector<server_slot> this requires copying the slot object which can be quite messy
  283. // therefore, we use json to temporarily store the slot.to_json() result
  284. json slots_data = json::array();
  285. virtual json to_json() override;
  286. };
  287. struct server_task_result_slot_save_load : server_task_result {
  288. std::string filename;
  289. bool is_save; // true = save, false = load
  290. size_t n_tokens;
  291. size_t n_bytes;
  292. double t_ms;
  293. virtual json to_json() override;
  294. };
  295. struct server_task_result_slot_erase : server_task_result {
  296. size_t n_erased;
  297. virtual json to_json() override;
  298. };
  299. struct server_task_result_apply_lora : server_task_result {
  300. virtual json to_json() override;
  301. };
  302. struct server_prompt_checkpoint {
  303. llama_pos pos_min;
  304. llama_pos pos_max;
  305. std::vector<uint8_t> data;
  306. size_t size() const {
  307. return data.size();
  308. }
  309. };
  310. struct server_prompt {
  311. server_tokens tokens;
  312. std::vector<uint8_t> data;
  313. std::list<server_prompt_checkpoint> checkpoints;
  314. size_t size() const {
  315. size_t res = data.size();
  316. for (const auto & checkpoint : checkpoints) {
  317. res += checkpoint.size();
  318. }
  319. return res;
  320. }
  321. int n_tokens() const {
  322. return tokens.size();
  323. }
  324. };
  325. struct server_prompt_cache {
  326. server_prompt_cache(int32_t limit_size_mib, size_t limit_tokens) {
  327. this->limit_size = 1024ull*1024ull*(limit_size_mib < 0 ? 0 : limit_size_mib);
  328. this->limit_tokens = limit_tokens;
  329. }
  330. std::list<server_prompt> states;
  331. // in bytes, 0 = no limit
  332. size_t limit_size = 0;
  333. // in tokens, 0 = no limit
  334. size_t limit_tokens = 0;
  335. size_t size() const;
  336. size_t n_tokens() const;
  337. server_prompt * alloc(const server_prompt & prompt, size_t state_size);
  338. bool load(server_prompt & prompt, const server_tokens & tokens_new, llama_context * ctx, int32_t id_slot);
  339. void update();
  340. };