common.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #include "common.h"
  2. #include <cassert>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <fstream>
  6. #include <string>
  7. #include <iterator>
  8. #include <algorithm>
  9. #include <sstream>
  10. #if defined(__APPLE__) && defined(__MACH__)
  11. #include <sys/types.h>
  12. #include <sys/sysctl.h>
  13. #endif
  14. #if defined (_WIN32)
  15. #include <fcntl.h>
  16. #include <io.h>
  17. #pragma comment(lib,"kernel32.lib")
  18. extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle);
  19. extern "C" __declspec(dllimport) int __stdcall GetConsoleMode(void* hConsoleHandle, unsigned long* lpMode);
  20. extern "C" __declspec(dllimport) int __stdcall SetConsoleMode(void* hConsoleHandle, unsigned long dwMode);
  21. extern "C" __declspec(dllimport) int __stdcall SetConsoleCP(unsigned int wCodePageID);
  22. extern "C" __declspec(dllimport) int __stdcall SetConsoleOutputCP(unsigned int wCodePageID);
  23. extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int CodePage, unsigned long dwFlags,
  24. const wchar_t * lpWideCharStr, int cchWideChar,
  25. char * lpMultiByteStr, int cbMultiByte,
  26. const char * lpDefaultChar, bool * lpUsedDefaultChar);
  27. #define CP_UTF8 65001
  28. #endif
  29. int32_t get_num_physical_cores() {
  30. #ifdef __linux__
  31. std::ifstream cpuinfo("/proc/cpuinfo");
  32. std::string line;
  33. while (std::getline(cpuinfo, line)) {
  34. std::size_t pos = line.find("cpu cores");
  35. if (pos != std::string::npos) {
  36. pos = line.find(": ", pos);
  37. if (pos != std::string::npos) {
  38. try {
  39. // Extract the number and return it
  40. return static_cast<int32_t>(std::stoul(line.substr(pos + 2)));
  41. } catch (const std::invalid_argument &) {
  42. // Ignore if we could not parse
  43. }
  44. }
  45. }
  46. }
  47. #elif defined(__APPLE__) && defined(__MACH__)
  48. int32_t num_physical_cores;
  49. size_t len = sizeof(num_physical_cores);
  50. int result = sysctlbyname("hw.perflevel0.physicalcpu", &num_physical_cores, &len, NULL, 0);
  51. if (result == 0) {
  52. return num_physical_cores;
  53. }
  54. result = sysctlbyname("hw.physicalcpu", &num_physical_cores, &len, NULL, 0);
  55. if (result == 0) {
  56. return num_physical_cores;
  57. }
  58. #elif defined(_WIN32)
  59. //TODO: Implement
  60. #endif
  61. unsigned int n_threads = std::thread::hardware_concurrency();
  62. return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
  63. }
  64. void process_escapes(std::string& input) {
  65. std::size_t input_len = input.length();
  66. std::size_t output_idx = 0;
  67. for (std::size_t input_idx = 0; input_idx < input_len; ++input_idx) {
  68. if (input[input_idx] == '\\' && input_idx + 1 < input_len) {
  69. switch (input[++input_idx]) {
  70. case 'n': input[output_idx++] = '\n'; break;
  71. case 'r': input[output_idx++] = '\r'; break;
  72. case 't': input[output_idx++] = '\t'; break;
  73. case '\'': input[output_idx++] = '\''; break;
  74. case '\"': input[output_idx++] = '\"'; break;
  75. case '\\': input[output_idx++] = '\\'; break;
  76. default: input[output_idx++] = '\\';
  77. input[output_idx++] = input[input_idx]; break;
  78. }
  79. } else {
  80. input[output_idx++] = input[input_idx];
  81. }
  82. }
  83. input.resize(output_idx);
  84. }
  85. bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
  86. bool invalid_param = false;
  87. bool escape_prompt = false;
  88. std::string arg;
  89. gpt_params default_params;
  90. for (int i = 1; i < argc; i++) {
  91. arg = argv[i];
  92. if (arg == "-s" || arg == "--seed") {
  93. if (++i >= argc) {
  94. invalid_param = true;
  95. break;
  96. }
  97. params.seed = std::stoi(argv[i]);
  98. } else if (arg == "-t" || arg == "--threads") {
  99. if (++i >= argc) {
  100. invalid_param = true;
  101. break;
  102. }
  103. params.n_threads = std::stoi(argv[i]);
  104. } else if (arg == "-p" || arg == "--prompt") {
  105. if (++i >= argc) {
  106. invalid_param = true;
  107. break;
  108. }
  109. params.prompt = argv[i];
  110. } else if (arg == "-e") {
  111. escape_prompt = true;
  112. } else if (arg == "--session") {
  113. if (++i >= argc) {
  114. invalid_param = true;
  115. break;
  116. }
  117. params.path_session = argv[i];
  118. } else if (arg == "-f" || arg == "--file") {
  119. if (++i >= argc) {
  120. invalid_param = true;
  121. break;
  122. }
  123. std::ifstream file(argv[i]);
  124. if (!file) {
  125. fprintf(stderr, "error: failed to open file '%s'\n", argv[i]);
  126. invalid_param = true;
  127. break;
  128. }
  129. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
  130. if (params.prompt.back() == '\n') {
  131. params.prompt.pop_back();
  132. }
  133. } else if (arg == "-n" || arg == "--n_predict") {
  134. if (++i >= argc) {
  135. invalid_param = true;
  136. break;
  137. }
  138. params.n_predict = std::stoi(argv[i]);
  139. } else if (arg == "--top_k") {
  140. if (++i >= argc) {
  141. invalid_param = true;
  142. break;
  143. }
  144. params.top_k = std::stoi(argv[i]);
  145. } else if (arg == "-c" || arg == "--ctx_size") {
  146. if (++i >= argc) {
  147. invalid_param = true;
  148. break;
  149. }
  150. params.n_ctx = std::stoi(argv[i]);
  151. } else if (arg == "--memory_f32") {
  152. params.memory_f16 = false;
  153. } else if (arg == "--top_p") {
  154. if (++i >= argc) {
  155. invalid_param = true;
  156. break;
  157. }
  158. params.top_p = std::stof(argv[i]);
  159. } else if (arg == "--temp") {
  160. if (++i >= argc) {
  161. invalid_param = true;
  162. break;
  163. }
  164. params.temp = std::stof(argv[i]);
  165. } else if (arg == "--tfs") {
  166. if (++i >= argc) {
  167. invalid_param = true;
  168. break;
  169. }
  170. params.tfs_z = std::stof(argv[i]);
  171. } else if (arg == "--typical") {
  172. if (++i >= argc) {
  173. invalid_param = true;
  174. break;
  175. }
  176. params.typical_p = std::stof(argv[i]);
  177. } else if (arg == "--repeat_last_n") {
  178. if (++i >= argc) {
  179. invalid_param = true;
  180. break;
  181. }
  182. params.repeat_last_n = std::stoi(argv[i]);
  183. } else if (arg == "--repeat_penalty") {
  184. if (++i >= argc) {
  185. invalid_param = true;
  186. break;
  187. }
  188. params.repeat_penalty = std::stof(argv[i]);
  189. } else if (arg == "--frequency_penalty") {
  190. if (++i >= argc) {
  191. invalid_param = true;
  192. break;
  193. }
  194. params.frequency_penalty = std::stof(argv[i]);
  195. } else if (arg == "--presence_penalty") {
  196. if (++i >= argc) {
  197. invalid_param = true;
  198. break;
  199. }
  200. params.presence_penalty = std::stof(argv[i]);
  201. } else if (arg == "--mirostat") {
  202. if (++i >= argc) {
  203. invalid_param = true;
  204. break;
  205. }
  206. params.mirostat = std::stoi(argv[i]);
  207. } else if (arg == "--mirostat_lr") {
  208. if (++i >= argc) {
  209. invalid_param = true;
  210. break;
  211. }
  212. params.mirostat_eta = std::stof(argv[i]);
  213. } else if (arg == "--mirostat_ent") {
  214. if (++i >= argc) {
  215. invalid_param = true;
  216. break;
  217. }
  218. params.mirostat_tau = std::stof(argv[i]);
  219. } else if (arg == "-b" || arg == "--batch_size") {
  220. if (++i >= argc) {
  221. invalid_param = true;
  222. break;
  223. }
  224. params.n_batch = std::stoi(argv[i]);
  225. params.n_batch = std::min(512, params.n_batch);
  226. } else if (arg == "--keep") {
  227. if (++i >= argc) {
  228. invalid_param = true;
  229. break;
  230. }
  231. params.n_keep = std::stoi(argv[i]);
  232. } else if (arg == "-m" || arg == "--model") {
  233. if (++i >= argc) {
  234. invalid_param = true;
  235. break;
  236. }
  237. params.model = argv[i];
  238. } else if (arg == "--lora") {
  239. if (++i >= argc) {
  240. invalid_param = true;
  241. break;
  242. }
  243. params.lora_adapter = argv[i];
  244. params.use_mmap = false;
  245. } else if (arg == "--lora-base") {
  246. if (++i >= argc) {
  247. invalid_param = true;
  248. break;
  249. }
  250. params.lora_base = argv[i];
  251. } else if (arg == "-i" || arg == "--interactive") {
  252. params.interactive = true;
  253. } else if (arg == "--embedding") {
  254. params.embedding = true;
  255. } else if (arg == "--interactive-first") {
  256. params.interactive_first = true;
  257. } else if (arg == "-ins" || arg == "--instruct") {
  258. params.instruct = true;
  259. } else if (arg == "--color") {
  260. params.use_color = true;
  261. } else if (arg == "--mlock") {
  262. params.use_mlock = true;
  263. } else if (arg == "--no-mmap") {
  264. params.use_mmap = false;
  265. } else if (arg == "--mtest") {
  266. params.mem_test = true;
  267. } else if (arg == "--verbose-prompt") {
  268. params.verbose_prompt = true;
  269. } else if (arg == "-r" || arg == "--reverse-prompt") {
  270. if (++i >= argc) {
  271. invalid_param = true;
  272. break;
  273. }
  274. params.antiprompt.push_back(argv[i]);
  275. } else if (arg == "--perplexity") {
  276. params.perplexity = true;
  277. } else if (arg == "--ignore-eos") {
  278. params.logit_bias[llama_token_eos()] = -INFINITY;
  279. } else if (arg == "--no-penalize-nl") {
  280. params.penalize_nl = false;
  281. } else if (arg == "-l" || arg == "--logit-bias") {
  282. if (++i >= argc) {
  283. invalid_param = true;
  284. break;
  285. }
  286. std::stringstream ss(argv[i]);
  287. llama_token key;
  288. char sign;
  289. std::string value_str;
  290. try {
  291. if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
  292. params.logit_bias[key] = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
  293. } else {
  294. throw std::exception();
  295. }
  296. } catch (const std::exception &e) {
  297. invalid_param = true;
  298. break;
  299. }
  300. } else if (arg == "--n_parts") {
  301. if (++i >= argc) {
  302. invalid_param = true;
  303. break;
  304. }
  305. params.n_parts = std::stoi(argv[i]);
  306. } else if (arg == "-h" || arg == "--help") {
  307. gpt_print_usage(argc, argv, default_params);
  308. exit(0);
  309. } else if (arg == "--random-prompt") {
  310. params.random_prompt = true;
  311. } else if (arg == "--in-prefix") {
  312. if (++i >= argc) {
  313. invalid_param = true;
  314. break;
  315. }
  316. params.input_prefix = argv[i];
  317. } else if (arg == "--in-suffix") {
  318. if (++i >= argc) {
  319. invalid_param = true;
  320. break;
  321. }
  322. params.input_suffix = argv[i];
  323. } else {
  324. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  325. gpt_print_usage(argc, argv, default_params);
  326. exit(1);
  327. }
  328. }
  329. if (invalid_param) {
  330. fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
  331. gpt_print_usage(argc, argv, default_params);
  332. exit(1);
  333. }
  334. if (escape_prompt) {
  335. process_escapes(params.prompt);
  336. }
  337. return true;
  338. }
  339. void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
  340. fprintf(stderr, "usage: %s [options]\n", argv[0]);
  341. fprintf(stderr, "\n");
  342. fprintf(stderr, "options:\n");
  343. fprintf(stderr, " -h, --help show this help message and exit\n");
  344. fprintf(stderr, " -i, --interactive run in interactive mode\n");
  345. fprintf(stderr, " --interactive-first run in interactive mode and wait for input right away\n");
  346. fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
  347. fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
  348. fprintf(stderr, " run in interactive mode and poll user input upon seeing PROMPT (can be\n");
  349. fprintf(stderr, " specified more than once for multiple prompts).\n");
  350. fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n");
  351. fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for < 0)\n");
  352. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  353. fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
  354. fprintf(stderr, " prompt to start generation with (default: empty)\n");
  355. fprintf(stderr, " -e process prompt escapes sequences (\\n, \\r, \\t, \\', \\\", \\\\)\n");
  356. fprintf(stderr, " --session FNAME file to cache model state in (may be large!) (default: none)\n");
  357. fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
  358. fprintf(stderr, " --in-prefix STRING string to prefix user inputs with (default: empty)\n");
  359. fprintf(stderr, " --in-suffix STRING string to suffix after user inputs with (default: empty)\n");
  360. fprintf(stderr, " -f FNAME, --file FNAME\n");
  361. fprintf(stderr, " prompt file to start generation.\n");
  362. fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d, -1 = infinity)\n", params.n_predict);
  363. fprintf(stderr, " --top_k N top-k sampling (default: %d, 0 = disabled)\n", params.top_k);
  364. fprintf(stderr, " --top_p N top-p sampling (default: %.1f, 1.0 = disabled)\n", (double)params.top_p);
  365. fprintf(stderr, " --tfs N tail free sampling, parameter z (default: %.1f, 1.0 = disabled)\n", (double)params.tfs_z);
  366. fprintf(stderr, " --typical N locally typical sampling, parameter p (default: %.1f, 1.0 = disabled)\n", (double)params.typical_p);
  367. fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d, 0 = disabled, -1 = ctx_size)\n", params.repeat_last_n);
  368. fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)\n", (double)params.repeat_penalty);
  369. fprintf(stderr, " --presence_penalty N repeat alpha presence penalty (default: %.1f, 0.0 = disabled)\n", (double)params.presence_penalty);
  370. fprintf(stderr, " --frequency_penalty N repeat alpha frequency penalty (default: %.1f, 0.0 = disabled)\n", (double)params.frequency_penalty);
  371. fprintf(stderr, " --mirostat N use Mirostat sampling.\n");
  372. fprintf(stderr, " Top K, Nucleus, Tail Free and Locally Typical samplers are ignored if used.\n");
  373. fprintf(stderr, " (default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)\n", params.mirostat);
  374. fprintf(stderr, " --mirostat_lr N Mirostat learning rate, parameter eta (default: %.1f)\n", (double)params.mirostat_eta);
  375. fprintf(stderr, " --mirostat_ent N Mirostat target entropy, parameter tau (default: %.1f)\n", (double)params.mirostat_tau);
  376. fprintf(stderr, " -l TOKEN_ID(+/-)BIAS, --logit-bias TOKEN_ID(+/-)BIAS\n");
  377. fprintf(stderr, " modifies the likelihood of token appearing in the completion,\n");
  378. fprintf(stderr, " i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n");
  379. fprintf(stderr, " or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'\n");
  380. fprintf(stderr, " -c N, --ctx_size N size of the prompt context (default: %d)\n", params.n_ctx);
  381. fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating (implies --logit-bias 2-inf)\n");
  382. fprintf(stderr, " --no-penalize-nl do not penalize newline token\n");
  383. fprintf(stderr, " --memory_f32 use f32 instead of f16 for memory key+value\n");
  384. fprintf(stderr, " --temp N temperature (default: %.1f)\n", (double)params.temp);
  385. fprintf(stderr, " --n_parts N number of model parts (default: -1 = determine from dimensions)\n");
  386. fprintf(stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n", params.n_batch);
  387. fprintf(stderr, " --perplexity compute perplexity over the prompt\n");
  388. fprintf(stderr, " --keep number of tokens to keep from the initial prompt (default: %d, -1 = all)\n", params.n_keep);
  389. if (llama_mlock_supported()) {
  390. fprintf(stderr, " --mlock force system to keep model in RAM rather than swapping or compressing\n");
  391. }
  392. if (llama_mmap_supported()) {
  393. fprintf(stderr, " --no-mmap do not memory-map model (slower load but may reduce pageouts if not using mlock)\n");
  394. }
  395. fprintf(stderr, " --mtest compute maximum memory usage\n");
  396. fprintf(stderr, " --verbose-prompt print prompt before generation\n");
  397. fprintf(stderr, " --lora FNAME apply LoRA adapter (implies --no-mmap)\n");
  398. fprintf(stderr, " --lora-base FNAME optional model to use as a base for the layers modified by the LoRA adapter\n");
  399. fprintf(stderr, " -m FNAME, --model FNAME\n");
  400. fprintf(stderr, " model path (default: %s)\n", params.model.c_str());
  401. fprintf(stderr, "\n");
  402. }
  403. std::string gpt_random_prompt(std::mt19937 & rng) {
  404. const int r = rng() % 10;
  405. switch (r) {
  406. case 0: return "So";
  407. case 1: return "Once upon a time";
  408. case 2: return "When";
  409. case 3: return "The";
  410. case 4: return "After";
  411. case 5: return "If";
  412. case 6: return "import";
  413. case 7: return "He";
  414. case 8: return "She";
  415. case 9: return "They";
  416. default: return "To";
  417. }
  418. return "The";
  419. }
  420. // TODO: not great allocating this every time
  421. std::vector<llama_token> llama_tokenize(struct llama_context * ctx, const std::string & text, bool add_bos) {
  422. // initialize to prompt numer of chars, since n_tokens <= n_prompt_chars
  423. std::vector<llama_token> res(text.size() + (int)add_bos);
  424. int n = llama_tokenize(ctx, text.c_str(), res.data(), res.size(), add_bos);
  425. assert(n >= 0);
  426. res.resize(n);
  427. return res;
  428. }
  429. struct llama_context * llama_init_from_gpt_params(const gpt_params & params) {
  430. auto lparams = llama_context_default_params();
  431. lparams.n_ctx = params.n_ctx;
  432. lparams.n_parts = params.n_parts;
  433. lparams.seed = params.seed;
  434. lparams.f16_kv = params.memory_f16;
  435. lparams.use_mmap = params.use_mmap;
  436. lparams.use_mlock = params.use_mlock;
  437. lparams.logits_all = params.perplexity;
  438. lparams.embedding = params.embedding;
  439. llama_context * lctx = llama_init_from_file(params.model.c_str(), lparams);
  440. if (lctx == NULL) {
  441. fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str());
  442. return NULL;
  443. }
  444. if (!params.lora_adapter.empty()) {
  445. int err = llama_apply_lora_from_file(lctx,
  446. params.lora_adapter.c_str(),
  447. params.lora_base.empty() ? NULL : params.lora_base.c_str(),
  448. params.n_threads);
  449. if (err != 0) {
  450. fprintf(stderr, "%s: error: failed to apply lora adapter\n", __func__);
  451. return NULL;
  452. }
  453. }
  454. return lctx;
  455. }
  456. /* Keep track of current color of output, and emit ANSI code if it changes. */
  457. void set_console_color(console_state & con_st, console_color_t color) {
  458. if (con_st.use_color && con_st.color != color) {
  459. switch(color) {
  460. case CONSOLE_COLOR_DEFAULT:
  461. printf(ANSI_COLOR_RESET);
  462. break;
  463. case CONSOLE_COLOR_PROMPT:
  464. printf(ANSI_COLOR_YELLOW);
  465. break;
  466. case CONSOLE_COLOR_USER_INPUT:
  467. printf(ANSI_BOLD ANSI_COLOR_GREEN);
  468. break;
  469. }
  470. con_st.color = color;
  471. }
  472. }
  473. #if defined (_WIN32)
  474. void win32_console_init(bool enable_color) {
  475. unsigned long dwMode = 0;
  476. void* hConOut = GetStdHandle((unsigned long)-11); // STD_OUTPUT_HANDLE (-11)
  477. if (!hConOut || hConOut == (void*)-1 || !GetConsoleMode(hConOut, &dwMode)) {
  478. hConOut = GetStdHandle((unsigned long)-12); // STD_ERROR_HANDLE (-12)
  479. if (hConOut && (hConOut == (void*)-1 || !GetConsoleMode(hConOut, &dwMode))) {
  480. hConOut = 0;
  481. }
  482. }
  483. if (hConOut) {
  484. // Enable ANSI colors on Windows 10+
  485. if (enable_color && !(dwMode & 0x4)) {
  486. SetConsoleMode(hConOut, dwMode | 0x4); // ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
  487. }
  488. // Set console output codepage to UTF8
  489. SetConsoleOutputCP(CP_UTF8);
  490. }
  491. void* hConIn = GetStdHandle((unsigned long)-10); // STD_INPUT_HANDLE (-10)
  492. if (hConIn && hConIn != (void*)-1 && GetConsoleMode(hConIn, &dwMode)) {
  493. // Set console input codepage to UTF16
  494. _setmode(_fileno(stdin), _O_WTEXT);
  495. }
  496. }
  497. // Convert a wide Unicode string to an UTF8 string
  498. void win32_utf8_encode(const std::wstring & wstr, std::string & str) {
  499. int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
  500. std::string strTo(size_needed, 0);
  501. WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
  502. str = strTo;
  503. }
  504. #endif