File tree Expand file tree Collapse file tree 5 files changed +26
-5
lines changed Expand file tree Collapse file tree 5 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -166,6 +166,8 @@ bool llama_batch_allocr::init(
166166
167167 // note: tracking the other way around is not necessary for now
168168 // seq_cpl[s0][s1] = true;
169+
170+ has_cpl = true ;
169171 }
170172 }
171173 }
@@ -459,9 +461,17 @@ llama_ubatch llama_batch_allocr::split_simple(uint32_t n_ubatch) {
459461 return ubatch_add (idxs, idxs.size (), false );
460462}
461463
462- llama_ubatch llama_batch_allocr::split_equal (uint32_t n_ubatch) {
464+ llama_ubatch llama_batch_allocr::split_equal (uint32_t n_ubatch, bool sequential) {
465+ if (sequential && has_cpl) {
466+ LLAMA_LOG_ERROR (" %s: sequential split is not supported when there are coupled sequences in the input batch\n " , __func__);
467+
468+ return {};
469+ }
470+
463471 std::vector<seq_set_t > cur_seq_set;
464472
473+ llama_seq_id last_seq_id = -1 ;
474+
465475 // determine the non-overlapping sequence sets participating in this ubatch
466476 for (int32_t i = 0 ; i < batch.n_tokens ; ++i) {
467477 if (used[i]) {
@@ -478,9 +488,16 @@ llama_ubatch llama_batch_allocr::split_equal(uint32_t n_ubatch) {
478488 }
479489 }
480490
491+ // accept only increasing sequence ids
492+ if (sequential) {
493+ add = add && (cur_seq_set.empty () || batch.seq_id [i][0 ] == last_seq_id + 1 );
494+ }
495+
481496 if (add) {
482497 cur_seq_set.push_back (seq_set[i]);
483498
499+ last_seq_id = batch.seq_id [i][0 ];
500+
484501 if (cur_seq_set.size () > n_ubatch) {
485502 break ;
486503 }
Original file line number Diff line number Diff line change @@ -69,7 +69,8 @@ class llama_batch_allocr {
6969 llama_ubatch split_simple (uint32_t n_ubatch);
7070
7171 // make ubatches of equal-length sequences sets
72- llama_ubatch split_equal (uint32_t n_ubatch);
72+ // if sequential == true, the tokens in the ubatch will have increasing sequential sequence ids
73+ llama_ubatch split_equal (uint32_t n_ubatch, bool sequential);
7374
7475 // sequence-set-wise split - each ubatch contains a single sequence-set
7576 llama_ubatch split_seq (uint32_t n_ubatch);
@@ -112,6 +113,9 @@ class llama_batch_allocr {
112113 using pos_set_t = std::set<llama_pos>;
113114 using seq_cpl_t = std::vector<bool >;
114115
116+ // helper flag to quickly determine if there are any coupled sequences in the batch
117+ bool has_cpl;
118+
115119 std::vector<pos_set_t > seq_pos; // seq_pos[s]: the set of positions in sequence s
116120 std::vector<seq_cpl_t > seq_cpl; // seq_cpl[s0][s1]: if sequence s0 is coupled to sequence s1
117121
Original file line number Diff line number Diff line change @@ -135,7 +135,7 @@ llama_memory_context_ptr llama_kv_cache_unified_iswa::init_batch(llama_batch_all
135135
136136 std::vector<llama_ubatch> ubatches;
137137 while (true ) {
138- auto ubatch = balloc.split_equal (n_ubatch);
138+ auto ubatch = balloc.split_equal (n_ubatch, false );
139139
140140 if (ubatch.n_tokens == 0 ) {
141141 break ;
Original file line number Diff line number Diff line change @@ -70,7 +70,7 @@ llama_memory_context_ptr llama_memory_hybrid::init_batch(llama_batch_allocr & ba
7070 // if all tokens are output, split by sequence
7171 ubatch = balloc.split_seq (n_ubatch);
7272 } else {
73- ubatch = balloc.split_equal (n_ubatch);
73+ ubatch = balloc.split_equal (n_ubatch, false );
7474 }
7575
7676 if (ubatch.n_tokens == 0 ) {
Original file line number Diff line number Diff line change @@ -374,7 +374,7 @@ llama_memory_context_ptr llama_memory_recurrent::init_batch(llama_batch_allocr &
374374 // if all tokens are output, split by sequence
375375 ubatch = balloc.split_seq (n_ubatch);
376376 } else {
377- ubatch = balloc.split_equal (n_ubatch);
377+ ubatch = balloc.split_equal (n_ubatch, false );
378378 }
379379
380380 if (ubatch.n_tokens == 0 ) {
You can’t perform that action at this time.
0 commit comments