utils.cpp 22 KB

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