ggml.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. #pragma once
  2. //
  3. // GGML Tensor Library
  4. //
  5. // This documentation is still a work in progress.
  6. // If you wish some specific topics to be covered, feel free to drop a comment:
  7. //
  8. // https://github.com/ggerganov/whisper.cpp/issues/40
  9. //
  10. // ## Overview
  11. //
  12. // This library implements:
  13. //
  14. // - a set of tensor operations
  15. // - automatic differentiation
  16. // - basic optimization algorithms
  17. //
  18. // The aim of this library is to provide a minimalistic approach for various machine learning tasks. This includes,
  19. // but is not limited to, the following:
  20. //
  21. // - linear regression
  22. // - support vector machines
  23. // - neural networks
  24. //
  25. // The library allows the user to define a certain function using the available tensor operations. This function
  26. // definition is represented internally via a computation graph. Each tensor operation in the function definition
  27. // corresponds to a node in the graph. Having the computation graph defined, the user can choose to compute the
  28. // function's value and/or its gradient with respect to the input variables. Optionally, the function can be optimized
  29. // using one of the available optimization algorithms.
  30. //
  31. // For example, here we define the function: f(x) = a*x^2 + b
  32. //
  33. // {
  34. // struct ggml_init_params params = {
  35. // .mem_size = 16*1024*1024,
  36. // .mem_buffer = NULL,
  37. // };
  38. //
  39. // // memory allocation happens here
  40. // struct ggml_context * ctx = ggml_init(params);
  41. //
  42. // struct ggml_tensor * x = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
  43. //
  44. // ggml_set_param(ctx, x); // x is an input variable
  45. //
  46. // struct ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
  47. // struct ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
  48. // struct ggml_tensor * x2 = ggml_mul(ctx, x, x);
  49. // struct ggml_tensor * f = ggml_add(ctx, ggml_mul(ctx, a, x2), b);
  50. //
  51. // ...
  52. // }
  53. //
  54. // Notice that the function definition above does not involve any actual computation. The computation is performed only
  55. // when the user explicitly requests it. For example, to compute the function's value at x = 2.0:
  56. //
  57. // {
  58. // ...
  59. //
  60. // struct ggml_cgraph gf = ggml_build_forward(f);
  61. //
  62. // // set the input variable and parameter values
  63. // ggml_set_f32(x, 2.0f);
  64. // ggml_set_f32(a, 3.0f);
  65. // ggml_set_f32(b, 4.0f);
  66. //
  67. // ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
  68. //
  69. // printf("f = %f\n", ggml_get_f32_1d(f, 0));
  70. //
  71. // ...
  72. // }
  73. //
  74. // The actual computation is performed in the ggml_graph_compute() function.
  75. //
  76. // The ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
  77. // ggml_init() function. You have to be careful not to exceed the memory buffer size. Therefore, you have to know
  78. // in advance how much memory you need for your computation. Alternatively, you can allocate a large enough memory
  79. // and after defining the computation graph, call the ggml_used_mem() function to find out how much memory was
  80. // actually needed.
  81. //
  82. // The ggml_set_param() function marks a tensor as an input variable. This is used by the automatic
  83. // differentiation and optimization algorithms.
  84. //
  85. // The described approach allows to define the function graph once and then compute its forward or backward graphs
  86. // multiple times. All computations will use the same memory buffer allocated in the ggml_init() function. This way
  87. // the user can avoid the memory allocation overhead at runtime.
  88. //
  89. // The library supports multi-dimensional tensors - up to 4 dimensions. The FP16 and FP32 data types are first class
  90. // citizens, but in theory the library can be extended to support FP8 and integer data types.
  91. //
  92. // Each tensor operation produces a new tensor. Initially the library was envisioned to support only the use of unary
  93. // and binary operations. Most of the available operations fall into one of these two categories. With time, it became
  94. // clear that the library needs to support more complex operations. The way to support these operations is not clear
  95. // yet, but a few examples are demonstrated in the following operations:
  96. //
  97. // - ggml_permute()
  98. // - ggml_conv_1d_1s()
  99. // - ggml_conv_1d_2s()
  100. //
  101. // For each tensor operator, the library implements a forward and backward computation function. The forward function
  102. // computes the output tensor value given the input tensor values. The backward function computes the adjoint of the
  103. // input tensors given the adjoint of the output tensor. For a detailed explanation of what this means, take a
  104. // calculus class, or watch the following video:
  105. //
  106. // What is Automatic Differentiation?
  107. // https://www.youtube.com/watch?v=wG_nF1awSSY
  108. //
  109. //
  110. // ## Tensor data (struct ggml_tensor)
  111. //
  112. // The tensors are stored in memory via the ggml_tensor struct. The structure provides information about the size of
  113. // the tensor, the data type, and the memory buffer where the tensor data is stored. Additionally, it contains
  114. // pointers to the "source" tensors - i.e. the tensors that were used to compute the current tensor. For example:
  115. //
  116. // {
  117. // struct ggml_tensor * c = ggml_add(ctx, a, b);
  118. //
  119. // assert(c->src[0] == a);
  120. // assert(c->src[1] == b);
  121. // }
  122. //
  123. // The multi-dimensional tensors are stored in row-major order. The ggml_tensor struct contains fields for the
  124. // number of elements in each dimension ("ne") as well as the number of bytes ("nb", a.k.a. stride). This allows
  125. // to store tensors that are not contiguous in memory, which is useful for operations such as transposition and
  126. // permutation. All tensor operations have to take the stride into account and not assume that the tensor is
  127. // contiguous in memory.
  128. //
  129. // The data of the tensor is accessed via the "data" pointer. For example:
  130. //
  131. // {
  132. // struct ggml_tensor * a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 2, 3);
  133. //
  134. // // a[2, 1] = 1.0f;
  135. // *(float *) ((char *) a->data + 2*a->nb[1] + 1*a->nb[0]) = 1.0f;
  136. //
  137. // // a[0, 2] = 2.0f;
  138. // *(float *) ((char *) a->data + 0*a->nb[1] + 2*a->nb[0]) = 2.0f;
  139. //
  140. // ...
  141. // }
  142. //
  143. // Alternatively, there are helper functions, such as ggml_get_f32_1d() and ggml_set_f32_1d() that can be used.
  144. //
  145. // ## The matrix multiplication operator (ggml_mul_mat)
  146. //
  147. // TODO
  148. //
  149. //
  150. // ## Multi-threading
  151. //
  152. // TODO
  153. //
  154. //
  155. // ## Overview of ggml.c
  156. //
  157. // TODO
  158. //
  159. //
  160. // ## SIMD optimizations
  161. //
  162. // TODO
  163. //
  164. //
  165. // ## Debugging ggml
  166. //
  167. // TODO
  168. //
  169. //
  170. #ifdef GGML_SHARED
  171. # if defined(_WIN32) && !defined(__MINGW32__)
  172. # ifdef GGML_BUILD
  173. # define GGML_API __declspec(dllexport)
  174. # else
  175. # define GGML_API __declspec(dllimport)
  176. # endif
  177. # else
  178. # define GGML_API __attribute__ ((visibility ("default")))
  179. # endif
  180. #else
  181. # define GGML_API
  182. #endif
  183. #include <stdint.h>
  184. #include <stddef.h>
  185. #include <stdbool.h>
  186. #define GGML_FILE_MAGIC 0x67676d6c // "ggml"
  187. #define GGML_FILE_VERSION 1
  188. #define GGML_QNT_VERSION 2 // bump this on quantization format changes
  189. #define GGML_QNT_VERSION_FACTOR 1000 // do not change this
  190. #define GGML_MAX_DIMS 4
  191. #define GGML_MAX_NODES 4096
  192. #define GGML_MAX_PARAMS 256
  193. #define GGML_MAX_CONTEXTS 64
  194. #define GGML_MAX_SRC 6
  195. #define GGML_MAX_NAME 48
  196. #define GGML_DEFAULT_N_THREADS 4
  197. #define GGML_EXIT_SUCCESS 0
  198. #define GGML_EXIT_ABORTED 1
  199. #define GGML_UNUSED(x) (void)(x)
  200. #define GGML_ASSERT(x) \
  201. do { \
  202. if (!(x)) { \
  203. fprintf(stderr, "GGML_ASSERT: %s:%d: %s\n", __FILE__, __LINE__, #x); \
  204. abort(); \
  205. } \
  206. } while (0)
  207. // used to copy the number of elements and stride in bytes of tensors into local variables.
  208. // main purpose is to reduce code duplication and improve readability.
  209. //
  210. // example:
  211. //
  212. // GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  213. // GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
  214. //
  215. #define GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
  216. const type prefix##0 = (pointer)->array[0]; \
  217. GGML_UNUSED(prefix##0);
  218. #define GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
  219. GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
  220. const type prefix##1 = (pointer)->array[1]; \
  221. GGML_UNUSED(prefix##1);
  222. #define GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
  223. GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
  224. const type prefix##2 = (pointer)->array[2]; \
  225. GGML_UNUSED(prefix##2);
  226. #define GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
  227. GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
  228. const type prefix##3 = (pointer)->array[3]; \
  229. GGML_UNUSED(prefix##3);
  230. #ifdef __cplusplus
  231. extern "C" {
  232. #endif
  233. #ifdef __ARM_NEON
  234. // we use the built-in 16-bit float type
  235. typedef __fp16 ggml_fp16_t;
  236. #else
  237. typedef uint16_t ggml_fp16_t;
  238. #endif
  239. // convert FP16 <-> FP32
  240. GGML_API float ggml_fp16_to_fp32(ggml_fp16_t x);
  241. GGML_API ggml_fp16_t ggml_fp32_to_fp16(float x);
  242. GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t * x, float * y, int n);
  243. GGML_API void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, int n);
  244. struct ggml_object;
  245. struct ggml_context;
  246. enum ggml_type {
  247. GGML_TYPE_F32 = 0,
  248. GGML_TYPE_F16 = 1,
  249. GGML_TYPE_Q4_0 = 2,
  250. GGML_TYPE_Q4_1 = 3,
  251. // GGML_TYPE_Q4_2 = 4, support has been removed
  252. // GGML_TYPE_Q4_3 (5) support has been removed
  253. GGML_TYPE_Q5_0 = 6,
  254. GGML_TYPE_Q5_1 = 7,
  255. GGML_TYPE_Q8_0 = 8,
  256. GGML_TYPE_Q8_1 = 9,
  257. // k-quantizations
  258. GGML_TYPE_Q2_K = 10,
  259. GGML_TYPE_Q3_K = 11,
  260. GGML_TYPE_Q4_K = 12,
  261. GGML_TYPE_Q5_K = 13,
  262. GGML_TYPE_Q6_K = 14,
  263. GGML_TYPE_Q8_K = 15,
  264. GGML_TYPE_I8,
  265. GGML_TYPE_I16,
  266. GGML_TYPE_I32,
  267. GGML_TYPE_COUNT,
  268. };
  269. enum ggml_backend {
  270. GGML_BACKEND_CPU = 0,
  271. GGML_BACKEND_GPU = 10,
  272. GGML_BACKEND_GPU_SPLIT = 20,
  273. };
  274. // model file types
  275. enum ggml_ftype {
  276. GGML_FTYPE_UNKNOWN = -1,
  277. GGML_FTYPE_ALL_F32 = 0,
  278. GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
  279. GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
  280. GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
  281. GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
  282. GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
  283. GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
  284. GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
  285. GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
  286. GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
  287. GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
  288. GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
  289. GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
  290. };
  291. // available tensor operations:
  292. enum ggml_op {
  293. GGML_OP_NONE = 0,
  294. GGML_OP_DUP,
  295. GGML_OP_ADD,
  296. GGML_OP_ADD1,
  297. GGML_OP_ACC,
  298. GGML_OP_SUB,
  299. GGML_OP_MUL,
  300. GGML_OP_DIV,
  301. GGML_OP_SQR,
  302. GGML_OP_SQRT,
  303. GGML_OP_LOG,
  304. GGML_OP_SUM,
  305. GGML_OP_SUM_ROWS,
  306. GGML_OP_MEAN,
  307. GGML_OP_ARGMAX,
  308. GGML_OP_REPEAT,
  309. GGML_OP_REPEAT_BACK,
  310. GGML_OP_ABS,
  311. GGML_OP_SGN,
  312. GGML_OP_NEG,
  313. GGML_OP_STEP,
  314. GGML_OP_TANH,
  315. GGML_OP_ELU,
  316. GGML_OP_RELU,
  317. GGML_OP_GELU,
  318. GGML_OP_GELU_QUICK,
  319. GGML_OP_SILU,
  320. GGML_OP_SILU_BACK,
  321. GGML_OP_NORM, // normalize
  322. GGML_OP_RMS_NORM,
  323. GGML_OP_RMS_NORM_BACK,
  324. GGML_OP_MUL_MAT,
  325. GGML_OP_OUT_PROD,
  326. GGML_OP_SCALE,
  327. GGML_OP_SET,
  328. GGML_OP_CPY,
  329. GGML_OP_CONT,
  330. GGML_OP_RESHAPE,
  331. GGML_OP_VIEW,
  332. GGML_OP_PERMUTE,
  333. GGML_OP_TRANSPOSE,
  334. GGML_OP_GET_ROWS,
  335. GGML_OP_GET_ROWS_BACK,
  336. GGML_OP_DIAG,
  337. GGML_OP_DIAG_MASK_INF,
  338. GGML_OP_DIAG_MASK_ZERO,
  339. GGML_OP_SOFT_MAX,
  340. GGML_OP_SOFT_MAX_BACK,
  341. GGML_OP_ROPE,
  342. GGML_OP_ROPE_BACK,
  343. GGML_OP_ALIBI,
  344. GGML_OP_CLAMP,
  345. GGML_OP_CONV_1D,
  346. GGML_OP_CONV_2D,
  347. GGML_OP_POOL_1D,
  348. GGML_OP_POOL_2D,
  349. GGML_OP_FLASH_ATTN,
  350. GGML_OP_FLASH_FF,
  351. GGML_OP_FLASH_ATTN_BACK,
  352. GGML_OP_WIN_PART,
  353. GGML_OP_WIN_UNPART,
  354. GGML_OP_MAP_UNARY,
  355. GGML_OP_MAP_BINARY,
  356. GGML_OP_MAP_CUSTOM1,
  357. GGML_OP_MAP_CUSTOM2,
  358. GGML_OP_MAP_CUSTOM3,
  359. GGML_OP_CROSS_ENTROPY_LOSS,
  360. GGML_OP_CROSS_ENTROPY_LOSS_BACK,
  361. GGML_OP_COUNT,
  362. };
  363. // ggml object
  364. struct ggml_object {
  365. size_t offs;
  366. size_t size;
  367. struct ggml_object * next;
  368. char padding[8];
  369. };
  370. static const size_t GGML_OBJECT_SIZE = sizeof(struct ggml_object);
  371. // n-dimensional tensor
  372. struct ggml_tensor {
  373. enum ggml_type type;
  374. enum ggml_backend backend;
  375. int n_dims;
  376. int64_t ne[GGML_MAX_DIMS]; // number of elements
  377. size_t nb[GGML_MAX_DIMS]; // stride in bytes:
  378. // nb[0] = sizeof(type)
  379. // nb[1] = nb[0] * ne[0] + padding
  380. // nb[i] = nb[i-1] * ne[i-1]
  381. // compute data
  382. enum ggml_op op;
  383. bool is_param;
  384. struct ggml_tensor * grad;
  385. struct ggml_tensor * src[GGML_MAX_SRC];
  386. // performance
  387. int perf_runs;
  388. int64_t perf_cycles;
  389. int64_t perf_time_us;
  390. void * data;
  391. char name[GGML_MAX_NAME];
  392. void * extra; // extra things e.g. for ggml-cuda.cu
  393. char padding[8];
  394. };
  395. static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
  396. // the compute plan that needs to be prepared for ggml_graph_compute()
  397. // since https://github.com/ggerganov/ggml/issues/287
  398. struct ggml_cplan {
  399. size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
  400. uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`
  401. int n_threads;
  402. // the `n_tasks` of nodes, 1:1 mapping to cgraph nodes
  403. int n_tasks[GGML_MAX_NODES];
  404. // abort ggml_graph_compute when true
  405. bool (*abort_callback)(void * data);
  406. void * abort_callback_data;
  407. };
  408. // computation graph
  409. struct ggml_cgraph {
  410. int n_nodes;
  411. int n_leafs;
  412. struct ggml_tensor * nodes[GGML_MAX_NODES];
  413. struct ggml_tensor * grads[GGML_MAX_NODES];
  414. struct ggml_tensor * leafs[GGML_MAX_NODES];
  415. // performance
  416. int perf_runs;
  417. int64_t perf_cycles;
  418. int64_t perf_time_us;
  419. };
  420. // scratch buffer
  421. struct ggml_scratch {
  422. size_t offs;
  423. size_t size;
  424. void * data;
  425. };
  426. struct ggml_init_params {
  427. // memory pool
  428. size_t mem_size; // bytes
  429. void * mem_buffer; // if NULL, memory will be allocated internally
  430. bool no_alloc; // don't allocate memory for the tensor data
  431. };
  432. // compute types
  433. // NOTE: the INIT or FINALIZE pass is not scheduled unless explicitly enabled.
  434. // This behavior was changed since https://github.com/ggerganov/llama.cpp/pull/1995.
  435. enum ggml_task_type {
  436. GGML_TASK_INIT = 0,
  437. GGML_TASK_COMPUTE,
  438. GGML_TASK_FINALIZE,
  439. };
  440. struct ggml_compute_params {
  441. enum ggml_task_type type;
  442. // ith = thread index, nth = number of threads
  443. int ith, nth;
  444. // work buffer for all threads
  445. size_t wsize;
  446. void * wdata;
  447. };
  448. // misc
  449. GGML_API void ggml_time_init(void); // call this once at the beginning of the program
  450. GGML_API int64_t ggml_time_ms(void);
  451. GGML_API int64_t ggml_time_us(void);
  452. GGML_API int64_t ggml_cycles(void);
  453. GGML_API int64_t ggml_cycles_per_ms(void);
  454. GGML_API void ggml_numa_init(void); // call once for better performance on NUMA systems
  455. GGML_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
  456. GGML_API void ggml_print_object (const struct ggml_object * obj);
  457. GGML_API void ggml_print_objects(const struct ggml_context * ctx);
  458. GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor);
  459. GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor);
  460. GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor);
  461. GGML_API size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split);
  462. GGML_API int ggml_blck_size (enum ggml_type type);
  463. GGML_API size_t ggml_type_size (enum ggml_type type); // size in bytes for all elements in a block
  464. GGML_API float ggml_type_sizef(enum ggml_type type); // ggml_type_size()/ggml_blck_size() as float
  465. GGML_API const char * ggml_type_name(enum ggml_type type);
  466. GGML_API const char * ggml_op_name (enum ggml_op op);
  467. GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
  468. GGML_API bool ggml_is_quantized(enum ggml_type type);
  469. // TODO: temporary until model loading of ggml examples is refactored
  470. GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
  471. GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor);
  472. GGML_API bool ggml_is_contiguous(const struct ggml_tensor * tensor);
  473. GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor);
  474. // use this to compute the memory overhead of a tensor
  475. GGML_API size_t ggml_tensor_overhead(void);
  476. // main
  477. GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
  478. GGML_API void ggml_free(struct ggml_context * ctx);
  479. GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
  480. GGML_API size_t ggml_set_scratch (struct ggml_context * ctx, struct ggml_scratch scratch);
  481. GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
  482. GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx);
  483. GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx);
  484. GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
  485. GGML_API struct ggml_tensor * ggml_new_tensor(
  486. struct ggml_context * ctx,
  487. enum ggml_type type,
  488. int n_dims,
  489. const int64_t *ne);
  490. GGML_API struct ggml_tensor * ggml_new_tensor_1d(
  491. struct ggml_context * ctx,
  492. enum ggml_type type,
  493. int64_t ne0);
  494. GGML_API struct ggml_tensor * ggml_new_tensor_2d(
  495. struct ggml_context * ctx,
  496. enum ggml_type type,
  497. int64_t ne0,
  498. int64_t ne1);
  499. GGML_API struct ggml_tensor * ggml_new_tensor_3d(
  500. struct ggml_context * ctx,
  501. enum ggml_type type,
  502. int64_t ne0,
  503. int64_t ne1,
  504. int64_t ne2);
  505. GGML_API struct ggml_tensor * ggml_new_tensor_4d(
  506. struct ggml_context * ctx,
  507. enum ggml_type type,
  508. int64_t ne0,
  509. int64_t ne1,
  510. int64_t ne2,
  511. int64_t ne3);
  512. GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
  513. GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
  514. GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
  515. GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, const struct ggml_tensor * src);
  516. GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
  517. GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
  518. GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
  519. GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
  520. GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
  521. GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
  522. GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
  523. GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
  524. GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
  525. GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
  526. GGML_API const char * ggml_get_name(const struct ggml_tensor * tensor);
  527. GGML_API struct ggml_tensor * ggml_set_name(struct ggml_tensor * tensor, const char * name);
  528. GGML_API struct ggml_tensor * ggml_format_name(struct ggml_tensor * tensor, const char * fmt, ...);
  529. //
  530. // operations on tensors with backpropagation
  531. //
  532. GGML_API struct ggml_tensor * ggml_dup(
  533. struct ggml_context * ctx,
  534. struct ggml_tensor * a);
  535. GGML_API struct ggml_tensor * ggml_add(
  536. struct ggml_context * ctx,
  537. struct ggml_tensor * a,
  538. struct ggml_tensor * b);
  539. GGML_API struct ggml_tensor * ggml_add_inplace(
  540. struct ggml_context * ctx,
  541. struct ggml_tensor * a,
  542. struct ggml_tensor * b);
  543. GGML_API struct ggml_tensor * ggml_add1(
  544. struct ggml_context * ctx,
  545. struct ggml_tensor * a,
  546. struct ggml_tensor * b);
  547. GGML_API struct ggml_tensor * ggml_add1_inplace(
  548. struct ggml_context * ctx,
  549. struct ggml_tensor * a,
  550. struct ggml_tensor * b);
  551. GGML_API struct ggml_tensor * ggml_acc(
  552. struct ggml_context * ctx,
  553. struct ggml_tensor * a,
  554. struct ggml_tensor * b,
  555. size_t nb1,
  556. size_t nb2,
  557. size_t nb3,
  558. size_t offset);
  559. GGML_API struct ggml_tensor * ggml_acc_inplace(
  560. struct ggml_context * ctx,
  561. struct ggml_tensor * a,
  562. struct ggml_tensor * b,
  563. size_t nb1,
  564. size_t nb2,
  565. size_t nb3,
  566. size_t offset);
  567. GGML_API struct ggml_tensor * ggml_sub(
  568. struct ggml_context * ctx,
  569. struct ggml_tensor * a,
  570. struct ggml_tensor * b);
  571. GGML_API struct ggml_tensor * ggml_sub_inplace(
  572. struct ggml_context * ctx,
  573. struct ggml_tensor * a,
  574. struct ggml_tensor * b);
  575. GGML_API struct ggml_tensor * ggml_mul(
  576. struct ggml_context * ctx,
  577. struct ggml_tensor * a,
  578. struct ggml_tensor * b);
  579. GGML_API struct ggml_tensor * ggml_mul_inplace(
  580. struct ggml_context * ctx,
  581. struct ggml_tensor * a,
  582. struct ggml_tensor * b);
  583. GGML_API struct ggml_tensor * ggml_div(
  584. struct ggml_context * ctx,
  585. struct ggml_tensor * a,
  586. struct ggml_tensor * b);
  587. GGML_API struct ggml_tensor * ggml_div_inplace(
  588. struct ggml_context * ctx,
  589. struct ggml_tensor * a,
  590. struct ggml_tensor * b);
  591. GGML_API struct ggml_tensor * ggml_sqr(
  592. struct ggml_context * ctx,
  593. struct ggml_tensor * a);
  594. GGML_API struct ggml_tensor * ggml_sqr_inplace(
  595. struct ggml_context * ctx,
  596. struct ggml_tensor * a);
  597. GGML_API struct ggml_tensor * ggml_sqrt(
  598. struct ggml_context * ctx,
  599. struct ggml_tensor * a);
  600. GGML_API struct ggml_tensor * ggml_sqrt_inplace(
  601. struct ggml_context * ctx,
  602. struct ggml_tensor * a);
  603. GGML_API struct ggml_tensor * ggml_log(
  604. struct ggml_context * ctx,
  605. struct ggml_tensor * a);
  606. GGML_API struct ggml_tensor * ggml_log_inplace(
  607. struct ggml_context * ctx,
  608. struct ggml_tensor * a);
  609. // return scalar
  610. GGML_API struct ggml_tensor * ggml_sum(
  611. struct ggml_context * ctx,
  612. struct ggml_tensor * a);
  613. // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
  614. GGML_API struct ggml_tensor * ggml_sum_rows(
  615. struct ggml_context * ctx,
  616. struct ggml_tensor * a);
  617. // mean along rows
  618. GGML_API struct ggml_tensor * ggml_mean(
  619. struct ggml_context * ctx,
  620. struct ggml_tensor * a);
  621. // argmax along rows
  622. GGML_API struct ggml_tensor * ggml_argmax(
  623. struct ggml_context * ctx,
  624. struct ggml_tensor * a);
  625. // if a is the same shape as b, and a is not parameter, return a
  626. // otherwise, return a new tensor: repeat(a) to fit in b
  627. GGML_API struct ggml_tensor * ggml_repeat(
  628. struct ggml_context * ctx,
  629. struct ggml_tensor * a,
  630. struct ggml_tensor * b);
  631. GGML_API struct ggml_tensor * ggml_repeat_back(
  632. struct ggml_context * ctx,
  633. struct ggml_tensor * a,
  634. struct ggml_tensor * b);
  635. GGML_API struct ggml_tensor * ggml_abs(
  636. struct ggml_context * ctx,
  637. struct ggml_tensor * a);
  638. GGML_API struct ggml_tensor * ggml_abs_inplace(
  639. struct ggml_context * ctx,
  640. struct ggml_tensor * a);
  641. GGML_API struct ggml_tensor * ggml_sgn(
  642. struct ggml_context * ctx,
  643. struct ggml_tensor * a);
  644. GGML_API struct ggml_tensor * ggml_sgn_inplace(
  645. struct ggml_context * ctx,
  646. struct ggml_tensor * a);
  647. GGML_API struct ggml_tensor * ggml_neg(
  648. struct ggml_context * ctx,
  649. struct ggml_tensor * a);
  650. GGML_API struct ggml_tensor * ggml_neg_inplace(
  651. struct ggml_context * ctx,
  652. struct ggml_tensor * a);
  653. GGML_API struct ggml_tensor * ggml_step(
  654. struct ggml_context * ctx,
  655. struct ggml_tensor * a);
  656. GGML_API struct ggml_tensor * ggml_step_inplace(
  657. struct ggml_context * ctx,
  658. struct ggml_tensor * a);
  659. GGML_API struct ggml_tensor * ggml_tanh(
  660. struct ggml_context * ctx,
  661. struct ggml_tensor * a);
  662. GGML_API struct ggml_tensor * ggml_tanh_inplace(
  663. struct ggml_context * ctx,
  664. struct ggml_tensor * a);
  665. GGML_API struct ggml_tensor * ggml_elu(
  666. struct ggml_context * ctx,
  667. struct ggml_tensor * a);
  668. GGML_API struct ggml_tensor * ggml_elu_inplace(
  669. struct ggml_context * ctx,
  670. struct ggml_tensor * a);
  671. GGML_API struct ggml_tensor * ggml_relu(
  672. struct ggml_context * ctx,
  673. struct ggml_tensor * a);
  674. GGML_API struct ggml_tensor * ggml_relu_inplace(
  675. struct ggml_context * ctx,
  676. struct ggml_tensor * a);
  677. // TODO: double-check this computation is correct
  678. GGML_API struct ggml_tensor * ggml_gelu(
  679. struct ggml_context * ctx,
  680. struct ggml_tensor * a);
  681. GGML_API struct ggml_tensor * ggml_gelu_inplace(
  682. struct ggml_context * ctx,
  683. struct ggml_tensor * a);
  684. GGML_API struct ggml_tensor * ggml_gelu_quick(
  685. struct ggml_context * ctx,
  686. struct ggml_tensor * a);
  687. GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
  688. struct ggml_context * ctx,
  689. struct ggml_tensor * a);
  690. GGML_API struct ggml_tensor * ggml_silu(
  691. struct ggml_context * ctx,
  692. struct ggml_tensor * a);
  693. GGML_API struct ggml_tensor * ggml_silu_inplace(
  694. struct ggml_context * ctx,
  695. struct ggml_tensor * a);
  696. // a - x
  697. // b - dy
  698. GGML_API struct ggml_tensor * ggml_silu_back(
  699. struct ggml_context * ctx,
  700. struct ggml_tensor * a,
  701. struct ggml_tensor * b);
  702. // normalize along rows
  703. // TODO: eps is hardcoded to 1e-5 for now
  704. GGML_API struct ggml_tensor * ggml_norm(
  705. struct ggml_context * ctx,
  706. struct ggml_tensor * a);
  707. GGML_API struct ggml_tensor * ggml_norm_inplace(
  708. struct ggml_context * ctx,
  709. struct ggml_tensor * a);
  710. GGML_API struct ggml_tensor * ggml_rms_norm(
  711. struct ggml_context * ctx,
  712. struct ggml_tensor * a);
  713. GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
  714. struct ggml_context * ctx,
  715. struct ggml_tensor * a);
  716. // a - x
  717. // b - dy
  718. GGML_API struct ggml_tensor * ggml_rms_norm_back(
  719. struct ggml_context * ctx,
  720. struct ggml_tensor * a,
  721. struct ggml_tensor * b);
  722. // A: n columns, m rows
  723. // B: n columns, p rows (i.e. we transpose it internally)
  724. // result is m columns, p rows
  725. GGML_API struct ggml_tensor * ggml_mul_mat(
  726. struct ggml_context * ctx,
  727. struct ggml_tensor * a,
  728. struct ggml_tensor * b);
  729. // A: m columns, n rows,
  730. // B: p columns, n rows,
  731. // result is m columns, p rows
  732. GGML_API struct ggml_tensor * ggml_out_prod(
  733. struct ggml_context * ctx,
  734. struct ggml_tensor * a,
  735. struct ggml_tensor * b);
  736. //
  737. // operations on tensors without backpropagation
  738. //
  739. GGML_API struct ggml_tensor * ggml_scale(
  740. struct ggml_context * ctx,
  741. struct ggml_tensor * a,
  742. struct ggml_tensor * b);
  743. // in-place, returns view(a)
  744. GGML_API struct ggml_tensor * ggml_scale_inplace(
  745. struct ggml_context * ctx,
  746. struct ggml_tensor * a,
  747. struct ggml_tensor * b);
  748. // b -> view(a,offset,nb1,nb2,3), return modified a
  749. GGML_API struct ggml_tensor * ggml_set(
  750. struct ggml_context * ctx,
  751. struct ggml_tensor * a,
  752. struct ggml_tensor * b,
  753. size_t nb1,
  754. size_t nb2,
  755. size_t nb3,
  756. size_t offset);
  757. // b -> view(a,offset,nb1,nb2,3), return view(a)
  758. GGML_API struct ggml_tensor * ggml_set_inplace(
  759. struct ggml_context * ctx,
  760. struct ggml_tensor * a,
  761. struct ggml_tensor * b,
  762. size_t nb1,
  763. size_t nb2,
  764. size_t nb3,
  765. size_t offset);
  766. GGML_API struct ggml_tensor * ggml_set_1d(
  767. struct ggml_context * ctx,
  768. struct ggml_tensor * a,
  769. struct ggml_tensor * b,
  770. size_t offset);
  771. GGML_API struct ggml_tensor * ggml_set_1d_inplace(
  772. struct ggml_context * ctx,
  773. struct ggml_tensor * a,
  774. struct ggml_tensor * b,
  775. size_t offset);
  776. // b -> view(a,offset,nb1,nb2,3), return modified a
  777. GGML_API struct ggml_tensor * ggml_set_2d(
  778. struct ggml_context * ctx,
  779. struct ggml_tensor * a,
  780. struct ggml_tensor * b,
  781. size_t nb1,
  782. size_t offset);
  783. // b -> view(a,offset,nb1,nb2,3), return view(a)
  784. GGML_API struct ggml_tensor * ggml_set_2d_inplace(
  785. struct ggml_context * ctx,
  786. struct ggml_tensor * a,
  787. struct ggml_tensor * b,
  788. size_t nb1,
  789. size_t offset);
  790. // a -> b, return view(b)
  791. GGML_API struct ggml_tensor * ggml_cpy(
  792. struct ggml_context * ctx,
  793. struct ggml_tensor * a,
  794. struct ggml_tensor * b);
  795. // make contiguous
  796. GGML_API struct ggml_tensor * ggml_cont(
  797. struct ggml_context * ctx,
  798. struct ggml_tensor * a);
  799. // return view(a), b specifies the new shape
  800. // TODO: when we start computing gradient, make a copy instead of view
  801. GGML_API struct ggml_tensor * ggml_reshape(
  802. struct ggml_context * ctx,
  803. struct ggml_tensor * a,
  804. struct ggml_tensor * b);
  805. // return view(a)
  806. // TODO: when we start computing gradient, make a copy instead of view
  807. GGML_API struct ggml_tensor * ggml_reshape_1d(
  808. struct ggml_context * ctx,
  809. struct ggml_tensor * a,
  810. int64_t ne0);
  811. GGML_API struct ggml_tensor * ggml_reshape_2d(
  812. struct ggml_context * ctx,
  813. struct ggml_tensor * a,
  814. int64_t ne0,
  815. int64_t ne1);
  816. // return view(a)
  817. // TODO: when we start computing gradient, make a copy instead of view
  818. GGML_API struct ggml_tensor * ggml_reshape_3d(
  819. struct ggml_context * ctx,
  820. struct ggml_tensor * a,
  821. int64_t ne0,
  822. int64_t ne1,
  823. int64_t ne2);
  824. GGML_API struct ggml_tensor * ggml_reshape_4d(
  825. struct ggml_context * ctx,
  826. struct ggml_tensor * a,
  827. int64_t ne0,
  828. int64_t ne1,
  829. int64_t ne2,
  830. int64_t ne3);
  831. // offset in bytes
  832. GGML_API struct ggml_tensor * ggml_view_1d(
  833. struct ggml_context * ctx,
  834. struct ggml_tensor * a,
  835. int64_t ne0,
  836. size_t offset);
  837. GGML_API struct ggml_tensor * ggml_view_2d(
  838. struct ggml_context * ctx,
  839. struct ggml_tensor * a,
  840. int64_t ne0,
  841. int64_t ne1,
  842. size_t nb1, // row stride in bytes
  843. size_t offset);
  844. GGML_API struct ggml_tensor * ggml_view_3d(
  845. struct ggml_context * ctx,
  846. struct ggml_tensor * a,
  847. int64_t ne0,
  848. int64_t ne1,
  849. int64_t ne2,
  850. size_t nb1, // row stride in bytes
  851. size_t nb2, // slice stride in bytes
  852. size_t offset);
  853. GGML_API struct ggml_tensor * ggml_view_4d(
  854. struct ggml_context * ctx,
  855. struct ggml_tensor * a,
  856. int64_t ne0,
  857. int64_t ne1,
  858. int64_t ne2,
  859. int64_t ne3,
  860. size_t nb1, // row stride in bytes
  861. size_t nb2, // slice stride in bytes
  862. size_t nb3,
  863. size_t offset);
  864. GGML_API struct ggml_tensor * ggml_permute(
  865. struct ggml_context * ctx,
  866. struct ggml_tensor * a,
  867. int axis0,
  868. int axis1,
  869. int axis2,
  870. int axis3);
  871. // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
  872. GGML_API struct ggml_tensor * ggml_transpose(
  873. struct ggml_context * ctx,
  874. struct ggml_tensor * a);
  875. GGML_API struct ggml_tensor * ggml_get_rows(
  876. struct ggml_context * ctx,
  877. struct ggml_tensor * a,
  878. struct ggml_tensor * b);
  879. GGML_API struct ggml_tensor * ggml_get_rows_back(
  880. struct ggml_context * ctx,
  881. struct ggml_tensor * a,
  882. struct ggml_tensor * b,
  883. struct ggml_tensor * c);
  884. GGML_API struct ggml_tensor * ggml_diag(
  885. struct ggml_context * ctx,
  886. struct ggml_tensor * a);
  887. // set elements above the diagonal to -INF
  888. GGML_API struct ggml_tensor * ggml_diag_mask_inf(
  889. struct ggml_context * ctx,
  890. struct ggml_tensor * a,
  891. int n_past);
  892. // in-place, returns view(a)
  893. GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
  894. struct ggml_context * ctx,
  895. struct ggml_tensor * a,
  896. int n_past);
  897. // set elements above the diagonal to 0
  898. GGML_API struct ggml_tensor * ggml_diag_mask_zero(
  899. struct ggml_context * ctx,
  900. struct ggml_tensor * a,
  901. int n_past);
  902. // in-place, returns view(a)
  903. GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
  904. struct ggml_context * ctx,
  905. struct ggml_tensor * a,
  906. int n_past);
  907. GGML_API struct ggml_tensor * ggml_soft_max(
  908. struct ggml_context * ctx,
  909. struct ggml_tensor * a);
  910. // in-place, returns view(a)
  911. GGML_API struct ggml_tensor * ggml_soft_max_inplace(
  912. struct ggml_context * ctx,
  913. struct ggml_tensor * a);
  914. GGML_API struct ggml_tensor * ggml_soft_max_back(
  915. struct ggml_context * ctx,
  916. struct ggml_tensor * a,
  917. struct ggml_tensor * b);
  918. // in-place, returns view(a)
  919. GGML_API struct ggml_tensor * ggml_soft_max_back_inplace(
  920. struct ggml_context * ctx,
  921. struct ggml_tensor * a,
  922. struct ggml_tensor * b);
  923. // rotary position embedding
  924. // if mode & 1 == 1, skip n_past elements
  925. // if mode & 2 == 1, GPT-NeoX style
  926. // if mode & 4 == 1, ChatGLM style
  927. // TODO: avoid creating a new tensor every time
  928. GGML_API struct ggml_tensor * ggml_rope(
  929. struct ggml_context * ctx,
  930. struct ggml_tensor * a,
  931. int n_past,
  932. int n_dims,
  933. int mode,
  934. int n_ctx);
  935. // in-place, returns view(a)
  936. GGML_API struct ggml_tensor * ggml_rope_inplace(
  937. struct ggml_context * ctx,
  938. struct ggml_tensor * a,
  939. int n_past,
  940. int n_dims,
  941. int mode,
  942. int n_ctx);
  943. // custom RoPE, in-place, returns view(a)
  944. GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
  945. struct ggml_context * ctx,
  946. struct ggml_tensor * a,
  947. int n_past,
  948. int n_dims,
  949. int mode,
  950. int n_ctx,
  951. float freq_base,
  952. float freq_scale);
  953. // rotary position embedding backward, i.e compute dx from dy
  954. // a - dy
  955. GGML_API struct ggml_tensor * ggml_rope_back(
  956. struct ggml_context * ctx,
  957. struct ggml_tensor * a,
  958. int n_past,
  959. int n_dims,
  960. int mode,
  961. int n_ctx);
  962. // alibi position embedding
  963. // in-place, returns view(a)
  964. struct ggml_tensor * ggml_alibi(
  965. struct ggml_context * ctx,
  966. struct ggml_tensor * a,
  967. int n_past,
  968. int n_head,
  969. float bias_max);
  970. // clamp
  971. // in-place, returns view(a)
  972. struct ggml_tensor * ggml_clamp(
  973. struct ggml_context * ctx,
  974. struct ggml_tensor * a,
  975. float min,
  976. float max);
  977. GGML_API struct ggml_tensor * ggml_conv_1d(
  978. struct ggml_context * ctx,
  979. struct ggml_tensor * a,
  980. struct ggml_tensor * b,
  981. int s0, // stride
  982. int p0, // padding
  983. int d0); // dilation
  984. GGML_API struct ggml_tensor * ggml_conv_2d(
  985. struct ggml_context * ctx,
  986. struct ggml_tensor * a,
  987. struct ggml_tensor * b,
  988. int s0,
  989. int s1,
  990. int p0,
  991. int p1,
  992. int d0,
  993. int d1);
  994. // conv_1d with padding = half
  995. // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
  996. GGML_API struct ggml_tensor* ggml_conv_1d_ph(
  997. struct ggml_context * ctx,
  998. struct ggml_tensor * a,
  999. struct ggml_tensor * b,
  1000. int s,
  1001. int d);
  1002. enum ggml_op_pool {
  1003. GGML_OP_POOL_MAX,
  1004. GGML_OP_POOL_AVG,
  1005. GGML_OP_POOL_COUNT,
  1006. };
  1007. GGML_API struct ggml_tensor* ggml_pool_1d(
  1008. struct ggml_context * ctx,
  1009. struct ggml_tensor * a,
  1010. enum ggml_op_pool op,
  1011. int k0, // kernel size
  1012. int s0, // stride
  1013. int p0); // padding
  1014. GGML_API struct ggml_tensor* ggml_pool_2d(
  1015. struct ggml_context * ctx,
  1016. struct ggml_tensor * a,
  1017. enum ggml_op_pool op,
  1018. int k0,
  1019. int k1,
  1020. int s0,
  1021. int s1,
  1022. int p0,
  1023. int p1);
  1024. GGML_API struct ggml_tensor * ggml_flash_attn(
  1025. struct ggml_context * ctx,
  1026. struct ggml_tensor * q,
  1027. struct ggml_tensor * k,
  1028. struct ggml_tensor * v,
  1029. bool masked);
  1030. GGML_API struct ggml_tensor * ggml_flash_attn_back(
  1031. struct ggml_context * ctx,
  1032. struct ggml_tensor * q,
  1033. struct ggml_tensor * k,
  1034. struct ggml_tensor * v,
  1035. struct ggml_tensor * d,
  1036. bool masked);
  1037. GGML_API struct ggml_tensor * ggml_flash_ff(
  1038. struct ggml_context * ctx,
  1039. struct ggml_tensor * a,
  1040. struct ggml_tensor * b0,
  1041. struct ggml_tensor * b1,
  1042. struct ggml_tensor * c0,
  1043. struct ggml_tensor * c1);
  1044. // partition into non-overlapping windows with padding if needed
  1045. // example:
  1046. // a: 768 64 64 1
  1047. // w: 14
  1048. // res: 768 14 14 25
  1049. // used in sam
  1050. GGML_API struct ggml_tensor * ggml_win_part(
  1051. struct ggml_context * ctx,
  1052. struct ggml_tensor * a,
  1053. int w);
  1054. // reverse of ggml_win_part
  1055. // used in sam
  1056. GGML_API struct ggml_tensor * ggml_win_unpart(
  1057. struct ggml_context * ctx,
  1058. struct ggml_tensor * a,
  1059. int w0,
  1060. int h0,
  1061. int w);
  1062. // custom operators
  1063. typedef void (*ggml_unary_op_f32_t) (const int, float *, const float *);
  1064. typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
  1065. typedef void (*ggml_custom1_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *);
  1066. typedef void (*ggml_custom2_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
  1067. typedef void (*ggml_custom3_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
  1068. GGML_API struct ggml_tensor * ggml_map_unary_f32(
  1069. struct ggml_context * ctx,
  1070. struct ggml_tensor * a,
  1071. ggml_unary_op_f32_t fun);
  1072. GGML_API struct ggml_tensor * ggml_map_unary_inplace_f32(
  1073. struct ggml_context * ctx,
  1074. struct ggml_tensor * a,
  1075. ggml_unary_op_f32_t fun);
  1076. GGML_API struct ggml_tensor * ggml_map_binary_f32(
  1077. struct ggml_context * ctx,
  1078. struct ggml_tensor * a,
  1079. struct ggml_tensor * b,
  1080. ggml_binary_op_f32_t fun);
  1081. GGML_API struct ggml_tensor * ggml_map_binary_inplace_f32(
  1082. struct ggml_context * ctx,
  1083. struct ggml_tensor * a,
  1084. struct ggml_tensor * b,
  1085. ggml_binary_op_f32_t fun);
  1086. GGML_API struct ggml_tensor * ggml_map_custom1_f32(
  1087. struct ggml_context * ctx,
  1088. struct ggml_tensor * a,
  1089. ggml_custom1_op_f32_t fun);
  1090. GGML_API struct ggml_tensor * ggml_map_custom1_inplace_f32(
  1091. struct ggml_context * ctx,
  1092. struct ggml_tensor * a,
  1093. ggml_custom1_op_f32_t fun);
  1094. GGML_API struct ggml_tensor * ggml_map_custom2_f32(
  1095. struct ggml_context * ctx,
  1096. struct ggml_tensor * a,
  1097. struct ggml_tensor * b,
  1098. ggml_custom2_op_f32_t fun);
  1099. GGML_API struct ggml_tensor * ggml_map_custom2_inplace_f32(
  1100. struct ggml_context * ctx,
  1101. struct ggml_tensor * a,
  1102. struct ggml_tensor * b,
  1103. ggml_custom2_op_f32_t fun);
  1104. GGML_API struct ggml_tensor * ggml_map_custom3_f32(
  1105. struct ggml_context * ctx,
  1106. struct ggml_tensor * a,
  1107. struct ggml_tensor * b,
  1108. struct ggml_tensor * c,
  1109. ggml_custom3_op_f32_t fun);
  1110. GGML_API struct ggml_tensor * ggml_map_custom3_inplace_f32(
  1111. struct ggml_context * ctx,
  1112. struct ggml_tensor * a,
  1113. struct ggml_tensor * b,
  1114. struct ggml_tensor * c,
  1115. ggml_custom3_op_f32_t fun);
  1116. // loss function
  1117. GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
  1118. struct ggml_context * ctx,
  1119. struct ggml_tensor * a,
  1120. struct ggml_tensor * b);
  1121. GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
  1122. struct ggml_context * ctx,
  1123. struct ggml_tensor * a,
  1124. struct ggml_tensor * b,
  1125. struct ggml_tensor * c);
  1126. //
  1127. // automatic differentiation
  1128. //
  1129. GGML_API void ggml_set_param(
  1130. struct ggml_context * ctx,
  1131. struct ggml_tensor * tensor);
  1132. GGML_API void ggml_build_forward_expand(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
  1133. GGML_API struct ggml_cgraph ggml_build_forward (struct ggml_tensor * tensor);
  1134. GGML_API struct ggml_cgraph ggml_build_backward(struct ggml_context * ctx, struct ggml_cgraph * gf, bool keep);
  1135. // ggml_graph_plan() has to be called before ggml_graph_compute()
  1136. // when plan.work_size > 0, caller must allocate memory for plan.work_data
  1137. GGML_API struct ggml_cplan ggml_graph_plan (struct ggml_cgraph * cgraph, int n_threads /*= GGML_DEFAULT_N_THREADS*/);
  1138. GGML_API int ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan);
  1139. GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph);
  1140. // same as ggml_graph_compute() but the work data is allocated as a part of the context
  1141. // note: the drawback of this API is that you must have ensured that the context has enough memory for the work data
  1142. GGML_API void ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
  1143. GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name);
  1144. GGML_API void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname);
  1145. GGML_API struct ggml_cgraph ggml_graph_import(const char * fname, struct ggml_context ** ctx_data, struct ggml_context ** ctx_eval);
  1146. // print info and performance information for the graph
  1147. GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
  1148. // dump the graph into a file using the dot format
  1149. GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
  1150. //
  1151. // optimization
  1152. //
  1153. // optimization methods
  1154. enum ggml_opt_type {
  1155. GGML_OPT_ADAM,
  1156. GGML_OPT_LBFGS,
  1157. };
  1158. // linesearch methods
  1159. enum ggml_linesearch {
  1160. GGML_LINESEARCH_DEFAULT = 1,
  1161. GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0,
  1162. GGML_LINESEARCH_BACKTRACKING_WOLFE = 1,
  1163. GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2,
  1164. };
  1165. // optimization return values
  1166. enum ggml_opt_result {
  1167. GGML_OPT_OK = 0,
  1168. GGML_OPT_DID_NOT_CONVERGE,
  1169. GGML_OPT_NO_CONTEXT,
  1170. GGML_OPT_INVALID_WOLFE,
  1171. GGML_OPT_FAIL,
  1172. GGML_LINESEARCH_FAIL = -128,
  1173. GGML_LINESEARCH_MINIMUM_STEP,
  1174. GGML_LINESEARCH_MAXIMUM_STEP,
  1175. GGML_LINESEARCH_MAXIMUM_ITERATIONS,
  1176. GGML_LINESEARCH_INVALID_PARAMETERS,
  1177. };
  1178. // optimization parameters
  1179. //
  1180. // see ggml.c (ggml_opt_default_params) for default values
  1181. //
  1182. struct ggml_opt_params {
  1183. enum ggml_opt_type type;
  1184. int n_threads;
  1185. // delta-based convergence test
  1186. //
  1187. // if past == 0 - disabled
  1188. // if past > 0:
  1189. // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|)
  1190. //
  1191. int past;
  1192. float delta;
  1193. // maximum number of iterations without improvement
  1194. //
  1195. // if 0 - disabled
  1196. // if > 0:
  1197. // assume convergence if no cost improvement in this number of iterations
  1198. //
  1199. int max_no_improvement;
  1200. bool print_forward_graph;
  1201. bool print_backward_graph;
  1202. // ADAM parameters
  1203. struct {
  1204. int n_iter;
  1205. float sched; // schedule multiplier (fixed, decay or warmup)
  1206. float decay; // weight decay for AdamW, use 0.0f to disable
  1207. float alpha; // learning rate
  1208. float beta1;
  1209. float beta2;
  1210. float eps; // epsilon for numerical stability
  1211. float eps_f; // epsilon for convergence test
  1212. float eps_g; // epsilon for convergence test
  1213. } adam;
  1214. // LBFGS parameters
  1215. struct {
  1216. int m; // number of corrections to approximate the inv. Hessian
  1217. int n_iter;
  1218. int max_linesearch;
  1219. float eps; // convergence tolerance
  1220. float ftol; // line search tolerance
  1221. float wolfe;
  1222. float min_step;
  1223. float max_step;
  1224. enum ggml_linesearch linesearch;
  1225. } lbfgs;
  1226. };
  1227. struct ggml_opt_context {
  1228. struct ggml_context * ctx;
  1229. struct ggml_opt_params params;
  1230. int iter;
  1231. int64_t nx; // number of parameter elements
  1232. bool just_initialized;
  1233. struct {
  1234. struct ggml_tensor * x; // view of the parameters
  1235. struct ggml_tensor * g1; // gradient
  1236. struct ggml_tensor * g2; // gradient squared
  1237. struct ggml_tensor * m; // first moment
  1238. struct ggml_tensor * v; // second moment
  1239. struct ggml_tensor * mh; // first moment hat
  1240. struct ggml_tensor * vh; // second moment hat
  1241. struct ggml_tensor * pf; // past function values
  1242. float fx_best;
  1243. float fx_prev;
  1244. int n_no_improvement;
  1245. } adam;
  1246. struct {
  1247. struct ggml_tensor * x; // current parameters
  1248. struct ggml_tensor * xp; // previous parameters
  1249. struct ggml_tensor * g; // current gradient
  1250. struct ggml_tensor * gp; // previous gradient
  1251. struct ggml_tensor * d; // search direction
  1252. struct ggml_tensor * pf; // past function values
  1253. struct ggml_tensor * lmal; // the L-BFGS memory alpha
  1254. struct ggml_tensor * lmys; // the L-BFGS memory ys
  1255. struct ggml_tensor * lms; // the L-BFGS memory s
  1256. struct ggml_tensor * lmy; // the L-BFGS memory y
  1257. float fx_best;
  1258. float step;
  1259. int j;
  1260. int k;
  1261. int end;
  1262. int n_no_improvement;
  1263. } lbfgs;
  1264. };
  1265. GGML_API struct ggml_opt_params ggml_opt_default_params(enum ggml_opt_type type);
  1266. // optimize the function defined by the tensor f
  1267. GGML_API enum ggml_opt_result ggml_opt(
  1268. struct ggml_context * ctx,
  1269. struct ggml_opt_params params,
  1270. struct ggml_tensor * f);
  1271. // initialize optimizer context
  1272. GGML_API void ggml_opt_init(
  1273. struct ggml_context * ctx,
  1274. struct ggml_opt_context * opt,
  1275. struct ggml_opt_params params,
  1276. int64_t nx);
  1277. // continue optimizing the function defined by the tensor f
  1278. GGML_API enum ggml_opt_result ggml_opt_resume(
  1279. struct ggml_context * ctx,
  1280. struct ggml_opt_context * opt,
  1281. struct ggml_tensor * f);
  1282. // continue optimizing the function defined by the tensor f
  1283. GGML_API enum ggml_opt_result ggml_opt_resume_g(
  1284. struct ggml_context * ctx,
  1285. struct ggml_opt_context * opt,
  1286. struct ggml_tensor * f,
  1287. struct ggml_cgraph * gf,
  1288. struct ggml_cgraph * gb);
  1289. //
  1290. // quantization
  1291. //
  1292. GGML_API size_t ggml_quantize_q4_0(const float * src, void * dst, int n, int k, int64_t * hist);
  1293. GGML_API size_t ggml_quantize_q4_1(const float * src, void * dst, int n, int k, int64_t * hist);
  1294. GGML_API size_t ggml_quantize_q5_0(const float * src, void * dst, int n, int k, int64_t * hist);
  1295. GGML_API size_t ggml_quantize_q5_1(const float * src, void * dst, int n, int k, int64_t * hist);
  1296. GGML_API size_t ggml_quantize_q8_0(const float * src, void * dst, int n, int k, int64_t * hist);
  1297. GGML_API size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, int start, int n, int64_t * hist);
  1298. //
  1299. // system info
  1300. //
  1301. GGML_API int ggml_cpu_has_avx (void);
  1302. GGML_API int ggml_cpu_has_avx2 (void);
  1303. GGML_API int ggml_cpu_has_avx512 (void);
  1304. GGML_API int ggml_cpu_has_avx512_vbmi(void);
  1305. GGML_API int ggml_cpu_has_avx512_vnni(void);
  1306. GGML_API int ggml_cpu_has_fma (void);
  1307. GGML_API int ggml_cpu_has_neon (void);
  1308. GGML_API int ggml_cpu_has_arm_fma (void);
  1309. GGML_API int ggml_cpu_has_f16c (void);
  1310. GGML_API int ggml_cpu_has_fp16_va (void);
  1311. GGML_API int ggml_cpu_has_wasm_simd (void);
  1312. GGML_API int ggml_cpu_has_blas (void);
  1313. GGML_API int ggml_cpu_has_cublas (void);
  1314. GGML_API int ggml_cpu_has_clblast (void);
  1315. GGML_API int ggml_cpu_has_gpublas (void);
  1316. GGML_API int ggml_cpu_has_sse3 (void);
  1317. GGML_API int ggml_cpu_has_vsx (void);
  1318. //
  1319. // Internal types and functions exposed for tests and benchmarks
  1320. //
  1321. #ifdef __cplusplus
  1322. // restrict not standard in C++
  1323. #define GGML_RESTRICT
  1324. #else
  1325. #define GGML_RESTRICT restrict
  1326. #endif
  1327. typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int k);
  1328. typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int k);
  1329. typedef void (*ggml_vec_dot_t) (const int n, float * GGML_RESTRICT s, const void * GGML_RESTRICT x, const void * GGML_RESTRICT y);
  1330. typedef struct {
  1331. ggml_to_float_t to_float;
  1332. ggml_from_float_t from_float;
  1333. ggml_from_float_t from_float_reference;
  1334. ggml_vec_dot_t vec_dot;
  1335. enum ggml_type vec_dot_type;
  1336. } ggml_type_traits_t;
  1337. ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type i);
  1338. #ifdef __cplusplus
  1339. }
  1340. #endif