1
0

test-grad0.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. #define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnings on Windows
  2. #include "ggml.h"
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cassert>
  7. #if defined(_MSC_VER)
  8. #pragma warning(disable: 4244 4267) // possible loss of data
  9. #endif
  10. #if defined(__GNUC__)
  11. #pragma GCC diagnostic ignored "-Wdouble-promotion"
  12. #endif
  13. #define MAX_NARGS 3
  14. #undef MIN
  15. #undef MAX
  16. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  17. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  18. #define GGML_SILU_FP16
  19. //
  20. // logging
  21. //
  22. #if (GGML_DEBUG >= 1)
  23. #define GGML_PRINT_DEBUG(...) printf(__VA_ARGS__)
  24. #else
  25. #define GGML_PRINT_DEBUG(...)
  26. #endif
  27. #if (GGML_DEBUG >= 5)
  28. #define GGML_PRINT_DEBUG_5(...) printf(__VA_ARGS__)
  29. #else
  30. #define GGML_PRINT_DEBUG_5(...)
  31. #endif
  32. #if (GGML_DEBUG >= 10)
  33. #define GGML_PRINT_DEBUG_10(...) printf(__VA_ARGS__)
  34. #else
  35. #define GGML_PRINT_DEBUG_10(...)
  36. #endif
  37. #define GGML_PRINT(...) printf(__VA_ARGS__)
  38. static float frand(void) {
  39. return (float)rand()/(float)RAND_MAX;
  40. }
  41. static int irand(int n) {
  42. if (n == 0) return 0;
  43. return rand()%n;
  44. }
  45. static void get_random_dims(int64_t * dims, int ndims) {
  46. dims[0] = dims[1] = dims[2] = dims[3] = 1;
  47. for (int i = 0; i < ndims; i++) {
  48. dims[i] = 1 + irand(4);
  49. }
  50. }
  51. static struct ggml_tensor * get_random_tensor_f32(
  52. struct ggml_context * ctx0,
  53. int ndims,
  54. int64_t ne[],
  55. float fmin,
  56. float fmax) {
  57. struct ggml_tensor * result = ggml_new_tensor(ctx0, GGML_TYPE_F32, ndims, ne);
  58. switch (ndims) {
  59. case 1:
  60. for (int i0 = 0; i0 < ne[0]; i0++) {
  61. ((float *)result->data)[i0] = frand()*(fmax - fmin) + fmin;
  62. }
  63. break;
  64. case 2:
  65. for (int i1 = 0; i1 < ne[1]; i1++) {
  66. for (int i0 = 0; i0 < ne[0]; i0++) {
  67. ((float *)result->data)[i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
  68. }
  69. }
  70. break;
  71. case 3:
  72. for (int i2 = 0; i2 < ne[2]; i2++) {
  73. for (int i1 = 0; i1 < ne[1]; i1++) {
  74. for (int i0 = 0; i0 < ne[0]; i0++) {
  75. ((float *)result->data)[i2*ne[1]*ne[0] + i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
  76. }
  77. }
  78. }
  79. break;
  80. case 4:
  81. for (int i3 = 0; i3 < ne[3]; i3++) {
  82. for (int i2 = 0; i2 < ne[2]; i2++) {
  83. for (int i1 = 0; i1 < ne[1]; i1++) {
  84. for (int i0 = 0; i0 < ne[0]; i0++) {
  85. ((float *)result->data)[i3*ne[2]*ne[1]*ne[0] + i2*ne[1]*ne[0] + i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
  86. }
  87. }
  88. }
  89. }
  90. break;
  91. default:
  92. assert(false);
  93. }
  94. return result;
  95. }
  96. static struct ggml_tensor * get_random_tensor_f16(
  97. struct ggml_context * ctx0,
  98. int ndims,
  99. int64_t ne[],
  100. float fmin,
  101. float fmax) {
  102. struct ggml_tensor * result = ggml_new_tensor(ctx0, GGML_TYPE_F16, ndims, ne);
  103. switch (ndims) {
  104. case 1:
  105. for (int i0 = 0; i0 < ne[0]; i0++) {
  106. ((ggml_fp16_t *)result->data)[i0] = ggml_fp32_to_fp16(frand()*(fmax - fmin) + fmin);
  107. }
  108. break;
  109. case 2:
  110. for (int i1 = 0; i1 < ne[1]; i1++) {
  111. for (int i0 = 0; i0 < ne[0]; i0++) {
  112. ((ggml_fp16_t *)result->data)[i1*ne[0] + i0] = ggml_fp32_to_fp16(frand()*(fmax - fmin) + fmin);
  113. }
  114. }
  115. break;
  116. case 3:
  117. for (int i2 = 0; i2 < ne[2]; i2++) {
  118. for (int i1 = 0; i1 < ne[1]; i1++) {
  119. for (int i0 = 0; i0 < ne[0]; i0++) {
  120. ((ggml_fp16_t *)result->data)[i2*ne[1]*ne[0] + i1*ne[0] + i0] = ggml_fp32_to_fp16(frand()*(fmax - fmin) + fmin);
  121. }
  122. }
  123. }
  124. break;
  125. case 4:
  126. for (int i3 = 0; i3 < ne[3]; i3++) {
  127. for (int i2 = 0; i2 < ne[2]; i2++) {
  128. for (int i1 = 0; i1 < ne[1]; i1++) {
  129. for (int i0 = 0; i0 < ne[0]; i0++) {
  130. ((ggml_fp16_t *)result->data)[i3*ne[2]*ne[1]*ne[0] + i2*ne[1]*ne[0] + i1*ne[0] + i0] = ggml_fp32_to_fp16(frand()*(fmax - fmin) + fmin);
  131. }
  132. }
  133. }
  134. }
  135. break;
  136. default:
  137. assert(false);
  138. }
  139. return result;
  140. }
  141. static struct ggml_tensor * get_random_tensor_i32(
  142. struct ggml_context * ctx0,
  143. int ndims,
  144. int64_t ne[],
  145. int32_t imin,
  146. int32_t imax) {
  147. struct ggml_tensor * result = ggml_new_tensor(ctx0, GGML_TYPE_I32, ndims, ne);
  148. switch (ndims) {
  149. case 1:
  150. for (int i0 = 0; i0 < ne[0]; i0++) {
  151. ((int32_t *)result->data)[i0] = irand(imax - imin) + imin;
  152. }
  153. break;
  154. case 2:
  155. for (int i1 = 0; i1 < ne[1]; i1++) {
  156. for (int i0 = 0; i0 < ne[0]; i0++) {
  157. ((int32_t *)result->data)[i1*ne[0] + i0] = irand(imax - imin) + imin;
  158. }
  159. }
  160. break;
  161. case 3:
  162. for (int i2 = 0; i2 < ne[2]; i2++) {
  163. for (int i1 = 0; i1 < ne[1]; i1++) {
  164. for (int i0 = 0; i0 < ne[0]; i0++) {
  165. ((int32_t *)result->data)[i2*ne[1]*ne[0] + i1*ne[0] + i0] = irand(imax - imin) + imin;
  166. }
  167. }
  168. }
  169. break;
  170. case 4:
  171. for (int i3 = 0; i3 < ne[3]; i3++) {
  172. for (int i2 = 0; i2 < ne[2]; i2++) {
  173. for (int i1 = 0; i1 < ne[1]; i1++) {
  174. for (int i0 = 0; i0 < ne[0]; i0++) {
  175. ((int32_t *)result->data)[i3*ne[2]*ne[1]*ne[0] + i2*ne[1]*ne[0] + i1*ne[0] + i0] = irand(imax - imin) + imin;
  176. }
  177. }
  178. }
  179. }
  180. break;
  181. default:
  182. assert(false);
  183. }
  184. return result;
  185. }
  186. static bool check_gradient(
  187. const char * op_name,
  188. struct ggml_context * ctx0,
  189. struct ggml_tensor * x[],
  190. struct ggml_tensor * f,
  191. int ndims,
  192. int nargs,
  193. float eps,
  194. float max_error_abs,
  195. float max_error_rel) {
  196. static int n_threads = -1;
  197. if (n_threads < 0) {
  198. n_threads = GGML_DEFAULT_N_THREADS;
  199. const char *env = getenv("GGML_N_THREADS");
  200. if (env) {
  201. n_threads = atoi(env);
  202. }
  203. printf("GGML_N_THREADS = %d\n", n_threads);
  204. }
  205. struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, GGML_DEFAULT_GRAPH_SIZE, true);
  206. struct ggml_cgraph * gb = ggml_new_graph_custom(ctx0, GGML_DEFAULT_GRAPH_SIZE, true);
  207. ggml_build_forward_expand(gf, f);
  208. ggml_graph_cpy(gf, gb);
  209. ggml_build_backward_expand(ctx0, gf, gb, false);
  210. ggml_graph_compute_with_ctx(ctx0, gf, n_threads);
  211. ggml_graph_reset (gf);
  212. ggml_set_f32 (f->grad, 1.0f);
  213. ggml_graph_compute_with_ctx(ctx0, gb, n_threads);
  214. // ggml_graph_dump_dot(gf, NULL, "test-grad0-forward.dot");
  215. // ggml_graph_dump_dot(gb, gf, "test-grad0-backward.dot");
  216. for (int i = 0; i < nargs; ++i) {
  217. const int nelements = ggml_nelements(x[i]);
  218. for (int k = 0; k < nelements; ++k) {
  219. // compute gradient using finite differences
  220. const float x0 = ggml_get_f32_1d(x[i], k);
  221. const float xm = x0 - eps;
  222. const float xp = x0 + eps;
  223. ggml_set_f32_1d(x[i], k, xp);
  224. ggml_graph_compute_with_ctx(ctx0, gf, n_threads);
  225. const double f0 = ggml_get_f32_1d(f, 0);
  226. ggml_set_f32_1d(x[i], k, xm);
  227. ggml_graph_compute_with_ctx(ctx0, gf, n_threads);
  228. const double f1 = ggml_get_f32_1d(f, 0);
  229. const double g0 = (f0 - f1)/(2.0*(double) eps);
  230. ggml_set_f32_1d(x[i], k, x0);
  231. // compute gradient using backward graph
  232. ggml_graph_reset (gf);
  233. ggml_set_f32 (f->grad, 1.0f);
  234. ggml_graph_compute_with_ctx(ctx0, gb, n_threads);
  235. const double g1 = ggml_get_f32_1d(x[i]->grad, k);
  236. const double error_abs = fabs(g0 - g1);
  237. const double error_rel = g0 != 0 ? fabs(g0 - g1)/fabs(g0) : 0;
  238. if (error_abs > max_error_abs || error_rel > max_error_rel) {
  239. printf("%s: ndims=%d, i=%d, k=%d, x0=%f, xm=%f, xp=%f, f0=%f, f1=%f, g0=%f, g1=%f, eps=%f, error_abs=%f, error_rel=%f\n",
  240. op_name, ndims, i, k, x0, xm, xp, f0, f1, g0, g1, eps, error_abs, error_rel);
  241. //assert(false);
  242. return false;
  243. }
  244. }
  245. }
  246. return true;
  247. }
  248. // TODO: clean-up this ..
  249. static bool check_mat_mul(
  250. const struct ggml_tensor * y,
  251. const struct ggml_tensor * x0,
  252. const struct ggml_tensor * x1) {
  253. float * dst = (float *) y->data;
  254. float * src0 = (float *) x0->data;
  255. float * src1 = (float *) x1->data;
  256. const int nc = x0->ne[1];
  257. const int nr = x1->ne[1];
  258. const int nk = x0->ne[0];
  259. GGML_PRINT_DEBUG("check_mat_mul: nc=%d, nr=%d, nk=%d\n", nc, nr, nk);
  260. GGML_PRINT_DEBUG("x0:\n");
  261. for (int j = 0; j < x0->ne[1]; ++j) {
  262. for (int i = 0; i < x0->ne[0]; ++i) {
  263. GGML_PRINT_DEBUG("%6.3f ", src0[j*nk + i]);
  264. }
  265. GGML_PRINT_DEBUG("\n");
  266. }
  267. GGML_PRINT_DEBUG("\n");
  268. GGML_PRINT_DEBUG("x1:\n");
  269. for (int j = 0; j < x1->ne[1]; ++j) {
  270. for (int i = 0; i < x1->ne[0]; ++i) {
  271. GGML_PRINT_DEBUG("%6.3f ", src1[j*nk + i]);
  272. }
  273. GGML_PRINT_DEBUG("\n");
  274. }
  275. GGML_PRINT_DEBUG("\n");
  276. GGML_PRINT_DEBUG("y: n_dims = %d, (%lld, %lld)\n", y->n_dims, y->ne[0], y->ne[1]);
  277. for (int j = 0; j < y->ne[1]; ++j) {
  278. for (int i = 0; i < y->ne[0]; ++i) {
  279. GGML_PRINT_DEBUG("%6.3f ", dst[j*nr + i]);
  280. }
  281. GGML_PRINT_DEBUG("\n");
  282. }
  283. for (int i = 0; i < nr; ++i) {
  284. for (int j = 0; j < nc; ++j) {
  285. float sum = 0.0f;
  286. for (int k = 0; k < nk; ++k) {
  287. sum += src0[j*nk + k]*src1[i*nk + k];
  288. }
  289. if (fabsf(dst[i*nc + j] - sum) > 1e-5f) {
  290. fprintf(stderr, "check_mat_mul: dst[%d] = %f, sum = %f\n", i*nc + j, dst[i*nc + j], sum);
  291. assert(false);
  292. return false;
  293. }
  294. }
  295. }
  296. return true;
  297. }
  298. #define NUM_PERMUTATIONS (4*3*2*1)
  299. int main(int argc, const char ** argv) {
  300. struct ggml_init_params params = {
  301. /* .mem_size = */ 256*1024*1024,
  302. /* .mem_buffer = */ NULL,
  303. /* .no_alloc = */ false,
  304. };
  305. int64_t ne[4];
  306. int all_permutations[4 * NUM_PERMUTATIONS];
  307. {
  308. int count = 0;
  309. for (int ax0=0; ax0<4; ++ax0) {
  310. for (int ax1=0; ax1<4; ++ax1) {
  311. if (ax1 == ax0) continue;
  312. for (int ax2=0; ax2<4; ++ax2) {
  313. if (ax2 == ax0) continue;
  314. if (ax2 == ax1) continue;
  315. for (int ax3=0; ax3<4; ++ax3) {
  316. if (ax3 == ax0) continue;
  317. if (ax3 == ax1) continue;
  318. if (ax3 == ax2) continue;
  319. assert(count < NUM_PERMUTATIONS);
  320. all_permutations[count*4+0] = ax0;
  321. all_permutations[count*4+1] = ax1;
  322. all_permutations[count*4+2] = ax2;
  323. all_permutations[count*4+3] = ax3;
  324. ++count;
  325. }
  326. }
  327. }
  328. }
  329. }
  330. unsigned seed_iter = 1;
  331. // original loop: 1000
  332. int niter = 4;
  333. const char *env = getenv("GGML_NLOOP");
  334. if (env != NULL) {
  335. niter = atoi(env);
  336. }
  337. if (argc > 1) {
  338. niter = atoi(argv[1]);
  339. }
  340. for (int iter = 0; iter < niter; ++iter) {
  341. srand(seed_iter);
  342. seed_iter = rand();
  343. unsigned seed = rand();
  344. printf("test-grad0: iter:%d/%d\n", iter, niter);
  345. struct ggml_context * ctx0 = ggml_init(params);
  346. get_random_dims(ne, 4);
  347. struct ggml_tensor * x[MAX_NARGS];
  348. // add f32
  349. {
  350. srand(seed);
  351. const int nargs = 2;
  352. for (int ndims = 1; ndims <= 4; ++ndims) {
  353. for (int i = 0; i < nargs; ++i) {
  354. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  355. ggml_set_param(ctx0, x[i]);
  356. }
  357. struct ggml_tensor * f = ggml_sum(ctx0, ggml_add(ctx0, x[0], x[1]));
  358. check_gradient("add f32", ctx0, x, f, ndims, nargs, 1e-3f, 2e-3f, 2e-3f);
  359. }
  360. }
  361. // add f16
  362. {
  363. srand(seed);
  364. const int nargs = 2;
  365. for (int ndims = 1; ndims <= 4; ++ndims) {
  366. for (int i = 0; i < nargs; ++i) {
  367. x[i] = get_random_tensor_f16(ctx0, ndims, ne, -1.0f, 1.0f);
  368. ggml_set_param(ctx0, x[i]);
  369. }
  370. struct ggml_tensor * f = ggml_sum(ctx0, ggml_add(ctx0, x[0], x[1]));
  371. check_gradient("add f16", ctx0, x, f, ndims, nargs, 1e-1f, 2e-1f, 2e-1f);
  372. }
  373. }
  374. // sub
  375. {
  376. srand(seed);
  377. const int nargs = 2;
  378. for (int ndims = 1; ndims <= 4; ++ndims) {
  379. for (int i = 0; i < nargs; ++i) {
  380. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  381. ggml_set_param(ctx0, x[i]);
  382. }
  383. struct ggml_tensor * f = ggml_sum(ctx0, ggml_sub(ctx0, x[0], x[1]));
  384. check_gradient("sub", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  385. }
  386. }
  387. // mul
  388. {
  389. srand(seed);
  390. const int nargs = 2;
  391. for (int ndims = 1; ndims <= 4; ++ndims) {
  392. for (int i = 0; i < nargs; ++i) {
  393. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  394. ggml_set_param(ctx0, x[i]);
  395. }
  396. struct ggml_tensor * f = ggml_sum(ctx0, ggml_mul(ctx0, x[0], x[1]));
  397. check_gradient("mul", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  398. }
  399. }
  400. // div
  401. {
  402. srand(seed);
  403. const int nargs = 2;
  404. for (int ndims = 1; ndims <= 4; ++ndims) {
  405. for (int i = 0; i < nargs; ++i) {
  406. x[i] = get_random_tensor_f32(ctx0, ndims, ne, 0.5f, 1.0f);
  407. ggml_set_param(ctx0, x[i]);
  408. }
  409. struct ggml_tensor * f = ggml_sum(ctx0, ggml_div(ctx0, x[0], x[1]));
  410. check_gradient("div", ctx0, x, f, ndims, nargs, 1e-3f, 1e-1f, 1e-1f);
  411. }
  412. }
  413. // sqr
  414. {
  415. srand(seed);
  416. const int nargs = 1;
  417. for (int ndims = 1; ndims <= 2; ++ndims) {
  418. for (int i = 0; i < nargs; ++i) {
  419. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  420. ggml_set_param(ctx0, x[i]);
  421. }
  422. struct ggml_tensor * f = ggml_sum(ctx0, ggml_sqr(ctx0, x[0]));
  423. check_gradient("sqr", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  424. }
  425. }
  426. // sqrt
  427. {
  428. srand(seed);
  429. const int nargs = 1;
  430. for (int ndims = 1; ndims <= 2; ++ndims) {
  431. for (int i = 0; i < nargs; ++i) {
  432. x[i] = get_random_tensor_f32(ctx0, ndims, ne, 2.0f*1e-3f, 1.0f);
  433. ggml_set_param(ctx0, x[i]);
  434. }
  435. struct ggml_tensor * f = ggml_sum(ctx0, ggml_sqrt(ctx0, x[0]));
  436. check_gradient("sqrt", ctx0, x, f, ndims, nargs, 1e-3f, 2e-2f, 1e-1f);
  437. }
  438. }
  439. // log
  440. {
  441. srand(seed);
  442. const int nargs = 1;
  443. for (int ndims = 1; ndims <= 2; ++ndims) {
  444. for (int i = 0; i < nargs; ++i) {
  445. x[i] = get_random_tensor_f32(ctx0, ndims, ne, 2.0f*1e-3f, 1.0f);
  446. ggml_set_param(ctx0, x[i]);
  447. }
  448. struct ggml_tensor * f = ggml_sum(ctx0, ggml_log(ctx0, x[0]));
  449. check_gradient("log", ctx0, x, f, ndims, nargs, 1e-3f, INFINITY, 1e-1f);
  450. }
  451. }
  452. // sum
  453. {
  454. srand(seed);
  455. const int nargs = 1;
  456. for (int ndims = 1; ndims <= 2; ++ndims) {
  457. for (int i = 0; i < nargs; ++i) {
  458. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  459. ggml_set_param(ctx0, x[i]);
  460. }
  461. struct ggml_tensor * f = ggml_sum(ctx0, x[0]);
  462. check_gradient("sum", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  463. }
  464. }
  465. // sum_rows
  466. {
  467. srand(seed);
  468. const int nargs = 1;
  469. for (int ndims = 1; ndims <= 4; ++ndims) {
  470. for (int i = 0; i < nargs; ++i) {
  471. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  472. ggml_set_param(ctx0, x[i]);
  473. }
  474. struct ggml_tensor * f = ggml_sum(ctx0, ggml_sqr(ctx0, ggml_sum_rows(ctx0, x[0])));
  475. check_gradient("sum_rows", ctx0, x, f, ndims, nargs, 1e-3f, 1e-2f, INFINITY);
  476. }
  477. }
  478. // mean, not yet fully implemented
  479. if(0)
  480. {
  481. srand(seed);
  482. const int nargs = 1;
  483. for (int ndims = 1; ndims <= 4; ++ndims) {
  484. for (int i = 0; i < nargs; ++i) {
  485. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  486. ggml_set_param(ctx0, x[i]);
  487. }
  488. struct ggml_tensor * f = ggml_sum(ctx0, ggml_mean(ctx0, x[0]));
  489. check_gradient("mean", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  490. }
  491. }
  492. // argmax
  493. if (0)
  494. {
  495. srand(seed);
  496. const int nargs = 1;
  497. for (int ndims = 1; ndims <= 4; ++ndims) {
  498. for (int i = 0; i < nargs; ++i) {
  499. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  500. ggml_set_param(ctx0, x[i]);
  501. }
  502. struct ggml_tensor * f = ggml_sum(ctx0, ggml_argmax(ctx0, x[0]));
  503. check_gradient("argmax", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  504. }
  505. }
  506. // repeat
  507. {
  508. srand(seed);
  509. int64_t ne2[4];
  510. get_random_dims(ne2, 4);
  511. ne2[0] = ne[0] * ne2[0];
  512. ne2[1] = ne[1] * ne2[1];
  513. ne2[2] = 1;
  514. ne2[3] = 1;
  515. const int nargs = 1;
  516. for (int ndims = 1; ndims <= 2; ++ndims) {
  517. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  518. x[1] = get_random_tensor_f32(ctx0, ndims, ne2, -1.0f, 1.0f);
  519. ggml_set_param(ctx0, x[0]);
  520. struct ggml_tensor * f = ggml_sum(ctx0, ggml_sqr(ctx0, ggml_sub(ctx0, x[1], ggml_repeat(ctx0, x[0], x[1]))));
  521. check_gradient("repeat", ctx0, x, f, ndims, nargs, 1e-3f, 1e-2f, INFINITY);
  522. }
  523. }
  524. // repeat back
  525. {
  526. srand(seed);
  527. int64_t ne2[4];
  528. get_random_dims(ne2, 4);
  529. ne2[0] = ne[0] * ne2[0];
  530. ne2[1] = ne[1] * ne2[1];
  531. ne2[2] = 1;
  532. ne2[3] = 1;
  533. const int nargs = 1;
  534. for (int ndims = 1; ndims <= 2; ++ndims) {
  535. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  536. x[1] = get_random_tensor_f32(ctx0, ndims, ne2, -1.0f, 1.0f);
  537. ggml_set_param(ctx0, x[0]);
  538. struct ggml_tensor * f = ggml_sum(ctx0, ggml_sqr(ctx0, ggml_sub(ctx0, x[0], ggml_repeat_back(ctx0, x[1], x[0]))));
  539. check_gradient("repeat back", ctx0, x, f, ndims, nargs, 1e-3f, 1e-2f, INFINITY);
  540. }
  541. }
  542. // abs (finite differences do not work)
  543. //{
  544. // const int nargs = 1;
  545. // for (int ndims = 1; ndims <= 2; ++ndims) {
  546. // for (int i = 0; i < nargs; ++i) {
  547. // x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  548. // ggml_set_param(ctx0, x[i]);
  549. // }
  550. // struct ggml_tensor * f = ggml_sum(ctx0, ggml_abs(ctx0, x[0]));
  551. // check_gradient("abs", ctx0, x, f, ndims, nargs, 1e-3f, INFINITY, 1e-3f);
  552. // }
  553. //}
  554. // sgn
  555. {
  556. srand(seed);
  557. const int nargs = 1;
  558. for (int ndims = 1; ndims <= 4; ++ndims) {
  559. for (int i = 0; i < nargs; ++i) {
  560. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  561. ggml_set_param(ctx0, x[i]);
  562. }
  563. struct ggml_tensor* f = ggml_sum(ctx0, ggml_sgn(ctx0, x[0]));
  564. check_gradient("sgn", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  565. }
  566. }
  567. // neg
  568. {
  569. srand(seed);
  570. const int nargs = 1;
  571. for (int ndims = 1; ndims <= 4; ++ndims) {
  572. for (int i = 0; i < nargs; ++i) {
  573. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  574. ggml_set_param(ctx0, x[i]);
  575. }
  576. struct ggml_tensor* f = ggml_sum(ctx0, ggml_neg(ctx0, x[0]));
  577. check_gradient("neg", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  578. }
  579. }
  580. // step
  581. {
  582. srand(seed);
  583. const int nargs = 1;
  584. for (int ndims = 1; ndims <= 4; ++ndims) {
  585. for (int i = 0; i < nargs; ++i) {
  586. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  587. ggml_set_param(ctx0, x[i]);
  588. }
  589. struct ggml_tensor* f = ggml_sum(ctx0, ggml_step(ctx0, x[0]));
  590. check_gradient("step", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  591. }
  592. }
  593. // tanh, not yet fully implemented
  594. if(0)
  595. {
  596. srand(seed);
  597. const int nargs = 1;
  598. for (int ndims = 1; ndims <= 4; ++ndims) {
  599. for (int i = 0; i < nargs; ++i) {
  600. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  601. ggml_set_param(ctx0, x[i]);
  602. }
  603. struct ggml_tensor* f = ggml_sum(ctx0, ggml_tanh(ctx0, x[0]));
  604. check_gradient("tanh", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  605. }
  606. }
  607. // mul_mat
  608. {
  609. srand(seed);
  610. const int nargs = 2;
  611. for (int ndims = 2; ndims <= 4; ++ndims) {
  612. int max_nrep = (ndims >= 3) ? 2 : 1;
  613. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  614. for (int nrep2 = 1; nrep2 < max_nrep; ++nrep2) {
  615. for (int nrep3 = 1; nrep3 < max_nrep; ++nrep3) {
  616. {
  617. int64_t ne2[4];
  618. get_random_dims(ne2, 4);
  619. ne2[0] = ne[0];
  620. ne2[2] = nrep2 * ne[2];
  621. ne2[3] = nrep3 * ne[3];
  622. x[1] = get_random_tensor_f32(ctx0, ndims, ne2, -1.0f, 1.0f);
  623. }
  624. ggml_set_param(ctx0, x[0]);
  625. ggml_set_param(ctx0, x[1]);
  626. struct ggml_tensor * m = ggml_mul_mat(ctx0, x[1], x[0]);
  627. struct ggml_tensor * f = ggml_sum(ctx0, m);
  628. GGML_PRINT_DEBUG("testing: mul_mat, [%lld, %lld] (%d) * [%lld, %lld] (%d)\n", x[1]->ne[0], x[1]->ne[1], x[1]->n_dims, x[0]->ne[0], x[0]->ne[1], x[0]->n_dims);
  629. check_gradient("mul_mat", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  630. if (ndims == 2) {
  631. // check_mat_mul does not support ndims > 2
  632. check_mat_mul(m, x[1], x[0]);
  633. }
  634. }
  635. }
  636. }
  637. }
  638. // elu, not yet fully implemented
  639. if(0)
  640. {
  641. srand(seed);
  642. const int nargs = 1;
  643. for (int ndims = 1; ndims <= 4; ++ndims) {
  644. for (int i = 0; i < nargs; ++i) {
  645. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  646. ggml_set_param(ctx0, x[i]);
  647. }
  648. struct ggml_tensor* f = ggml_sum(ctx0, ggml_elu(ctx0, x[0]));
  649. check_gradient("elu", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  650. }
  651. }
  652. // relu
  653. {
  654. srand(seed);
  655. const int nargs = 1;
  656. for (int ndims = 1; ndims <= 4; ++ndims) {
  657. for (int i = 0; i < nargs; ++i) {
  658. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  659. ggml_set_param(ctx0, x[i]);
  660. }
  661. struct ggml_tensor* f = ggml_sum(ctx0, ggml_relu(ctx0, x[0]));
  662. check_gradient("relu", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  663. }
  664. }
  665. // gelu, not yet fully implemented
  666. if(0)
  667. {
  668. srand(seed);
  669. const int nargs = 1;
  670. for (int ndims = 1; ndims <= 4; ++ndims) {
  671. for (int i = 0; i < nargs; ++i) {
  672. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  673. ggml_set_param(ctx0, x[i]);
  674. }
  675. struct ggml_tensor* f = ggml_sum(ctx0, ggml_gelu(ctx0, x[0]));
  676. check_gradient("gelu", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, 1e-3f);
  677. }
  678. }
  679. // silu
  680. {
  681. srand(seed);
  682. const int nargs = 1;
  683. for (int ndims = 1; ndims <= 2; ++ndims) {
  684. for (int i = 0; i < nargs; ++i) {
  685. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  686. ggml_set_param(ctx0, x[i]);
  687. }
  688. struct ggml_tensor * f = ggml_sum(ctx0, ggml_silu(ctx0, x[0]));
  689. #ifdef GGML_SILU_FP16
  690. // due to GGML_SILU_FP16 the finite difference method will be slightly wrong -> increase error bounds.
  691. check_gradient("silu", ctx0, x, f, ndims, nargs, 1e-3f, 0.5, INFINITY);
  692. #else
  693. check_gradient("silu", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  694. #endif
  695. }
  696. }
  697. // rms_norm
  698. {
  699. srand(seed);
  700. const int nargs = 1;
  701. for (int ndims = 1; ndims <= 2; ++ndims) {
  702. for (int i = 0; i < nargs; ++i) {
  703. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  704. ggml_set_param(ctx0, x[i]);
  705. }
  706. struct ggml_tensor * f = ggml_sum(ctx0, ggml_rms_norm(ctx0, x[0], 1e-6f));
  707. check_gradient("rms_norm", ctx0, x, f, ndims, nargs, 1e-4f, 1.0f, INFINITY);
  708. }
  709. }
  710. // scale
  711. {
  712. srand(seed);
  713. const int nargs = 1;
  714. for (int ndims = 1; ndims <= 2; ++ndims) {
  715. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  716. const float s = -1.0f + 2.0f*frand();
  717. ggml_set_param(ctx0, x[0]);
  718. struct ggml_tensor * f = ggml_sum(ctx0, ggml_scale(ctx0, x[0], s));
  719. check_gradient("scale", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  720. }
  721. }
  722. // cpy f32
  723. {
  724. srand(seed);
  725. const int nargs = 2;
  726. for (int ndims = 1; ndims <= 2; ++ndims) {
  727. for (int i = 0; i < nargs; ++i) {
  728. x[i] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  729. ggml_set_param(ctx0, x[i]);
  730. }
  731. // x[1] is overwritten by x[0], so the gradients don't propagate to x[1]
  732. struct ggml_tensor * f = ggml_sum(ctx0, ggml_cpy(ctx0, x[0], x[1]));
  733. check_gradient("cpy f32", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  734. }
  735. }
  736. // cpy f16
  737. {
  738. srand(seed);
  739. const int nargs = 2;
  740. for (int ndims = 1; ndims <= 2; ++ndims) {
  741. for (int i = 0; i < nargs; ++i) {
  742. x[i] = get_random_tensor_f16(ctx0, ndims, ne, -1.0f, 1.0f);
  743. ggml_set_param(ctx0, x[i]);
  744. }
  745. // x[1] is overwritten by x[0], so the gradients don't propagate to x[1]
  746. struct ggml_tensor * f = ggml_sum(ctx0, ggml_cpy(ctx0, x[0], x[1]));
  747. check_gradient("cpy f16", ctx0, x, f, ndims, nargs, 1e-1f, 1e-1f, INFINITY);
  748. }
  749. }
  750. // reshape (1d->nd)
  751. {
  752. srand(seed);
  753. const int nargs = 1;
  754. for (int ndims = 1; ndims <= 2; ++ndims) {
  755. int64_t ne2[4];
  756. ne2[0] = 1;
  757. ne2[1] = 1;
  758. ne2[2] = 1;
  759. ne2[3] = 1;
  760. for (int i = 0; i < ndims; ++i) {
  761. ne2[0] *= ne[i];
  762. }
  763. x[0] = get_random_tensor_f32(ctx0, 1, ne2, -1.0f, 1.0f);
  764. x[1] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  765. ggml_set_param(ctx0, x[0]);
  766. struct ggml_tensor * f = ggml_sum(ctx0, ggml_reshape(ctx0, x[0], x[1]));
  767. check_gradient("reshape", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  768. }
  769. }
  770. // reshape (nd->1d)
  771. {
  772. srand(seed);
  773. const int nargs = 1;
  774. for (int ndims = 1; ndims <= 2; ++ndims) {
  775. int64_t ne2[4];
  776. ne2[0] = 1;
  777. ne2[1] = 1;
  778. ne2[2] = 1;
  779. ne2[3] = 1;
  780. for (int i = 0; i < ndims; ++i) {
  781. ne2[0] *= ne[i];
  782. }
  783. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  784. x[1] = get_random_tensor_f32(ctx0, 1, ne2, -1.0f, 1.0f);
  785. ggml_set_param(ctx0, x[0]);
  786. struct ggml_tensor * f = ggml_sum(ctx0, ggml_reshape(ctx0, x[0], x[1]));
  787. check_gradient("reshape", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  788. }
  789. }
  790. // acc 1d
  791. {
  792. srand(seed);
  793. int64_t ne2[4] = { 1, 1, 1, 1 };
  794. const int nargs = 2;
  795. for (int ndims = 1; ndims <= 4; ++ndims) {
  796. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  797. ggml_set_param(ctx0, x[0]);
  798. get_random_dims(ne2, 1);
  799. while ((ne2[0] > ne[0]) || (ne2[0] > ggml_nelements(x[0]))) {
  800. get_random_dims(ne2, 1);
  801. }
  802. x[1] = get_random_tensor_f32(ctx0, 1, ne2, -1.0f, 1.0f);
  803. ggml_set_param(ctx0, x[1]);
  804. const int max_offset = MAX(0, ggml_nelements(x[0]) - ggml_nelements(x[1]));
  805. const int offset = irand(max_offset) * ggml_element_size(x[0]);
  806. struct ggml_tensor * f = ggml_sum(ctx0, ggml_acc(ctx0, x[0], x[1], x[0]->nb[1], x[0]->nb[2], x[0]->nb[3], offset));
  807. check_gradient("acc 1d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  808. }
  809. }
  810. // acc 2d
  811. {
  812. srand(seed);
  813. int64_t ne2[4] = { 1, 1, 1, 1 };
  814. int64_t max_offsets[4] = { 0, 0, 0, 0 };
  815. int64_t offsets[4] = { 0, 0, 0, 0 };
  816. const int nargs = 2;
  817. for (int ndims = 2; ndims <= 4; ++ndims) {
  818. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  819. ggml_set_param(ctx0, x[0]);
  820. get_random_dims(ne2, 2);
  821. while ((ne2[0] > ne[0]) || (ne2[1] > ne[1]) || (ne2[0]*ne2[1] > ggml_nelements(x[0]))) {
  822. get_random_dims(ne2, 2);
  823. }
  824. x[1] = get_random_tensor_f32(ctx0, 2, ne2, -1.0f, 1.0f);
  825. ggml_set_param(ctx0, x[1]);
  826. max_offsets[0] = MAX(0, x[0]->ne[0] - x[1]->ne[0]);
  827. max_offsets[1] = MAX(0, x[0]->ne[1] - x[1]->ne[1]);
  828. offsets[0] = irand(max_offsets[0]) * x[0]->nb[0];
  829. offsets[1] = irand(max_offsets[1]) * x[0]->nb[1];
  830. const int offset = offsets[0] + offsets[1];
  831. struct ggml_tensor * f = ggml_sum(ctx0, ggml_acc(ctx0, x[0], x[1], x[0]->nb[1], x[0]->nb[2], x[0]->nb[3], offset));
  832. check_gradient("acc 2d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  833. }
  834. }
  835. // acc 3d
  836. {
  837. srand(seed);
  838. int64_t ne2[4] = { 1, 1, 1, 1 };
  839. int64_t max_offsets[4] = { 0, 0, 0, 0 };
  840. int64_t offsets[4] = { 0, 0, 0, 0 };
  841. const int nargs = 2;
  842. for (int ndims = 3; ndims <= 4; ++ndims) {
  843. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  844. ggml_set_param(ctx0, x[0]);
  845. get_random_dims(ne2, 3);
  846. while ((ne2[0] > ne[0]) || (ne2[1] > ne[1]) || (ne2[2] > ne[2]) || (ne2[0]*ne2[1]*ne2[2] > ggml_nelements(x[0]))) {
  847. get_random_dims(ne2, 3);
  848. }
  849. x[1] = get_random_tensor_f32(ctx0, 3, ne2, -1.0f, 1.0f);
  850. ggml_set_param(ctx0, x[1]);
  851. max_offsets[0] = MAX(0, x[0]->ne[0] - x[1]->ne[0]);
  852. max_offsets[1] = MAX(0, x[0]->ne[1] - x[1]->ne[1]);
  853. max_offsets[2] = MAX(0, x[0]->ne[2] - x[1]->ne[2]);
  854. offsets[0] = irand(max_offsets[0]) * x[0]->nb[0];
  855. offsets[1] = irand(max_offsets[1]) * x[0]->nb[1];
  856. offsets[2] = irand(max_offsets[2]) * x[0]->nb[2];
  857. const int offset = offsets[0] + offsets[1] + offsets[2];
  858. struct ggml_tensor * f = ggml_sum(ctx0, ggml_acc(ctx0, x[0], x[1], x[0]->nb[1], x[0]->nb[2], x[0]->nb[3], offset));
  859. check_gradient("acc 3d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  860. }
  861. }
  862. // acc 4d
  863. {
  864. srand(seed);
  865. int64_t ne2[4] = { 1, 1, 1, 1 };
  866. int64_t max_offsets[4] = { 0, 0, 0, 0 };
  867. int64_t offsets[4] = { 0, 0, 0, 0 };
  868. const int nargs = 2;
  869. for (int ndims = 4; ndims <= 4; ++ndims) {
  870. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  871. ggml_set_param(ctx0, x[0]);
  872. get_random_dims(ne2, 4);
  873. while ((ne2[0] > ne[0]) || (ne2[1] > ne[1]) || (ne2[2] > ne[2]) || (ne2[3] > ne[3]) || (ne2[0]*ne2[1]*ne2[2]*ne2[3] > ggml_nelements(x[0]))) {
  874. get_random_dims(ne2, 4);
  875. }
  876. x[1] = get_random_tensor_f32(ctx0, 4, ne2, -1.0f, 1.0f);
  877. ggml_set_param(ctx0, x[1]);
  878. max_offsets[0] = MAX(0, x[0]->ne[0] - x[1]->ne[0]);
  879. max_offsets[1] = MAX(0, x[0]->ne[1] - x[1]->ne[1]);
  880. max_offsets[2] = MAX(0, x[0]->ne[2] - x[1]->ne[2]);
  881. max_offsets[3] = MAX(0, x[0]->ne[3] - x[1]->ne[3]);
  882. offsets[0] = irand(max_offsets[0]) * x[0]->nb[0];
  883. offsets[1] = irand(max_offsets[1]) * x[0]->nb[1];
  884. offsets[2] = irand(max_offsets[2]) * x[0]->nb[2];
  885. offsets[3] = irand(max_offsets[3]) * x[0]->nb[3];
  886. const int offset = offsets[0] + offsets[1] + offsets[2] + offsets[3];
  887. struct ggml_tensor * f = ggml_sum(ctx0, ggml_acc(ctx0, x[0], x[1], x[0]->nb[1], x[0]->nb[2], x[0]->nb[3], offset));
  888. check_gradient("acc 4d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  889. }
  890. }
  891. // set_1d
  892. {
  893. srand(seed);
  894. int64_t ne2[4];
  895. const int nargs = 2;
  896. for (int ndims = 1; ndims <= 4; ++ndims) {
  897. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  898. ggml_set_param(ctx0, x[0]);
  899. get_random_dims(ne2, 1);
  900. while ((ne2[0] > ne[0]) || (ne2[0] > ggml_nelements(x[0]))) {
  901. get_random_dims(ne2, 1);
  902. }
  903. x[1] = get_random_tensor_f32(ctx0, 1, ne2, -1.0f, 1.0f);
  904. ggml_set_param(ctx0, x[1]);
  905. const int max_offset = MAX(0, ggml_nelements(x[0]) - ggml_nelements(x[1]));
  906. const int offset = irand(max_offset) * ggml_element_size(x[0]);
  907. struct ggml_tensor * f = ggml_sum(ctx0, ggml_set_1d(ctx0, x[0], x[1], offset));
  908. check_gradient("set_1d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  909. }
  910. }
  911. // set_2d
  912. {
  913. srand(seed);
  914. int64_t ne2[4];
  915. int64_t max_offsets[4] = { 0, 0, 0, 0 };
  916. int64_t offsets[4] = { 0, 0, 0, 0 };
  917. const int nargs = 1;
  918. for (int ndims = 2; ndims <= 4; ++ndims) {
  919. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  920. ggml_set_param(ctx0, x[0]);
  921. get_random_dims(ne2, 2);
  922. while ((ne2[0] > ne[0]) || (ne2[1] > ne[1]) || (ne2[0]*ne2[1] > ggml_nelements(x[0]))) {
  923. get_random_dims(ne2, 2);
  924. }
  925. x[1] = get_random_tensor_f32(ctx0, 2, ne2, -1.0f, 1.0f);
  926. ggml_set_param(ctx0, x[1]);
  927. max_offsets[0] = MAX(0, x[0]->ne[0] - x[1]->ne[0]);
  928. max_offsets[1] = MAX(0, x[0]->ne[1] - x[1]->ne[1]);
  929. offsets[0] = irand(max_offsets[0]) * x[0]->nb[0];
  930. offsets[1] = irand(max_offsets[1]) * x[0]->nb[1];
  931. const int offset = offsets[0] + offsets[1];
  932. struct ggml_tensor * f = ggml_sum(ctx0, ggml_set_2d(ctx0, x[0], x[1], x[1]->nb[1], offset));
  933. check_gradient("set_2d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  934. }
  935. }
  936. // view_1d
  937. {
  938. srand(seed);
  939. const int nargs = 1;
  940. for (int ndims = 1; ndims <= 4; ++ndims) {
  941. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  942. ggml_set_param(ctx0, x[0]);
  943. const int k0 = irand(ggml_nelements(x[0]));
  944. const int k1 = irand(ggml_nelements(x[0]));
  945. const int i0 = MIN(k0, k1);
  946. const int i1 = MAX(k0, k1);
  947. const int offset = i0 * sizeof(float);
  948. const int nelem = i1 - i0;
  949. struct ggml_tensor * f = ggml_sum(ctx0, ggml_view_1d(ctx0, x[0], nelem, offset));
  950. check_gradient("view_1d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  951. }
  952. }
  953. // view_2d
  954. {
  955. srand(seed);
  956. int64_t ne2[4];
  957. int64_t nb2[4];
  958. const int nargs = 1;
  959. for (int ndims = 1; ndims <= 4; ++ndims) {
  960. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  961. get_random_dims(ne2, 2);
  962. while (ne2[0]*ne2[1] > ggml_nelements(x[0])) {
  963. get_random_dims(ne2, 2);
  964. }
  965. const int count = ne2[0]*ne2[1];
  966. nb2[0] = sizeof(float);
  967. nb2[1] = nb2[0]*ne2[0];
  968. ggml_set_param(ctx0, x[0]);
  969. const int max_offset = ggml_nelements(x[0]) - count;
  970. const int offset = irand(max_offset+1) * sizeof(float);
  971. struct ggml_tensor * f = ggml_sum(ctx0, ggml_view_2d(ctx0, x[0], ne2[0], ne2[1], nb2[1], offset));
  972. check_gradient("view_2d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  973. }
  974. }
  975. // view_3d
  976. {
  977. srand(seed);
  978. int64_t ne2[4] = {1,1,1,1};
  979. int64_t nb2[4] = {0,0,0,0};
  980. const int nargs = 1;
  981. for (int ndims = 1; ndims <= 4; ++ndims) {
  982. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  983. get_random_dims(ne2, 3);
  984. while (ne2[0]*ne2[1]*ne2[2] > ggml_nelements(x[0])) {
  985. get_random_dims(ne2, 3);
  986. }
  987. const int count = ne2[0]*ne2[1]*ne2[2];
  988. nb2[0] = sizeof(float);
  989. nb2[1] = nb2[0]*ne2[0];
  990. nb2[2] = nb2[1]*ne2[1];
  991. ggml_set_param(ctx0, x[0]);
  992. const int max_offset = ggml_nelements(x[0]) - count;
  993. const int offset = irand(max_offset+1) * sizeof(float);
  994. struct ggml_tensor * f = ggml_sum(ctx0, ggml_view_3d(ctx0, x[0], ne2[0], ne2[1], ne2[2], nb2[1], nb2[2], offset));
  995. check_gradient("view_3d", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  996. }
  997. }
  998. // permute
  999. {
  1000. srand(seed);
  1001. int64_t ne2[4];
  1002. const int nargs = 1;
  1003. for (int ndims = 1; ndims <= 4; ++ndims)
  1004. {
  1005. // ggml_permute will set axes of dimensions below n_dims to 1.
  1006. // to make ggml_permute work correctly on all axes,
  1007. // the input tensor needs maximal n_dim of 4.
  1008. for (int i=0; i<ndims; ++i) {
  1009. ne2[i] = ne[i];
  1010. }
  1011. for (int i=ndims; i<4; ++i) {
  1012. ne2[i] = 1;
  1013. }
  1014. x[0] = get_random_tensor_f32(ctx0, 4, ne2, -1.0f, 1.0f);
  1015. ggml_set_param(ctx0, x[0]);
  1016. const int p = irand(NUM_PERMUTATIONS);
  1017. const int ax0 = all_permutations[p*4+0];
  1018. const int ax1 = all_permutations[p*4+1];
  1019. const int ax2 = all_permutations[p*4+2];
  1020. const int ax3 = all_permutations[p*4+3];
  1021. // sum requires contiguous tensor rows
  1022. struct ggml_tensor * f = ggml_sum(ctx0, ggml_cont(ctx0, ggml_permute(ctx0, x[0], ax0, ax1, ax2, ax3)));
  1023. check_gradient("permute", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  1024. }
  1025. }
  1026. // transpose
  1027. {
  1028. srand(seed);
  1029. int64_t ne2[4];
  1030. const int nargs = 1;
  1031. for (int ndims = 1; ndims <= 4; ++ndims)
  1032. {
  1033. // ggml_transpose will set axes of dimensions below n_dims to 1.
  1034. // to make ggml_transpose work correctly on all axes,
  1035. // the input tensor needs maximal n_dim of 4.
  1036. for (int i=0; i<ndims; ++i) {
  1037. ne2[i] = ne[i];
  1038. }
  1039. for (int i=ndims; i<4; ++i) {
  1040. ne2[i] = 1;
  1041. }
  1042. x[0] = get_random_tensor_f32(ctx0, 4, ne2, -1.0f, 1.0f);
  1043. ggml_set_param(ctx0, x[0]);
  1044. // sum requires contiguous tensor rows
  1045. struct ggml_tensor * f = ggml_sum(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, x[0])));
  1046. check_gradient("transpose", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  1047. }
  1048. }
  1049. // get_rows
  1050. {
  1051. srand(seed);
  1052. int64_t ne2[4] = {ne[0], ne[1], 1, 1};
  1053. int64_t ne3[4] = {1+irand(ne[1]), 1, 1, 1};
  1054. const int nargs = 1;
  1055. const int ndims = 2;
  1056. x[0] = get_random_tensor_f32(ctx0, ndims, ne2, -1.0f, 1.0f);
  1057. x[1] = get_random_tensor_i32(ctx0, 1, ne3, 0, ne2[1]);
  1058. ggml_set_param(ctx0, x[0]);
  1059. struct ggml_tensor * f = ggml_sum(ctx0, ggml_get_rows(ctx0, x[0], x[1]));
  1060. check_gradient("get_rows", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  1061. }
  1062. // diag_mask_inf
  1063. {
  1064. srand(seed);
  1065. const int nargs = 1;
  1066. const int ndims = 2;
  1067. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  1068. ggml_set_param(ctx0, x[0]);
  1069. int n_past = irand(ne[0]);
  1070. struct ggml_tensor * f = ggml_sum(ctx0, ggml_diag_mask_inf(ctx0, x[0], n_past));
  1071. check_gradient("diag_mask_inf", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  1072. }
  1073. // diag_mask_zero
  1074. {
  1075. srand(seed);
  1076. const int nargs = 1;
  1077. const int ndims = 2;
  1078. x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
  1079. ggml_set_param(ctx0, x[0]);
  1080. int n_past = irand(ne[0]);
  1081. struct ggml_tensor * f = ggml_sum(ctx0, ggml_diag_mask_zero(ctx0, x[0], n_past));
  1082. check_gradient("diag_mask_zero", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
  1083. }
  1084. // softmax
  1085. {
  1086. srand(seed);
  1087. const int nargs = 1;
  1088. int64_t ne2[4];
  1089. get_random_dims(ne2, 4);
  1090. for (int ndims = 1; ndims <= 3; ++ndims) {
  1091. x[0] = get_random_tensor_f32(ctx0, ndims, ne2, -1.0f, 1.0f);
  1092. ggml_set_param(ctx0, x[0]);
  1093. float eps = 1e-6f;
  1094. // dont use only sum as aggregation, because sum of softmax is always 1 -> finite differences should not work
  1095. // instead use sum(log(soft_max()*(1-eps)+eps)); use eps to avoid log(0)
  1096. struct ggml_tensor * f = ggml_sum(ctx0,
  1097. ggml_log(ctx0,
  1098. ggml_add1(ctx0,
  1099. ggml_scale(ctx0,
  1100. ggml_soft_max(ctx0, x[0]),
  1101. 1.0f - eps),
  1102. ggml_new_f32(ctx0, eps))));
  1103. check_gradient("softmax", ctx0, x, f, ndims, nargs, 1e-3f, 2e-1f, INFINITY);
  1104. // NOTE: softmax forward is computed using f16 table lookup instead of using actual expf, but backward assumes actual expf.
  1105. // this may result in different gradients too finite differences.
  1106. // when this test reports errors, first try to replace the table lookup with actual expf and test again to see if just that was the cause.
  1107. // if only the table lookup causes gradients to differ this is acceptable.
  1108. }
  1109. }
  1110. // cross_entropy_loss
  1111. {
  1112. srand(seed);
  1113. const int nargs = 1;
  1114. int64_t ne2[4];
  1115. get_random_dims(ne2, 4);
  1116. for (int ndims = 1; ndims <= 4; ++ndims) {
  1117. x[0] = get_random_tensor_f32(ctx0, ndims, ne2, -0.1f, 0.1f);
  1118. x[1] = get_random_tensor_f32(ctx0, ndims, ne2, 0.0f, 1.0f);
  1119. // the second argument to cross_entropy_loss must sum up to 1 for each row
  1120. int nr = ggml_nrows(x[1]);
  1121. int nc = ggml_nelements(x[1]) / nr;
  1122. for (int ir = 0; ir < nr; ++ir) {
  1123. float sum = 0;
  1124. for (int ic = 0; ic < nc; ++ic) {
  1125. sum += ((float *) x[1]->data)[ic + ir*nc];
  1126. }
  1127. for (int ic = 0; ic < nc; ++ic) {
  1128. ((float *) x[1]->data)[ic + ir*nc] /= sum;
  1129. }
  1130. }
  1131. ggml_set_param(ctx0, x[0]);
  1132. struct ggml_tensor * f = ggml_cross_entropy_loss(ctx0, x[0], x[1]);
  1133. check_gradient("cross_entropy_loss", ctx0, x, f, ndims, nargs, 1e-4f, 1e-3f, INFINITY);
  1134. }
  1135. }
  1136. // rope f32
  1137. {
  1138. srand(seed);
  1139. const int nargs = 1;
  1140. int64_t ne2[4];
  1141. get_random_dims(ne2, 4);
  1142. ne2[0] += ne2[0] % 2;
  1143. int n_rot = ne2[0];
  1144. for (int ndims = 3; ndims <= 4; ++ndims) {
  1145. for (int mode = 0; mode < 4; ++mode) {
  1146. for (int n_past = 1; n_past < ne2[2]; ++n_past) {
  1147. x[0] = get_random_tensor_f32(ctx0, ndims, ne2, -1.0f, 1.0f);
  1148. struct ggml_tensor * p = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ne2[2]);
  1149. for (int i = 0; i < ne2[2]; ++i) {
  1150. ((int32_t *) p->data)[i] = n_past + i;
  1151. }
  1152. ggml_set_param(ctx0, x[0]);
  1153. const bool skip_past = (mode & 1);
  1154. if (skip_past) {
  1155. // we have no past, so this would have to work on uninitialized memory.
  1156. // we only test the gradients here;
  1157. // skip_past should have no influence on gradient computation.
  1158. // so when other modes work, we assume that this does as well.
  1159. continue;
  1160. }
  1161. struct ggml_tensor * f = ggml_sum(ctx0, ggml_rope(ctx0, x[0], p, n_rot, mode));
  1162. GGML_PRINT_DEBUG("rope f32: n_past: %d n_rot: %d mode: %d\n", n_past, n_rot, mode);
  1163. check_gradient("rope f32", ctx0, x, f, ndims, nargs, 1e-2f, 1e-3f, INFINITY);
  1164. }
  1165. }
  1166. }
  1167. }
  1168. // rope f16
  1169. {
  1170. srand(seed);
  1171. const int nargs = 1;
  1172. int64_t ne2[4];
  1173. get_random_dims(ne2, 4);
  1174. ne2[0] += ne2[0] % 2;
  1175. int n_rot = ne2[0];
  1176. for (int ndims = 3; ndims <= 4; ++ndims) {
  1177. for (int mode = 0; mode < 4; ++mode) {
  1178. for (int n_past = 1; n_past < ne2[2]; ++n_past) {
  1179. x[0] = get_random_tensor_f16(ctx0, ndims, ne2, -1.0f, 1.0f);
  1180. struct ggml_tensor * p = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ne2[2]);
  1181. for (int i = 0; i < ne2[2]; ++i) {
  1182. ((int32_t *) p->data)[i] = n_past + i;
  1183. }
  1184. ggml_set_param(ctx0, x[0]);
  1185. const bool skip_past = (mode & 1);
  1186. if (skip_past) {
  1187. // we have no past, so this would have to work on uninitialized memory.
  1188. // we only test the gradients here;
  1189. // skip_past should have no influence on gradient computation.
  1190. // so when other modes work, we assume that this does as well.
  1191. continue;
  1192. }
  1193. struct ggml_tensor * f = ggml_sum(ctx0, ggml_rope(ctx0, x[0], p, n_rot, mode));
  1194. GGML_PRINT_DEBUG("rope f16: n_past: %d n_rot: %d mode: %d\n", n_past, n_rot, mode);
  1195. check_gradient("rope f16", ctx0, x, f, ndims, nargs, 1e-1f, 1e-1f, INFINITY);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. // flash_attn f32
  1201. // TODO: adapt to ggml_flash_attn_ext() changes
  1202. //{
  1203. // srand(seed);
  1204. // const int nargs = 3;
  1205. // int64_t ne2[4];
  1206. // get_random_dims(ne2, 4);
  1207. // int64_t D = ne2[0];
  1208. // int64_t N = ne2[1];
  1209. // int64_t M = ne2[2] + N;
  1210. // int64_t B = ne2[3];
  1211. // for (int masked = 0; masked <= 1; ++masked) {
  1212. // for (int ndims = 2; ndims <= 4; ++ndims) {
  1213. // int max_nrep = (ndims >= 3) ? 2 : 1;
  1214. // for (int nrep = 1; nrep < max_nrep; ++nrep) {
  1215. // int64_t neq[4] = { D, N, B*nrep, ne[3] };
  1216. // int64_t nek[4] = { D, M, B, ne[3] };
  1217. // int64_t nev[4] = { M, D, B, ne[3] };
  1218. // if (ndims == 2) {
  1219. // neq[2] = 1; neq[3] = 1;
  1220. // nek[2] = 1; nek[3] = 1;
  1221. // nev[2] = 1; nev[3] = 1;
  1222. // } else if (ndims == 3) {
  1223. // neq[3] = 1;
  1224. // nek[3] = 1;
  1225. // nev[3] = 1;
  1226. // }
  1227. // x[0] = get_random_tensor_f32(ctx0, ndims, neq, -0.1250f, 0.1250f);
  1228. // x[1] = get_random_tensor_f32(ctx0, ndims, nek, -0.1250f, 0.1250f);
  1229. // x[2] = get_random_tensor_f32(ctx0, ndims, nev, -0.1250f, 0.1250f);
  1230. // ggml_set_param(ctx0, x[0]);
  1231. // ggml_set_param(ctx0, x[1]);
  1232. // ggml_set_param(ctx0, x[2]);
  1233. // struct ggml_tensor * f = ggml_sum(ctx0, ggml_flash_attn(ctx0, x[0], x[1], x[2], (masked == 0)));
  1234. // check_gradient("flash_attn f32", ctx0, x, f, ndims, nargs, 1.5e-4f, 1e-3f, INFINITY);
  1235. // }
  1236. // }
  1237. // }
  1238. //}
  1239. ggml_free(ctx0);
  1240. }
  1241. return 0;
  1242. }