Skip to content

Commit cb2336f

Browse files
NexesenexLostRuins
andauthored
Gradient rope formula with offsets (ggml-org#938)
* Gradient rope formula with offsets Positive for Solar models Negative for Llama 1 and 2 models * Update gpttype_adapter.cpp Remove L1/L2 * cleanup PR, skip llama models, keep prints behind debug mode --------- Co-authored-by: Concedo <[email protected]>
1 parent dd5cda0 commit cb2336f

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

gpttype_adapter.cpp

+52-8
Original file line numberDiff line numberDiff line change
@@ -789,16 +789,59 @@ static int GetBatchSize(int desiredBlasBatchSize,FileFormat in_file_format)
789789
}
790790

791791
//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)
792+
static float CalcGradientAIRopeFreqBase(float original_rope_base, int n_ctx_train, int n_ctx_desired, GGUFArch model_arch)
793793
{
794794
if(n_ctx_desired <= n_ctx_train || n_ctx_desired <= 2048)
795795
{
796796
return original_rope_base;
797797
}
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));
798+
else
799+
{
800+
float ctx_multiplier = (model_arch==GGUFArch::ARCH_SOLAR?8.0f:1.0f);
801+
float chi_ctx_train_value = (n_ctx_train * ctx_multiplier) / 6.28318;
802+
float chi_ctx_value = (n_ctx_desired * ctx_multiplier) / 6.28318;
803+
float gradient_ai_rope_freq_base_value = powf(original_rope_base, log10f(chi_ctx_value) / log10f(chi_ctx_train_value));
804+
805+
if(debugmode==1)
806+
{
807+
printf("Trained max context length (value:%.d).\n", n_ctx_train);
808+
printf("Desired context length (value:%.d).\n", n_ctx_desired);
809+
printf("Solar context multiplier (value:%.3f).\n", ctx_multiplier);
810+
printf("Chi context train (value:%.3f).\n", chi_ctx_train_value);
811+
printf("Chi chosen context (value:%.3f).\n", chi_ctx_value);
812+
printf("Log Chi context train (value:%.3f).\n", log10f(chi_ctx_train_value));
813+
printf("Log Chi chosen context (value:%.3f).\n", log10f(chi_ctx_value));
814+
printf("RoPE Frequency Base value (value:%.3f).\n", original_rope_base);
815+
printf("RoPE base calculated via Gradient AI formula. (value:%.1f).\n", gradient_ai_rope_freq_base_value);
816+
}
817+
818+
if(model_arch==GGUFArch::ARCH_SOLAR)
819+
{
820+
float extended_rope_positive_offset_value = 1 + ((log10f(chi_ctx_value) - log10f(chi_ctx_train_value)) / ((log10f(chi_ctx_value) * log10f(chi_ctx_train_value)) - (log10f(chi_ctx_value) + log10f(chi_ctx_train_value))));
821+
float rope_freq_base_with_positive_offset = gradient_ai_rope_freq_base_value * extended_rope_positive_offset_value;
822+
if(debugmode==1)
823+
{
824+
printf("Extended RoPE Positive Offset (multiplicator) for Solar based models. (value:%.3f).\n", extended_rope_positive_offset_value);
825+
printf("RoPE base calculated via Gradient AI formula for Solar based models. (value:%.1f).\n", rope_freq_base_with_positive_offset);
826+
}
827+
return rope_freq_base_with_positive_offset;
828+
}
829+
// else if(model_arch==GGUFArch::ARCH_MISTRAL_LLAMA_1_AND_2)
830+
// {
831+
// float extended_rope_negative_offset_value = 1 + ((log10f(chi_ctx_value) - log10f(chi_ctx_train_value)) / (3.14159265358979323846 * 3.14159265358979323846));
832+
// float rope_freq_base_with_negative_offset = gradient_ai_rope_freq_base_value / extended_rope_negative_offset_value;
833+
// if(debugmode==1)
834+
// {
835+
// printf("Extended RoPE Negative Offset (divisor) for Llama 1 and 2 based models. (value:%.3f).\n", extended_rope_negative_offset_value);
836+
// printf("RoPE base calculated via Gradient AI formula for Llama 1 and 2 based models. (value:%.1f).\n", rope_freq_base_with_negative_offset);
837+
// }
838+
// return rope_freq_base_with_negative_offset;
839+
// }
840+
else
841+
{
842+
return gradient_ai_rope_freq_base_value;
843+
}
844+
}
802845
}
803846

804847
ModelLoadResult gpttype_load_model(const load_model_inputs inputs, FileFormat in_file_format, FileFormatExtraMeta in_file_format_meta)
@@ -850,10 +893,11 @@ ModelLoadResult gpttype_load_model(const load_model_inputs inputs, FileFormat in
850893
else
851894
{
852895
//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);
896+
rope_freq_base = CalcGradientAIRopeFreqBase(10000.0f,2048,kcpp_params->n_ctx, GGUFArch::ARCH_DEFAULT);
854897
if(file_format==FileFormat::GGUF_GENERIC)
855898
{
856-
printf("Using automatic RoPE scaling. If the model has customized RoPE settings, they will be used directly instead!\n");
899+
printf("Using automatic RoPE scaling for GGUF. If the model has custom RoPE settings, they'll be used directly instead!\n");
900+
printf("It means that the RoPE values written above will be replaced by the RoPE values indicated after loading.\n");
857901
}
858902
else
859903
{
@@ -1099,7 +1143,7 @@ ModelLoadResult gpttype_load_model(const load_model_inputs inputs, FileFormat in
10991143
else
11001144
{
11011145
//Calculate rope_freq_base using the gradientAI formula, solar requires ctx *8 for correct scaling
1102-
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);
1146+
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);
11031147
llama_ctx_params.rope_freq_base = rope_freq_base;
11041148
llama_ctx_params.rope_freq_scale = rope_freq_scale;
11051149
printf("Automatic RoPE Scaling: Using (scale:%.3f, base:%.1f).\n", rope_freq_scale, rope_freq_base);

model_adapter.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,16 @@ void print_tok_vec(std::vector<float> &embd)
306306
{
307307
fileformatmeta->model_architecture = GGUFArch::ARCH_MAMBA;
308308
}
309-
else if(modelarch=="llama" && freq_base_train==10000.0f && n_tensors==435)
309+
else if(modelarch=="llama" && freq_base_train==10000.0f && (n_tensors==435 || n_tensors==611))
310310
{
311311
fileformatmeta->model_architecture = GGUFArch::ARCH_SOLAR;
312312
}
313+
else if(modelarch=="llama" && freq_base_train==10000.0f)
314+
{
315+
fileformatmeta->model_architecture = GGUFArch::ARCH_MISTRAL_LLAMA_1_AND_2;
316+
}
317+
printf("Arch Category: %d\n",fileformatmeta->model_architecture);
318+
313319
}
314320

315321
gguf_free(ctx);

model_adapter.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ enum FileFormat
5252

5353
enum GGUFArch
5454
{
55-
ARCH_DEFAULT = 0, //used for llama and other generic gguf
55+
ARCH_DEFAULT = 0, //used for llama3 and other generic gguf
5656
ARCH_FALCON = 1,
5757
ARCH_PHI = 2,
5858
ARCH_MAMBA = 3,
5959
ARCH_SOLAR = 4,
60+
ARCH_MISTRAL_LLAMA_1_AND_2 = 5,
6061
};
6162

6263
struct FileFormatExtraMeta

0 commit comments

Comments
 (0)