utils.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. #include "utils.h"
  2. #include <cassert>
  3. #include <cstring>
  4. #include <fstream>
  5. #include <regex>
  6. #include <iostream>
  7. #include <iterator>
  8. #include <queue>
  9. #include <string>
  10. #include <math.h>
  11. #if defined(_MSC_VER) || defined(__MINGW32__)
  12. #include <malloc.h> // using malloc.h with MSC/MINGW
  13. #elif !defined(__FreeBSD__) && !defined(__NetBSD__)
  14. #include <alloca.h>
  15. #endif
  16. bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
  17. // determine sensible default number of threads.
  18. // std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
  19. #ifdef __linux__
  20. std::ifstream cpuinfo("/proc/cpuinfo");
  21. params.n_threads = std::count(std::istream_iterator<std::string>(cpuinfo),
  22. std::istream_iterator<std::string>(),
  23. std::string("processor"));
  24. #endif
  25. if (params.n_threads == 0) {
  26. params.n_threads = std::max(1, (int32_t) std::thread::hardware_concurrency());
  27. }
  28. for (int i = 1; i < argc; i++) {
  29. std::string arg = argv[i];
  30. if (arg == "-s" || arg == "--seed") {
  31. params.seed = std::stoi(argv[++i]);
  32. } else if (arg == "-t" || arg == "--threads") {
  33. params.n_threads = std::stoi(argv[++i]);
  34. } else if (arg == "-p" || arg == "--prompt") {
  35. params.prompt = argv[++i];
  36. } else if (arg == "-f" || arg == "--file") {
  37. std::ifstream file(argv[++i]);
  38. std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
  39. if (params.prompt.back() == '\n') {
  40. params.prompt.pop_back();
  41. }
  42. } else if (arg == "-n" || arg == "--n_predict") {
  43. params.n_predict = std::stoi(argv[++i]);
  44. } else if (arg == "--top_k") {
  45. params.top_k = std::stoi(argv[++i]);
  46. } else if (arg == "-c" || arg == "--ctx_size") {
  47. params.n_ctx = std::stoi(argv[++i]);
  48. } else if (arg == "--memory_f16") {
  49. params.memory_f16 = true;
  50. } else if (arg == "--top_p") {
  51. params.top_p = std::stof(argv[++i]);
  52. } else if (arg == "--temp") {
  53. params.temp = std::stof(argv[++i]);
  54. } else if (arg == "--repeat_last_n") {
  55. params.repeat_last_n = std::stoi(argv[++i]);
  56. } else if (arg == "--repeat_penalty") {
  57. params.repeat_penalty = std::stof(argv[++i]);
  58. } else if (arg == "-b" || arg == "--batch_size") {
  59. params.n_batch = std::stoi(argv[++i]);
  60. } else if (arg == "-m" || arg == "--model") {
  61. params.model = argv[++i];
  62. } else if (arg == "-i" || arg == "--interactive") {
  63. params.interactive = true;
  64. } else if (arg == "-ins" || arg == "--instruct") {
  65. params.instruct = true;
  66. } else if (arg == "--color") {
  67. params.use_color = true;
  68. } else if (arg == "-r" || arg == "--reverse-prompt") {
  69. params.antiprompt.push_back(argv[++i]);
  70. } else if (arg == "--ignore-eos") {
  71. params.ignore_eos = true;
  72. } else if (arg == "-h" || arg == "--help") {
  73. gpt_print_usage(argc, argv, params);
  74. exit(0);
  75. } else if (arg == "--random-prompt") {
  76. params.random_prompt = true;
  77. } else {
  78. fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
  79. gpt_print_usage(argc, argv, params);
  80. exit(0);
  81. }
  82. }
  83. return true;
  84. }
  85. void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
  86. fprintf(stderr, "usage: %s [options]\n", argv[0]);
  87. fprintf(stderr, "\n");
  88. fprintf(stderr, "options:\n");
  89. fprintf(stderr, " -h, --help show this help message and exit\n");
  90. fprintf(stderr, " -i, --interactive run in interactive mode\n");
  91. fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
  92. fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
  93. fprintf(stderr, " in interactive mode, poll user input upon seeing PROMPT (can be\n");
  94. fprintf(stderr, " specified more than once for multiple prompts).\n");
  95. fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n");
  96. fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n");
  97. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  98. fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
  99. fprintf(stderr, " prompt to start generation with (default: empty)\n");
  100. fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
  101. fprintf(stderr, " -f FNAME, --file FNAME\n");
  102. fprintf(stderr, " prompt file to start generation.\n");
  103. fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d)\n", params.n_predict);
  104. fprintf(stderr, " --top_k N top-k sampling (default: %d)\n", params.top_k);
  105. fprintf(stderr, " --top_p N top-p sampling (default: %.1f)\n", params.top_p);
  106. fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d)\n", params.repeat_last_n);
  107. fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f)\n", params.repeat_penalty);
  108. fprintf(stderr, " -c N, --ctx_size N size of the prompt context (default: %d)\n", params.n_ctx);
  109. fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating\n");
  110. fprintf(stderr, " --memory_f16 use f16 instead of f32 for memory key+value\n");
  111. fprintf(stderr, " --temp N temperature (default: %.1f)\n", params.temp);
  112. fprintf(stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n", params.n_batch);
  113. fprintf(stderr, " -m FNAME, --model FNAME\n");
  114. fprintf(stderr, " model path (default: %s)\n", params.model.c_str());
  115. fprintf(stderr, "\n");
  116. }
  117. std::string gpt_random_prompt(std::mt19937 & rng) {
  118. const int r = rng() % 10;
  119. switch (r) {
  120. case 0: return "So";
  121. case 1: return "Once upon a time";
  122. case 2: return "When";
  123. case 3: return "The";
  124. case 4: return "After";
  125. case 5: return "If";
  126. case 6: return "import";
  127. case 7: return "He";
  128. case 8: return "She";
  129. case 9: return "They";
  130. default: return "To";
  131. }
  132. return "The";
  133. }
  134. void replace(std::string & str, const std::string & needle, const std::string & replacement) {
  135. size_t pos = 0;
  136. while ((pos = str.find(needle, pos)) != std::string::npos) {
  137. str.replace(pos, needle.length(), replacement);
  138. pos += replacement.length();
  139. }
  140. }
  141. std::map<std::string, int32_t> json_parse(const std::string & fname) {
  142. std::map<std::string, int32_t> result;
  143. // read file into string
  144. std::string json;
  145. {
  146. std::ifstream ifs(fname);
  147. if (!ifs) {
  148. fprintf(stderr, "Failed to open %s\n", fname.c_str());
  149. exit(1);
  150. }
  151. json = std::string((std::istreambuf_iterator<char>(ifs)),
  152. (std::istreambuf_iterator<char>()));
  153. }
  154. if (json[0] != '{') {
  155. return result;
  156. }
  157. // parse json
  158. {
  159. bool has_key = false;
  160. bool in_token = false;
  161. std::string str_key = "";
  162. std::string str_val = "";
  163. int n = json.size();
  164. for (int i = 1; i < n; ++i) {
  165. if (!in_token) {
  166. if (json[i] == ' ') continue;
  167. if (json[i] == '"') {
  168. in_token = true;
  169. continue;
  170. }
  171. } else {
  172. if (json[i] == '\\' && i+1 < n) {
  173. if (has_key == false) {
  174. str_key += json[i];
  175. } else {
  176. str_val += json[i];
  177. }
  178. ++i;
  179. } else if (json[i] == '"') {
  180. if (has_key == false) {
  181. has_key = true;
  182. ++i;
  183. while (json[i] == ' ') ++i;
  184. ++i; // :
  185. while (json[i] == ' ') ++i;
  186. if (json[i] != '\"') {
  187. while (json[i] != ',' && json[i] != '}') {
  188. str_val += json[i++];
  189. }
  190. has_key = false;
  191. } else {
  192. in_token = true;
  193. continue;
  194. }
  195. } else {
  196. has_key = false;
  197. }
  198. ::replace(str_key, "\\u0120", " " ); // \u0120 -> space
  199. ::replace(str_key, "\\u010a", "\n"); // \u010a -> new line
  200. ::replace(str_key, "\\\"", "\""); // \\\" -> "
  201. try {
  202. result[str_key] = std::stoi(str_val);
  203. } catch (...) {
  204. //fprintf(stderr, "%s: ignoring key '%s' with value '%s'\n", fname.c_str(), str_key.c_str(), str_val.c_str());
  205. }
  206. str_key = "";
  207. str_val = "";
  208. in_token = false;
  209. continue;
  210. }
  211. if (has_key == false) {
  212. str_key += json[i];
  213. } else {
  214. str_val += json[i];
  215. }
  216. }
  217. }
  218. }
  219. return result;
  220. }
  221. static size_t utf8_len(char src) {
  222. const size_t lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 };
  223. uint8_t highbits = static_cast<uint8_t>(src) >> 4;
  224. return lookup[highbits];
  225. }
  226. struct llama_sp_symbol {
  227. using index = int;
  228. index prev;
  229. index next;
  230. const char * text;
  231. size_t n;
  232. };
  233. struct llama_sp_bigram {
  234. struct comparator {
  235. bool operator()(llama_sp_bigram & l, llama_sp_bigram & r) {
  236. return (l.score < r.score) || (l.score == r.score && l.left > r.left);
  237. }
  238. };
  239. using queue_storage = std::vector<llama_sp_bigram>;
  240. using queue = std::priority_queue<llama_sp_bigram, queue_storage, comparator>;
  241. llama_sp_symbol::index left;
  242. llama_sp_symbol::index right;
  243. float score;
  244. size_t size;
  245. };
  246. // original implementation:
  247. // https://github.com/ggerganov/llama.cpp/commit/074bea2eb1f1349a0118239c4152914aecaa1be4
  248. struct llama_tokenizer {
  249. llama_tokenizer(const llama_vocab & vocab): vocab_(vocab) {}
  250. void tokenize(const std::string & text, std::vector<llama_vocab::id> & output) {
  251. // split string into utf8 chars
  252. int index = 0;
  253. size_t offs = 0;
  254. while (offs < text.size()) {
  255. llama_sp_symbol sym;
  256. size_t char_len = std::min(text.size() - offs, utf8_len(text[offs]));
  257. sym.text = text.c_str() + offs;
  258. sym.n = char_len;
  259. offs += char_len;
  260. sym.prev = index - 1;
  261. sym.next = offs == text.size() ? -1 : index + 1;
  262. index++;
  263. symbols_.emplace_back(std::move(sym));
  264. }
  265. // seed the work queue with all possible 2-character tokens.
  266. for (size_t i = 1; i < symbols_.size(); ++i) {
  267. try_add_bigram(i - 1, i);
  268. }
  269. // keep substituting the highest frequency pairs for as long as we can.
  270. while (!work_queue_.empty()) {
  271. auto bigram = work_queue_.top();
  272. work_queue_.pop();
  273. auto & left_sym = symbols_[bigram.left];
  274. auto & right_sym = symbols_[bigram.right];
  275. // if one of the symbols already got merged, skip it.
  276. if (left_sym.n == 0 || right_sym.n == 0 ||
  277. left_sym.n + right_sym.n != bigram.size) {
  278. continue;
  279. }
  280. // merge the right sym into the left one
  281. left_sym.n += right_sym.n;
  282. right_sym.n = 0;
  283. //printf("left = '%*s' size = %zu\n", (int) left_sym.n, left_sym.text, bigram.size);
  284. // remove the right sym from the chain
  285. left_sym.next = right_sym.next;
  286. if (right_sym.next >= 0) {
  287. symbols_[right_sym.next].prev = bigram.left;
  288. }
  289. // find more substitutions
  290. try_add_bigram(left_sym.prev, bigram.left);
  291. try_add_bigram(bigram.left, left_sym.next);
  292. }
  293. for (int i = 0; i != -1; i = symbols_[i].next) {
  294. auto & symbol = symbols_[i];
  295. auto token = vocab_.token_to_id.find(std::string(symbol.text, symbol.n));
  296. if (token == vocab_.token_to_id.end()) {
  297. // output any symbols that did not form tokens as bytes.
  298. for (int j = 0; j < (int) symbol.n; ++j) {
  299. llama_vocab::id token_id = static_cast<uint8_t>(symbol.text[j]) + 3;
  300. output.push_back(token_id);
  301. }
  302. } else {
  303. output.push_back((*token).second);
  304. }
  305. }
  306. }
  307. private:
  308. void try_add_bigram(int left, int right) {
  309. if (left == -1 || right == -1) {
  310. return;
  311. }
  312. const std::string text = std::string(symbols_[left].text, symbols_[left].n + symbols_[right].n);
  313. auto token = vocab_.token_to_id.find(text);
  314. if (token == vocab_.token_to_id.end()) {
  315. return;
  316. }
  317. auto score = vocab_.score.find((*token).second);
  318. if (score == vocab_.score.end()) {
  319. return;
  320. }
  321. llama_sp_bigram bigram;
  322. bigram.left = left;
  323. bigram.right = right;
  324. bigram.score = (*score).second;
  325. bigram.size = text.size();
  326. work_queue_.push(bigram);
  327. }
  328. const llama_vocab & vocab_;
  329. std::vector<llama_sp_symbol> symbols_;
  330. llama_sp_bigram::queue work_queue_;
  331. };
  332. // TODO: temporary code duplication with llama.cpp
  333. // will resolve after #77 is merged
  334. bool llama_vocab_load(const std::string & fname, llama_vocab & vocab) {
  335. std::ifstream fin(fname, std::ios::binary);
  336. if (!fin.is_open()) {
  337. return false;
  338. }
  339. int n_vocab = 0;
  340. fin.read((char *) &n_vocab, sizeof(n_vocab));
  341. std::string word;
  342. std::vector<char> tmp(64);
  343. for (int i = 0; i < n_vocab; i++) {
  344. uint32_t len;
  345. fin.read((char *) &len, sizeof(len));
  346. word.resize(len);
  347. if (len > 0) {
  348. tmp.resize(len);
  349. fin.read(tmp.data(), len);
  350. word.assign(tmp.data(), len);
  351. } else {
  352. word.clear();
  353. }
  354. float score;
  355. fin.read((char *) &score, sizeof(score));
  356. vocab.token_to_id[word] = i;
  357. vocab.id_to_token[i] = word;
  358. vocab.score[i] = score;
  359. }
  360. return true;
  361. }
  362. std::vector<llama_vocab::id> llama_tokenize(const llama_vocab & vocab, const std::string & text, bool bos) {
  363. llama_tokenizer tokenizer(vocab);
  364. std::vector<llama_vocab::id> output;
  365. if (text.size() == 0) {
  366. return output;
  367. }
  368. if (bos) {
  369. output.push_back(1);
  370. }
  371. tokenizer.tokenize(text, output);
  372. return output;
  373. }
  374. void sample_top_k(std::vector<std::pair<double, llama_vocab::id>> & logits_id, int top_k) {
  375. // find the top K tokens
  376. std::partial_sort(
  377. logits_id.begin(),
  378. logits_id.begin() + top_k, logits_id.end(),
  379. [](const std::pair<double, llama_vocab::id> & a, const std::pair<double, llama_vocab::id> & b) {
  380. return a.first > b.first;
  381. });
  382. logits_id.resize(top_k);
  383. }
  384. llama_vocab::id llama_sample_top_p_top_k(
  385. const llama_vocab & vocab,
  386. const float * logits,
  387. std::vector<llama_vocab::id> & last_n_tokens,
  388. double repeat_penalty,
  389. int top_k,
  390. double top_p,
  391. double temp,
  392. std::mt19937 & rng) {
  393. int n_logits = vocab.id_to_token.size();
  394. std::vector<std::pair<double, llama_vocab::id>> logits_id;
  395. logits_id.reserve(n_logits);
  396. {
  397. const double scale = 1.0/temp;
  398. for (int i = 0; i < n_logits; ++i) {
  399. // repetition penalty from CTRL paper (https://arxiv.org/abs/1909.05858)
  400. // credit https://github.com/facebookresearch/llama/compare/main...shawwn:llama:main
  401. if (std::find(last_n_tokens.begin(), last_n_tokens.end(), i) != last_n_tokens.end()) {
  402. // if score < 0 then repetition penalty has to multiplied to reduce the previous token probability
  403. if (logits[i] < 0.0) {
  404. logits_id.push_back(std::make_pair(logits[i]*scale*repeat_penalty, i));
  405. } else {
  406. logits_id.push_back(std::make_pair(logits[i]*scale/repeat_penalty, i));
  407. }
  408. } else {
  409. logits_id.push_back(std::make_pair(logits[i]*scale, i));
  410. }
  411. }
  412. }
  413. sample_top_k(logits_id, top_k);
  414. double maxl = -INFINITY;
  415. for (const auto & kv : logits_id) {
  416. maxl = std::max(maxl, kv.first);
  417. }
  418. // compute probs for the top K tokens
  419. std::vector<double> probs;
  420. probs.reserve(logits_id.size());
  421. double sum = 0.0;
  422. for (const auto & kv : logits_id) {
  423. double p = exp(kv.first - maxl);
  424. probs.push_back(p);
  425. sum += p;
  426. }
  427. // normalize the probs
  428. for (auto & p : probs) {
  429. p /= sum;
  430. }
  431. if (top_p < 1.0f) {
  432. double cumsum = 0.0f;
  433. for (int i = 0; i < (int) probs.size(); i++) {
  434. cumsum += probs[i];
  435. if (cumsum >= top_p) {
  436. probs.resize(i + 1);
  437. logits_id.resize(i + 1);
  438. break;
  439. }
  440. }
  441. cumsum = 1.0/cumsum;
  442. for (int i = 0; i < (int) probs.size(); i++) {
  443. probs[i] *= cumsum;
  444. }
  445. }
  446. //printf("\n");
  447. //for (int i = 0; i < (int) 10; i++) {
  448. // printf("%d: '%s' %f\n", i, vocab.id_to_token.at(logits_id[i].second).c_str(), probs[i]);
  449. //}
  450. //printf("\n\n");
  451. //exit(0);
  452. std::discrete_distribution<> dist(probs.begin(), probs.end());
  453. int idx = dist(rng);
  454. return logits_id[idx].second;
  455. }
  456. size_t ggml_quantize_q4_0(float * src, void * dst, int n, int k, int qk, int64_t * hist) {
  457. const int nb = k / qk;
  458. const size_t bs = (sizeof(float) + sizeof(uint8_t)*qk/2);
  459. const size_t row_size = nb*bs;
  460. assert(k % qk == 0);
  461. const size_t pp_size = qk / 2;
  462. uint8_t *pp = static_cast<uint8_t*>(alloca(pp_size));
  463. char * pdst = (char *) dst;
  464. for (int j = 0; j < n; j += k) {
  465. uint8_t * pd = (uint8_t *) (pdst + (j/k)*row_size + 0*bs);
  466. uint8_t * pb = (uint8_t *) (pdst + (j/k)*row_size + 0*bs + sizeof(float));
  467. for (int i = 0; i < nb; i++) {
  468. float amax = 0.0f; // absolute max
  469. {
  470. for (int l = 0; l < qk; l++) {
  471. const float v = src[j + i*qk + l];
  472. amax = std::max(amax, fabsf(v));
  473. }
  474. const float d = amax / ((1 << 3) - 1);
  475. const float id = d ? 1.0f/d : 0.0f;
  476. *(float *) pd = d;
  477. pd += bs;
  478. for (int l = 0; l < qk; l += 2) {
  479. const float v0 = (src[j + i*qk + l + 0])*id;
  480. const float v1 = (src[j + i*qk + l + 1])*id;
  481. const uint8_t vi0 = ((int8_t) (round(v0))) + 8;
  482. const uint8_t vi1 = ((int8_t) (round(v1))) + 8;
  483. assert(vi0 >= 0 && vi0 < 16);
  484. assert(vi1 >= 0 && vi1 < 16);
  485. hist[vi0]++;
  486. hist[vi1]++;
  487. pp[l/2] = vi0 | (vi1 << 4);
  488. }
  489. memcpy(pb, pp, pp_size);
  490. pb += bs;
  491. }
  492. }
  493. }
  494. return (n/k)*row_size;
  495. }
  496. size_t ggml_quantize_q4_1(float * src, void * dst, int n, int k, int qk, int64_t * hist) {
  497. const int nb = k / qk;
  498. const size_t bs = (2*sizeof(float) + sizeof(uint8_t)*qk/2);
  499. const size_t row_size = nb*bs;
  500. assert(k % qk == 0);
  501. const size_t pp_size = qk / 2;
  502. uint8_t *pp = static_cast<uint8_t*>(alloca(pp_size));
  503. char * pdst = (char *) dst;
  504. for (int j = 0; j < n; j += k) {
  505. uint8_t * pd = (uint8_t *) (pdst + (j/k)*row_size + 0*bs);
  506. uint8_t * pm = (uint8_t *) (pdst + (j/k)*row_size + 0*bs + sizeof(float));
  507. uint8_t * pb = (uint8_t *) (pdst + (j/k)*row_size + 0*bs + 2*sizeof(float));
  508. //printf("n = %d, k = %d, nb = %d, row_size = %d, j = %d, pm = %p, pd = %p, pb = %p\n", n, k, nb, row_size, j, pm, pd, pb);
  509. for (int i = 0; i < nb; i++) {
  510. float min = std::numeric_limits<float>::max();
  511. float max = std::numeric_limits<float>::min();
  512. {
  513. for (int l = 0; l < qk; l++) {
  514. const float v = src[j + i*qk + l];
  515. if (v < min) min = v;
  516. if (v > max) max = v;
  517. }
  518. const float d = (max - min) / ((1 << 4) - 1);
  519. const float id = d ? 1.0f/d : 0.0f;
  520. *(float *) pd = d;
  521. *(float *) pm = min;
  522. pd += bs;
  523. pm += bs;
  524. for (int l = 0; l < qk; l += 2) {
  525. const float v0 = (src[j + i*qk + l + 0] - min)*id;
  526. const float v1 = (src[j + i*qk + l + 1] - min)*id;
  527. const uint8_t vi0 = round(v0);
  528. const uint8_t vi1 = round(v1);
  529. assert(vi0 >= 0 && vi0 < 16);
  530. assert(vi1 >= 0 && vi1 < 16);
  531. hist[vi0]++;
  532. hist[vi1]++;
  533. pp[l/2] = vi0 | (vi1 << 4);
  534. }
  535. memcpy(pb, pp, pp_size);
  536. pb += bs;
  537. }
  538. }
  539. }
  540. return (n/k)*row_size;
  541. }