Skip to content

Commit 1e72b65

Browse files
GradientAI Auto ROPE Base calculation (LostRuins#910)
* GradientAI Auto ROPE Base calculation https://gradient.ai/blog/scaling-rotational-embeddings-for-long-context-language-models has a formula that better fits the ideal rope scaling. Tested with Lllama3, checked calculation is correct for llama2. Retains logic for not scaling rope if under trained CTX. * add in solar scaling logic Solar based models require the context values to be multiplied by 8. This is (i'm guessing) because the positions as based on a 32k context, but sliding window of 4k. * Update model_adapter.h adding in tensor count to identify solar models based on tensor count of 435. * Update model_adapter.cpp add in n_tensor count for solar identification * refactor and cleanup GradientAI rope scaling --------- Co-authored-by: Concedo <39025047+LostRuins@users.noreply.github.com>
1 parent 49e4c3f commit 1e72b65

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

gpttype_adapter.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//No dynamic memory allocation! Setup structs with FIXED (known) shapes and sizes for ALL output fields
88
//Python will ALWAYS provide the memory, we just write to it.
99

10+
#include <cmath>
1011
#include <time.h>
1112
#include <mutex>
1213
#include "model_adapter.h"
@@ -787,6 +788,19 @@ static int GetBatchSize(int desiredBlasBatchSize,FileFormat in_file_format)
787788
return desiredBlasBatchSize;
788789
}
789790

791+
//this function applies automatic scaling to rope freq base when the desired context exceeds trained context
792+
static float CalcGradientAIRopeFreqBase(float original_rope_base, int n_ctx_train, int n_ctx_desired, bool is_solar)
793+
{
794+
if(n_ctx_desired <= n_ctx_train || n_ctx_desired <= 2048)
795+
{
796+
return original_rope_base;
797+
}
798+
float ctx_multiplier = (is_solar?8.0f:1.0f);
799+
float chi_ctx_train_value = (n_ctx_train * ctx_multiplier) / 6.28318;
800+
float chi_ctx_value = (n_ctx_desired * ctx_multiplier) / 6.28318;
801+
return powf(original_rope_base, logf(chi_ctx_value) / logf(chi_ctx_train_value));
802+
}
803+
790804
ModelLoadResult gpttype_load_model(const load_model_inputs inputs, FileFormat in_file_format, FileFormatExtraMeta in_file_format_meta)
791805
{
792806
ggml_time_init();
@@ -835,28 +849,16 @@ ModelLoadResult gpttype_load_model(const load_model_inputs inputs, FileFormat in
835849
}
836850
else
837851
{
838-
rope_freq_scale = 1.0f;
839-
if (kcpp_params->n_ctx <= 2048) //normie mode
852+
//Set freq base for all, including non GGUF. If we are using GGUF, this will be overwritten with more accurate values later.
853+
rope_freq_base = CalcGradientAIRopeFreqBase(10000.0f,2048,kcpp_params->n_ctx,false);
854+
if(file_format==FileFormat::GGUF_GENERIC)
840855
{
841-
rope_freq_base = 10000.0f;
856+
printf("Using automatic RoPE scaling. If the model has customized RoPE settings, they will be used directly instead!\n");
842857
}
843858
else
844859
{
845-
//approximate NTK aware ctx
846-
auto effectivenctx = kcpp_params->n_ctx;
847-
if((file_format == FileFormat::GGUF_GENERIC) && file_format_meta.n_ctx_train > 2048)
848-
{
849-
float factor = file_format_meta.n_ctx_train/2048;
850-
effectivenctx = effectivenctx/factor;
851-
}
852-
float magic_multiplier = 8.0f;
853-
float base_multiplier = effectivenctx*magic_multiplier;
854-
float base_raw = 10000.0f;
855-
rope_freq_base = (effectivenctx <= 2048 ? base_raw : base_multiplier);
856-
860+
printf("Using Automatic RoPE scaling, Pre-GGUF (scale:%.3f, base:%.1f).\n",rope_freq_scale, rope_freq_base);
857861
}
858-
859-
printf("Using automatic RoPE scaling. If the model has customized RoPE settings, they will be used directly instead!\n");
860862
}
861863
gptj_ctx_v3.hparams.rope_freq_scale = neox_ctx_v3.hparams.rope_freq_scale = rope_freq_scale;
862864
gptj_ctx_v3.hparams.rope_freq_base = neox_ctx_v3.hparams.rope_freq_base = rope_freq_base;
@@ -1085,7 +1087,7 @@ ModelLoadResult gpttype_load_model(const load_model_inputs inputs, FileFormat in
10851087
}
10861088
else
10871089
{
1088-
//if the model modifes rope in any way, use the model values. Otherwise, use our automatic ones
1090+
//if the model modifes rope in any way, or uses yarn, use the model values. Otherwise, use our automatic ones
10891091
//special exception for llama, which uses auto scale
10901092
if((llamamodel->hparams.rope_freq_base_train!=10000.0f && llamamodel->hparams.rope_freq_base_train!=500000.0f) ||
10911093
llamamodel->hparams.rope_freq_scale_train!=1.0f ||
@@ -1095,8 +1097,8 @@ ModelLoadResult gpttype_load_model(const load_model_inputs inputs, FileFormat in
10951097
}
10961098
else
10971099
{
1098-
float multiplier_rope_base = llamamodel->hparams.rope_freq_base_train/10000.0f;
1099-
rope_freq_base *= multiplier_rope_base;
1100+
//Calculate rope_freq_base using the gradientAI formula, solar requires ctx *8 for correct scaling
1101+
rope_freq_base = CalcGradientAIRopeFreqBase(llamamodel->hparams.rope_freq_base_train, file_format_meta.n_ctx_train, kcpp_params->n_ctx, file_format_meta.model_architecture==GGUFArch::ARCH_SOLAR);
11001102
llama_ctx_params.rope_freq_base = rope_freq_base;
11011103
llama_ctx_params.rope_freq_scale = rope_freq_scale;
11021104
printf("Automatic RoPE Scaling: Using (scale:%.3f, base:%.1f).\n", rope_freq_scale, rope_freq_base);
@@ -2467,4 +2469,4 @@ generation_outputs gpttype_generate(const generation_inputs inputs)
24672469
concat_output_mtx.unlock();
24682470
output.text = concat_output_reader_copy_res.c_str();
24692471
return output;
2470-
}
2472+
}

model_adapter.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ void print_tok_vec(std::vector<float> &embd)
271271

272272
if(modelarch!="" && fileformatmeta!=nullptr)
273273
{
274+
int n_tensors = gguf_get_n_tensors(ctx);
275+
float freq_base_train = 0;
276+
274277
std::string fkey = modelarch+".context_length";
275278
int keyidx = gguf_find_key(ctx, fkey.c_str());
276279
if (keyidx != -1) {
@@ -281,8 +284,14 @@ void print_tok_vec(std::vector<float> &embd)
281284
if (keyidx != -1) {
282285
fileformatmeta->n_expert_count = gguf_get_val_u32(ctx, keyidx);
283286
}
287+
fkey = modelarch+".rope.freq_base";
288+
keyidx = gguf_find_key(ctx, fkey.c_str());
289+
if (keyidx != -1) {
290+
freq_base_train = gguf_get_val_f32(ctx, keyidx);
291+
}
284292

285293
int filever = gguf_get_version(ctx);
294+
286295
fileformatmeta->fileversion = filever;
287296
fileformatmeta->model_architecture = GGUFArch::ARCH_DEFAULT;
288297
if(modelarch=="phi2")
@@ -297,7 +306,12 @@ void print_tok_vec(std::vector<float> &embd)
297306
{
298307
fileformatmeta->model_architecture = GGUFArch::ARCH_MAMBA;
299308
}
309+
else if(modelarch=="llama" && freq_base_train==10000.0f && n_tensors==435)
310+
{
311+
fileformatmeta->model_architecture = GGUFArch::ARCH_SOLAR;
312+
}
300313
}
314+
301315
gguf_free(ctx);
302316
}
303317

@@ -531,4 +545,4 @@ void print_tok_vec(std::vector<float> &embd)
531545
//remove all tokens between start part and start of LCS in new prompt, thus avoiding shift
532546
//if LCS not found or mismatched, regenerate. chop new prompt and repeat from step B
533547
}
534-
}
548+
}

model_adapter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ enum GGUFArch
5656
ARCH_FALCON = 1,
5757
ARCH_PHI = 2,
5858
ARCH_MAMBA = 3,
59+
ARCH_SOLAR = 4,
5960
};
6061

6162
struct FileFormatExtraMeta

0 commit comments

Comments
 (0)