Skip to content

Commit 9615831

Browse files
committed
llama : fix long/short rope factor selection by sequence length
1 parent f449e05 commit 9615831

29 files changed

Lines changed: 60 additions & 29 deletions

src/llama-batch.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
#include <algorithm>
1010
#include <sstream>
1111

12+
int32_t llama_ubatch::pos_max() const {
13+
int32_t res = 0;
14+
if (pos) {
15+
for (uint32_t i = 0; i < n_tokens; ++i) {
16+
res = std::max(res, (int32_t) pos[i]);
17+
}
18+
}
19+
return res;
20+
}
21+
1222
llama_batch_allocr::llama_batch_allocr(uint32_t n_pos_per_embd) : n_pos_per_embd(n_pos_per_embd) {
1323
const char * LLAMA_BATCH_DEBUG = getenv("LLAMA_BATCH_DEBUG");
1424
debug = LLAMA_BATCH_DEBUG ? atoi(LLAMA_BATCH_DEBUG) : 0;

src/llama-batch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ struct llama_ubatch {
2727
return n_pos >= 3;
2828
}
2929

30+
// largest token position in the ubatch (max over all sequences)
31+
int32_t pos_max() const;
32+
3033
uint32_t b_equal_seqs; // note: this is a boolean, but we use an int32_t for alignment
3134
// otherwise address sanitizer complains
3235
// TODO: whole_seqs for embeddings?

src/llama-context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,7 @@ llm_graph_params llama_context::graph_params(
24002400
/*.hparams =*/ model.hparams,
24012401
/*.cparams =*/ cparams,
24022402
/*.ubatch =*/ ubatch,
2403+
/*.pos_max =*/ ubatch.pos_max(),
24032404
/*.gtype =*/ gtype,
24042405
/*.sched =*/ sched.get(),
24052406
/*.backend_cpu =*/ backend_cpu,

src/llama-graph.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ llm_graph_context::llm_graph_context(const llm_graph_params & params) :
10541054
n_tokens (ubatch.n_tokens),
10551055
n_outputs (params.n_outputs),
10561056
n_ctx_orig (cparams.n_ctx_orig_yarn),
1057+
pos_max (params.pos_max),
10571058
pooling_type (cparams.pooling_type),
10581059
rope_type (hparams.rope_type),
10591060
sched (params.sched),

src/llama-graph.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@ struct llm_graph_params {
593593

594594
llama_ubatch ubatch; // note: intentionally make a copy
595595

596+
int32_t pos_max;
597+
596598
llm_graph_type gtype;
597599

598600
ggml_backend_sched_t sched;
@@ -660,6 +662,11 @@ struct llm_graph_params {
660662
return false;
661663
}
662664

665+
if ((pos_max >= (int32_t) hparams.n_ctx_orig_yarn) !=
666+
(other.pos_max >= (int32_t) hparams.n_ctx_orig_yarn)) {
667+
return false;
668+
}
669+
663670
if (n_outputs != other.n_outputs) {
664671
return false;
665672
}
@@ -813,6 +820,8 @@ struct llm_graph_context {
813820
const int64_t n_outputs;
814821
const int32_t n_ctx_orig; // yarn
815822

823+
const int32_t pos_max;
824+
816825
const enum llama_pooling_type pooling_type;
817826
const enum llama_rope_type rope_type;
818827

src/llama-kv-cache.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,13 @@ ggml_cgraph * llama_kv_cache::build_graph_shift(llm_graph_result * res, llama_co
19181918

19191919
const auto & cparams = lctx->get_cparams();
19201920

1921+
int32_t pos_max = 0;
1922+
for (uint32_t s = 0; s < n_stream; ++s) {
1923+
for (llama_seq_id seq_id = 0; seq_id < LLAMA_MAX_SEQ; ++seq_id) {
1924+
pos_max = std::max(pos_max, (int32_t) v_cells[s].seq_pos_max(seq_id));
1925+
}
1926+
}
1927+
19211928
for (const auto & layer : layers) {
19221929
const uint32_t il = layer.il;
19231930

@@ -1931,7 +1938,7 @@ ggml_cgraph * llama_kv_cache::build_graph_shift(llm_graph_result * res, llama_co
19311938
const float freq_base_l = model.get_rope_freq_base (cparams, il);
19321939
const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
19331940

1934-
ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
1941+
ggml_tensor * rope_factors = model.get_rope_factors(cparams, pos_max, il);
19351942

19361943
ggml_tensor * k =
19371944
ggml_view_3d(ctx, layer.k,

src/llama-model.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,15 +1985,15 @@ float llama_model::get_rope_freq_scale(const llama_cparams & cparams, int il) co
19851985
return hparams.is_swa(il) ? hparams.rope_freq_scale_train_swa : cparams.rope_freq_scale;
19861986
}
19871987

1988-
ggml_tensor * llama_model::get_rope_factors(const llama_cparams & cparams, int il) const {
1989-
const uint32_t n_ctx_seq = cparams.n_ctx_seq;
1988+
ggml_tensor * llama_model::get_rope_factors(const llama_cparams & cparams, int32_t pos_max, int il) const {
1989+
GGML_UNUSED(cparams);
19901990

1991-
// choose long/short freq factors based on the context size
19921991
if (layers[il].rope_freqs != nullptr) {
19931992
return layers[il].rope_freqs;
19941993
}
19951994

1996-
if (n_ctx_seq > hparams.n_ctx_orig_yarn) {
1995+
// long factors once positions reach the original training length
1996+
if (pos_max >= (int32_t) hparams.n_ctx_orig_yarn) {
19971997
return layers[il].rope_long;
19981998
}
19991999

src/llama-model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ struct llama_model {
638638
float get_rope_freq_base (const llama_cparams & cparams, int il) const;
639639
float get_rope_freq_scale(const llama_cparams & cparams, int il) const;
640640

641-
ggml_tensor * get_rope_factors(const llama_cparams & cparams, int il) const;
641+
ggml_tensor * get_rope_factors(const llama_cparams & cparams, int32_t pos_max, int il) const;
642642

643643
llama_memory_i * create_memory(const llama_memory_params & params, const llama_cparams & cparams) const;
644644

src/models/apertus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ llama_model_apertus::graph::graph(const llama_model & model, const llm_graph_par
8484

8585
// self-attention
8686
{
87-
ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
87+
ggml_tensor * rope_factors = model.get_rope_factors(cparams, pos_max, il);
8888

8989
// compute Q and K and RoPE them
9090
auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,

src/models/arcee.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ llama_model_arcee::graph::graph(const llama_model & model, const llm_graph_param
7777
// self-attention
7878
{
7979
// rope freq factors for llama3; may return nullptr for llama2 and other models
80-
ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
80+
ggml_tensor * rope_factors = model.get_rope_factors(cparams, pos_max, il);
8181

8282
// compute Q and K and RoPE them
8383
auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,

0 commit comments

Comments
 (0)