ggml.h 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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. // TODO: support for clang
  184. #ifdef __GNUC__
  185. # define GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
  186. #elif defined(_MSC_VER)
  187. # define GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
  188. #else
  189. # define GGML_DEPRECATED(func, hint) func
  190. #endif
  191. #include <stdint.h>
  192. #include <stddef.h>
  193. #include <stdbool.h>
  194. #define GGML_FILE_MAGIC 0x67676d6c // "ggml"
  195. #define GGML_FILE_VERSION 1
  196. #define GGML_QNT_VERSION 2 // bump this on quantization format changes
  197. #define GGML_QNT_VERSION_FACTOR 1000 // do not change this
  198. #define GGML_MAX_DIMS 4
  199. #define GGML_MAX_NODES 4096
  200. #define GGML_MAX_PARAMS 256
  201. #define GGML_MAX_CONTEXTS 64
  202. #define GGML_MAX_SRC 6
  203. #define GGML_MAX_NAME 64
  204. #define GGML_MAX_OP_PARAMS 32
  205. #define GGML_DEFAULT_N_THREADS 4
  206. #define GGML_EXIT_SUCCESS 0
  207. #define GGML_EXIT_ABORTED 1
  208. #define GGUF_MAGIC 0x46554747 // "GGUF"
  209. #define GGUF_VERSION 1
  210. #define GGUF_DEFAULT_ALIGNMENT 32
  211. #define GGML_UNUSED(x) (void)(x)
  212. #define GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
  213. #define GGML_ASSERT(x) \
  214. do { \
  215. if (!(x)) { \
  216. fprintf(stderr, "GGML_ASSERT: %s:%d: %s\n", __FILE__, __LINE__, #x); \
  217. abort(); \
  218. } \
  219. } while (0)
  220. // used to copy the number of elements and stride in bytes of tensors into local variables.
  221. // main purpose is to reduce code duplication and improve readability.
  222. //
  223. // example:
  224. //
  225. // GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  226. // GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
  227. //
  228. #define GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
  229. const type prefix##0 = (pointer)->array[0]; \
  230. GGML_UNUSED(prefix##0);
  231. #define GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
  232. GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
  233. const type prefix##1 = (pointer)->array[1]; \
  234. GGML_UNUSED(prefix##1);
  235. #define GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
  236. GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
  237. const type prefix##2 = (pointer)->array[2]; \
  238. GGML_UNUSED(prefix##2);
  239. #define GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
  240. GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
  241. const type prefix##3 = (pointer)->array[3]; \
  242. GGML_UNUSED(prefix##3);
  243. #ifdef __cplusplus
  244. extern "C" {
  245. #endif
  246. #if defined(__ARM_NEON) && defined(__CUDACC__)
  247. typedef half ggml_fp16_t;
  248. #elif defined(__ARM_NEON)
  249. typedef __fp16 ggml_fp16_t;
  250. #else
  251. typedef uint16_t ggml_fp16_t;
  252. #endif
  253. // convert FP16 <-> FP32
  254. GGML_API float ggml_fp16_to_fp32(ggml_fp16_t x);
  255. GGML_API ggml_fp16_t ggml_fp32_to_fp16(float x);
  256. GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t * x, float * y, int n);
  257. GGML_API void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, int n);
  258. struct ggml_object;
  259. struct ggml_context;
  260. enum ggml_type {
  261. GGML_TYPE_F32 = 0,
  262. GGML_TYPE_F16 = 1,
  263. GGML_TYPE_Q4_0 = 2,
  264. GGML_TYPE_Q4_1 = 3,
  265. // GGML_TYPE_Q4_2 = 4, support has been removed
  266. // GGML_TYPE_Q4_3 (5) support has been removed
  267. GGML_TYPE_Q5_0 = 6,
  268. GGML_TYPE_Q5_1 = 7,
  269. GGML_TYPE_Q8_0 = 8,
  270. GGML_TYPE_Q8_1 = 9,
  271. // k-quantizations
  272. GGML_TYPE_Q2_K = 10,
  273. GGML_TYPE_Q3_K = 11,
  274. GGML_TYPE_Q4_K = 12,
  275. GGML_TYPE_Q5_K = 13,
  276. GGML_TYPE_Q6_K = 14,
  277. GGML_TYPE_Q8_K = 15,
  278. GGML_TYPE_I8,
  279. GGML_TYPE_I16,
  280. GGML_TYPE_I32,
  281. GGML_TYPE_COUNT,
  282. };
  283. enum ggml_backend {
  284. GGML_BACKEND_CPU = 0,
  285. GGML_BACKEND_GPU = 10,
  286. GGML_BACKEND_GPU_SPLIT = 20,
  287. };
  288. // model file types
  289. enum ggml_ftype {
  290. GGML_FTYPE_UNKNOWN = -1,
  291. GGML_FTYPE_ALL_F32 = 0,
  292. GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
  293. GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
  294. GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
  295. GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
  296. GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
  297. GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
  298. GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
  299. GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
  300. GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
  301. GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
  302. GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
  303. GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
  304. };
  305. // available tensor operations:
  306. enum ggml_op {
  307. GGML_OP_NONE = 0,
  308. GGML_OP_DUP,
  309. GGML_OP_ADD,
  310. GGML_OP_ADD1,
  311. GGML_OP_ACC,
  312. GGML_OP_SUB,
  313. GGML_OP_MUL,
  314. GGML_OP_DIV,
  315. GGML_OP_SQR,
  316. GGML_OP_SQRT,
  317. GGML_OP_LOG,
  318. GGML_OP_SUM,
  319. GGML_OP_SUM_ROWS,
  320. GGML_OP_MEAN,
  321. GGML_OP_ARGMAX,
  322. GGML_OP_REPEAT,
  323. GGML_OP_REPEAT_BACK,
  324. GGML_OP_SILU_BACK,
  325. GGML_OP_NORM, // normalize
  326. GGML_OP_RMS_NORM,
  327. GGML_OP_RMS_NORM_BACK,
  328. GGML_OP_MUL_MAT,
  329. GGML_OP_OUT_PROD,
  330. GGML_OP_SCALE,
  331. GGML_OP_SET,
  332. GGML_OP_CPY,
  333. GGML_OP_CONT,
  334. GGML_OP_RESHAPE,
  335. GGML_OP_VIEW,
  336. GGML_OP_PERMUTE,
  337. GGML_OP_TRANSPOSE,
  338. GGML_OP_GET_ROWS,
  339. GGML_OP_GET_ROWS_BACK,
  340. GGML_OP_DIAG,
  341. GGML_OP_DIAG_MASK_INF,
  342. GGML_OP_DIAG_MASK_ZERO,
  343. GGML_OP_SOFT_MAX,
  344. GGML_OP_SOFT_MAX_BACK,
  345. GGML_OP_ROPE,
  346. GGML_OP_ROPE_BACK,
  347. GGML_OP_ALIBI,
  348. GGML_OP_CLAMP,
  349. GGML_OP_CONV_1D,
  350. GGML_OP_CONV_2D,
  351. GGML_OP_POOL_1D,
  352. GGML_OP_POOL_2D,
  353. GGML_OP_FLASH_ATTN,
  354. GGML_OP_FLASH_FF,
  355. GGML_OP_FLASH_ATTN_BACK,
  356. GGML_OP_WIN_PART,
  357. GGML_OP_WIN_UNPART,
  358. GGML_OP_UNARY,
  359. GGML_OP_MAP_UNARY,
  360. GGML_OP_MAP_BINARY,
  361. GGML_OP_MAP_CUSTOM1_F32,
  362. GGML_OP_MAP_CUSTOM2_F32,
  363. GGML_OP_MAP_CUSTOM3_F32,
  364. GGML_OP_MAP_CUSTOM1,
  365. GGML_OP_MAP_CUSTOM2,
  366. GGML_OP_MAP_CUSTOM3,
  367. GGML_OP_CROSS_ENTROPY_LOSS,
  368. GGML_OP_CROSS_ENTROPY_LOSS_BACK,
  369. GGML_OP_COUNT,
  370. };
  371. enum ggml_unary_op {
  372. GGML_UNARY_OP_ABS,
  373. GGML_UNARY_OP_SGN,
  374. GGML_UNARY_OP_NEG,
  375. GGML_UNARY_OP_STEP,
  376. GGML_UNARY_OP_TANH,
  377. GGML_UNARY_OP_ELU,
  378. GGML_UNARY_OP_RELU,
  379. GGML_UNARY_OP_GELU,
  380. GGML_UNARY_OP_GELU_QUICK,
  381. GGML_UNARY_OP_SILU,
  382. };
  383. enum ggml_object_type {
  384. GGML_OBJECT_TENSOR,
  385. GGML_OBJECT_GRAPH,
  386. GGML_OBJECT_WORK_BUFFER
  387. };
  388. // ggml object
  389. struct ggml_object {
  390. size_t offs;
  391. size_t size;
  392. struct ggml_object * next;
  393. enum ggml_object_type type;
  394. char padding[4];
  395. };
  396. static const size_t GGML_OBJECT_SIZE = sizeof(struct ggml_object);
  397. // n-dimensional tensor
  398. struct ggml_tensor {
  399. enum ggml_type type;
  400. enum ggml_backend backend;
  401. int n_dims;
  402. int64_t ne[GGML_MAX_DIMS]; // number of elements
  403. size_t nb[GGML_MAX_DIMS]; // stride in bytes:
  404. // nb[0] = sizeof(type)
  405. // nb[1] = nb[0] * ne[0] + padding
  406. // nb[i] = nb[i-1] * ne[i-1]
  407. // compute data
  408. enum ggml_op op;
  409. // op params - allocated as int32_t for alignment
  410. int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
  411. bool is_param;
  412. struct ggml_tensor * grad;
  413. struct ggml_tensor * src[GGML_MAX_SRC];
  414. // performance
  415. int perf_runs;
  416. int64_t perf_cycles;
  417. int64_t perf_time_us;
  418. void * data;
  419. char name[GGML_MAX_NAME];
  420. void * extra; // extra things e.g. for ggml-cuda.cu
  421. char padding[4];
  422. };
  423. static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
  424. // the compute plan that needs to be prepared for ggml_graph_compute()
  425. // since https://github.com/ggerganov/ggml/issues/287
  426. struct ggml_cplan {
  427. size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
  428. uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`
  429. int n_threads;
  430. // the `n_tasks` of nodes, 1:1 mapping to cgraph nodes
  431. int n_tasks[GGML_MAX_NODES];
  432. // abort ggml_graph_compute when true
  433. bool (*abort_callback)(void * data);
  434. void * abort_callback_data;
  435. };
  436. // next prime after GGML_MAX_NODES
  437. // #define GGML_GRAPH_HASHTABLE_SIZE 4099
  438. // next prime after GGML_MAX_NODES * 2 (nodes + leafs)
  439. #define GGML_GRAPH_HASHTABLE_SIZE 8273
  440. // computation graph
  441. struct ggml_cgraph {
  442. int n_nodes;
  443. int n_leafs;
  444. struct ggml_tensor * nodes[GGML_MAX_NODES];
  445. struct ggml_tensor * grads[GGML_MAX_NODES];
  446. struct ggml_tensor * leafs[GGML_MAX_NODES];
  447. void * visited_hash_table[GGML_GRAPH_HASHTABLE_SIZE];
  448. // performance
  449. int perf_runs;
  450. int64_t perf_cycles;
  451. int64_t perf_time_us;
  452. };
  453. static const size_t GGML_GRAPH_SIZE = sizeof(struct ggml_cgraph);
  454. // scratch buffer
  455. struct ggml_scratch {
  456. size_t offs;
  457. size_t size;
  458. void * data;
  459. };
  460. struct ggml_init_params {
  461. // memory pool
  462. size_t mem_size; // bytes
  463. void * mem_buffer; // if NULL, memory will be allocated internally
  464. bool no_alloc; // don't allocate memory for the tensor data
  465. };
  466. // compute types
  467. // NOTE: the INIT or FINALIZE pass is not scheduled unless explicitly enabled.
  468. // This behavior was changed since https://github.com/ggerganov/llama.cpp/pull/1995.
  469. enum ggml_task_type {
  470. GGML_TASK_INIT = 0,
  471. GGML_TASK_COMPUTE,
  472. GGML_TASK_FINALIZE,
  473. };
  474. struct ggml_compute_params {
  475. enum ggml_task_type type;
  476. // ith = thread index, nth = number of threads
  477. int ith, nth;
  478. // work buffer for all threads
  479. size_t wsize;
  480. void * wdata;
  481. };
  482. // misc
  483. GGML_API void ggml_time_init(void); // call this once at the beginning of the program
  484. GGML_API int64_t ggml_time_ms(void);
  485. GGML_API int64_t ggml_time_us(void);
  486. GGML_API int64_t ggml_cycles(void);
  487. GGML_API int64_t ggml_cycles_per_ms(void);
  488. GGML_API void ggml_numa_init(void); // call once for better performance on NUMA systems
  489. GGML_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
  490. GGML_API void ggml_print_object (const struct ggml_object * obj);
  491. GGML_API void ggml_print_objects(const struct ggml_context * ctx);
  492. GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor);
  493. GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor);
  494. GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor);
  495. GGML_API size_t ggml_nbytes_pad (const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN
  496. GGML_API size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split);
  497. GGML_API int ggml_blck_size (enum ggml_type type);
  498. GGML_API size_t ggml_type_size (enum ggml_type type); // size in bytes for all elements in a block
  499. GGML_API float ggml_type_sizef(enum ggml_type type); // ggml_type_size()/ggml_blck_size() as float
  500. GGML_API const char * ggml_type_name(enum ggml_type type);
  501. GGML_API const char * ggml_op_name (enum ggml_op op);
  502. GGML_API const char * ggml_op_symbol(enum ggml_op op);
  503. GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
  504. GGML_API bool ggml_is_quantized(enum ggml_type type);
  505. // TODO: temporary until model loading of ggml examples is refactored
  506. GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
  507. GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor);
  508. GGML_API bool ggml_is_contiguous(const struct ggml_tensor * tensor);
  509. GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor);
  510. GGML_API bool ggml_are_same_shape(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
  511. // use this to compute the memory overhead of a tensor
  512. GGML_API size_t ggml_tensor_overhead(void);
  513. // main
  514. GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
  515. GGML_API void ggml_free(struct ggml_context * ctx);
  516. GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
  517. GGML_API size_t ggml_set_scratch (struct ggml_context * ctx, struct ggml_scratch scratch);
  518. GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
  519. GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
  520. GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx);
  521. GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx);
  522. GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
  523. GGML_API struct ggml_tensor * ggml_new_tensor(
  524. struct ggml_context * ctx,
  525. enum ggml_type type,
  526. int n_dims,
  527. const int64_t *ne);
  528. GGML_API struct ggml_tensor * ggml_new_tensor_1d(
  529. struct ggml_context * ctx,
  530. enum ggml_type type,
  531. int64_t ne0);
  532. GGML_API struct ggml_tensor * ggml_new_tensor_2d(
  533. struct ggml_context * ctx,
  534. enum ggml_type type,
  535. int64_t ne0,
  536. int64_t ne1);
  537. GGML_API struct ggml_tensor * ggml_new_tensor_3d(
  538. struct ggml_context * ctx,
  539. enum ggml_type type,
  540. int64_t ne0,
  541. int64_t ne1,
  542. int64_t ne2);
  543. GGML_API struct ggml_tensor * ggml_new_tensor_4d(
  544. struct ggml_context * ctx,
  545. enum ggml_type type,
  546. int64_t ne0,
  547. int64_t ne1,
  548. int64_t ne2,
  549. int64_t ne3);
  550. GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
  551. GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
  552. GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
  553. GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, const struct ggml_tensor * src);
  554. GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
  555. GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
  556. GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
  557. GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
  558. GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
  559. GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
  560. GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
  561. GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
  562. GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
  563. GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
  564. GGML_API enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor);
  565. GGML_API const char * ggml_get_name (const struct ggml_tensor * tensor);
  566. GGML_API struct ggml_tensor * ggml_set_name ( struct ggml_tensor * tensor, const char * name);
  567. GGML_API struct ggml_tensor * ggml_format_name( struct ggml_tensor * tensor, const char * fmt, ...);
  568. //
  569. // operations on tensors with backpropagation
  570. //
  571. GGML_API struct ggml_tensor * ggml_dup(
  572. struct ggml_context * ctx,
  573. struct ggml_tensor * a);
  574. // in-place, returns view(a)
  575. GGML_API struct ggml_tensor * ggml_dup_inplace(
  576. struct ggml_context * ctx,
  577. struct ggml_tensor * a);
  578. GGML_API struct ggml_tensor * ggml_add(
  579. struct ggml_context * ctx,
  580. struct ggml_tensor * a,
  581. struct ggml_tensor * b);
  582. GGML_API struct ggml_tensor * ggml_add_inplace(
  583. struct ggml_context * ctx,
  584. struct ggml_tensor * a,
  585. struct ggml_tensor * b);
  586. GGML_API struct ggml_tensor * ggml_add1(
  587. struct ggml_context * ctx,
  588. struct ggml_tensor * a,
  589. struct ggml_tensor * b);
  590. GGML_API struct ggml_tensor * ggml_add1_inplace(
  591. struct ggml_context * ctx,
  592. struct ggml_tensor * a,
  593. struct ggml_tensor * b);
  594. GGML_API struct ggml_tensor * ggml_acc(
  595. struct ggml_context * ctx,
  596. struct ggml_tensor * a,
  597. struct ggml_tensor * b,
  598. size_t nb1,
  599. size_t nb2,
  600. size_t nb3,
  601. size_t offset);
  602. GGML_API struct ggml_tensor * ggml_acc_inplace(
  603. struct ggml_context * ctx,
  604. struct ggml_tensor * a,
  605. struct ggml_tensor * b,
  606. size_t nb1,
  607. size_t nb2,
  608. size_t nb3,
  609. size_t offset);
  610. GGML_API struct ggml_tensor * ggml_sub(
  611. struct ggml_context * ctx,
  612. struct ggml_tensor * a,
  613. struct ggml_tensor * b);
  614. GGML_API struct ggml_tensor * ggml_sub_inplace(
  615. struct ggml_context * ctx,
  616. struct ggml_tensor * a,
  617. struct ggml_tensor * b);
  618. GGML_API struct ggml_tensor * ggml_mul(
  619. struct ggml_context * ctx,
  620. struct ggml_tensor * a,
  621. struct ggml_tensor * b);
  622. GGML_API struct ggml_tensor * ggml_mul_inplace(
  623. struct ggml_context * ctx,
  624. struct ggml_tensor * a,
  625. struct ggml_tensor * b);
  626. GGML_API struct ggml_tensor * ggml_div(
  627. struct ggml_context * ctx,
  628. struct ggml_tensor * a,
  629. struct ggml_tensor * b);
  630. GGML_API struct ggml_tensor * ggml_div_inplace(
  631. struct ggml_context * ctx,
  632. struct ggml_tensor * a,
  633. struct ggml_tensor * b);
  634. GGML_API struct ggml_tensor * ggml_sqr(
  635. struct ggml_context * ctx,
  636. struct ggml_tensor * a);
  637. GGML_API struct ggml_tensor * ggml_sqr_inplace(
  638. struct ggml_context * ctx,
  639. struct ggml_tensor * a);
  640. GGML_API struct ggml_tensor * ggml_sqrt(
  641. struct ggml_context * ctx,
  642. struct ggml_tensor * a);
  643. GGML_API struct ggml_tensor * ggml_sqrt_inplace(
  644. struct ggml_context * ctx,
  645. struct ggml_tensor * a);
  646. GGML_API struct ggml_tensor * ggml_log(
  647. struct ggml_context * ctx,
  648. struct ggml_tensor * a);
  649. GGML_API struct ggml_tensor * ggml_log_inplace(
  650. struct ggml_context * ctx,
  651. struct ggml_tensor * a);
  652. // return scalar
  653. GGML_API struct ggml_tensor * ggml_sum(
  654. struct ggml_context * ctx,
  655. struct ggml_tensor * a);
  656. // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
  657. GGML_API struct ggml_tensor * ggml_sum_rows(
  658. struct ggml_context * ctx,
  659. struct ggml_tensor * a);
  660. // mean along rows
  661. GGML_API struct ggml_tensor * ggml_mean(
  662. struct ggml_context * ctx,
  663. struct ggml_tensor * a);
  664. // argmax along rows
  665. GGML_API struct ggml_tensor * ggml_argmax(
  666. struct ggml_context * ctx,
  667. struct ggml_tensor * a);
  668. // if a is the same shape as b, and a is not parameter, return a
  669. // otherwise, return a new tensor: repeat(a) to fit in b
  670. GGML_API struct ggml_tensor * ggml_repeat(
  671. struct ggml_context * ctx,
  672. struct ggml_tensor * a,
  673. struct ggml_tensor * b);
  674. GGML_API struct ggml_tensor * ggml_repeat_back(
  675. struct ggml_context * ctx,
  676. struct ggml_tensor * a,
  677. struct ggml_tensor * b);
  678. GGML_API struct ggml_tensor * ggml_abs(
  679. struct ggml_context * ctx,
  680. struct ggml_tensor * a);
  681. GGML_API struct ggml_tensor * ggml_abs_inplace(
  682. struct ggml_context * ctx,
  683. struct ggml_tensor * a);
  684. GGML_API struct ggml_tensor * ggml_sgn(
  685. struct ggml_context * ctx,
  686. struct ggml_tensor * a);
  687. GGML_API struct ggml_tensor * ggml_sgn_inplace(
  688. struct ggml_context * ctx,
  689. struct ggml_tensor * a);
  690. GGML_API struct ggml_tensor * ggml_neg(
  691. struct ggml_context * ctx,
  692. struct ggml_tensor * a);
  693. GGML_API struct ggml_tensor * ggml_neg_inplace(
  694. struct ggml_context * ctx,
  695. struct ggml_tensor * a);
  696. GGML_API struct ggml_tensor * ggml_step(
  697. struct ggml_context * ctx,
  698. struct ggml_tensor * a);
  699. GGML_API struct ggml_tensor * ggml_step_inplace(
  700. struct ggml_context * ctx,
  701. struct ggml_tensor * a);
  702. GGML_API struct ggml_tensor * ggml_tanh(
  703. struct ggml_context * ctx,
  704. struct ggml_tensor * a);
  705. GGML_API struct ggml_tensor * ggml_tanh_inplace(
  706. struct ggml_context * ctx,
  707. struct ggml_tensor * a);
  708. GGML_API struct ggml_tensor * ggml_elu(
  709. struct ggml_context * ctx,
  710. struct ggml_tensor * a);
  711. GGML_API struct ggml_tensor * ggml_elu_inplace(
  712. struct ggml_context * ctx,
  713. struct ggml_tensor * a);
  714. GGML_API struct ggml_tensor * ggml_relu(
  715. struct ggml_context * ctx,
  716. struct ggml_tensor * a);
  717. GGML_API struct ggml_tensor * ggml_relu_inplace(
  718. struct ggml_context * ctx,
  719. struct ggml_tensor * a);
  720. // TODO: double-check this computation is correct
  721. GGML_API struct ggml_tensor * ggml_gelu(
  722. struct ggml_context * ctx,
  723. struct ggml_tensor * a);
  724. GGML_API struct ggml_tensor * ggml_gelu_inplace(
  725. struct ggml_context * ctx,
  726. struct ggml_tensor * a);
  727. GGML_API struct ggml_tensor * ggml_gelu_quick(
  728. struct ggml_context * ctx,
  729. struct ggml_tensor * a);
  730. GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
  731. struct ggml_context * ctx,
  732. struct ggml_tensor * a);
  733. GGML_API struct ggml_tensor * ggml_silu(
  734. struct ggml_context * ctx,
  735. struct ggml_tensor * a);
  736. GGML_API struct ggml_tensor * ggml_silu_inplace(
  737. struct ggml_context * ctx,
  738. struct ggml_tensor * a);
  739. // a - x
  740. // b - dy
  741. GGML_API struct ggml_tensor * ggml_silu_back(
  742. struct ggml_context * ctx,
  743. struct ggml_tensor * a,
  744. struct ggml_tensor * b);
  745. // normalize along rows
  746. // TODO: eps is hardcoded to 1e-5 for now
  747. GGML_API struct ggml_tensor * ggml_norm(
  748. struct ggml_context * ctx,
  749. struct ggml_tensor * a);
  750. GGML_API struct ggml_tensor * ggml_norm_inplace(
  751. struct ggml_context * ctx,
  752. struct ggml_tensor * a);
  753. GGML_API struct ggml_tensor * ggml_rms_norm(
  754. struct ggml_context * ctx,
  755. struct ggml_tensor * a,
  756. float eps);
  757. GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
  758. struct ggml_context * ctx,
  759. struct ggml_tensor * a,
  760. float eps);
  761. // a - x
  762. // b - dy
  763. // TODO: update with configurable eps
  764. GGML_API struct ggml_tensor * ggml_rms_norm_back(
  765. struct ggml_context * ctx,
  766. struct ggml_tensor * a,
  767. struct ggml_tensor * b);
  768. // A: n columns, m rows
  769. // B: n columns, p rows (i.e. we transpose it internally)
  770. // result is m columns, p rows
  771. GGML_API struct ggml_tensor * ggml_mul_mat(
  772. struct ggml_context * ctx,
  773. struct ggml_tensor * a,
  774. struct ggml_tensor * b);
  775. // A: m columns, n rows,
  776. // B: p columns, n rows,
  777. // result is m columns, p rows
  778. GGML_API struct ggml_tensor * ggml_out_prod(
  779. struct ggml_context * ctx,
  780. struct ggml_tensor * a,
  781. struct ggml_tensor * b);
  782. //
  783. // operations on tensors without backpropagation
  784. //
  785. GGML_API struct ggml_tensor * ggml_scale(
  786. struct ggml_context * ctx,
  787. struct ggml_tensor * a,
  788. struct ggml_tensor * b);
  789. // in-place, returns view(a)
  790. GGML_API struct ggml_tensor * ggml_scale_inplace(
  791. struct ggml_context * ctx,
  792. struct ggml_tensor * a,
  793. struct ggml_tensor * b);
  794. // b -> view(a,offset,nb1,nb2,3), return modified a
  795. GGML_API struct ggml_tensor * ggml_set(
  796. struct ggml_context * ctx,
  797. struct ggml_tensor * a,
  798. struct ggml_tensor * b,
  799. size_t nb1,
  800. size_t nb2,
  801. size_t nb3,
  802. size_t offset);
  803. // b -> view(a,offset,nb1,nb2,3), return view(a)
  804. GGML_API struct ggml_tensor * ggml_set_inplace(
  805. struct ggml_context * ctx,
  806. struct ggml_tensor * a,
  807. struct ggml_tensor * b,
  808. size_t nb1,
  809. size_t nb2,
  810. size_t nb3,
  811. size_t offset);
  812. GGML_API struct ggml_tensor * ggml_set_1d(
  813. struct ggml_context * ctx,
  814. struct ggml_tensor * a,
  815. struct ggml_tensor * b,
  816. size_t offset);
  817. GGML_API struct ggml_tensor * ggml_set_1d_inplace(
  818. struct ggml_context * ctx,
  819. struct ggml_tensor * a,
  820. struct ggml_tensor * b,
  821. size_t offset);
  822. // b -> view(a,offset,nb1,nb2,3), return modified a
  823. GGML_API struct ggml_tensor * ggml_set_2d(
  824. struct ggml_context * ctx,
  825. struct ggml_tensor * a,
  826. struct ggml_tensor * b,
  827. size_t nb1,
  828. size_t offset);
  829. // b -> view(a,offset,nb1,nb2,3), return view(a)
  830. GGML_API struct ggml_tensor * ggml_set_2d_inplace(
  831. struct ggml_context * ctx,
  832. struct ggml_tensor * a,
  833. struct ggml_tensor * b,
  834. size_t nb1,
  835. size_t offset);
  836. // a -> b, return view(b)
  837. GGML_API struct ggml_tensor * ggml_cpy(
  838. struct ggml_context * ctx,
  839. struct ggml_tensor * a,
  840. struct ggml_tensor * b);
  841. // a -> b, in-place, return view(b)
  842. GGML_API struct ggml_tensor * ggml_cpy_inplace(
  843. struct ggml_context * ctx,
  844. struct ggml_tensor * a,
  845. struct ggml_tensor * b);
  846. // make contiguous
  847. GGML_API struct ggml_tensor * ggml_cont(
  848. struct ggml_context * ctx,
  849. struct ggml_tensor * a);
  850. // make contiguous, in-place
  851. GGML_API struct ggml_tensor * ggml_cont_inplace(
  852. struct ggml_context * ctx,
  853. struct ggml_tensor * a);
  854. // return view(a), b specifies the new shape
  855. // TODO: when we start computing gradient, make a copy instead of view
  856. GGML_API struct ggml_tensor * ggml_reshape(
  857. struct ggml_context * ctx,
  858. struct ggml_tensor * a,
  859. struct ggml_tensor * b);
  860. // return view(a)
  861. // TODO: when we start computing gradient, make a copy instead of view
  862. GGML_API struct ggml_tensor * ggml_reshape_1d(
  863. struct ggml_context * ctx,
  864. struct ggml_tensor * a,
  865. int64_t ne0);
  866. GGML_API struct ggml_tensor * ggml_reshape_2d(
  867. struct ggml_context * ctx,
  868. struct ggml_tensor * a,
  869. int64_t ne0,
  870. int64_t ne1);
  871. // return view(a)
  872. // TODO: when we start computing gradient, make a copy instead of view
  873. GGML_API struct ggml_tensor * ggml_reshape_3d(
  874. struct ggml_context * ctx,
  875. struct ggml_tensor * a,
  876. int64_t ne0,
  877. int64_t ne1,
  878. int64_t ne2);
  879. GGML_API struct ggml_tensor * ggml_reshape_4d(
  880. struct ggml_context * ctx,
  881. struct ggml_tensor * a,
  882. int64_t ne0,
  883. int64_t ne1,
  884. int64_t ne2,
  885. int64_t ne3);
  886. // offset in bytes
  887. GGML_API struct ggml_tensor * ggml_view_1d(
  888. struct ggml_context * ctx,
  889. struct ggml_tensor * a,
  890. int64_t ne0,
  891. size_t offset);
  892. GGML_API struct ggml_tensor * ggml_view_2d(
  893. struct ggml_context * ctx,
  894. struct ggml_tensor * a,
  895. int64_t ne0,
  896. int64_t ne1,
  897. size_t nb1, // row stride in bytes
  898. size_t offset);
  899. GGML_API struct ggml_tensor * ggml_view_3d(
  900. struct ggml_context * ctx,
  901. struct ggml_tensor * a,
  902. int64_t ne0,
  903. int64_t ne1,
  904. int64_t ne2,
  905. size_t nb1, // row stride in bytes
  906. size_t nb2, // slice stride in bytes
  907. size_t offset);
  908. GGML_API struct ggml_tensor * ggml_view_4d(
  909. struct ggml_context * ctx,
  910. struct ggml_tensor * a,
  911. int64_t ne0,
  912. int64_t ne1,
  913. int64_t ne2,
  914. int64_t ne3,
  915. size_t nb1, // row stride in bytes
  916. size_t nb2, // slice stride in bytes
  917. size_t nb3,
  918. size_t offset);
  919. GGML_API struct ggml_tensor * ggml_permute(
  920. struct ggml_context * ctx,
  921. struct ggml_tensor * a,
  922. int axis0,
  923. int axis1,
  924. int axis2,
  925. int axis3);
  926. // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
  927. GGML_API struct ggml_tensor * ggml_transpose(
  928. struct ggml_context * ctx,
  929. struct ggml_tensor * a);
  930. GGML_API struct ggml_tensor * ggml_get_rows(
  931. struct ggml_context * ctx,
  932. struct ggml_tensor * a,
  933. struct ggml_tensor * b);
  934. GGML_API struct ggml_tensor * ggml_get_rows_back(
  935. struct ggml_context * ctx,
  936. struct ggml_tensor * a,
  937. struct ggml_tensor * b,
  938. struct ggml_tensor * c);
  939. GGML_API struct ggml_tensor * ggml_diag(
  940. struct ggml_context * ctx,
  941. struct ggml_tensor * a);
  942. // set elements above the diagonal to -INF
  943. GGML_API struct ggml_tensor * ggml_diag_mask_inf(
  944. struct ggml_context * ctx,
  945. struct ggml_tensor * a,
  946. int n_past);
  947. // in-place, returns view(a)
  948. GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
  949. struct ggml_context * ctx,
  950. struct ggml_tensor * a,
  951. int n_past);
  952. // set elements above the diagonal to 0
  953. GGML_API struct ggml_tensor * ggml_diag_mask_zero(
  954. struct ggml_context * ctx,
  955. struct ggml_tensor * a,
  956. int n_past);
  957. // in-place, returns view(a)
  958. GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
  959. struct ggml_context * ctx,
  960. struct ggml_tensor * a,
  961. int n_past);
  962. GGML_API struct ggml_tensor * ggml_soft_max(
  963. struct ggml_context * ctx,
  964. struct ggml_tensor * a);
  965. // in-place, returns view(a)
  966. GGML_API struct ggml_tensor * ggml_soft_max_inplace(
  967. struct ggml_context * ctx,
  968. struct ggml_tensor * a);
  969. GGML_API struct ggml_tensor * ggml_soft_max_back(
  970. struct ggml_context * ctx,
  971. struct ggml_tensor * a,
  972. struct ggml_tensor * b);
  973. // in-place, returns view(a)
  974. GGML_API struct ggml_tensor * ggml_soft_max_back_inplace(
  975. struct ggml_context * ctx,
  976. struct ggml_tensor * a,
  977. struct ggml_tensor * b);
  978. // rotary position embedding
  979. // if mode & 1 == 1, skip n_past elements
  980. // if mode & 2 == 1, GPT-NeoX style
  981. // if mode & 4 == 1, ChatGLM style
  982. // TODO: avoid creating a new tensor every time
  983. GGML_API struct ggml_tensor * ggml_rope(
  984. struct ggml_context * ctx,
  985. struct ggml_tensor * a,
  986. int n_past,
  987. int n_dims,
  988. int mode,
  989. int n_ctx);
  990. // in-place, returns view(a)
  991. GGML_API struct ggml_tensor * ggml_rope_inplace(
  992. struct ggml_context * ctx,
  993. struct ggml_tensor * a,
  994. int n_past,
  995. int n_dims,
  996. int mode,
  997. int n_ctx);
  998. // custom RoPE
  999. GGML_API struct ggml_tensor * ggml_rope_custom(
  1000. struct ggml_context * ctx,
  1001. struct ggml_tensor * a,
  1002. int n_past,
  1003. int n_dims,
  1004. int mode,
  1005. int n_ctx,
  1006. float freq_base,
  1007. float freq_scale);
  1008. // in-place, returns view(a)
  1009. GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
  1010. struct ggml_context * ctx,
  1011. struct ggml_tensor * a,
  1012. int n_past,
  1013. int n_dims,
  1014. int mode,
  1015. int n_ctx,
  1016. float freq_base,
  1017. float freq_scale);
  1018. // rotary position embedding backward, i.e compute dx from dy
  1019. // a - dy
  1020. GGML_API struct ggml_tensor * ggml_rope_back(
  1021. struct ggml_context * ctx,
  1022. struct ggml_tensor * a,
  1023. int n_past,
  1024. int n_dims,
  1025. int mode,
  1026. int n_ctx);
  1027. // alibi position embedding
  1028. // in-place, returns view(a)
  1029. struct ggml_tensor * ggml_alibi(
  1030. struct ggml_context * ctx,
  1031. struct ggml_tensor * a,
  1032. int n_past,
  1033. int n_head,
  1034. float bias_max);
  1035. // clamp
  1036. // in-place, returns view(a)
  1037. struct ggml_tensor * ggml_clamp(
  1038. struct ggml_context * ctx,
  1039. struct ggml_tensor * a,
  1040. float min,
  1041. float max);
  1042. GGML_API struct ggml_tensor * ggml_conv_1d(
  1043. struct ggml_context * ctx,
  1044. struct ggml_tensor * a,
  1045. struct ggml_tensor * b,
  1046. int s0, // stride
  1047. int p0, // padding
  1048. int d0); // dilation
  1049. GGML_API struct ggml_tensor * ggml_conv_2d(
  1050. struct ggml_context * ctx,
  1051. struct ggml_tensor * a,
  1052. struct ggml_tensor * b,
  1053. int s0,
  1054. int s1,
  1055. int p0,
  1056. int p1,
  1057. int d0,
  1058. int d1);
  1059. // conv_1d with padding = half
  1060. // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
  1061. GGML_API struct ggml_tensor * ggml_conv_1d_ph(
  1062. struct ggml_context * ctx,
  1063. struct ggml_tensor * a,
  1064. struct ggml_tensor * b,
  1065. int s,
  1066. int d);
  1067. enum ggml_op_pool {
  1068. GGML_OP_POOL_MAX,
  1069. GGML_OP_POOL_AVG,
  1070. GGML_OP_POOL_COUNT,
  1071. };
  1072. GGML_API struct ggml_tensor * ggml_pool_1d(
  1073. struct ggml_context * ctx,
  1074. struct ggml_tensor * a,
  1075. enum ggml_op_pool op,
  1076. int k0, // kernel size
  1077. int s0, // stride
  1078. int p0); // padding
  1079. GGML_API struct ggml_tensor * ggml_pool_2d(
  1080. struct ggml_context * ctx,
  1081. struct ggml_tensor * a,
  1082. enum ggml_op_pool op,
  1083. int k0,
  1084. int k1,
  1085. int s0,
  1086. int s1,
  1087. int p0,
  1088. int p1);
  1089. GGML_API struct ggml_tensor * ggml_flash_attn(
  1090. struct ggml_context * ctx,
  1091. struct ggml_tensor * q,
  1092. struct ggml_tensor * k,
  1093. struct ggml_tensor * v,
  1094. bool masked);
  1095. GGML_API struct ggml_tensor * ggml_flash_attn_back(
  1096. struct ggml_context * ctx,
  1097. struct ggml_tensor * q,
  1098. struct ggml_tensor * k,
  1099. struct ggml_tensor * v,
  1100. struct ggml_tensor * d,
  1101. bool masked);
  1102. GGML_API struct ggml_tensor * ggml_flash_ff(
  1103. struct ggml_context * ctx,
  1104. struct ggml_tensor * a,
  1105. struct ggml_tensor * b0,
  1106. struct ggml_tensor * b1,
  1107. struct ggml_tensor * c0,
  1108. struct ggml_tensor * c1);
  1109. // partition into non-overlapping windows with padding if needed
  1110. // example:
  1111. // a: 768 64 64 1
  1112. // w: 14
  1113. // res: 768 14 14 25
  1114. // used in sam
  1115. GGML_API struct ggml_tensor * ggml_win_part(
  1116. struct ggml_context * ctx,
  1117. struct ggml_tensor * a,
  1118. int w);
  1119. // reverse of ggml_win_part
  1120. // used in sam
  1121. GGML_API struct ggml_tensor * ggml_win_unpart(
  1122. struct ggml_context * ctx,
  1123. struct ggml_tensor * a,
  1124. int w0,
  1125. int h0,
  1126. int w);
  1127. GGML_API struct ggml_tensor * ggml_unary(
  1128. struct ggml_context * ctx,
  1129. struct ggml_tensor * a,
  1130. enum ggml_unary_op op);
  1131. GGML_API struct ggml_tensor * ggml_unary_inplace(
  1132. struct ggml_context * ctx,
  1133. struct ggml_tensor * a,
  1134. enum ggml_unary_op op);
  1135. // custom operators
  1136. typedef void (*ggml_unary_op_f32_t) (const int, float *, const float *);
  1137. typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
  1138. typedef void (*ggml_custom1_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *);
  1139. typedef void (*ggml_custom2_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
  1140. typedef void (*ggml_custom3_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
  1141. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_f32(
  1142. struct ggml_context * ctx,
  1143. struct ggml_tensor * a,
  1144. ggml_unary_op_f32_t fun),
  1145. "use ggml_map_custom1 instead");
  1146. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_inplace_f32(
  1147. struct ggml_context * ctx,
  1148. struct ggml_tensor * a,
  1149. ggml_unary_op_f32_t fun),
  1150. "use ggml_map_custom1_inplace instead");
  1151. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_f32(
  1152. struct ggml_context * ctx,
  1153. struct ggml_tensor * a,
  1154. struct ggml_tensor * b,
  1155. ggml_binary_op_f32_t fun),
  1156. "use ggml_map_custom2 instead");
  1157. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_inplace_f32(
  1158. struct ggml_context * ctx,
  1159. struct ggml_tensor * a,
  1160. struct ggml_tensor * b,
  1161. ggml_binary_op_f32_t fun),
  1162. "use ggml_map_custom2_inplace instead");
  1163. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_f32(
  1164. struct ggml_context * ctx,
  1165. struct ggml_tensor * a,
  1166. ggml_custom1_op_f32_t fun),
  1167. "use ggml_map_custom1 instead");
  1168. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_inplace_f32(
  1169. struct ggml_context * ctx,
  1170. struct ggml_tensor * a,
  1171. ggml_custom1_op_f32_t fun),
  1172. "use ggml_map_custom1_inplace instead");
  1173. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_f32(
  1174. struct ggml_context * ctx,
  1175. struct ggml_tensor * a,
  1176. struct ggml_tensor * b,
  1177. ggml_custom2_op_f32_t fun),
  1178. "use ggml_map_custom2 instead");
  1179. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_inplace_f32(
  1180. struct ggml_context * ctx,
  1181. struct ggml_tensor * a,
  1182. struct ggml_tensor * b,
  1183. ggml_custom2_op_f32_t fun),
  1184. "use ggml_map_custom2_inplace instead");
  1185. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_f32(
  1186. struct ggml_context * ctx,
  1187. struct ggml_tensor * a,
  1188. struct ggml_tensor * b,
  1189. struct ggml_tensor * c,
  1190. ggml_custom3_op_f32_t fun),
  1191. "use ggml_map_custom3 instead");
  1192. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_inplace_f32(
  1193. struct ggml_context * ctx,
  1194. struct ggml_tensor * a,
  1195. struct ggml_tensor * b,
  1196. struct ggml_tensor * c,
  1197. ggml_custom3_op_f32_t fun),
  1198. "use ggml_map_custom3_inplace instead");
  1199. // custom operators v2
  1200. typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
  1201. typedef void (*ggml_custom2_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata);
  1202. typedef void (*ggml_custom3_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata);
  1203. #define GGML_N_TASKS_MAX -1
  1204. GGML_API struct ggml_tensor * ggml_map_custom1(
  1205. struct ggml_context * ctx,
  1206. struct ggml_tensor * a,
  1207. ggml_custom1_op_t fun,
  1208. int n_tasks,
  1209. void * userdata);
  1210. GGML_API struct ggml_tensor * ggml_map_custom1_inplace(
  1211. struct ggml_context * ctx,
  1212. struct ggml_tensor * a,
  1213. ggml_custom1_op_t fun,
  1214. int n_tasks,
  1215. void * userdata);
  1216. GGML_API struct ggml_tensor * ggml_map_custom2(
  1217. struct ggml_context * ctx,
  1218. struct ggml_tensor * a,
  1219. struct ggml_tensor * b,
  1220. ggml_custom2_op_t fun,
  1221. int n_tasks,
  1222. void * userdata);
  1223. GGML_API struct ggml_tensor * ggml_map_custom2_inplace(
  1224. struct ggml_context * ctx,
  1225. struct ggml_tensor * a,
  1226. struct ggml_tensor * b,
  1227. ggml_custom2_op_t fun,
  1228. int n_tasks,
  1229. void * userdata);
  1230. GGML_API struct ggml_tensor * ggml_map_custom3(
  1231. struct ggml_context * ctx,
  1232. struct ggml_tensor * a,
  1233. struct ggml_tensor * b,
  1234. struct ggml_tensor * c,
  1235. ggml_custom3_op_t fun,
  1236. int n_tasks,
  1237. void * userdata);
  1238. GGML_API struct ggml_tensor * ggml_map_custom3_inplace(
  1239. struct ggml_context * ctx,
  1240. struct ggml_tensor * a,
  1241. struct ggml_tensor * b,
  1242. struct ggml_tensor * c,
  1243. ggml_custom3_op_t fun,
  1244. int n_tasks,
  1245. void * userdata);
  1246. // loss function
  1247. GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
  1248. struct ggml_context * ctx,
  1249. struct ggml_tensor * a,
  1250. struct ggml_tensor * b);
  1251. GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
  1252. struct ggml_context * ctx,
  1253. struct ggml_tensor * a,
  1254. struct ggml_tensor * b,
  1255. struct ggml_tensor * c);
  1256. //
  1257. // automatic differentiation
  1258. //
  1259. GGML_API void ggml_set_param(
  1260. struct ggml_context * ctx,
  1261. struct ggml_tensor * tensor);
  1262. GGML_API void ggml_build_forward_expand(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
  1263. GGML_API struct ggml_cgraph ggml_build_forward (struct ggml_tensor * tensor);
  1264. GGML_API struct ggml_cgraph ggml_build_backward(struct ggml_context * ctx, struct ggml_cgraph * gf, bool keep);
  1265. // graph allocation in a context
  1266. GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx);
  1267. GGML_API struct ggml_cgraph * ggml_build_forward_ctx(struct ggml_context * ctx, struct ggml_tensor * tensor);
  1268. GGML_API size_t ggml_graph_overhead(void);
  1269. // ggml_graph_plan() has to be called before ggml_graph_compute()
  1270. // when plan.work_size > 0, caller must allocate memory for plan.work_data
  1271. GGML_API struct ggml_cplan ggml_graph_plan (struct ggml_cgraph * cgraph, int n_threads /*= GGML_DEFAULT_N_THREADS*/);
  1272. GGML_API int ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan);
  1273. GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph);
  1274. // same as ggml_graph_compute() but the work data is allocated as a part of the context
  1275. // note: the drawback of this API is that you must have ensured that the context has enough memory for the work data
  1276. GGML_API void ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
  1277. GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name);
  1278. GGML_API void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname);
  1279. GGML_API struct ggml_cgraph ggml_graph_import(const char * fname, struct ggml_context ** ctx_data, struct ggml_context ** ctx_eval);
  1280. // print info and performance information for the graph
  1281. GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
  1282. // dump the graph into a file using the dot format
  1283. GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
  1284. //
  1285. // optimization
  1286. //
  1287. // optimization methods
  1288. enum ggml_opt_type {
  1289. GGML_OPT_ADAM,
  1290. GGML_OPT_LBFGS,
  1291. };
  1292. // linesearch methods
  1293. enum ggml_linesearch {
  1294. GGML_LINESEARCH_DEFAULT = 1,
  1295. GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0,
  1296. GGML_LINESEARCH_BACKTRACKING_WOLFE = 1,
  1297. GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2,
  1298. };
  1299. // optimization return values
  1300. enum ggml_opt_result {
  1301. GGML_OPT_OK = 0,
  1302. GGML_OPT_DID_NOT_CONVERGE,
  1303. GGML_OPT_NO_CONTEXT,
  1304. GGML_OPT_INVALID_WOLFE,
  1305. GGML_OPT_FAIL,
  1306. GGML_LINESEARCH_FAIL = -128,
  1307. GGML_LINESEARCH_MINIMUM_STEP,
  1308. GGML_LINESEARCH_MAXIMUM_STEP,
  1309. GGML_LINESEARCH_MAXIMUM_ITERATIONS,
  1310. GGML_LINESEARCH_INVALID_PARAMETERS,
  1311. };
  1312. // optimization parameters
  1313. //
  1314. // see ggml.c (ggml_opt_default_params) for default values
  1315. //
  1316. struct ggml_opt_params {
  1317. enum ggml_opt_type type;
  1318. int n_threads;
  1319. // delta-based convergence test
  1320. //
  1321. // if past == 0 - disabled
  1322. // if past > 0:
  1323. // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|)
  1324. //
  1325. int past;
  1326. float delta;
  1327. // maximum number of iterations without improvement
  1328. //
  1329. // if 0 - disabled
  1330. // if > 0:
  1331. // assume convergence if no cost improvement in this number of iterations
  1332. //
  1333. int max_no_improvement;
  1334. bool print_forward_graph;
  1335. bool print_backward_graph;
  1336. // ADAM parameters
  1337. struct {
  1338. int n_iter;
  1339. float sched; // schedule multiplier (fixed, decay or warmup)
  1340. float decay; // weight decay for AdamW, use 0.0f to disable
  1341. float alpha; // learning rate
  1342. float beta1;
  1343. float beta2;
  1344. float eps; // epsilon for numerical stability
  1345. float eps_f; // epsilon for convergence test
  1346. float eps_g; // epsilon for convergence test
  1347. } adam;
  1348. // LBFGS parameters
  1349. struct {
  1350. int m; // number of corrections to approximate the inv. Hessian
  1351. int n_iter;
  1352. int max_linesearch;
  1353. float eps; // convergence tolerance
  1354. float ftol; // line search tolerance
  1355. float wolfe;
  1356. float min_step;
  1357. float max_step;
  1358. enum ggml_linesearch linesearch;
  1359. } lbfgs;
  1360. };
  1361. struct ggml_opt_context {
  1362. struct ggml_context * ctx;
  1363. struct ggml_opt_params params;
  1364. int iter;
  1365. int64_t nx; // number of parameter elements
  1366. bool just_initialized;
  1367. struct {
  1368. struct ggml_tensor * x; // view of the parameters
  1369. struct ggml_tensor * g1; // gradient
  1370. struct ggml_tensor * g2; // gradient squared
  1371. struct ggml_tensor * m; // first moment
  1372. struct ggml_tensor * v; // second moment
  1373. struct ggml_tensor * mh; // first moment hat
  1374. struct ggml_tensor * vh; // second moment hat
  1375. struct ggml_tensor * pf; // past function values
  1376. float fx_best;
  1377. float fx_prev;
  1378. int n_no_improvement;
  1379. } adam;
  1380. struct {
  1381. struct ggml_tensor * x; // current parameters
  1382. struct ggml_tensor * xp; // previous parameters
  1383. struct ggml_tensor * g; // current gradient
  1384. struct ggml_tensor * gp; // previous gradient
  1385. struct ggml_tensor * d; // search direction
  1386. struct ggml_tensor * pf; // past function values
  1387. struct ggml_tensor * lmal; // the L-BFGS memory alpha
  1388. struct ggml_tensor * lmys; // the L-BFGS memory ys
  1389. struct ggml_tensor * lms; // the L-BFGS memory s
  1390. struct ggml_tensor * lmy; // the L-BFGS memory y
  1391. float fx_best;
  1392. float step;
  1393. int j;
  1394. int k;
  1395. int end;
  1396. int n_no_improvement;
  1397. } lbfgs;
  1398. };
  1399. GGML_API struct ggml_opt_params ggml_opt_default_params(enum ggml_opt_type type);
  1400. // optimize the function defined by the tensor f
  1401. GGML_API enum ggml_opt_result ggml_opt(
  1402. struct ggml_context * ctx,
  1403. struct ggml_opt_params params,
  1404. struct ggml_tensor * f);
  1405. // initialize optimizer context
  1406. GGML_API void ggml_opt_init(
  1407. struct ggml_context * ctx,
  1408. struct ggml_opt_context * opt,
  1409. struct ggml_opt_params params,
  1410. int64_t nx);
  1411. // continue optimizing the function defined by the tensor f
  1412. GGML_API enum ggml_opt_result ggml_opt_resume(
  1413. struct ggml_context * ctx,
  1414. struct ggml_opt_context * opt,
  1415. struct ggml_tensor * f);
  1416. // continue optimizing the function defined by the tensor f
  1417. GGML_API enum ggml_opt_result ggml_opt_resume_g(
  1418. struct ggml_context * ctx,
  1419. struct ggml_opt_context * opt,
  1420. struct ggml_tensor * f,
  1421. struct ggml_cgraph * gf,
  1422. struct ggml_cgraph * gb);
  1423. //
  1424. // quantization
  1425. //
  1426. GGML_API size_t ggml_quantize_q4_0(const float * src, void * dst, int n, int k, int64_t * hist);
  1427. GGML_API size_t ggml_quantize_q4_1(const float * src, void * dst, int n, int k, int64_t * hist);
  1428. GGML_API size_t ggml_quantize_q5_0(const float * src, void * dst, int n, int k, int64_t * hist);
  1429. GGML_API size_t ggml_quantize_q5_1(const float * src, void * dst, int n, int k, int64_t * hist);
  1430. GGML_API size_t ggml_quantize_q8_0(const float * src, void * dst, int n, int k, int64_t * hist);
  1431. GGML_API size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, int start, int n, int64_t * hist);
  1432. //
  1433. // gguf
  1434. //
  1435. enum gguf_type {
  1436. GGUF_TYPE_UINT8 = 0,
  1437. GGUF_TYPE_INT8 = 1,
  1438. GGUF_TYPE_UINT16 = 2,
  1439. GGUF_TYPE_INT16 = 3,
  1440. GGUF_TYPE_UINT32 = 4,
  1441. GGUF_TYPE_INT32 = 5,
  1442. GGUF_TYPE_FLOAT32 = 6,
  1443. GGUF_TYPE_BOOL = 7,
  1444. GGUF_TYPE_STRING = 8,
  1445. GGUF_TYPE_ARRAY = 9,
  1446. GGUF_TYPE_COUNT, // marks the end of the enum
  1447. };
  1448. struct gguf_context;
  1449. struct gguf_init_params {
  1450. bool no_alloc;
  1451. // if not NULL, create a ggml_context and allocate the tensor data in it
  1452. struct ggml_context ** ctx;
  1453. };
  1454. GGML_API struct gguf_context * gguf_init_empty(void);
  1455. GGML_API struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params);
  1456. //GGML_API struct gguf_context * gguf_init_from_buffer(..);
  1457. GGML_API void gguf_free(struct gguf_context * ctx);
  1458. GGML_API const char * gguf_type_name(enum gguf_type type);
  1459. GGML_API int gguf_get_version (struct gguf_context * ctx);
  1460. GGML_API size_t gguf_get_alignment (struct gguf_context * ctx);
  1461. GGML_API size_t gguf_get_data_offset(struct gguf_context * ctx);
  1462. GGML_API void * gguf_get_data (struct gguf_context * ctx);
  1463. GGML_API int gguf_get_n_kv(struct gguf_context * ctx);
  1464. GGML_API int gguf_find_key(struct gguf_context * ctx, const char * key);
  1465. GGML_API const char * gguf_get_key (struct gguf_context * ctx, int i);
  1466. GGML_API enum gguf_type gguf_get_kv_type (struct gguf_context * ctx, int i);
  1467. GGML_API enum gguf_type gguf_get_arr_type(struct gguf_context * ctx, int i);
  1468. // results are undefined if the wrong type is used for the key
  1469. GGML_API uint8_t gguf_get_val_u8 (struct gguf_context * ctx, int i);
  1470. GGML_API int8_t gguf_get_val_i8 (struct gguf_context * ctx, int i);
  1471. GGML_API uint16_t gguf_get_val_u16 (struct gguf_context * ctx, int i);
  1472. GGML_API int16_t gguf_get_val_i16 (struct gguf_context * ctx, int i);
  1473. GGML_API uint32_t gguf_get_val_u32 (struct gguf_context * ctx, int i);
  1474. GGML_API int32_t gguf_get_val_i32 (struct gguf_context * ctx, int i);
  1475. GGML_API float gguf_get_val_f32 (struct gguf_context * ctx, int i);
  1476. GGML_API bool gguf_get_val_bool(struct gguf_context * ctx, int i);
  1477. GGML_API const char * gguf_get_val_str (struct gguf_context * ctx, int i);
  1478. GGML_API int gguf_get_arr_n (struct gguf_context * ctx, int i);
  1479. GGML_API const void * gguf_get_arr_data(struct gguf_context * ctx, int i);
  1480. GGML_API const char * gguf_get_arr_str (struct gguf_context * ctx, int key_id, int i);
  1481. GGML_API int gguf_get_n_tensors (struct gguf_context * ctx);
  1482. GGML_API int gguf_find_tensor (struct gguf_context * ctx, const char * name);
  1483. GGML_API size_t gguf_get_tensor_offset(struct gguf_context * ctx, int i);
  1484. GGML_API char * gguf_get_tensor_name (struct gguf_context * ctx, int i);
  1485. // overrides existing values or adds a new one
  1486. GGML_API void gguf_set_val_u8 (struct gguf_context * ctx, const char * key, uint8_t val);
  1487. GGML_API void gguf_set_val_i8 (struct gguf_context * ctx, const char * key, int8_t val);
  1488. GGML_API void gguf_set_val_u16 (struct gguf_context * ctx, const char * key, uint16_t val);
  1489. GGML_API void gguf_set_val_i16 (struct gguf_context * ctx, const char * key, int16_t val);
  1490. GGML_API void gguf_set_val_u32 (struct gguf_context * ctx, const char * key, uint32_t val);
  1491. GGML_API void gguf_set_val_i32 (struct gguf_context * ctx, const char * key, int32_t val);
  1492. GGML_API void gguf_set_val_f32 (struct gguf_context * ctx, const char * key, float val);
  1493. GGML_API void gguf_set_val_bool(struct gguf_context * ctx, const char * key, bool val);
  1494. GGML_API void gguf_set_val_str (struct gguf_context * ctx, const char * key, const char * val);
  1495. GGML_API void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_type type, const void * data, int n);
  1496. GGML_API void gguf_set_arr_str (struct gguf_context * ctx, const char * key, const char ** data, int n);
  1497. // set or add KV pairs from another context
  1498. GGML_API void gguf_set_kv(struct gguf_context * ctx, struct gguf_context * src);
  1499. // manage tensor info
  1500. GGML_API void gguf_add_tensor(struct gguf_context * ctx, const struct ggml_tensor * tensor);
  1501. GGML_API void gguf_set_tensor_type(struct gguf_context * ctx, const char * name, enum ggml_type type);
  1502. GGML_API void gguf_set_tensor_data(struct gguf_context * ctx, const char * name, const void * data, size_t size);
  1503. // writing gguf files can be done in 2 ways:
  1504. //
  1505. // - write the entire gguf_context to a binary file in a single pass:
  1506. //
  1507. // gguf_write_to_file(ctx, fname);
  1508. //
  1509. // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
  1510. //
  1511. // FILE * f = fopen(fname, "wb");
  1512. // fseek(f, gguf_get_meta_size(ctx), SEEK_SET);
  1513. // fwrite(f, ...);
  1514. // void * data = gguf_meta_get_meta_data(ctx);
  1515. // fseek(f, 0, SEEK_SET);
  1516. // fwrite(f, data, gguf_get_meta_size(ctx));
  1517. // free(data);
  1518. // fclose(f);
  1519. //
  1520. // write the entire context to a binary file
  1521. GGML_API void gguf_write_to_file(struct gguf_context * ctx, const char * fname, bool only_meta);
  1522. // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
  1523. GGML_API size_t gguf_get_meta_size(struct gguf_context * ctx);
  1524. GGML_API void gguf_get_meta_data(struct gguf_context * ctx, void * data);
  1525. //
  1526. // system info
  1527. //
  1528. GGML_API int ggml_cpu_has_avx (void);
  1529. GGML_API int ggml_cpu_has_avx2 (void);
  1530. GGML_API int ggml_cpu_has_avx512 (void);
  1531. GGML_API int ggml_cpu_has_avx512_vbmi(void);
  1532. GGML_API int ggml_cpu_has_avx512_vnni(void);
  1533. GGML_API int ggml_cpu_has_fma (void);
  1534. GGML_API int ggml_cpu_has_neon (void);
  1535. GGML_API int ggml_cpu_has_arm_fma (void);
  1536. GGML_API int ggml_cpu_has_f16c (void);
  1537. GGML_API int ggml_cpu_has_fp16_va (void);
  1538. GGML_API int ggml_cpu_has_wasm_simd (void);
  1539. GGML_API int ggml_cpu_has_blas (void);
  1540. GGML_API int ggml_cpu_has_cublas (void);
  1541. GGML_API int ggml_cpu_has_clblast (void);
  1542. GGML_API int ggml_cpu_has_gpublas (void);
  1543. GGML_API int ggml_cpu_has_sse3 (void);
  1544. GGML_API int ggml_cpu_has_vsx (void);
  1545. //
  1546. // Internal types and functions exposed for tests and benchmarks
  1547. //
  1548. #ifdef __cplusplus
  1549. // restrict not standard in C++
  1550. #define GGML_RESTRICT
  1551. #else
  1552. #define GGML_RESTRICT restrict
  1553. #endif
  1554. typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int k);
  1555. typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int k);
  1556. typedef void (*ggml_vec_dot_t) (const int n, float * GGML_RESTRICT s, const void * GGML_RESTRICT x, const void * GGML_RESTRICT y);
  1557. typedef struct {
  1558. const char * type_name;
  1559. int blck_size;
  1560. size_t type_size;
  1561. bool is_quantized;
  1562. ggml_to_float_t to_float;
  1563. ggml_from_float_t from_float;
  1564. ggml_from_float_t from_float_reference;
  1565. ggml_vec_dot_t vec_dot;
  1566. enum ggml_type vec_dot_type;
  1567. } ggml_type_traits_t;
  1568. ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type);
  1569. #ifdef __cplusplus
  1570. }
  1571. #endif