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(
166
166
167
167
// note: tracking the other way around is not necessary for now
168
168
// seq_cpl[s0][s1] = true;
169
+
170
+ has_cpl = true ;
169
171
}
170
172
}
171
173
}
@@ -472,9 +474,17 @@ llama_ubatch llama_batch_allocr::split_simple(uint32_t n_ubatch) {
472
474
return ubatch_add (idxs, idxs.size (), false );
473
475
}
474
476
475
- llama_ubatch llama_batch_allocr::split_equal (uint32_t n_ubatch) {
477
+ llama_ubatch llama_batch_allocr::split_equal (uint32_t n_ubatch, bool sequential) {
478
+ if (sequential && has_cpl) {
479
+ LLAMA_LOG_ERROR (" %s: sequential split is not supported when there are coupled sequences in the input batch\n " , __func__);
480
+
481
+ return {};
482
+ }
483
+
476
484
std::vector<seq_set_t > cur_seq_set;
477
485
486
+ llama_seq_id last_seq_id = -1 ;
487
+
478
488
// determine the non-overlapping sequence sets participating in this ubatch
479
489
for (int32_t i = 0 ; i < batch.n_tokens ; ++i) {
480
490
if (used[i]) {
@@ -491,9 +501,16 @@ llama_ubatch llama_batch_allocr::split_equal(uint32_t n_ubatch) {
491
501
}
492
502
}
493
503
504
+ // accept only increasing sequence ids
505
+ if (sequential) {
506
+ add = add && (cur_seq_set.empty () || batch.seq_id [i][0 ] == last_seq_id + 1 );
507
+ }
508
+
494
509
if (add) {
495
510
cur_seq_set.push_back (seq_set[i]);
496
511
512
+ last_seq_id = batch.seq_id [i][0 ];
513
+
497
514
if (cur_seq_set.size () > n_ubatch) {
498
515
break ;
499
516
}
Original file line number Diff line number Diff line change @@ -72,7 +72,8 @@ class llama_batch_allocr {
72
72
llama_ubatch split_simple (uint32_t n_ubatch);
73
73
74
74
// make ubatches of equal-length sequences sets
75
- llama_ubatch split_equal (uint32_t n_ubatch);
75
+ // if sequential == true, the tokens in the ubatch will have increasing sequential sequence ids
76
+ llama_ubatch split_equal (uint32_t n_ubatch, bool sequential);
76
77
77
78
// sequence-set-wise split - each ubatch contains a single sequence-set
78
79
llama_ubatch split_seq (uint32_t n_ubatch);
@@ -115,6 +116,9 @@ class llama_batch_allocr {
115
116
using pos_set_t = std::set<llama_pos>;
116
117
using seq_cpl_t = std::vector<bool >;
117
118
119
+ // helper flag to quickly determine if there are any coupled sequences in the batch
120
+ bool has_cpl;
121
+
118
122
std::vector<pos_set_t > seq_pos; // seq_pos[s]: the set of positions in sequence s
119
123
std::vector<seq_cpl_t > seq_cpl; // seq_cpl[s0][s1]: if sequence s0 is coupled to sequence s1
120
124
Original file line number Diff line number Diff line change @@ -140,7 +140,7 @@ llama_memory_context_ptr llama_kv_cache_unified_iswa::init_batch(llama_batch_all
140
140
141
141
std::vector<llama_ubatch> ubatches;
142
142
while (true ) {
143
- auto ubatch = balloc.split_equal (n_ubatch);
143
+ auto ubatch = balloc.split_equal (n_ubatch, false );
144
144
145
145
if (ubatch.n_tokens == 0 ) {
146
146
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
70
70
// if all tokens are output, split by sequence
71
71
ubatch = balloc.split_seq (n_ubatch);
72
72
} else {
73
- ubatch = balloc.split_equal (n_ubatch);
73
+ ubatch = balloc.split_equal (n_ubatch, false );
74
74
}
75
75
76
76
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 &
374
374
// if all tokens are output, split by sequence
375
375
ubatch = balloc.split_seq (n_ubatch);
376
376
} else {
377
- ubatch = balloc.split_equal (n_ubatch);
377
+ ubatch = balloc.split_equal (n_ubatch, false );
378
378
}
379
379
380
380
if (balloc.get_n_used () < balloc.get_n_tokens ()) {
You can’t perform that action at this time.
0 commit comments