char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
73460f627
char/aliceai-t5-moe
1#include "ggml.h" 2#include "ggml-cpu.h" 3 4#include <cmath> 5#include <cstdio> 6#include <cstdlib> 7#include <cassert> 8#include <vector> 9 10#if defined(_MSC_VER ) 11#pragma warning(disable: 4244 4267) // possible loss of data 12#endif 13 14#if defined(__GNUC__ ) 15#pragma GCC diagnostic ignored "-Wdouble-promotion" 16#endif 17 18#define MAX_NARGS 3 19 20#undef MIN 21#undef MAX 22#define MIN (a ,b ) ((a) < (b) ? (a) : (b)) 23#define MAX (a ,b ) ((a) > (b) ? (a) : (b)) 24 25#define GGML_SILU_FP16 26 27// 28// logging 29// 30 31#if (GGML_DEBUG >=1 ) 32#define GGML_PRINT_DEBUG (...) printf(__VA_ARGS__) 33#else 34#define GGML_PRINT_DEBUG (...) 35#endif 36 37#if (GGML_DEBUG >=5 ) 38#define GGML_PRINT_DEBUG_5 (...) printf(__VA_ARGS__) 39#else 40#define GGML_PRINT_DEBUG_5 (...) 41#endif 42 43#if (GGML_DEBUG >=10 ) 44#define GGML_PRINT_DEBUG_10 (...) printf(__VA_ARGS__) 45#else 46#define GGML_PRINT_DEBUG_10 (...) 47#endif 48 49#define GGML_PRINT (...) printf(__VA_ARGS__) 50 51static float frand (void ) { 52return (float )rand ()/(float )RAND_MAX ; 53} 54 55static int irand (int n ) { 56if (n == 0 )return 0 ; 57return rand ()%n ; 58} 59 60static void get_random_dims (int64_t * dims ,int ndims ) { 61dims [0 ]= dims [1 ]= dims [2 ]= dims [3 ]= 1 ; 62 63for (int i = 0 ;i < ndims ;i ++ ) { 64dims [i ]= 1 + irand (4 ); 65 } 66} 67 68static struct ggml_tensor * get_random_tensor_f32 ( 69struct ggml_context * ctx0 , 70int ndims , 71const int64_t ne [], 72float fmin , 73float fmax ) { 74struct ggml_tensor * result = ggml_new_tensor (ctx0 ,GGML_TYPE_F32 ,ndims ,ne ); 75 76switch (ndims ) { 77case 1 : 78for (int i0 = 0 ;i0 < ne [0 ];i0 ++ ) { 79 ((float * )result -> data )[i0 ]= frand ()* (fmax - fmin )+ fmin ; 80 } 81break ; 82case 2 : 83for (int i1 = 0 ;i1 < ne [1 ];i1 ++ ) { 84for (int i0 = 0 ;i0 < ne [0 ];i0 ++ ) { 85 ((float * )result -> data )[i1 * ne [0 ]+ i0 ]= frand ()* (fmax - fmin )+ fmin ; 86 } 87 } 88break ; 89case 3 : 90for (int i2 = 0 ;i2 < ne [2 ];i2 ++ ) { 91for (int i1 = 0 ;i1 < ne [1 ];i1 ++ ) { 92for (int i0 = 0 ;i0 < ne [0 ];i0 ++ ) { 93 ((float * )result -> data )[i2 * ne [1 ]* ne [0 ]+ i1 * ne [0 ]+ i0 ]= frand ()* (fmax - fmin )+ fmin ; 94 } 95 } 96 } 97break ; 98case 4 : 99for (int i3 = 0 ;i3 < ne [3 ];i3 ++ ) { 100for (int i2 = 0 ;i2 < ne [2 ];i2 ++ ) { 101for (int i1 = 0 ;i1 < ne [1 ];i1 ++ ) { 102for (int i0 = 0 ;i0 < ne [0 ];i0 ++ ) { 103 ((float * )result -> data )[i3 * ne [2 ]* ne [1 ]* ne [0 ]+ i2 * ne [1 ]* ne [0 ]+ i1 * ne [0 ]+ i0 ]= frand ()* (fmax - fmin )+ fmin ; 104 } 105 } 106 } 107 } 108break ; 109default : 110assert (false); 111 }; 112 113return result ; 114} 115 116static void ggml_graph_compute_helper (std::vector < uint8_t > & buf ,ggml_cgraph * graph ,int n_threads ) { 117struct ggml_cplan plan = ggml_graph_plan (graph ,n_threads ,nullptr ); 118 119if (plan .work_size > 0 ) { 120buf .resize (plan .work_size ); 121plan .work_data = buf .data (); 122 } 123 124ggml_graph_compute (graph ,& plan ); 125} 126 127int main (int /*argc*/ ,const char ** /*argv*/ ) { 128struct ggml_init_params params = { 129/* .mem_size = */ 128 * 1024 * 1024 , 130/* .mem_buffer = */ NULL , 131/* .no_alloc = */ false, 132 }; 133 134 std::vector < uint8_t > work_buffer ; 135 136struct ggml_context * ctx0 = ggml_init (params ); 137 138struct ggml_tensor * x ; 139 140// rope f32 141for (int m = 0 ;m < 5 ;++ m ) { 142const int ndims = 4 ; 143 144const int64_t n_rot = 128 ; 145const int64_t ne [4 ]= {2 * n_rot ,32 ,73 ,1 }; 146 147const int n_past_0 = 100 ; 148const int n_past_2 = 33 ; 149 150struct ggml_tensor * r0 ; 151struct ggml_tensor * r1 ; 152struct ggml_tensor * r2 ; 153x = get_random_tensor_f32 (ctx0 ,ndims ,ne ,-1.0f ,1.0f ); 154int mode = -1 ; 155 156if (m < 2 ) { 157struct ggml_tensor * p0 = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ne [2 ]); 158struct ggml_tensor * p1 = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ne [2 ]); 159struct ggml_tensor * p2 = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ne [2 ]); 160 161for (int i = 0 ;i < ne [2 ];++ i ) { 162 ((int32_t * )p0 -> data )[i ]= n_past_0 + i ; 163 ((int32_t * )p1 -> data )[i ]= n_past_2 - n_past_0 ; 164 ((int32_t * )p2 -> data )[i ]= n_past_2 + i ; 165 } 166// test mode 0, 2 (standard, GPT-NeoX) 167mode = m == 0 ?GGML_ROPE_TYPE_NORMAL :GGML_ROPE_TYPE_NEOX ; 168 169// 100, 101, 102, ..., 172 170r0 = ggml_rope (ctx0 ,x ,p0 ,n_rot ,mode ); 171// -67, -67, -67, ..., -67 172r1 = ggml_rope (ctx0 ,r0 ,p1 ,n_rot ,mode );// "context swap", i.e. forget n_past_0 - n_past_2 tokens 173 174// 33, 34, 35, ..., 105 175r2 = ggml_rope (ctx0 ,x ,p2 ,n_rot ,mode ); 176 }else { 177// testing multi-dimension rope position embedding mode 178struct ggml_tensor * p0 = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ne [2 ]* 4 ); 179struct ggml_tensor * p1 = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ne [2 ]* 4 ); 180struct ggml_tensor * p2 = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ne [2 ]* 4 ); 181 182int sections [4 ]= {16 ,24 ,24 ,0 }; 183 184mode = (m == 2 ) ?GGML_ROPE_TYPE_MROPE : (m == 3 ) ?GGML_ROPE_TYPE_VISION :GGML_ROPE_TYPE_IMROPE ; 185 186for (int i = 0 ;i < ne [2 ];++ i ) { 187for (int j = 0 ;j < 4 ;++ j ) { 188 ((int32_t * )p0 -> data )[i + ne [2 ]* j ]= n_past_0 + i + j ; 189 ((int32_t * )p1 -> data )[i + ne [2 ]* j ]= n_past_2 - n_past_0 ; 190 ((int32_t * )p2 -> data )[i + ne [2 ]* j ]= n_past_2 + i + j ; 191 } 192 } 193 194// [[100, 101, 102, ..., 172], 195// [101, 102, 103, ..., 173], 196// [102, 103, 104, ..., 174]] 197r0 = ggml_rope_multi ( 198ctx0 ,x ,p0 ,nullptr , 199n_rot ,sections ,mode ,32768 ,1000000 ,1 ,0 ,1 ,32 ,1 ); 200// [[-67, -67, -67, ..., -67] 201// [-67, -67, -67, ..., -67] 202// [-67, -67, -67, ..., -67]] 203r1 = ggml_rope_multi ( 204ctx0 ,r0 ,p1 ,nullptr , 205n_rot ,sections ,mode ,32768 ,1000000 ,1 ,0 ,1 ,32 ,1 ); 206 207// [[33, 34, 35, ..., 105] 208// [34, 35, 36, ..., 106] 209// [35, 36, 37, ..., 107]] 210r2 = ggml_rope_multi ( 211ctx0 ,x ,p2 ,nullptr , 212n_rot ,sections ,mode ,32768 ,1000000 ,1 ,0 ,1 ,32 ,1 ); 213 } 214 215ggml_cgraph * gf = ggml_new_graph (ctx0 ); 216 217ggml_build_forward_expand (gf ,r0 ); 218ggml_build_forward_expand (gf ,r1 ); 219ggml_build_forward_expand (gf ,r2 ); 220 221ggml_graph_compute_helper (work_buffer ,gf ,4 ); 222 223// check that r1 and r2 are the same 224 { 225double sum0 = 0.0f ; 226double sum1 = 0.0f ; 227double diff = 0.0f ; 228 229const float * r1_data = (float * )r1 -> data ; 230const float * r2_data = (float * )r2 -> data ; 231 232const int n_elements = ggml_nelements (r1 ); 233 234for (int i = 0 ;i < n_elements ;++ i ) { 235sum0 += fabs (r1_data [i ]); 236sum1 += fabs (r2_data [i ]); 237diff += fabs (r1_data [i ]- r2_data [i ]); 238//if (fabs(r1_data[i] - r2_data[i]) > 0.0001f) { 239// printf("%d: %f %f\n", i, r1_data[i], r2_data[i]); 240// printf("diff: %f\n", fabs(r1_data[i] - r2_data[i])); 241//} 242 } 243 244//for (int i = 4096; i < 4096 + 128; ++i) { 245// printf("%f %f\n", r1_data[i], r2_data[i]); 246//} 247 248printf ("mode: %d\n" ,mode ); 249printf ("sum0: %f\n" ,sum0 ); 250printf ("sum1: %f\n" ,sum1 ); 251printf ("diff: %f\n" ,diff ); 252printf ("rel err: %f\n" ,diff /sum0 ); 253printf ("rel err: %f\n" ,diff /sum1 ); 254 255GGML_ASSERT (diff /sum0 < 0.0001f ); 256GGML_ASSERT (diff /sum1 < 0.0001f ); 257 } 258 } 259 260ggml_free (ctx0 ); 261 262return 0 ; 263}