Skip to content

Commit 7db161a

Browse files
committed
Added notes about what is actually stored in batch's pos
1 parent 54aa805 commit 7db161a

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/llama-graph.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ void llm_graph_input_pos::set_input(const llama_ubatch * ubatch) {
5555
ggml_backend_tensor_set(pos, pos_data.data(), 0, pos_data.size()*ggml_element_size(pos));
5656
} else {
5757
llama_pos * pos_ptr = ubatch->pos;
58+
// Normally, ubatch->pos stores linearly increasing position
59+
// However, some multi-modal models requires special position embedding (e.g. M-Rope in qwen2vl and qwen2.5vl)
60+
// But linearly increasing position is still needed for proper causal attention masking
61+
// So we store both of them: the first n_tokens elements are not changed, while model-specific positions are appended after that.
5862
if (ubatch->embd && n_pos_per_embd > 1) pos_ptr += n_tokens; // use mrope positions
5963
ggml_backend_tensor_set(pos, pos_ptr, 0, n_tokens * n_pos_per_embd * ggml_element_size(pos));
6064
}

tools/mtmd/mtmd-helper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ llama_pos mtmd_helper_get_n_pos(const mtmd_input_chunks * chunks) {
5555

5656
// helper struct to make working with embd batch easier
5757
// note: this will be removed after llama_batch_ext refactoring
58+
// notes2: Normally, batch's `pos` stores linearly increasing position
59+
// However, some multi-modal models requires special position embedding (e.g. M-Rope in qwen2vl and qwen2.5vl)
60+
// But linearly increasing position is still needed for proper causal attention masking
61+
// So we store both of them: the first n_tokens elements are not changed, while model-specific positions are appended after that.
62+
// So `pos` has `n_tokens * (n_pos_per_embd + 1)` elements
5863
struct decode_embd_batch {
5964
int n_pos_per_embd;
6065
int n_mmproj_embd;

0 commit comments

Comments
 (0)