common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Various helper functions and utilities
  2. #pragma once
  3. #include "llama.h"
  4. #include <string>
  5. #include <vector>
  6. #include <random>
  7. #include <thread>
  8. #include <unordered_map>
  9. //
  10. // CLI argument parsing
  11. //
  12. struct gpt_params {
  13. int32_t seed = -1; // RNG seed
  14. int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
  15. int32_t n_predict = 128; // new tokens to predict
  16. int32_t n_parts = -1; // amount of model parts (-1 = determine from model dimensions)
  17. int32_t n_ctx = 512; // context size
  18. int32_t n_batch = 512; // batch size for prompt processing (must be >=32 to use BLAS)
  19. int32_t n_keep = 0; // number of tokens to keep from initial prompt
  20. // sampling parameters
  21. std::unordered_map<llama_token, float> logit_bias; // logit bias for specific tokens
  22. int32_t top_k = 0; // <= 0 to use vocab size
  23. float top_p = 1.0f; // 1.0 = disabled
  24. float tfs_z = 1.0f; // 1.0 = disabled
  25. float typical_p = 1.0f; // 1.0 = disabled
  26. float temp = 1.0f; // 1.0 = disabled
  27. float repeat_penalty = 1.0f; // 1.0 = disabled
  28. int32_t repeat_last_n = -1; // last n tokens to penalize (0 = disable penalty, -1 = context size)
  29. float frequency_penalty = 0.0f; // 0.0 = disabled
  30. float presence_penalty = 0.0f; // 0.0 = disabled
  31. int mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
  32. float mirostat_tau = 5.0f; // target entropy
  33. float mirostat_eta = 0.1f; // learning rate
  34. std::string model = "models/lamma-7B/ggml-model.bin"; // model path
  35. std::string prompt = "";
  36. std::string path_session = ""; // path to file for saving/loading model eval state
  37. std::string input_prefix = ""; // string to prefix user inputs with
  38. std::vector<std::string> antiprompt; // string upon seeing which more user input is prompted
  39. std::string lora_adapter = ""; // lora adapter path
  40. std::string lora_base = ""; // base model path for the lora adapter
  41. bool memory_f16 = true; // use f16 instead of f32 for memory kv
  42. bool random_prompt = false; // do not randomize prompt if none provided
  43. bool use_color = false; // use color to distinguish generations and inputs
  44. bool interactive = false; // interactive mode
  45. bool embedding = false; // get only sentence embedding
  46. bool interactive_first = false; // wait for user input immediately
  47. bool instruct = false; // instruction mode (used for Alpaca models)
  48. bool penalize_nl = true; // consider newlines as a repeatable token
  49. bool perplexity = false; // compute perplexity over the prompt
  50. bool use_mmap = true; // use mmap for faster loads
  51. bool use_mlock = false; // use mlock to keep model in memory
  52. bool mem_test = false; // compute maximum memory usage
  53. bool verbose_prompt = false; // print prompt tokens before generation
  54. };
  55. bool gpt_params_parse(int argc, char ** argv, gpt_params & params);
  56. void gpt_print_usage(int argc, char ** argv, const gpt_params & params);
  57. std::string gpt_random_prompt(std::mt19937 & rng);
  58. //
  59. // Vocab utils
  60. //
  61. std::vector<llama_token> llama_tokenize(struct llama_context * ctx, const std::string & text, bool add_bos);
  62. //
  63. // Console utils
  64. //
  65. #define ANSI_COLOR_RED "\x1b[31m"
  66. #define ANSI_COLOR_GREEN "\x1b[32m"
  67. #define ANSI_COLOR_YELLOW "\x1b[33m"
  68. #define ANSI_COLOR_BLUE "\x1b[34m"
  69. #define ANSI_COLOR_MAGENTA "\x1b[35m"
  70. #define ANSI_COLOR_CYAN "\x1b[36m"
  71. #define ANSI_COLOR_RESET "\x1b[0m"
  72. #define ANSI_BOLD "\x1b[1m"
  73. enum console_color_t {
  74. CONSOLE_COLOR_DEFAULT=0,
  75. CONSOLE_COLOR_PROMPT,
  76. CONSOLE_COLOR_USER_INPUT
  77. };
  78. struct console_state {
  79. bool use_color = false;
  80. console_color_t color = CONSOLE_COLOR_DEFAULT;
  81. };
  82. void set_console_color(console_state & con_st, console_color_t color);
  83. #if defined (_WIN32)
  84. void win32_console_init(bool enable_color);
  85. void win32_utf8_encode(const std::wstring & wstr, std::string & str);
  86. #endif