Thanks to visit codestin.com
Credit goes to github.com

Skip to content

batch : rework llama_batch_allocr #14153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 13, 2025
Merged

batch : rework llama_batch_allocr #14153

merged 5 commits into from
Jun 13, 2025

Conversation

ggerganov
Copy link
Member

@ggerganov ggerganov commented Jun 12, 2025

  • Move input batch validation logic to the llama_batch_allocr
  • Persist the state of the llama_batch_allocr in llama_context to avoid allocating memory for each batch
  • Fix some integer types (note: we are not very consistent with the int32_t and uint32_t, need to do some refactoring and fix this)
  • Count outputs in llama_batch_allocr
  • Prepare for llama_ubatch indexing refactor

Next PRs:

  • Add multi-seq validation for input batch
  • Add LLAMA_BATCH_DEBUG env
  • Rework llama_ubatch indexing

Comment on lines +838 to 839
for (uint32_t i = 0; i < n_tokens; i++) {
const llama_seq_id seq_id = ubatch.seq_id[i][0];
Copy link
Member Author

@ggerganov ggerganov Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compilade Regarding this comment from earlier, how does this sequence traversal work correctly when the ubatch is created with split_simple()?

AFAIU the original meaning of ubatch.seq_id[i][j] was "the jth sequence of the ith token". With split_equal(), this now changes to "the ith sequence and j == 0". What is not clear to me is if I used split_simple() how could the sequence traversal be correct?

for (uint32_t s = 0; s < ubatch.n_seqs; ++s) {
const llama_seq_id seq_id = ubatch.seq_id[s][0];
if (embd_seq_out.find(seq_id) != embd_seq_out.end()) {
continue;
}
embd_seq_out[seq_id].resize(n_embd);

I am planning to rework this in some way, so any suggestions how to improve this logic are welcome.

Copy link
Collaborator

@compilade compilade Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggerganov
With split_simple(), an invariant is that ubatch.n_seqs == n_tokens, and ubatch.n_seq_tokens == 1, because the sequences are not aggregated.

ubatch.n_seqs += ubatch.equal_seqs ? 1 : length; // virtual sequences for simple splits

This makes traversal which would work correctly with split_equal also be correct with split_simple, even though the seq_ids are definitely repeated (when ubatch.equal_seqs == false, ubatch.n_seqs doesn't really map to distinct sequences).

I'm not sure how to make it more obvious while still sharing the same traversal code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I understand that this traversal over the tokens is correct for both split strategies:

for (uint32_t s = 0; s < n_seqs; ++s) {
const llama_seq_id seq_id = ubatch->seq_id[s][0];
for (uint32_t j = 0; j < n_seq_tokens; ++j) {
const uint32_t idx = s*n_seq_tokens + j;
const llama_pos p1 = ubatch->pos[idx];

However, if I want to traverse over the unique sequence ids in the ubatch, or traverse over all sequence ids to which a token in the ubatch is assigned, there is no way to do it correctly for both splits. Is this correct?

For example, in the snippet above, if I wanted to get the list of all sequence ids of token idx, there is no way to do it without checking the ubatch.equal_seqs. Correct?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I want to traverse over the unique sequence ids in the ubatch

Yes, traversing unique seq_ids with simple splits (when ubatch.equal_seqs == false) is a bit more complicated, because they are not aggregated (simple splits are plain slices of the user-provided batch).

traverse over all sequence ids to which a token in the ubatch is assigned

This is easier, though, and possible by traversing ubatch.seq_id[s][_] with ubatch.n_seq_id[s]. For example:

for (uint32_t s = 0; s < n_seqs; ++s) {
const uint32_t n_seq_id = ubatch.n_seq_id[s];
for (uint32_t j = 0; j < n_seq_id; ++j) {
const llama_seq_id seq_id = ubatch.seq_id[s][j];


for (uint32_t s = 0; s < n_seqs; ++s) {
const llama_seq_id seq_id = ubatch->seq_id[s][0];
for (uint32_t j = 0; j < n_seq_tokens; ++j) {
const uint32_t idx = s*n_seq_tokens + j;
const llama_pos p1 = ubatch->pos[idx];

[...]

For example, in the snippet above, if I wanted to get the list of all sequence ids of token idx

In that snippet, seq_id would need to be defined later:

 for (uint32_t s = 0; s < n_seqs; ++s) {

     for (uint32_t j = 0; j < n_seq_tokens; ++j) {
         const uint32_t idx = s*n_seq_tokens + j;

         const llama_pos p1 = ubatch->pos[idx];

         for (uint32_t k = 0; k < ubatch.n_seq_id[s]; ++k) {
             const llama_seq_id seq_id = ubatch->seq_id[s][k];

Although depending on what you need it's also possible to swap the two inner loops:

 for (uint32_t s = 0; s < n_seqs; ++s) {

     for (uint32_t k = 0; k < ubatch.n_seq_id[s]; ++k) {
         const llama_seq_id seq_id = ubatch->seq_id[s][k];

         for (uint32_t j = 0; j < n_seq_tokens; ++j) {
             const uint32_t idx = s*n_seq_tokens + j;

             const llama_pos p1 = ubatch->pos[idx];

In this situation, you would not need to check ubatch.equal_seqs unless unique sequences are required.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thank you. I think I understand now.

Comment on lines -21 to -22
int32_t * n_seq_id; // [n_seqs] // TODO: remove, should belong to only 1 sequence
llama_seq_id ** seq_id; // [n_seqs] // TODO: become llama_seq_id * seq_id;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided against these TODOs because multiple sequences per input token actually has some useful properties that cannot be achieved otherwise (for example see the hellaswag usage). Instead, will add logic to guarantee that the provided ids are valid, utilizing the memory's seq_pos_min() and seq_pos_max() methods.

@ggerganov ggerganov marked this pull request as ready for review June 13, 2025 08:02
@ggerganov ggerganov merged commit 60c6663 into master Jun 13, 2025
55 checks passed
@ggerganov ggerganov deleted the gg/batch-allocr-rework branch June 13, 2025 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants