llama-batch.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "llama-batch.h"
  2. #include <cstring>
  3. #include <algorithm>
  4. llama_ubatch llama_sbatch::reserve_ubatch(size_t n_ubatch, bool has_embd) {
  5. // clear empty sequences
  6. // the previous ubatch is assumed to be gone,
  7. // so nothing should refer to values in these sequences anymore.
  8. for (size_t i = seq.size(); i-- > 0;) {
  9. if (seq[i].length == 0) {
  10. seq.pop_back();
  11. } else {
  12. break;
  13. }
  14. }
  15. ubatch_token.resize(!has_embd ? n_ubatch : 0);
  16. ubatch_embd.resize(has_embd ? n_embd * n_ubatch : 0);
  17. ubatch_pos.resize(n_ubatch);
  18. ubatch_n_seq_id.resize(n_ubatch);
  19. ubatch_seq_id.resize(n_ubatch);
  20. ubatch_output.resize(n_ubatch);
  21. llama_ubatch ubatch = {
  22. /*equal_seqs =*/ true,
  23. /*n_tokens =*/ 0,
  24. /*n_seq_tokens =*/ 0,
  25. /*n_seqs =*/ 0,
  26. /*token =*/ !has_embd ? ubatch_token.data() : nullptr,
  27. /*embd =*/ has_embd ? ubatch_embd.data() : nullptr,
  28. /*pos =*/ ubatch_pos.data(),
  29. /*n_seq_id =*/ ubatch_n_seq_id.data(),
  30. /*seq_id =*/ ubatch_seq_id.data(),
  31. /*output =*/ ubatch_output.data(),
  32. };
  33. return ubatch;
  34. }
  35. void llama_sbatch::add_seq_to_ubatch(llama_ubatch & ubatch, llama_sbatch_seq & seq, size_t length) {
  36. GGML_ASSERT(batch != nullptr);
  37. GGML_ASSERT(length <= seq.length);
  38. // Can only add sequences of equal lengths to a batch,
  39. // otherwise it isn't clear to which sequence a token belongs
  40. GGML_ASSERT(seq.n_seq_id == 0 || ubatch.n_seqs == 0 || length == (size_t) ubatch.n_tokens / ubatch.n_seqs);
  41. GGML_ASSERT((seq.n_seq_id != 0) == ubatch.equal_seqs);
  42. // NOTE: loops are separated for cache-friendliness
  43. if (batch->token) {
  44. if (ubatch.equal_seqs) {
  45. for (size_t i = 0; i < length; ++i) {
  46. ubatch.token[ubatch.n_tokens + i] = batch->token[ids[seq.offset + i]];
  47. }
  48. } else {
  49. // simple split
  50. ubatch.token = batch->token + seq.offset;
  51. }
  52. } else {
  53. ubatch.token = nullptr;
  54. }
  55. if (batch->embd) {
  56. if (ubatch.equal_seqs) {
  57. for (size_t i = 0; i < length; ++i) {
  58. memcpy(
  59. ubatch.embd + (n_embd * (ubatch.n_tokens + i)),
  60. batch->embd + (n_embd * ids[seq.offset + i]),
  61. n_embd * sizeof(float)
  62. );
  63. }
  64. } else {
  65. // simple split
  66. ubatch.embd = batch->embd + (n_embd * seq.offset);
  67. }
  68. } else {
  69. ubatch.embd = nullptr;
  70. }
  71. if (ubatch.equal_seqs) {
  72. for (size_t i = 0; i < length; ++i) {
  73. ubatch.pos[ubatch.n_tokens + i] = batch->pos[ids[seq.offset + i]];
  74. }
  75. } else {
  76. // simple split
  77. ubatch.pos = batch->pos + seq.offset;
  78. }
  79. if (ubatch.equal_seqs) {
  80. ubatch.n_seq_id[ubatch.n_seqs] = seq.n_seq_id;
  81. if (seq.seq_id) {
  82. ubatch.seq_id[ubatch.n_seqs] = seq.seq_id;
  83. }
  84. } else {
  85. // simple split
  86. if (batch->n_seq_id) {
  87. ubatch.n_seq_id = batch->n_seq_id + seq.offset;
  88. } else {
  89. for (size_t i = 0; i < length; ++i) {
  90. ubatch.n_seq_id[ubatch.n_seqs + i] = 1;
  91. }
  92. }
  93. if (batch->seq_id) {
  94. ubatch.seq_id = batch->seq_id + seq.offset;
  95. }
  96. }
  97. if (logits_all) {
  98. for (size_t i = 0; i < length; ++i) {
  99. ubatch.output[ubatch.n_tokens + i] = 1;
  100. out_ids.push_back(ids[seq.offset + i]);
  101. }
  102. } else if (batch->logits) {
  103. if (ubatch.equal_seqs) {
  104. for (size_t i = 0; i < length; ++i) {
  105. size_t id = ids[seq.offset + i];
  106. int8_t is_output = batch->logits[id];
  107. ubatch.output[ubatch.n_tokens + i] = is_output;
  108. if (is_output) { out_ids.push_back(id); }
  109. }
  110. } else {
  111. // simple split
  112. ubatch.output = batch->logits + seq.offset;
  113. for (size_t i = 0; i < length; ++i) {
  114. if (ubatch.output[i] != 0) { out_ids.push_back(seq.offset + i); }
  115. }
  116. }
  117. } else {
  118. // only get last output
  119. for (size_t i = 0; i < length; ++i) {
  120. size_t id = ids[seq.offset + i];
  121. int8_t is_last = id == ids.size() - 1;
  122. ubatch.output[ubatch.n_tokens + i] = is_last;
  123. if (is_last) { out_ids.push_back(id); }
  124. }
  125. }
  126. if (ubatch.n_tokens == 0 && ubatch.n_seqs == 0) {
  127. ubatch.n_seq_tokens = ubatch.equal_seqs ? length : 1;
  128. }
  129. ubatch.n_tokens += length;
  130. ubatch.n_seqs += ubatch.equal_seqs ? 1 : length; // virtual sequences for simple splits
  131. seq.offset += length;
  132. seq.length -= length;
  133. n_tokens -= length;
  134. GGML_ASSERT(ubatch.n_tokens == ubatch.n_seq_tokens * ubatch.n_seqs);
  135. }
  136. llama_ubatch llama_sbatch::split_simple(size_t n_ubatch) {
  137. n_ubatch = n_tokens < n_ubatch ? n_tokens : n_ubatch;
  138. llama_ubatch ubatch = reserve_ubatch(n_ubatch, /* has_embd */ batch->embd != nullptr);
  139. ubatch.equal_seqs = false;
  140. if (!seq.empty()) {
  141. llama_sbatch_seq & s = seq[0];
  142. size_t length = s.length < n_ubatch ? s.length : n_ubatch;
  143. GGML_ASSERT(seq.size() == 1 && s.n_seq_id == 0); // don't mix with other splits
  144. add_seq_to_ubatch(ubatch, s, length);
  145. }
  146. return ubatch;
  147. }
  148. llama_ubatch llama_sbatch::split_equal(size_t n_ubatch) {
  149. n_ubatch = n_tokens < n_ubatch ? n_tokens : n_ubatch;
  150. llama_ubatch ubatch = reserve_ubatch(n_ubatch, /* has_embd */ batch->embd != nullptr);
  151. if (!seq.empty()) {
  152. size_t length = 0;
  153. size_t n_tokens_in_ubatch = 0;
  154. GGML_ASSERT(seq[0].n_seq_id > 0); // should not be mixed with simple splits
  155. // smallest first, because it's easier to split this way;
  156. // starting from the end to pop in constant time.
  157. for (size_t i = seq.size(); i-- > 0;) {
  158. llama_sbatch_seq & s = seq[i];
  159. GGML_ASSERT(s.length > 0);
  160. if (length == 0) {
  161. length = s.length < n_ubatch ? s.length : n_ubatch;
  162. }
  163. add_seq_to_ubatch(ubatch, s, length);
  164. n_tokens_in_ubatch += length;
  165. // shared prompts can't be mixed with any of their sequences,
  166. // so it's safer to compute them in their own ubatch
  167. if (s.n_seq_id > 1) { break; }
  168. // stop when there isn't enough space for another sequence
  169. if (length + n_tokens_in_ubatch > n_ubatch) { break; }
  170. }
  171. }
  172. return ubatch;
  173. }
  174. llama_ubatch llama_sbatch::split_seq(size_t n_ubatch) {
  175. n_ubatch = n_tokens < n_ubatch ? n_tokens : n_ubatch;
  176. llama_ubatch ubatch = reserve_ubatch(n_ubatch, /* has_embd */ batch->embd != nullptr);
  177. if (!seq.empty()) {
  178. llama_sbatch_seq & s = seq[seq.size() - 1];
  179. size_t length = s.length < n_ubatch ? s.length : n_ubatch;
  180. GGML_ASSERT(s.n_seq_id > 0); // should not be mixed with simple splits
  181. add_seq_to_ubatch(ubatch, s, length);
  182. }
  183. return ubatch;
  184. }
  185. void llama_sbatch::from_batch(const llama_batch & batch, size_t n_embd, bool simple_split, bool logits_all) {
  186. GGML_ASSERT(batch.n_tokens >= 0);
  187. this->batch = &batch;
  188. this->n_embd = n_embd;
  189. this->logits_all = logits_all;
  190. n_tokens = batch.n_tokens;
  191. ids.resize(n_tokens);
  192. out_ids.clear();
  193. // TODO: reserve out_ids and seq
  194. for (size_t i = 0; i < n_tokens; ++i) {
  195. ids[i] = i;
  196. }
  197. if (simple_split) {
  198. seq.resize(1);
  199. llama_sbatch_seq & s = seq[0];
  200. s.n_seq_id = 0;
  201. s.seq_id = nullptr;
  202. s.offset = 0;
  203. s.length = n_tokens;
  204. return;
  205. }
  206. std::sort(ids.begin(), ids.end(),
  207. [&batch](size_t a, size_t b) {
  208. int32_t n_seq_a = batch.n_seq_id ? batch.n_seq_id[a] : 1;
  209. int32_t n_seq_b = batch.n_seq_id ? batch.n_seq_id[b] : 1;
  210. // sort by seq_id, then by pos
  211. if (n_seq_a == n_seq_b) {
  212. if (batch.seq_id) {
  213. for (int32_t i = 0; i < n_seq_a; ++i) {
  214. llama_seq_id seq_id_a = batch.seq_id[a][i];
  215. llama_seq_id seq_id_b = batch.seq_id[b][i];
  216. // smaller seq_ids go first
  217. if (seq_id_a != seq_id_b) {
  218. return seq_id_a < seq_id_b;
  219. }
  220. }
  221. }
  222. // when all else is equal, sort by pos
  223. if (batch.pos) {
  224. return batch.pos[a] < batch.pos[b];
  225. }
  226. // no pos, sort by id
  227. return a < b;
  228. }
  229. // shared prompts go first
  230. return n_seq_a > n_seq_b;
  231. }
  232. );
  233. // init seq
  234. llama_sbatch_seq * last_seq = nullptr;
  235. for (size_t i = 0; i < n_tokens; ++i) {
  236. const size_t bi = ids[i];
  237. const int32_t n_seqs = batch.n_seq_id[bi];
  238. llama_seq_id * seq_ids = batch.seq_id[bi];
  239. if (last_seq != nullptr) {
  240. bool same = n_seqs == last_seq->n_seq_id;
  241. for (int32_t j = 0; same && j < n_seqs; ++j) {
  242. if (seq_ids[j] != last_seq->seq_id[j]) {
  243. same = false;
  244. }
  245. }
  246. if (same) {
  247. last_seq->length += 1;
  248. continue;
  249. }
  250. }
  251. llama_sbatch_seq new_seq = {n_seqs, seq_ids, i, 1};
  252. seq.push_back(new_seq);
  253. last_seq = &seq.back();
  254. }
  255. // keep shared prompts first at the end, then sort by length descending.
  256. std::sort(seq.begin(), seq.end(),
  257. [](llama_sbatch_seq & a, llama_sbatch_seq & b) {
  258. if (a.n_seq_id == b.n_seq_id) {
  259. return a.length > b.length;
  260. }
  261. return a.n_seq_id < b.n_seq_id;
  262. }
  263. );
  264. }
  265. llama_batch_allocr::llama_batch_allocr(struct llama_batch in_batch, llama_pos p0) {
  266. batch = in_batch;
  267. GGML_ASSERT(batch.n_tokens > 0);
  268. if (!batch.pos) {
  269. pos.resize(batch.n_tokens);
  270. for (int32_t i = 0; i < batch.n_tokens; i++) {
  271. pos[i] = i + p0;
  272. }
  273. batch.pos = pos.data();
  274. }
  275. if (!batch.n_seq_id) {
  276. n_seq_id.resize(batch.n_tokens);
  277. for (int32_t i = 0; i < batch.n_tokens; i++) {
  278. n_seq_id[i] = seq_id_0.size();
  279. }
  280. batch.n_seq_id = n_seq_id.data();
  281. }
  282. if (!batch.seq_id) {
  283. seq_id.resize(batch.n_tokens + 1);
  284. seq_id[batch.n_tokens] = NULL;
  285. for (int32_t i = 0; i < batch.n_tokens; i++) {
  286. seq_id[i] = seq_id_0.data();
  287. }
  288. batch.seq_id = seq_id.data();
  289. }
  290. if (!batch.logits) {
  291. logits.resize(batch.n_tokens);
  292. logits[logits.size() - 1] = true;
  293. batch.logits = logits.data();
  294. }
  295. }
  296. //
  297. // interface implementation
  298. //
  299. struct llama_batch llama_batch_get_one(
  300. llama_token * tokens,
  301. int32_t n_tokens) {
  302. return {
  303. /*n_tokens =*/ n_tokens,
  304. /*tokens =*/ tokens,
  305. /*embd =*/ nullptr,
  306. /*n_embd =*/ 0,
  307. /*pos =*/ nullptr,
  308. /*n_seq_id =*/ nullptr,
  309. /*seq_id =*/ nullptr,
  310. /*logits =*/ nullptr,
  311. };
  312. }
  313. struct llama_batch llama_batch_init(int32_t n_tokens_alloc, int32_t embd, int32_t n_seq_max) {
  314. llama_batch batch = {
  315. /*n_tokens =*/ 0,
  316. /*tokens =*/ nullptr,
  317. /*embd =*/ nullptr,
  318. /*n_embd =*/ 0,
  319. /*pos =*/ nullptr,
  320. /*n_seq_id =*/ nullptr,
  321. /*seq_id =*/ nullptr,
  322. /*logits =*/ nullptr,
  323. };
  324. if (embd) {
  325. batch.embd = (float *) malloc(sizeof(float) * n_tokens_alloc * embd);
  326. batch.n_embd = embd;
  327. } else {
  328. batch.token = (llama_token *) malloc(sizeof(llama_token) * n_tokens_alloc);
  329. }
  330. batch.pos = (llama_pos *) malloc(sizeof(llama_pos) * n_tokens_alloc);
  331. batch.n_seq_id = (int32_t *) malloc(sizeof(int32_t) * n_tokens_alloc);
  332. batch.seq_id = (llama_seq_id **) malloc(sizeof(llama_seq_id *) * (n_tokens_alloc + 1));
  333. for (int i = 0; i < n_tokens_alloc; ++i) {
  334. batch.seq_id[i] = (llama_seq_id *) malloc(sizeof(llama_seq_id) * n_seq_max);
  335. }
  336. batch.seq_id[n_tokens_alloc] = nullptr;
  337. batch.logits = (int8_t *) malloc(sizeof(int8_t) * n_tokens_alloc);
  338. return batch;
  339. }
  340. void llama_batch_free(struct llama_batch batch) {
  341. if (batch.token) free(batch.token);
  342. if (batch.embd) free(batch.embd);
  343. if (batch.pos) free(batch.pos);
  344. if (batch.n_seq_id) free(batch.n_seq_id);
  345. if (batch.seq_id) {
  346. for (int i = 0; batch.seq_id[i] != nullptr; ++i) {
  347. free(batch.seq_id[i]);
  348. }
  349. free(batch.seq_id);
  350. }
  351. if (batch.logits) free(batch.logits);
  352. }