okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fe57a0cb5
main
1// src/rope.hpp's YaRN against ggml's, on the CPU: the band (ggml_rope_yarn_corr_dims), the per-pair angle 2// (rope_yarn with ext_factor 1, theta from ggml-cuda's theta_scale powers) and the net magnitude, for the scales and 3// trained lengths the docs measured, the ramp moved by FNX_YARN_ORIG_CTX=131072 as the counter-case the long-prompt 4// check tells apart, and the settings yarn_rope refuses; and choose_rope, the scaling a model's own YaRN keys and the 5// settings come to, every agreement and disagreement. Exits 1 after reporting every difference. 6// bin/rope_test 7#include <cmath> 8#include <cstdio> 9#include <stdexcept> 10#include <string> 11 12#include "../src/rope.hpp" 13 14namespace { 15 16int fails = 0 ; 17void fail (const std::string & what ) { 18 std::printf ("FAIL: %s\n" ,what .c_str ()); 19++ fails ; 20} 21 22// ggml.c at a37cc46, ggml_rope_yarn_corr_dim and ggml_rope_yarn_corr_dims 23float ggml_corr_dim (int n_dims ,int n_ctx_orig ,float n_rot ,float base ) { 24return n_dims * logf (n_ctx_orig / (n_rot * 2 * (float )3.14159265358979323846 )) / (2 * logf (base )); 25} 26void ggml_corr_dims (int n_dims ,int n_ctx_orig ,float freq_base ,float beta_fast ,float beta_slow ,float dims [2 ]) { 27const float start = floorf (ggml_corr_dim (n_dims ,n_ctx_orig ,beta_fast ,freq_base )); 28const float end = ceilf (ggml_corr_dim (n_dims ,n_ctx_orig ,beta_slow ,freq_base )); 29dims [0 ]= fmaxf (0 ,start ); 30dims [1 ]= fminf (n_dims - 1 ,end ); 31} 32// ggml-cuda/rope.cu at a37cc46: rope_yarn_ramp and rope_yarn's theta (ext_factor 1), i0 the dimension index 33float ggml_ramp (float low ,float high ,int i0 ) { 34const float y = (i0 /2 - low ) /fmaxf (0.001f ,high - low ); 35return 1.0f - fminf (1.0f ,fmaxf (0.0f ,y )); 36} 37float ggml_theta (float theta_extrap ,float freq_scale ,const float dims [2 ],int i0 ) { 38const float theta_interp = freq_scale * theta_extrap ; 39const float ramp_mix = ggml_ramp (dims [0 ],dims [1 ],i0 ); 40return theta_interp * (1 - ramp_mix )+ theta_extrap * ramp_mix ; 41} 42 43void check (float base ,int orig ,double S ,int nrot ,float want_low ,float want_high ) { 44char tag [128 ]; 45 std::snprintf (tag ,sizeof tag ,"base %g orig %d x%g nrot %d" ,base ,orig ,S ,nrot ); 46const fnx::Rope r = fnx::yarn_rope (base ,orig ,S ,nrot ); 47float dims [2 ]; 48ggml_corr_dims (nrot ,orig ,base ,32.0f ,1.0f ,dims ); 49if (r .c_low != dims [0 ]|| r .c_high != dims [1 ]) 50fail (std::string (tag )+ ": band " + std::to_string (r .c_low )+ ".." + std::to_string (r .c_high )+ ", ggml " + std::to_string (dims [0 ])+ 51".." + std::to_string (dims [1 ])); 52if (r .c_low != want_low || r .c_high != want_high ) 53fail (std::string (tag )+ ": band " + std::to_string (r .c_low )+ ".." + std::to_string (r .c_high )+ ", recorded " + 54 std::to_string (want_low )+ ".." + std::to_string (want_high )); 55// llama-context.cpp: attn_factor = get_mscale(S, 1) / (1 + 0.1 ln S); rope_yarn: mscale = attn_factor * (1 + 0.1 ln S) 56const float factor = (float )S ,get_mscale = 0.1f * logf (factor )+ 1.0f ; 57const float attn_factor = get_mscale * (1.0f / (1.0f + 0.1f * logf (factor ))); 58const float mscale = attn_factor * (1.0f + 0.1f * logf (1.0f / (float ) (1.0 /S ))); 59if (std::fabs (r .mscale - mscale )> 1e-6f * mscale )fail (std::string (tag )+ ": mscale " + std::to_string (r .mscale )+ ", llama.cpp " + std::to_string (mscale )); 60const float theta_scale = powf (base ,-2.0f /nrot ); 61for (int pos : {1 ,1000 ,262143 ,900000 }) 62for (int i = 0 ;i < nrot /2 ;++ i ) { 63const float mine = (float )pos * fnx::rope_unit_freq (r ,i ,nrot ); 64const float theirs = ggml_theta ((float )pos * powf (theta_scale , (float )i ), (float ) (1.0 /S ),dims ,2 * i ); 65if (std::fabs (mine - theirs )> 2e-5f * std::fabs (theirs )) { 66fail (std::string (tag )+ ": pair " + std::to_string (i )+ " at " + std::to_string (pos )+ ": angle " + std::to_string (mine )+ 67", ggml " + std::to_string (theirs )); 68return ; 69 } 70 } 71} 72 73// choose_rope: the model's YaRN keys and the settings, each case (model factor, model orig, context_length, env scale 74// -1 unset / 0 off, env orig 0 unset) with the scale, trained length and limit it must give, or a refusal 75void choice (const char * what ,double mf ,long long mo ,long long ctx ,double es ,long long eo ,double S ,long long orig ,long long limit ) { 76try { 77const fnx::RopeChoice c = fnx::choose_rope (mf ,mo ,ctx ,es ,eo ); 78if (c .S != S || c .orig != orig || c .limit != limit ) 79fail (std::string (what )+ ": x" + std::to_string (c .S )+ " from " + std::to_string (c .orig )+ ", limit " + std::to_string (c .limit )+ 80"; want x" + std::to_string (S )+ " from " + std::to_string (orig )+ ", limit " + std::to_string (limit )); 81 }catch (const std::runtime_error & e ) { 82fail (std::string (what )+ ": refused (" + e .what ()+ ")" ); 83 } 84} 85void choice_refused (const char * what ,double mf ,long long mo ,long long ctx ,double es ,long long eo ) { 86try { 87 fnx::choose_rope (mf ,mo ,ctx ,es ,eo ); 88fail (std::string ("accepted " )+ what ); 89 }catch (const std::runtime_error & ) { 90 } 91} 92 93void refuses (float base ,long long orig ,double S ,const char * why ) { 94try { 95 fnx::yarn_rope (base ,orig ,S ,64 ); 96fail (std::string ("accepted " )+ why ); 97 }catch (const std::runtime_error & ) { 98 } 99} 100 101}// namespace 102 103int main () { 104// this model: base 1e7, 64 rotated dims, trained 262144 (the engine's log: pairs 14..22, mscale 1.0693 at x2) 105check (1e7f ,262144 ,2.0 ,64 ,14 ,22 ); 106check (1e7f ,262144 ,4.0 ,64 ,14 ,22 ); 107// the counter-case: a ramp moved by FNX_YARN_ORIG_CTX=131072, 1.4 dimensions down; it must differ from the above 108check (1e7f ,131072 ,2.0 ,64 ,12 ,20 ); 109// a common shape elsewhere: base 1e4, 128 rotated dims, trained 4096 110check (1e4f ,4096 ,4.0 ,128 ,20 ,46 ); 111// unscaled: exactly the model's frequencies, magnitude 1 112const fnx::Rope plain {1e7f }; 113for (int i = 0 ;i < 32 ;++ i ) 114if (fnx::rope_unit_freq (plain ,i ,64 )!= powf (1e7f ,-2.0f * (float )i /64.0f ))fail ("unscaled pair " + std::to_string (i )+ " is not the model's" ); 115if (plain .mscale != 1.0f )fail ("unscaled mscale is not 1" ); 116// this model (no YaRN keys, context_length 262144): unscaled, scaled by the setting, its trained length moved 117choice ("no keys, no setting" ,0 ,0 ,262144 ,-1 ,0 ,0 ,0 ,262144 ); 118choice ("no keys, FNX_ROPE_SCALE=1" ,0 ,0 ,262144 ,0 ,0 ,0 ,0 ,262144 ); 119choice ("no keys, x2" ,0 ,0 ,262144 ,2 ,0 ,2 ,262144 ,524288 ); 120choice ("no keys, x2 from 131072" ,0 ,0 ,262144 ,2 ,131072 ,2 ,131072 ,262144 ); 121choice_refused ("FNX_YARN_ORIG_CTX without a scale" ,0 ,0 ,262144 ,-1 ,131072 ); 122choice_refused ("FNX_YARN_ORIG_CTX past the trained length (an extra digit)" ,0 ,0 ,262144 ,2 ,2621440 ); 123// a model with its own YaRN (factor 4 from 32768, context_length 131072): run as llama.cpp runs it, with or without 124// the same settings; a setting that disagrees is refused 125choice ("yarn keys, no setting" ,4 ,32768 ,131072 ,-1 ,0 ,4 ,32768 ,131072 ); 126choice ("yarn keys, the same x4" ,4 ,32768 ,131072 ,4 ,0 ,4 ,32768 ,131072 ); 127choice ("yarn keys, the same x4 from 32768" ,4 ,32768 ,131072 ,4 ,32768 ,4 ,32768 ,131072 ); 128choice_refused ("yarn keys x4, setting x2" ,4 ,32768 ,131072 ,2 ,0 ); 129choice_refused ("yarn keys x4, FNX_ROPE_SCALE=1" ,4 ,32768 ,131072 ,0 ,0 ); 130choice_refused ("yarn keys from 32768, FNX_YARN_ORIG_CTX 65536" ,4 ,32768 ,131072 ,4 ,65536 ); 131// YaRN keys without original_context_length scale from context_length, as llama.cpp's n_ctx_orig_yarn does 132choice ("yarn keys, no original length" ,2 ,0 ,262144 ,-1 ,0 ,2 ,262144 ,524288 ); 133refuses (1e7f ,2000000000 ,2.0 ,"a trained length whose band falls past the last pair" ); 134refuses (1.0f ,262144 ,2.0 ,"a rotary base of 1" ); 135refuses (1e7f ,0 ,2.0 ,"a trained length of 0" ); 136refuses (1e7f ,262144 ,1.0 ,"a scale of 1" ); 137if (fails ) { 138 std::printf ("rope_test: %d failed\n" ,fails ); 139return 1 ; 140 } 141 std::printf ("rope_test: the band, every pair's angle and the magnitude match ggml's; the bad settings are refused\n" ); 142return 0 ; 143}