llama-batch.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. #include "llama-batch.h"
  2. #include "llama-impl.h"
  3. #include "llama-vocab.h"
  4. #include "llama-memory.h"
  5. #include <cassert>
  6. #include <cstring>
  7. #include <algorithm>
  8. #include <sstream>
  9. llama_batch_allocr::llama_batch_allocr(uint32_t n_pos_per_embd) : n_pos_per_embd(n_pos_per_embd) {
  10. const char * LLAMA_BATCH_DEBUG = getenv("LLAMA_BATCH_DEBUG");
  11. debug = LLAMA_BATCH_DEBUG ? atoi(LLAMA_BATCH_DEBUG) : 0;
  12. seq_pos.resize(LLAMA_MAX_SEQ);
  13. seq_cpl.resize(LLAMA_MAX_SEQ);
  14. for (auto & cur : seq_cpl) {
  15. cur.resize(LLAMA_MAX_SEQ);
  16. }
  17. seq_idx.resize(LLAMA_MAX_SEQ, -1);
  18. }
  19. bool llama_batch_allocr::init(
  20. const llama_batch & batch_inp,
  21. const llama_vocab & vocab,
  22. const llama_memory_i * memory,
  23. uint32_t n_embd,
  24. bool output_all) {
  25. clear();
  26. batch = batch_inp;
  27. this->vocab = &vocab;
  28. GGML_ASSERT(batch.n_tokens > 0);
  29. //
  30. // validate input batch
  31. //
  32. if (batch.token) {
  33. for (int32_t i = 0; i < batch.n_tokens; ++i) {
  34. if (batch.token[i] < 0 || (uint32_t) batch.token[i] >= vocab.n_tokens()) {
  35. LLAMA_LOG_ERROR("%s: invalid token[%d] = %d\n", __func__, i, batch.token[i]);
  36. return false;
  37. }
  38. }
  39. }
  40. if (batch.seq_id) {
  41. for (int32_t i = 0; i < batch.n_tokens; ++i) {
  42. for (int32_t s = 0; s < batch.n_seq_id[i]; ++s) {
  43. if (batch.seq_id && (batch.seq_id[i][s] < 0 || batch.seq_id[i][s] >= LLAMA_MAX_SEQ)) {
  44. LLAMA_LOG_ERROR("%s: invalid seq_id[%d][%d] = %d > %d\n", __func__, i, s, batch.seq_id[i][s], LLAMA_MAX_SEQ);
  45. return false;
  46. }
  47. }
  48. }
  49. }
  50. //
  51. // auto-generate missing fields
  52. //
  53. if (!batch.n_seq_id) {
  54. n_seq_id.resize(batch.n_tokens);
  55. for (int32_t i = 0; i < batch.n_tokens; i++) {
  56. n_seq_id[i] = seq_id_0.size();
  57. }
  58. batch.n_seq_id = n_seq_id.data();
  59. }
  60. if (!batch.seq_id) {
  61. seq_id.resize(batch.n_tokens + 1);
  62. seq_id[batch.n_tokens] = NULL;
  63. for (int32_t i = 0; i < batch.n_tokens; i++) {
  64. seq_id[i] = seq_id_0.data();
  65. }
  66. batch.seq_id = seq_id.data();
  67. }
  68. if (!batch.pos) {
  69. pos.resize(batch.n_tokens);
  70. // initialize the starting position for each sequence based on the positions in the memory
  71. llama_pos p0[LLAMA_MAX_SEQ];
  72. for (int32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
  73. if (!memory) {
  74. // if no memory -> start from 0
  75. p0[s] = 0;
  76. } else {
  77. p0[s] = memory->seq_pos_max(s) + 1;
  78. }
  79. }
  80. for (int32_t i = 0; i < batch.n_tokens; i++) {
  81. const llama_seq_id seq_id = batch.seq_id[i][0];
  82. pos[i] = p0[seq_id];
  83. // update the starting position for all sequences that are assigned to the this token
  84. for (int32_t s = 0; s < batch.n_seq_id[i]; ++s) {
  85. const llama_seq_id seq_id = batch.seq_id[i][s];
  86. p0[seq_id] = pos[i] + 1;
  87. }
  88. }
  89. batch.pos = pos.data();
  90. }
  91. if (!batch.logits) {
  92. if (output_all) {
  93. // return the output for all tokens
  94. output.resize(batch.n_tokens, true);
  95. } else {
  96. // return the output only for the last token
  97. output.resize(batch.n_tokens, false);
  98. output[output.size() - 1] = true;
  99. }
  100. batch.logits = output.data();
  101. } else if (output_all) {
  102. bool warn = false;
  103. for (int32_t i = 0; i < batch.n_tokens; ++i) {
  104. if (batch.logits[i] == 0) {
  105. warn = true;
  106. }
  107. }
  108. if (warn) {
  109. LLAMA_LOG_WARN("%s: embeddings required but some input tokens were not marked as outputs -> overriding\n", __func__);
  110. output.resize(batch.n_tokens, true);
  111. batch.logits = output.data();
  112. }
  113. }
  114. //
  115. // compute stats
  116. //
  117. this->n_embd = n_embd;
  118. // count the outputs in this batch
  119. for (int32_t i = 0; i < batch.n_tokens; ++i) {
  120. n_outputs += batch.logits[i] != 0;
  121. }
  122. // determine coupled sequences
  123. // these are pairs of sequences that have at least one token in the input batch that is assigned to both of them
  124. for (int32_t i = 0; i < batch.n_tokens; ++i) {
  125. const llama_seq_id s0 = batch.seq_id[i][0];
  126. for (int32_t s = 0; s < batch.n_seq_id[i]; ++s) {
  127. const llama_seq_id s1 = batch.seq_id[i][s];
  128. seq_pos[s1].insert(batch.pos[i]);
  129. if (s > 0) {
  130. // mark that sequence s1 is coupled to s0
  131. seq_cpl[s1][s0] = true;
  132. // note: tracking the other way around is not necessary for now
  133. //seq_cpl[s0][s1] = true;
  134. }
  135. }
  136. }
  137. // precompute the sequence sets for each token and determine the unique sequence ids that participate in the batch
  138. {
  139. seq_set_t seq_set_unq;
  140. for (int32_t i = 0; i < batch.n_tokens; ++i) {
  141. seq_set_t cur;
  142. for (int32_t s = 0; s < batch.n_seq_id[i]; ++s) {
  143. const llama_seq_id seq_id = batch.seq_id[i][s];
  144. cur .set(seq_id);
  145. seq_set_unq.set(seq_id);
  146. }
  147. seq_set.push_back(cur);
  148. seq_set_map[cur].push_back(i);
  149. }
  150. for (int32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
  151. if (seq_set_unq.test(s)) {
  152. seq_idx[s] = seq_id_unq.size();
  153. seq_id_unq.push_back(s);
  154. }
  155. }
  156. }
  157. if (debug > 0) {
  158. LLAMA_LOG_DEBUG("%s: input batch info:\n", __func__);
  159. llama_ubatch ubatch {
  160. /*.equal_seqs =*/ false,
  161. /*.n_tokens =*/ (uint32_t) batch.n_tokens,
  162. /*.n_seq_tokens =*/ (uint32_t) 1,
  163. /*.n_seqs =*/ (uint32_t) batch.n_tokens,
  164. /*.n_seqs_unq =*/ (uint32_t) this->seq_id_unq.size(),
  165. /*.token =*/ batch.token,
  166. /*.embd =*/ batch.embd,
  167. /*.pos =*/ batch.pos,
  168. /*.n_seq_id =*/ batch.n_seq_id,
  169. /*.seq_id =*/ batch.seq_id,
  170. /*.seq_id_unq =*/ this->seq_id_unq.data(),
  171. /*.seq_idx =*/ this->seq_idx.data(),
  172. /*.output =*/ batch.logits,
  173. };
  174. ubatch_print(ubatch, debug);
  175. LLAMA_LOG_DEBUG("%s: seq = [\n", __func__);
  176. for (int s0 = 0; s0 < (int) seq_pos.size(); ++s0) {
  177. if (seq_pos[s0].empty()) {
  178. continue;
  179. }
  180. std::stringstream ss;
  181. for (int s1 = 0; s1 < (int) seq_cpl[s0].size(); ++s1) {
  182. if (seq_cpl[s0][s1]) {
  183. ss << s1 << " ";
  184. }
  185. }
  186. LLAMA_LOG_DEBUG("%s: %4d: pos = [%4d, %4d], cpl = %s\n",
  187. __func__, s0, seq_pos_min(s0), seq_pos_max(s0), ss.str().empty() ? "-" : ss.str().c_str());
  188. }
  189. LLAMA_LOG_DEBUG("%s: ]\n", __func__);
  190. }
  191. //
  192. // consistency checks
  193. //
  194. for (int32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
  195. if (seq_pos[s].empty()) {
  196. continue;
  197. }
  198. const llama_pos p0 = memory ? memory->seq_pos_max(s) : -1;
  199. if (p0 >= 0) {
  200. bool ok = true;
  201. if (batch.token) {
  202. if (seq_pos_min(s) != p0 + 1) {
  203. ok = false;
  204. }
  205. } else {
  206. assert(batch.embd);
  207. // for embeddings (typically used as vision input), we allow them to have repeating positions
  208. // ref: https://github.com/ggml-org/llama.cpp/issues/13694#issuecomment-2983871762
  209. if (seq_pos_min(s) != p0 && seq_pos_min(s) != p0 + 1) {
  210. ok = false;
  211. }
  212. }
  213. if (!ok) {
  214. LLAMA_LOG_ERROR(
  215. "%s: the tokens of sequence %d in the input batch have inconsistent sequence positions:\n"
  216. " - the last position stored in the memory module of the context (i.e. the KV cache) for sequence %d is X = %d\n"
  217. " - the tokens for sequence %d in the input batch have a starting position of Y = %d\n"
  218. " it is required that the sequence positions remain consecutive: Y = X + 1\n",
  219. __func__, s, s, p0, s, seq_pos_min(s));
  220. return false;
  221. }
  222. }
  223. if (seq_pos_max(s) - seq_pos_min(s) + 1 > (int) seq_pos[s].size()) {
  224. LLAMA_LOG_ERROR("%s: sequence %d positions are not continuous\n", __func__, s);
  225. return false;
  226. }
  227. }
  228. if (memory) {
  229. for (int32_t s0 = 0; s0 < LLAMA_MAX_SEQ; ++s0) {
  230. for (int32_t s1 = 0; s1 < LLAMA_MAX_SEQ; ++s1) {
  231. if (seq_cpl[s0][s1]) {
  232. if (memory->seq_pos_min(s0) != memory->seq_pos_min(s1) ||
  233. memory->seq_pos_max(s0) != memory->seq_pos_max(s1)) {
  234. LLAMA_LOG_ERROR("%s: sequence %d is coupled to %d in the input batch, but have divereged\n", __func__, s0, s1);
  235. return false;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. // disallow partial sequence sub-sets:
  242. //
  243. // invalid: x
  244. // i: 0 1 2 ...
  245. // ---------------------------------------
  246. // seq_id[i][0]: 0 0 1
  247. // seq_id[i][1]: 1 1 2
  248. // seq_id[i][2]: 2
  249. //
  250. // disallow decreasing sequence positions:
  251. //
  252. // invalid: x
  253. // i: 0 1 2 3 4 5 6 ...
  254. // ---------------------------------------
  255. // pos[i]: 4 5 0 1 6 2 3
  256. // seq_id[i][0]: 0 0 1 1 0 1 0
  257. //
  258. {
  259. seq_set_t cur_seq_set[LLAMA_MAX_SEQ];
  260. for (int32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
  261. cur_seq_set[s].set();
  262. }
  263. llama_pos cur_seq_pos[LLAMA_MAX_SEQ];
  264. for (int32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
  265. cur_seq_pos[s] = -1;
  266. }
  267. for (int32_t i = 0; i < batch.n_tokens; ++i) {
  268. const llama_pos pos = batch.pos[i];
  269. for (int32_t s = 0; s < batch.n_seq_id[i]; ++s) {
  270. const llama_seq_id seq_id = batch.seq_id[i][s];
  271. cur_seq_set[seq_id] &= seq_set[i];
  272. if (cur_seq_set[seq_id].none()) {
  273. LLAMA_LOG_ERROR("%s: sequence %d belongs to incompatible sequence sets (not allowed)\n", __func__, seq_id);
  274. return false;
  275. }
  276. if (pos < cur_seq_pos[seq_id]) {
  277. LLAMA_LOG_ERROR("%s: sequence %d positions are decreasing (not allowed)\n", __func__, seq_id);
  278. return false;
  279. }
  280. }
  281. }
  282. }
  283. split_reset();
  284. return true;
  285. }
  286. llama_ubatch llama_batch_allocr::ubatch_reserve(uint32_t n_seq_tokens, uint32_t n_seqs) {
  287. const uint32_t n_tokens = n_seq_tokens*n_seqs;
  288. clear();
  289. split_reset();
  290. ubatches.emplace_back();
  291. auto & ubatch = ubatches.back();
  292. ubatch.token .resize(n_tokens);
  293. ubatch.embd .clear();
  294. ubatch.pos .resize(n_tokens);
  295. ubatch.n_seq_id .resize(n_tokens);
  296. ubatch.seq_id .resize(n_tokens);
  297. ubatch.seq_id_unq.resize(0);
  298. ubatch.seq_idx .resize(LLAMA_MAX_SEQ, -1);
  299. ubatch.output .resize(n_tokens);
  300. for (uint32_t s = 0; s < n_seqs; ++s) {
  301. ubatch.seq_idx[s] = s;
  302. ubatch.seq_id_unq.push_back(s);
  303. }
  304. llama_ubatch res {
  305. /*.equal_seqs =*/ true,
  306. /*.n_tokens =*/ n_tokens,
  307. /*.n_seq_tokens =*/ n_seq_tokens,
  308. /*.n_seqs =*/ n_seqs,
  309. /*.n_seqs_unq =*/ n_seqs,
  310. /*.token =*/ ubatch.token.data(),
  311. /*.embd =*/ nullptr,
  312. /*.pos =*/ ubatch.pos.data(),
  313. /*.n_seq_id =*/ ubatch.n_seq_id.data(),
  314. /*.seq_id =*/ ubatch.seq_id.data(),
  315. /*.seq_id_unq =*/ ubatch.seq_id_unq.data(),
  316. /*.seq_idx =*/ ubatch.seq_idx.data(),
  317. /*.output =*/ ubatch.output.data(),
  318. };
  319. return res;
  320. }
  321. const llama_batch & llama_batch_allocr::get_batch() const {
  322. return batch;
  323. }
  324. uint32_t llama_batch_allocr::get_n_tokens() const {
  325. return batch.n_tokens;
  326. }
  327. uint32_t llama_batch_allocr::get_n_outputs() const {
  328. return n_outputs;
  329. }
  330. std::vector<int32_t> & llama_batch_allocr::get_out_ids() {
  331. return out_ids;
  332. }
  333. llama_pos llama_batch_allocr::seq_pos_min(llama_seq_id seq_id) const {
  334. return seq_pos[seq_id].empty() ? -1 : *seq_pos[seq_id].begin();
  335. }
  336. llama_pos llama_batch_allocr::seq_pos_max(llama_seq_id seq_id) const {
  337. return seq_pos[seq_id].empty() ? -1 : *seq_pos[seq_id].rbegin();
  338. }
  339. void llama_batch_allocr::split_reset() {
  340. out_ids.clear();
  341. used.clear();
  342. used.resize(get_n_tokens(), false);
  343. ubatches.clear();
  344. }
  345. llama_ubatch llama_batch_allocr::split_simple(uint32_t n_ubatch) {
  346. // find the first unused token
  347. uint32_t cur_idx = 0;
  348. while (cur_idx < used.size() && used[cur_idx]) {
  349. ++cur_idx;
  350. }
  351. // we are done
  352. if (cur_idx >= used.size()) {
  353. return {};
  354. }
  355. std::vector<int32_t> idxs;
  356. while (true) {
  357. idxs.push_back(cur_idx);
  358. used[cur_idx] = true;
  359. ++cur_idx;
  360. if (cur_idx >= used.size()) {
  361. break;
  362. }
  363. if (idxs.size() >= n_ubatch) {
  364. break;
  365. }
  366. }
  367. return ubatch_add(idxs, idxs.size(), false);
  368. }
  369. llama_ubatch llama_batch_allocr::split_equal(uint32_t n_ubatch) {
  370. std::vector<seq_set_t> cur_seq_set;
  371. // determine the non-overlapping sequence sets participating in this ubatch
  372. for (int32_t i = 0; i < batch.n_tokens; ++i) {
  373. if (used[i]) {
  374. continue;
  375. }
  376. bool add = true;
  377. for (uint32_t s = 0; s < cur_seq_set.size(); ++s) {
  378. // no overlap with existing sequence sets:
  379. if (!(cur_seq_set[s] & seq_set[i]).none()) {
  380. add = false;
  381. break;
  382. }
  383. }
  384. if (add) {
  385. cur_seq_set.push_back(seq_set[i]);
  386. if (cur_seq_set.size() > n_ubatch) {
  387. break;
  388. }
  389. }
  390. }
  391. const uint32_t n_seqs = cur_seq_set.size();
  392. // we are done
  393. if (n_seqs == 0) {
  394. return {};
  395. }
  396. // the current batch index of each sequence set
  397. std::vector<int32_t> cur_idx(n_seqs, 0);
  398. for (uint32_t s = 0; s < n_seqs; ++s) {
  399. while (used[seq_set_map[cur_seq_set[s]][cur_idx[s]]]) {
  400. ++cur_idx[s];
  401. }
  402. }
  403. // the list of batch indices for each sequence set
  404. // at the end we will concat these to get the final ubatch
  405. std::vector<idx_vec_t> idxs_per_seq(n_seqs);
  406. while (true) {
  407. // we can only add new n_seq_tokens tokens if all the sequence sets have at least one more unused token and
  408. // if we haven't reached n_ubatch
  409. bool can_expand = true;
  410. for (uint32_t s = 0; s < n_seqs; ++s) {
  411. if (cur_idx[s] >= (int32_t) seq_set_map[cur_seq_set[s]].size()) {
  412. can_expand = false;
  413. break;
  414. }
  415. }
  416. if (!can_expand) {
  417. break;
  418. }
  419. for (uint32_t s = 0; s < n_seqs; ++s) {
  420. const int32_t idx = seq_set_map[cur_seq_set[s]][cur_idx[s]];
  421. idxs_per_seq[s].push_back(idx);
  422. used[idx] = true;
  423. ++cur_idx[s];
  424. }
  425. if ((idxs_per_seq[0].size() + 1)*n_seqs > n_ubatch) {
  426. break;
  427. }
  428. }
  429. // concat the per-sequence-set lists
  430. std::vector<int32_t> idxs;
  431. for (uint32_t s = 0; s < n_seqs; ++s) {
  432. idxs.insert(idxs.end(), idxs_per_seq[s].begin(), idxs_per_seq[s].end());
  433. }
  434. return ubatch_add(idxs, n_seqs, true);
  435. }
  436. llama_ubatch llama_batch_allocr::split_seq(uint32_t n_ubatch) {
  437. // find the first unused token
  438. uint32_t cur_idx = 0;
  439. while (cur_idx < used.size() && used[cur_idx]) {
  440. ++cur_idx;
  441. }
  442. // we are done
  443. if (cur_idx >= used.size()) {
  444. return {};
  445. }
  446. // this is the starting sequence set
  447. // we allow adding tokens only if their sequence set is a subset of the current sequence set
  448. auto cur_seq_set = seq_set[cur_idx];
  449. std::vector<int32_t> idxs;
  450. while (true) {
  451. idxs.push_back(cur_idx);
  452. used[cur_idx] = true;
  453. if (idxs.size() >= n_ubatch) {
  454. break;
  455. }
  456. do {
  457. ++cur_idx;
  458. } while (cur_idx < get_n_tokens() && (used[cur_idx] || ((cur_seq_set & seq_set[cur_idx]) != seq_set[cur_idx])));
  459. if (cur_idx == get_n_tokens()) {
  460. break;
  461. }
  462. cur_seq_set = seq_set[cur_idx];
  463. }
  464. return ubatch_add(idxs, 1, true);
  465. }
  466. void llama_batch_allocr::clear() {
  467. n_outputs = 0;
  468. batch = {};
  469. pos .clear();
  470. n_seq_id .clear();
  471. seq_id .clear();
  472. seq_id_unq.clear();
  473. output .clear();
  474. for (auto & cur : seq_pos) {
  475. cur.clear();
  476. }
  477. for (auto & cur : seq_cpl) {
  478. std::fill(cur.begin(), cur.end(), false);
  479. }
  480. seq_set.clear();
  481. seq_set_map.clear();
  482. std::fill(seq_idx.begin(), seq_idx.end(), -1);
  483. }
  484. llama_ubatch llama_batch_allocr::ubatch_add(const std::vector<int32_t> & idxs, uint32_t n_seqs, bool equal_seqs) {
  485. const uint32_t n_tokens = idxs.size();
  486. assert(n_tokens%n_seqs == 0);
  487. ubatches.emplace_back();
  488. auto & ubatch = ubatches.back();
  489. const int32_t n_pos_cur = batch.embd ? n_pos_per_embd : 1;
  490. const int64_t n_embd_all = batch.embd ? (int64_t) n_tokens*n_embd : 0;
  491. const int64_t n_pos_all = (int64_t) n_tokens*n_pos_cur;
  492. ubatch.token .resize(n_tokens);
  493. ubatch.embd .resize(n_embd_all);
  494. ubatch.pos .resize(n_pos_all);
  495. ubatch.n_seq_id .resize(n_tokens);
  496. ubatch.seq_id .resize(n_tokens);
  497. ubatch.seq_id_unq.resize(0);
  498. ubatch.seq_idx .resize(LLAMA_MAX_SEQ, -1);
  499. ubatch.output .resize(n_tokens);
  500. seq_set_t seq_set_unq;
  501. for (size_t i = 0; i < idxs.size(); ++i) {
  502. if (batch.token) {
  503. ubatch.token[i] = batch.token[idxs[i]];
  504. }
  505. if (batch.embd) {
  506. memcpy(ubatch.embd.data() + i*n_embd, batch.embd + (int64_t) idxs[i]*n_embd, n_embd*sizeof(float));
  507. }
  508. for (int j = 0; j < n_pos_cur; ++j) {
  509. ubatch.pos[j*n_tokens + i] = batch.pos[j*batch.n_tokens + idxs[i]];
  510. }
  511. ubatch.n_seq_id[i] = batch.n_seq_id[idxs[i]];
  512. ubatch.seq_id[i] = batch.seq_id[idxs[i]];
  513. ubatch.output[i] = batch.logits[idxs[i]];
  514. for (int s = 0; s < ubatch.n_seq_id[i]; ++s) {
  515. seq_set_unq.set(ubatch.seq_id[i][s]);
  516. }
  517. if (ubatch.output[i]) {
  518. out_ids.push_back(idxs[i]);
  519. }
  520. }
  521. for (int32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
  522. if (seq_set_unq.test(s)) {
  523. ubatch.seq_idx[s] = ubatch.seq_id_unq.size();
  524. ubatch.seq_id_unq.push_back(s);
  525. }
  526. }
  527. llama_ubatch res {
  528. /*.equal_seqs =*/ equal_seqs,
  529. /*.n_tokens =*/ n_tokens,
  530. /*.n_seq_tokens =*/ n_tokens/n_seqs,
  531. /*.n_seqs =*/ n_seqs,
  532. /*.n_seqs_unq =*/ (uint32_t) ubatch.seq_id_unq.size(),
  533. /*.token =*/ batch.token ? ubatch.token.data() : nullptr,
  534. /*.embd =*/ batch.embd ? ubatch.embd.data() : nullptr,
  535. /*.pos =*/ ubatch.pos.data(),
  536. /*.n_seq_id =*/ ubatch.n_seq_id.data(),
  537. /*.seq_id =*/ ubatch.seq_id.data(),
  538. /*.seq_id_unq =*/ ubatch.seq_id_unq.data(),
  539. /*.seq_idx =*/ ubatch.seq_idx.data(),
  540. /*.output =*/ ubatch.output.data(),
  541. };
  542. if (debug > 0) {
  543. LLAMA_LOG_DEBUG("%s: added ubatch %d to split:\n", __func__, (int) ubatches.size() - 1);
  544. ubatch_print(res, debug);
  545. }
  546. return res;
  547. }
  548. void llama_batch_allocr::ubatch_print(const llama_ubatch & ubatch, int debug) {
  549. if (debug > 0) {
  550. LLAMA_LOG_DEBUG("%s: equal_seqs = %d\n", __func__, ubatch.equal_seqs);
  551. LLAMA_LOG_DEBUG("%s: n_tokens = %d\n", __func__, ubatch.n_tokens);
  552. LLAMA_LOG_DEBUG("%s: n_seq_tokens = %d\n", __func__, ubatch.n_seq_tokens);
  553. LLAMA_LOG_DEBUG("%s: n_seqs = %d\n", __func__, ubatch.n_seqs);
  554. LLAMA_LOG_DEBUG("%s: n_seqs_unq = %d\n", __func__, ubatch.n_seqs_unq);
  555. std::stringstream ss_seq_id_unq;
  556. std::stringstream ss_seq_idx;
  557. ss_seq_id_unq << "[ ";
  558. ss_seq_idx << "[";
  559. for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
  560. ss_seq_id_unq << ubatch.seq_id_unq[s] << " ";
  561. }
  562. for (uint32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
  563. if (ubatch.seq_idx[s] >= 0) {
  564. ss_seq_idx << ubatch.seq_idx[s]%10;
  565. } else {
  566. ss_seq_idx << ".";
  567. }
  568. }
  569. ss_seq_id_unq << "]";
  570. ss_seq_idx << "]";
  571. LLAMA_LOG_DEBUG("%s: token = %p\n", __func__, (void *) ubatch.token);
  572. LLAMA_LOG_DEBUG("%s: embd = %p\n", __func__, (void *) ubatch.embd);
  573. LLAMA_LOG_DEBUG("%s: pos = %p\n", __func__, (void *) ubatch.pos);
  574. LLAMA_LOG_DEBUG("%s: n_seq_id = %p\n", __func__, (void *) ubatch.n_seq_id);
  575. LLAMA_LOG_DEBUG("%s: seq_id = %p\n", __func__, (void *) ubatch.seq_id);
  576. LLAMA_LOG_DEBUG("%s: seq_id_unq = %s\n", __func__, ss_seq_id_unq.str().c_str());
  577. LLAMA_LOG_DEBUG("%s: seq_idx = %s\n", __func__, ss_seq_idx.str().c_str());
  578. LLAMA_LOG_DEBUG("%s: output = %p\n", __func__, (void *) ubatch.output);
  579. LLAMA_LOG_DEBUG("%s: n_outputs = %d\n", __func__, n_outputs);
  580. if (debug > 1) {
  581. int seq_id_max = 0;
  582. for (uint32_t i = 0; i < ubatch.n_tokens; ++i) {
  583. for (int s = 0; s < ubatch.n_seq_id[i]; ++s) {
  584. for (int s = 0; s < ubatch.n_seq_id[i]; ++s) {
  585. seq_id_max = std::max(seq_id_max, ubatch.seq_id[i][s]);
  586. }
  587. }
  588. }
  589. ++seq_id_max;
  590. LLAMA_LOG_DEBUG("%s: token = [\n", __func__);
  591. for (uint32_t i = 0; i < ubatch.n_tokens; ++i) {
  592. std::vector<int8_t> seq_id(seq_id_max);
  593. for (int s = 0; s < ubatch.n_seq_id[i]; ++s) {
  594. seq_id[ubatch.seq_id[i][s]] = 1;
  595. }
  596. std::stringstream ss;
  597. for (int s = 0; s < seq_id_max; ++s) {
  598. if (seq_id[s]) {
  599. ss << s%10;
  600. } else {
  601. ss << ".";
  602. }
  603. }
  604. if (ubatch.token) {
  605. LLAMA_LOG_DEBUG("%s: %4d: id = %6d (%16s), pos = %4d, n_seq_id = %2d, seq_id = [%s], output = %d\n",
  606. __func__, i, ubatch.token[i], vocab->token_to_piece(ubatch.token[i]).c_str(),
  607. ubatch.pos[i], ubatch.n_seq_id[i], ss.str().c_str(), ubatch.output[i]);
  608. } else {
  609. LLAMA_LOG_DEBUG("%s: %4d: [embd], pos = %4d, n_seq_id = %2d, seq_id = [%s], output = %d\n",
  610. __func__, i, ubatch.pos[i], ubatch.n_seq_id[i], ss.str().c_str(), ubatch.output[i]);
  611. }
  612. }
  613. LLAMA_LOG_DEBUG("%s: ]\n", __func__);
  614. }
  615. }
  616. }
  617. //
  618. // interface implementation
  619. //
  620. struct llama_batch llama_batch_get_one(
  621. llama_token * tokens,
  622. int32_t n_tokens) {
  623. return {
  624. /*n_tokens =*/ n_tokens,
  625. /*tokens =*/ tokens,
  626. /*embd =*/ nullptr,
  627. /*pos =*/ nullptr,
  628. /*n_seq_id =*/ nullptr,
  629. /*seq_id =*/ nullptr,
  630. /*logits =*/ nullptr,
  631. };
  632. }
  633. struct llama_batch llama_batch_init(int32_t n_tokens_alloc, int32_t embd, int32_t n_seq_max) {
  634. llama_batch batch = {
  635. /*n_tokens =*/ 0,
  636. /*tokens =*/ nullptr,
  637. /*embd =*/ nullptr,
  638. /*pos =*/ nullptr,
  639. /*n_seq_id =*/ nullptr,
  640. /*seq_id =*/ nullptr,
  641. /*logits =*/ nullptr,
  642. };
  643. if (embd) {
  644. batch.embd = (float *) malloc(sizeof(float) * n_tokens_alloc * embd);
  645. } else {
  646. batch.token = (llama_token *) malloc(sizeof(llama_token) * n_tokens_alloc);
  647. }
  648. batch.pos = (llama_pos *) malloc(sizeof(llama_pos) * n_tokens_alloc);
  649. batch.n_seq_id = (int32_t *) malloc(sizeof(int32_t) * n_tokens_alloc);
  650. batch.seq_id = (llama_seq_id **) malloc(sizeof(llama_seq_id *) * (n_tokens_alloc + 1));
  651. for (int i = 0; i < n_tokens_alloc; ++i) {
  652. batch.seq_id[i] = (llama_seq_id *) malloc(sizeof(llama_seq_id) * n_seq_max);
  653. }
  654. batch.seq_id[n_tokens_alloc] = nullptr;
  655. batch.logits = (int8_t *) malloc(sizeof(int8_t) * n_tokens_alloc);
  656. return batch;
  657. }
  658. void llama_batch_free(struct llama_batch batch) {
  659. if (batch.token) free(batch.token);
  660. if (batch.embd) free(batch.embd);
  661. if (batch.pos) free(batch.pos);
  662. if (batch.n_seq_id) free(batch.n_seq_id);
  663. if (batch.seq_id) {
  664. for (int i = 0; batch.seq_id[i] != nullptr; ++i) {
  665. free(batch.seq_id[i]);
  666. }
  667. free(batch.seq_id);
  668. }
  669. if (batch.logits) free(batch.logits);
  670. }