ggml.h 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  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_new_graph(ctx);
  61. // ggml_build_forward_expand(gf, f);
  62. //
  63. // // set the input variable and parameter values
  64. // ggml_set_f32(x, 2.0f);
  65. // ggml_set_f32(a, 3.0f);
  66. // ggml_set_f32(b, 4.0f);
  67. //
  68. // ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
  69. //
  70. // printf("f = %f\n", ggml_get_f32_1d(f, 0));
  71. //
  72. // ...
  73. // }
  74. //
  75. // The actual computation is performed in the ggml_graph_compute() function.
  76. //
  77. // The ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
  78. // ggml_init() function. You have to be careful not to exceed the memory buffer size. Therefore, you have to know
  79. // in advance how much memory you need for your computation. Alternatively, you can allocate a large enough memory
  80. // and after defining the computation graph, call the ggml_used_mem() function to find out how much memory was
  81. // actually needed.
  82. //
  83. // The ggml_set_param() function marks a tensor as an input variable. This is used by the automatic
  84. // differentiation and optimization algorithms.
  85. //
  86. // The described approach allows to define the function graph once and then compute its forward or backward graphs
  87. // multiple times. All computations will use the same memory buffer allocated in the ggml_init() function. This way
  88. // the user can avoid the memory allocation overhead at runtime.
  89. //
  90. // The library supports multi-dimensional tensors - up to 4 dimensions. The FP16 and FP32 data types are first class
  91. // citizens, but in theory the library can be extended to support FP8 and integer data types.
  92. //
  93. // Each tensor operation produces a new tensor. Initially the library was envisioned to support only the use of unary
  94. // and binary operations. Most of the available operations fall into one of these two categories. With time, it became
  95. // clear that the library needs to support more complex operations. The way to support these operations is not clear
  96. // yet, but a few examples are demonstrated in the following operations:
  97. //
  98. // - ggml_permute()
  99. // - ggml_conv_1d_1s()
  100. // - ggml_conv_1d_2s()
  101. //
  102. // For each tensor operator, the library implements a forward and backward computation function. The forward function
  103. // computes the output tensor value given the input tensor values. The backward function computes the adjoint of the
  104. // input tensors given the adjoint of the output tensor. For a detailed explanation of what this means, take a
  105. // calculus class, or watch the following video:
  106. //
  107. // What is Automatic Differentiation?
  108. // https://www.youtube.com/watch?v=wG_nF1awSSY
  109. //
  110. //
  111. // ## Tensor data (struct ggml_tensor)
  112. //
  113. // The tensors are stored in memory via the ggml_tensor struct. The structure provides information about the size of
  114. // the tensor, the data type, and the memory buffer where the tensor data is stored. Additionally, it contains
  115. // pointers to the "source" tensors - i.e. the tensors that were used to compute the current tensor. For example:
  116. //
  117. // {
  118. // struct ggml_tensor * c = ggml_add(ctx, a, b);
  119. //
  120. // assert(c->src[0] == a);
  121. // assert(c->src[1] == b);
  122. // }
  123. //
  124. // The multi-dimensional tensors are stored in row-major order. The ggml_tensor struct contains fields for the
  125. // number of elements in each dimension ("ne") as well as the number of bytes ("nb", a.k.a. stride). This allows
  126. // to store tensors that are not contiguous in memory, which is useful for operations such as transposition and
  127. // permutation. All tensor operations have to take the stride into account and not assume that the tensor is
  128. // contiguous in memory.
  129. //
  130. // The data of the tensor is accessed via the "data" pointer. For example:
  131. //
  132. // {
  133. // const int nx = 2;
  134. // const int ny = 3;
  135. //
  136. // struct ggml_tensor * a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, nx, ny);
  137. //
  138. // for (int y = 0; y < ny; y++) {
  139. // for (int x = 0; x < nx; x++) {
  140. // *(float *) ((char *) a->data + y*a->nb[1] + x*a->nb[0]) = x + y;
  141. // }
  142. // }
  143. //
  144. // ...
  145. // }
  146. //
  147. // Alternatively, there are helper functions, such as ggml_get_f32_1d() and ggml_set_f32_1d() that can be used.
  148. //
  149. // ## The matrix multiplication operator (ggml_mul_mat)
  150. //
  151. // TODO
  152. //
  153. //
  154. // ## Multi-threading
  155. //
  156. // TODO
  157. //
  158. //
  159. // ## Overview of ggml.c
  160. //
  161. // TODO
  162. //
  163. //
  164. // ## SIMD optimizations
  165. //
  166. // TODO
  167. //
  168. //
  169. // ## Debugging ggml
  170. //
  171. // TODO
  172. //
  173. //
  174. #ifdef GGML_SHARED
  175. # if defined(_WIN32) && !defined(__MINGW32__)
  176. # ifdef GGML_BUILD
  177. # define GGML_API __declspec(dllexport) extern
  178. # else
  179. # define GGML_API __declspec(dllimport) extern
  180. # endif
  181. # else
  182. # define GGML_API __attribute__ ((visibility ("default"))) extern
  183. # endif
  184. #else
  185. # define GGML_API extern
  186. #endif
  187. // TODO: support for clang
  188. #ifdef __GNUC__
  189. # define GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
  190. #elif defined(_MSC_VER)
  191. # define GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
  192. #else
  193. # define GGML_DEPRECATED(func, hint) func
  194. #endif
  195. #ifndef __GNUC__
  196. # define GGML_ATTRIBUTE_FORMAT(...)
  197. #elif defined(__MINGW32__)
  198. # define GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
  199. #else
  200. # define GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
  201. #endif
  202. #include <stdbool.h>
  203. #include <stddef.h>
  204. #include <stdint.h>
  205. #include <stdio.h>
  206. #define GGML_FILE_MAGIC 0x67676d6c // "ggml"
  207. #define GGML_FILE_VERSION 2
  208. #define GGML_QNT_VERSION 2 // bump this on quantization format changes
  209. #define GGML_QNT_VERSION_FACTOR 1000 // do not change this
  210. #define GGML_MAX_DIMS 4
  211. #define GGML_MAX_PARAMS 2048
  212. #define GGML_MAX_SRC 10
  213. #define GGML_MAX_N_THREADS 512
  214. #define GGML_MAX_OP_PARAMS 64
  215. #ifndef GGML_MAX_NAME
  216. # define GGML_MAX_NAME 64
  217. #endif
  218. #define GGML_DEFAULT_N_THREADS 4
  219. #define GGML_DEFAULT_GRAPH_SIZE 2048
  220. #if UINTPTR_MAX == 0xFFFFFFFF
  221. #define GGML_MEM_ALIGN 4
  222. #else
  223. #define GGML_MEM_ALIGN 16
  224. #endif
  225. #define GGML_EXIT_SUCCESS 0
  226. #define GGML_EXIT_ABORTED 1
  227. #define GGML_ROPE_TYPE_NEOX 2
  228. #define GGUF_MAGIC "GGUF"
  229. #define GGUF_VERSION 3
  230. #define GGUF_DEFAULT_ALIGNMENT 32
  231. #define GGML_UNUSED(x) (void)(x)
  232. #define GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
  233. #ifndef NDEBUG
  234. # define GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
  235. #elif defined(__GNUC__)
  236. # define GGML_UNREACHABLE() __builtin_unreachable()
  237. #elif defined(_MSC_VER)
  238. # define GGML_UNREACHABLE() __assume(0)
  239. #else
  240. # define GGML_UNREACHABLE() ((void) 0)
  241. #endif
  242. #ifdef __cplusplus
  243. # define GGML_NORETURN [[noreturn]]
  244. #elif defined(_MSC_VER)
  245. # define GGML_NORETURN __declspec(noreturn)
  246. #else
  247. # define GGML_NORETURN _Noreturn
  248. #endif
  249. #define GGML_ABORT(...) ggml_abort(__FILE__, __LINE__, __VA_ARGS__)
  250. #define GGML_ASSERT(x) if (!(x)) GGML_ABORT("GGML_ASSERT(%s) failed", #x)
  251. // used to copy the number of elements and stride in bytes of tensors into local variables.
  252. // main purpose is to reduce code duplication and improve readability.
  253. //
  254. // example:
  255. //
  256. // GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  257. // GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
  258. //
  259. #define GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
  260. const type prefix##0 = (pointer)->array[0]; \
  261. GGML_UNUSED(prefix##0);
  262. #define GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
  263. GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
  264. const type prefix##1 = (pointer)->array[1]; \
  265. GGML_UNUSED(prefix##1);
  266. #define GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
  267. GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
  268. const type prefix##2 = (pointer)->array[2]; \
  269. GGML_UNUSED(prefix##2);
  270. #define GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
  271. GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
  272. const type prefix##3 = (pointer)->array[3]; \
  273. GGML_UNUSED(prefix##3);
  274. #define GGML_TENSOR_UNARY_OP_LOCALS \
  275. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
  276. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
  277. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
  278. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  279. #define GGML_TENSOR_BINARY_OP_LOCALS \
  280. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
  281. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
  282. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
  283. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
  284. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
  285. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  286. #define GGML_TENSOR_BINARY_OP_LOCALS01 \
  287. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
  288. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
  289. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
  290. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  291. #ifdef __cplusplus
  292. extern "C" {
  293. #endif
  294. GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
  295. GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
  296. enum ggml_status {
  297. GGML_STATUS_ALLOC_FAILED = -2,
  298. GGML_STATUS_FAILED = -1,
  299. GGML_STATUS_SUCCESS = 0,
  300. GGML_STATUS_ABORTED = 1,
  301. };
  302. // get ggml_status name string
  303. GGML_API const char * ggml_status_to_string(enum ggml_status status);
  304. // ieee 754-2008 half-precision float16
  305. // todo: make this not an integral type
  306. typedef uint16_t ggml_fp16_t;
  307. GGML_API float ggml_fp16_to_fp32(ggml_fp16_t);
  308. GGML_API ggml_fp16_t ggml_fp32_to_fp16(float);
  309. GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t *, float *, int64_t);
  310. GGML_API void ggml_fp32_to_fp16_row(const float *, ggml_fp16_t *, int64_t);
  311. // google brain half-precision bfloat16
  312. typedef struct { uint16_t bits; } ggml_bf16_t;
  313. GGML_API ggml_bf16_t ggml_fp32_to_bf16(float);
  314. GGML_API float ggml_bf16_to_fp32(ggml_bf16_t); // consider just doing << 16
  315. GGML_API void ggml_bf16_to_fp32_row(const ggml_bf16_t *, float *, int64_t);
  316. GGML_API void ggml_fp32_to_bf16_row_ref(const float *, ggml_bf16_t *, int64_t);
  317. GGML_API void ggml_fp32_to_bf16_row(const float *, ggml_bf16_t *, int64_t);
  318. struct ggml_object;
  319. struct ggml_context;
  320. struct ggml_cgraph;
  321. // NOTE: always add types at the end of the enum to keep backward compatibility
  322. enum ggml_type {
  323. GGML_TYPE_F32 = 0,
  324. GGML_TYPE_F16 = 1,
  325. GGML_TYPE_Q4_0 = 2,
  326. GGML_TYPE_Q4_1 = 3,
  327. // GGML_TYPE_Q4_2 = 4, support has been removed
  328. // GGML_TYPE_Q4_3 = 5, support has been removed
  329. GGML_TYPE_Q5_0 = 6,
  330. GGML_TYPE_Q5_1 = 7,
  331. GGML_TYPE_Q8_0 = 8,
  332. GGML_TYPE_Q8_1 = 9,
  333. GGML_TYPE_Q2_K = 10,
  334. GGML_TYPE_Q3_K = 11,
  335. GGML_TYPE_Q4_K = 12,
  336. GGML_TYPE_Q5_K = 13,
  337. GGML_TYPE_Q6_K = 14,
  338. GGML_TYPE_Q8_K = 15,
  339. GGML_TYPE_IQ2_XXS = 16,
  340. GGML_TYPE_IQ2_XS = 17,
  341. GGML_TYPE_IQ3_XXS = 18,
  342. GGML_TYPE_IQ1_S = 19,
  343. GGML_TYPE_IQ4_NL = 20,
  344. GGML_TYPE_IQ3_S = 21,
  345. GGML_TYPE_IQ2_S = 22,
  346. GGML_TYPE_IQ4_XS = 23,
  347. GGML_TYPE_I8 = 24,
  348. GGML_TYPE_I16 = 25,
  349. GGML_TYPE_I32 = 26,
  350. GGML_TYPE_I64 = 27,
  351. GGML_TYPE_F64 = 28,
  352. GGML_TYPE_IQ1_M = 29,
  353. GGML_TYPE_BF16 = 30,
  354. GGML_TYPE_Q4_0_4_4 = 31,
  355. GGML_TYPE_Q4_0_4_8 = 32,
  356. GGML_TYPE_Q4_0_8_8 = 33,
  357. GGML_TYPE_TQ1_0 = 34,
  358. GGML_TYPE_TQ2_0 = 35,
  359. GGML_TYPE_IQ4_NL_4_4 = 36,
  360. // GGML_TYPE_IQ4_NL_4_8 = 37,
  361. // GGML_TYPE_IQ4_NL_8_8 = 38,
  362. GGML_TYPE_COUNT,
  363. };
  364. // precision
  365. enum ggml_prec {
  366. GGML_PREC_DEFAULT,
  367. GGML_PREC_F32,
  368. };
  369. enum ggml_backend_type {
  370. GGML_BACKEND_TYPE_CPU = 0,
  371. GGML_BACKEND_TYPE_GPU = 10,
  372. GGML_BACKEND_TYPE_GPU_SPLIT = 20,
  373. };
  374. // model file types
  375. enum ggml_ftype {
  376. GGML_FTYPE_UNKNOWN = -1,
  377. GGML_FTYPE_ALL_F32 = 0,
  378. GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
  379. GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
  380. GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
  381. GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
  382. GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
  383. GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
  384. GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
  385. GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
  386. GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
  387. GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
  388. GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
  389. GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
  390. GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
  391. GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
  392. GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
  393. GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
  394. GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
  395. GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
  396. GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
  397. GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
  398. GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
  399. GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
  400. GGML_FTYPE_MOSTLY_Q4_0_4_4 = 25, // except 1d tensors
  401. GGML_FTYPE_MOSTLY_Q4_0_4_8 = 26, // except 1d tensors
  402. GGML_FTYPE_MOSTLY_Q4_0_8_8 = 27, // except 1d tensors
  403. };
  404. // available tensor operations:
  405. enum ggml_op {
  406. GGML_OP_NONE = 0,
  407. GGML_OP_DUP,
  408. GGML_OP_ADD,
  409. GGML_OP_ADD1,
  410. GGML_OP_ACC,
  411. GGML_OP_SUB,
  412. GGML_OP_MUL,
  413. GGML_OP_DIV,
  414. GGML_OP_SQR,
  415. GGML_OP_SQRT,
  416. GGML_OP_LOG,
  417. GGML_OP_SIN,
  418. GGML_OP_COS,
  419. GGML_OP_SUM,
  420. GGML_OP_SUM_ROWS,
  421. GGML_OP_MEAN,
  422. GGML_OP_ARGMAX,
  423. GGML_OP_COUNT_EQUAL,
  424. GGML_OP_REPEAT,
  425. GGML_OP_REPEAT_BACK,
  426. GGML_OP_CONCAT,
  427. GGML_OP_SILU_BACK,
  428. GGML_OP_NORM, // normalize
  429. GGML_OP_RMS_NORM,
  430. GGML_OP_RMS_NORM_BACK,
  431. GGML_OP_GROUP_NORM,
  432. GGML_OP_MUL_MAT,
  433. GGML_OP_MUL_MAT_ID,
  434. GGML_OP_OUT_PROD,
  435. GGML_OP_SCALE,
  436. GGML_OP_SET,
  437. GGML_OP_CPY,
  438. GGML_OP_CONT,
  439. GGML_OP_RESHAPE,
  440. GGML_OP_VIEW,
  441. GGML_OP_PERMUTE,
  442. GGML_OP_TRANSPOSE,
  443. GGML_OP_GET_ROWS,
  444. GGML_OP_GET_ROWS_BACK,
  445. GGML_OP_DIAG,
  446. GGML_OP_DIAG_MASK_INF,
  447. GGML_OP_DIAG_MASK_ZERO,
  448. GGML_OP_SOFT_MAX,
  449. GGML_OP_SOFT_MAX_BACK,
  450. GGML_OP_ROPE,
  451. GGML_OP_ROPE_BACK,
  452. GGML_OP_CLAMP,
  453. GGML_OP_CONV_TRANSPOSE_1D,
  454. GGML_OP_IM2COL,
  455. GGML_OP_IM2COL_BACK,
  456. GGML_OP_CONV_TRANSPOSE_2D,
  457. GGML_OP_POOL_1D,
  458. GGML_OP_POOL_2D,
  459. GGML_OP_POOL_2D_BACK,
  460. GGML_OP_UPSCALE, // nearest interpolate
  461. GGML_OP_PAD,
  462. GGML_OP_PAD_REFLECT_1D,
  463. GGML_OP_ARANGE,
  464. GGML_OP_TIMESTEP_EMBEDDING,
  465. GGML_OP_ARGSORT,
  466. GGML_OP_LEAKY_RELU,
  467. GGML_OP_FLASH_ATTN_EXT,
  468. GGML_OP_FLASH_ATTN_BACK,
  469. GGML_OP_SSM_CONV,
  470. GGML_OP_SSM_SCAN,
  471. GGML_OP_WIN_PART,
  472. GGML_OP_WIN_UNPART,
  473. GGML_OP_GET_REL_POS,
  474. GGML_OP_ADD_REL_POS,
  475. GGML_OP_RWKV_WKV6,
  476. GGML_OP_UNARY,
  477. GGML_OP_MAP_UNARY,
  478. GGML_OP_MAP_BINARY,
  479. GGML_OP_MAP_CUSTOM1_F32,
  480. GGML_OP_MAP_CUSTOM2_F32,
  481. GGML_OP_MAP_CUSTOM3_F32,
  482. GGML_OP_MAP_CUSTOM1,
  483. GGML_OP_MAP_CUSTOM2,
  484. GGML_OP_MAP_CUSTOM3,
  485. GGML_OP_CROSS_ENTROPY_LOSS,
  486. GGML_OP_CROSS_ENTROPY_LOSS_BACK,
  487. GGML_OP_OPT_STEP_ADAMW,
  488. GGML_OP_COUNT,
  489. };
  490. enum ggml_unary_op {
  491. GGML_UNARY_OP_ABS,
  492. GGML_UNARY_OP_SGN,
  493. GGML_UNARY_OP_NEG,
  494. GGML_UNARY_OP_STEP,
  495. GGML_UNARY_OP_TANH,
  496. GGML_UNARY_OP_ELU,
  497. GGML_UNARY_OP_RELU,
  498. GGML_UNARY_OP_SIGMOID,
  499. GGML_UNARY_OP_GELU,
  500. GGML_UNARY_OP_GELU_QUICK,
  501. GGML_UNARY_OP_SILU,
  502. GGML_UNARY_OP_HARDSWISH,
  503. GGML_UNARY_OP_HARDSIGMOID,
  504. GGML_UNARY_OP_EXP,
  505. GGML_UNARY_OP_COUNT,
  506. };
  507. enum ggml_object_type {
  508. GGML_OBJECT_TYPE_TENSOR,
  509. GGML_OBJECT_TYPE_GRAPH,
  510. GGML_OBJECT_TYPE_WORK_BUFFER
  511. };
  512. enum ggml_log_level {
  513. GGML_LOG_LEVEL_NONE = 0,
  514. GGML_LOG_LEVEL_DEBUG = 1,
  515. GGML_LOG_LEVEL_INFO = 2,
  516. GGML_LOG_LEVEL_WARN = 3,
  517. GGML_LOG_LEVEL_ERROR = 4,
  518. GGML_LOG_LEVEL_CONT = 5, // continue previous log
  519. };
  520. // this tensor...
  521. enum ggml_tensor_flag {
  522. GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
  523. GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
  524. GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
  525. GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
  526. };
  527. struct ggml_init_params {
  528. // memory pool
  529. size_t mem_size; // bytes
  530. void * mem_buffer; // if NULL, memory will be allocated internally
  531. bool no_alloc; // don't allocate memory for the tensor data
  532. };
  533. // n-dimensional tensor
  534. struct ggml_tensor {
  535. enum ggml_type type;
  536. GGML_DEPRECATED(enum ggml_backend_type backend, "use the buffer type to find the storage location of the tensor");
  537. struct ggml_backend_buffer * buffer;
  538. int64_t ne[GGML_MAX_DIMS]; // number of elements
  539. size_t nb[GGML_MAX_DIMS]; // stride in bytes:
  540. // nb[0] = ggml_type_size(type)
  541. // nb[1] = nb[0] * (ne[0] / ggml_blck_size(type)) + padding
  542. // nb[i] = nb[i-1] * ne[i-1]
  543. // compute data
  544. enum ggml_op op;
  545. // op params - allocated as int32_t for alignment
  546. int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
  547. int32_t flags;
  548. struct ggml_tensor * src[GGML_MAX_SRC];
  549. // source tensor and offset for views
  550. struct ggml_tensor * view_src;
  551. size_t view_offs;
  552. void * data;
  553. char name[GGML_MAX_NAME];
  554. void * extra; // extra things e.g. for ggml-cuda.cu
  555. char padding[8];
  556. };
  557. static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
  558. // Abort callback
  559. // If not NULL, called before ggml computation
  560. // If it returns true, the computation is aborted
  561. typedef bool (*ggml_abort_callback)(void * data);
  562. //
  563. // GUID
  564. //
  565. // GUID types
  566. typedef uint8_t ggml_guid[16];
  567. typedef ggml_guid * ggml_guid_t;
  568. GGML_API bool ggml_guid_matches(ggml_guid_t guid_a, ggml_guid_t guid_b);
  569. // misc
  570. GGML_API void ggml_time_init(void); // call this once at the beginning of the program
  571. GGML_API int64_t ggml_time_ms(void);
  572. GGML_API int64_t ggml_time_us(void);
  573. GGML_API int64_t ggml_cycles(void);
  574. GGML_API int64_t ggml_cycles_per_ms(void);
  575. // accepts a UTF-8 path, even on Windows
  576. GGML_API FILE * ggml_fopen(const char * fname, const char * mode);
  577. GGML_API void ggml_print_object (const struct ggml_object * obj);
  578. GGML_API void ggml_print_objects(const struct ggml_context * ctx);
  579. GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor);
  580. GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor);
  581. GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor);
  582. GGML_API size_t ggml_nbytes_pad(const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN
  583. GGML_API int64_t ggml_blck_size(enum ggml_type type);
  584. GGML_API size_t ggml_type_size(enum ggml_type type); // size in bytes for all elements in a block
  585. GGML_API size_t ggml_row_size (enum ggml_type type, int64_t ne); // size in bytes for all elements in a row
  586. GGML_DEPRECATED(
  587. GGML_API double ggml_type_sizef(enum ggml_type type), // ggml_type_size()/ggml_blck_size() as float
  588. "use ggml_row_size() instead");
  589. GGML_API const char * ggml_type_name(enum ggml_type type);
  590. GGML_API const char * ggml_op_name (enum ggml_op op);
  591. GGML_API const char * ggml_op_symbol(enum ggml_op op);
  592. GGML_API const char * ggml_unary_op_name(enum ggml_unary_op op);
  593. GGML_API const char * ggml_op_desc(const struct ggml_tensor * t); // unary or op name
  594. GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
  595. GGML_API bool ggml_is_quantized(enum ggml_type type);
  596. // TODO: temporary until model loading of ggml examples is refactored
  597. GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
  598. GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor);
  599. GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor);
  600. GGML_API bool ggml_is_empty (const struct ggml_tensor * tensor);
  601. GGML_API bool ggml_is_scalar (const struct ggml_tensor * tensor);
  602. GGML_API bool ggml_is_vector (const struct ggml_tensor * tensor);
  603. GGML_API bool ggml_is_matrix (const struct ggml_tensor * tensor);
  604. GGML_API bool ggml_is_3d (const struct ggml_tensor * tensor);
  605. GGML_API int ggml_n_dims (const struct ggml_tensor * tensor); // returns 1 for scalars
  606. GGML_API bool ggml_is_contiguous (const struct ggml_tensor * tensor);
  607. GGML_API bool ggml_is_contiguous_0(const struct ggml_tensor * tensor); // same as ggml_is_contiguous()
  608. GGML_API bool ggml_is_contiguous_1(const struct ggml_tensor * tensor); // contiguous for dims >= 1
  609. GGML_API bool ggml_is_contiguous_2(const struct ggml_tensor * tensor); // contiguous for dims >= 2
  610. GGML_API bool ggml_are_same_shape (const struct ggml_tensor * t0, const struct ggml_tensor * t1);
  611. GGML_API bool ggml_are_same_stride(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
  612. GGML_API bool ggml_can_repeat(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
  613. // use this to compute the memory overhead of a tensor
  614. GGML_API size_t ggml_tensor_overhead(void);
  615. GGML_API bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes);
  616. // main
  617. GGML_API struct ggml_context * ggml_init (struct ggml_init_params params);
  618. GGML_API void ggml_reset(struct ggml_context * ctx);
  619. GGML_API void ggml_free (struct ggml_context * ctx);
  620. GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
  621. GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
  622. GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
  623. GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx);
  624. GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx);
  625. GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
  626. GGML_API struct ggml_tensor * ggml_new_tensor(
  627. struct ggml_context * ctx,
  628. enum ggml_type type,
  629. int n_dims,
  630. const int64_t *ne);
  631. GGML_API struct ggml_tensor * ggml_new_tensor_1d(
  632. struct ggml_context * ctx,
  633. enum ggml_type type,
  634. int64_t ne0);
  635. GGML_API struct ggml_tensor * ggml_new_tensor_2d(
  636. struct ggml_context * ctx,
  637. enum ggml_type type,
  638. int64_t ne0,
  639. int64_t ne1);
  640. GGML_API struct ggml_tensor * ggml_new_tensor_3d(
  641. struct ggml_context * ctx,
  642. enum ggml_type type,
  643. int64_t ne0,
  644. int64_t ne1,
  645. int64_t ne2);
  646. GGML_API struct ggml_tensor * ggml_new_tensor_4d(
  647. struct ggml_context * ctx,
  648. enum ggml_type type,
  649. int64_t ne0,
  650. int64_t ne1,
  651. int64_t ne2,
  652. int64_t ne3);
  653. GGML_API void * ggml_new_buffer(struct ggml_context * ctx, size_t nbytes);
  654. GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
  655. GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, struct ggml_tensor * src);
  656. // Context tensor enumeration and lookup
  657. GGML_API struct ggml_tensor * ggml_get_first_tensor(const struct ggml_context * ctx);
  658. GGML_API struct ggml_tensor * ggml_get_next_tensor (const struct ggml_context * ctx, struct ggml_tensor * tensor);
  659. GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
  660. // Converts a flat index into coordinates
  661. GGML_API void ggml_unravel_index(const struct ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
  662. GGML_API enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor);
  663. GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
  664. GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
  665. GGML_API const char * ggml_get_name (const struct ggml_tensor * tensor);
  666. GGML_API struct ggml_tensor * ggml_set_name ( struct ggml_tensor * tensor, const char * name);
  667. GGML_ATTRIBUTE_FORMAT(2, 3)
  668. GGML_API struct ggml_tensor * ggml_format_name( struct ggml_tensor * tensor, const char * fmt, ...);
  669. // Tensor flags
  670. GGML_API void ggml_set_input(struct ggml_tensor * tensor);
  671. GGML_API void ggml_set_output(struct ggml_tensor * tensor);
  672. GGML_API void ggml_set_param(struct ggml_context * ctx, struct ggml_tensor * tensor);
  673. GGML_API void ggml_set_loss(struct ggml_tensor * tensor);
  674. //
  675. // operations on tensors with backpropagation
  676. //
  677. GGML_API struct ggml_tensor * ggml_dup(
  678. struct ggml_context * ctx,
  679. struct ggml_tensor * a);
  680. // in-place, returns view(a)
  681. GGML_API struct ggml_tensor * ggml_dup_inplace(
  682. struct ggml_context * ctx,
  683. struct ggml_tensor * a);
  684. GGML_API struct ggml_tensor * ggml_add(
  685. struct ggml_context * ctx,
  686. struct ggml_tensor * a,
  687. struct ggml_tensor * b);
  688. GGML_API struct ggml_tensor * ggml_add_inplace(
  689. struct ggml_context * ctx,
  690. struct ggml_tensor * a,
  691. struct ggml_tensor * b);
  692. GGML_API struct ggml_tensor * ggml_add_cast(
  693. struct ggml_context * ctx,
  694. struct ggml_tensor * a,
  695. struct ggml_tensor * b,
  696. enum ggml_type type);
  697. GGML_API struct ggml_tensor * ggml_add1(
  698. struct ggml_context * ctx,
  699. struct ggml_tensor * a,
  700. struct ggml_tensor * b);
  701. GGML_API struct ggml_tensor * ggml_add1_inplace(
  702. struct ggml_context * ctx,
  703. struct ggml_tensor * a,
  704. struct ggml_tensor * b);
  705. // dst = a
  706. // view(dst, nb1, nb2, nb3, offset) += b
  707. // return dst
  708. GGML_API struct ggml_tensor * ggml_acc(
  709. struct ggml_context * ctx,
  710. struct ggml_tensor * a,
  711. struct ggml_tensor * b,
  712. size_t nb1,
  713. size_t nb2,
  714. size_t nb3,
  715. size_t offset);
  716. GGML_API struct ggml_tensor * ggml_acc_inplace(
  717. struct ggml_context * ctx,
  718. struct ggml_tensor * a,
  719. struct ggml_tensor * b,
  720. size_t nb1,
  721. size_t nb2,
  722. size_t nb3,
  723. size_t offset);
  724. GGML_API struct ggml_tensor * ggml_sub(
  725. struct ggml_context * ctx,
  726. struct ggml_tensor * a,
  727. struct ggml_tensor * b);
  728. GGML_API struct ggml_tensor * ggml_sub_inplace(
  729. struct ggml_context * ctx,
  730. struct ggml_tensor * a,
  731. struct ggml_tensor * b);
  732. GGML_API struct ggml_tensor * ggml_mul(
  733. struct ggml_context * ctx,
  734. struct ggml_tensor * a,
  735. struct ggml_tensor * b);
  736. GGML_API struct ggml_tensor * ggml_mul_inplace(
  737. struct ggml_context * ctx,
  738. struct ggml_tensor * a,
  739. struct ggml_tensor * b);
  740. GGML_API struct ggml_tensor * ggml_div(
  741. struct ggml_context * ctx,
  742. struct ggml_tensor * a,
  743. struct ggml_tensor * b);
  744. GGML_API struct ggml_tensor * ggml_div_inplace(
  745. struct ggml_context * ctx,
  746. struct ggml_tensor * a,
  747. struct ggml_tensor * b);
  748. GGML_API struct ggml_tensor * ggml_sqr(
  749. struct ggml_context * ctx,
  750. struct ggml_tensor * a);
  751. GGML_API struct ggml_tensor * ggml_sqr_inplace(
  752. struct ggml_context * ctx,
  753. struct ggml_tensor * a);
  754. GGML_API struct ggml_tensor * ggml_sqrt(
  755. struct ggml_context * ctx,
  756. struct ggml_tensor * a);
  757. GGML_API struct ggml_tensor * ggml_sqrt_inplace(
  758. struct ggml_context * ctx,
  759. struct ggml_tensor * a);
  760. GGML_API struct ggml_tensor * ggml_log(
  761. struct ggml_context * ctx,
  762. struct ggml_tensor * a);
  763. GGML_API struct ggml_tensor * ggml_log_inplace(
  764. struct ggml_context * ctx,
  765. struct ggml_tensor * a);
  766. GGML_API struct ggml_tensor * ggml_sin(
  767. struct ggml_context * ctx,
  768. struct ggml_tensor * a);
  769. GGML_API struct ggml_tensor * ggml_sin_inplace(
  770. struct ggml_context * ctx,
  771. struct ggml_tensor * a);
  772. GGML_API struct ggml_tensor * ggml_cos(
  773. struct ggml_context * ctx,
  774. struct ggml_tensor * a);
  775. GGML_API struct ggml_tensor * ggml_cos_inplace(
  776. struct ggml_context * ctx,
  777. struct ggml_tensor * a);
  778. // return scalar
  779. GGML_API struct ggml_tensor * ggml_sum(
  780. struct ggml_context * ctx,
  781. struct ggml_tensor * a);
  782. // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
  783. GGML_API struct ggml_tensor * ggml_sum_rows(
  784. struct ggml_context * ctx,
  785. struct ggml_tensor * a);
  786. // mean along rows
  787. GGML_API struct ggml_tensor * ggml_mean(
  788. struct ggml_context * ctx,
  789. struct ggml_tensor * a);
  790. // argmax along rows
  791. GGML_API struct ggml_tensor * ggml_argmax(
  792. struct ggml_context * ctx,
  793. struct ggml_tensor * a);
  794. // count number of equal elements in a and b
  795. GGML_API struct ggml_tensor * ggml_count_equal(
  796. struct ggml_context * ctx,
  797. struct ggml_tensor * a,
  798. struct ggml_tensor * b);
  799. // if a is the same shape as b, and a is not parameter, return a
  800. // otherwise, return a new tensor: repeat(a) to fit in b
  801. GGML_API struct ggml_tensor * ggml_repeat(
  802. struct ggml_context * ctx,
  803. struct ggml_tensor * a,
  804. struct ggml_tensor * b);
  805. // sums repetitions in a into shape of b
  806. GGML_API struct ggml_tensor * ggml_repeat_back(
  807. struct ggml_context * ctx,
  808. struct ggml_tensor * a,
  809. struct ggml_tensor * b);
  810. // concat a and b along dim
  811. // used in stable-diffusion
  812. GGML_API struct ggml_tensor * ggml_concat(
  813. struct ggml_context * ctx,
  814. struct ggml_tensor * a,
  815. struct ggml_tensor * b,
  816. int dim);
  817. GGML_API struct ggml_tensor * ggml_abs(
  818. struct ggml_context * ctx,
  819. struct ggml_tensor * a);
  820. GGML_API struct ggml_tensor * ggml_abs_inplace(
  821. struct ggml_context * ctx,
  822. struct ggml_tensor * a);
  823. GGML_API struct ggml_tensor * ggml_sgn(
  824. struct ggml_context * ctx,
  825. struct ggml_tensor * a);
  826. GGML_API struct ggml_tensor * ggml_sgn_inplace(
  827. struct ggml_context * ctx,
  828. struct ggml_tensor * a);
  829. GGML_API struct ggml_tensor * ggml_neg(
  830. struct ggml_context * ctx,
  831. struct ggml_tensor * a);
  832. GGML_API struct ggml_tensor * ggml_neg_inplace(
  833. struct ggml_context * ctx,
  834. struct ggml_tensor * a);
  835. GGML_API struct ggml_tensor * ggml_step(
  836. struct ggml_context * ctx,
  837. struct ggml_tensor * a);
  838. GGML_API struct ggml_tensor * ggml_step_inplace(
  839. struct ggml_context * ctx,
  840. struct ggml_tensor * a);
  841. GGML_API struct ggml_tensor * ggml_tanh(
  842. struct ggml_context * ctx,
  843. struct ggml_tensor * a);
  844. GGML_API struct ggml_tensor * ggml_tanh_inplace(
  845. struct ggml_context * ctx,
  846. struct ggml_tensor * a);
  847. GGML_API struct ggml_tensor * ggml_elu(
  848. struct ggml_context * ctx,
  849. struct ggml_tensor * a);
  850. GGML_API struct ggml_tensor * ggml_elu_inplace(
  851. struct ggml_context * ctx,
  852. struct ggml_tensor * a);
  853. GGML_API struct ggml_tensor * ggml_relu(
  854. struct ggml_context * ctx,
  855. struct ggml_tensor * a);
  856. GGML_API struct ggml_tensor * ggml_leaky_relu(
  857. struct ggml_context * ctx,
  858. struct ggml_tensor * a, float negative_slope, bool inplace);
  859. GGML_API struct ggml_tensor * ggml_relu_inplace(
  860. struct ggml_context * ctx,
  861. struct ggml_tensor * a);
  862. GGML_API struct ggml_tensor * ggml_sigmoid(
  863. struct ggml_context * ctx,
  864. struct ggml_tensor * a);
  865. GGML_API struct ggml_tensor * ggml_sigmoid_inplace(
  866. struct ggml_context * ctx,
  867. struct ggml_tensor * a);
  868. GGML_API struct ggml_tensor * ggml_gelu(
  869. struct ggml_context * ctx,
  870. struct ggml_tensor * a);
  871. GGML_API struct ggml_tensor * ggml_gelu_inplace(
  872. struct ggml_context * ctx,
  873. struct ggml_tensor * a);
  874. GGML_API struct ggml_tensor * ggml_gelu_quick(
  875. struct ggml_context * ctx,
  876. struct ggml_tensor * a);
  877. GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
  878. struct ggml_context * ctx,
  879. struct ggml_tensor * a);
  880. GGML_API struct ggml_tensor * ggml_silu(
  881. struct ggml_context * ctx,
  882. struct ggml_tensor * a);
  883. GGML_API struct ggml_tensor * ggml_silu_inplace(
  884. struct ggml_context * ctx,
  885. struct ggml_tensor * a);
  886. // a - x
  887. // b - dy
  888. GGML_API struct ggml_tensor * ggml_silu_back(
  889. struct ggml_context * ctx,
  890. struct ggml_tensor * a,
  891. struct ggml_tensor * b);
  892. // hardswish(x) = x * relu6(x + 3) / 6
  893. GGML_API struct ggml_tensor * ggml_hardswish(
  894. struct ggml_context * ctx,
  895. struct ggml_tensor * a);
  896. // hardsigmoid(x) = relu6(x + 3) / 6
  897. GGML_API struct ggml_tensor * ggml_hardsigmoid(
  898. struct ggml_context * ctx,
  899. struct ggml_tensor * a);
  900. GGML_API struct ggml_tensor * ggml_exp(
  901. struct ggml_context * ctx,
  902. struct ggml_tensor * a);
  903. GGML_API struct ggml_tensor * ggml_exp_inplace(
  904. struct ggml_context * ctx,
  905. struct ggml_tensor * a);
  906. // normalize along rows
  907. GGML_API struct ggml_tensor * ggml_norm(
  908. struct ggml_context * ctx,
  909. struct ggml_tensor * a,
  910. float eps);
  911. GGML_API struct ggml_tensor * ggml_norm_inplace(
  912. struct ggml_context * ctx,
  913. struct ggml_tensor * a,
  914. float eps);
  915. GGML_API struct ggml_tensor * ggml_rms_norm(
  916. struct ggml_context * ctx,
  917. struct ggml_tensor * a,
  918. float eps);
  919. GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
  920. struct ggml_context * ctx,
  921. struct ggml_tensor * a,
  922. float eps);
  923. // group normalize along ne0*ne1*n_groups
  924. // used in stable-diffusion
  925. GGML_API struct ggml_tensor * ggml_group_norm(
  926. struct ggml_context * ctx,
  927. struct ggml_tensor * a,
  928. int n_groups,
  929. float eps);
  930. GGML_API struct ggml_tensor * ggml_group_norm_inplace(
  931. struct ggml_context * ctx,
  932. struct ggml_tensor * a,
  933. int n_groups,
  934. float eps);
  935. // a - x
  936. // b - dy
  937. GGML_API struct ggml_tensor * ggml_rms_norm_back(
  938. struct ggml_context * ctx,
  939. struct ggml_tensor * a,
  940. struct ggml_tensor * b,
  941. float eps);
  942. // A: k columns, n rows => [ne03, ne02, n, k]
  943. // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
  944. // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
  945. GGML_API struct ggml_tensor * ggml_mul_mat(
  946. struct ggml_context * ctx,
  947. struct ggml_tensor * a,
  948. struct ggml_tensor * b);
  949. // change the precision of a matrix multiplication
  950. // set to GGML_PREC_F32 for higher precision (useful for phi-2)
  951. GGML_API void ggml_mul_mat_set_prec(
  952. struct ggml_tensor * a,
  953. enum ggml_prec prec);
  954. // indirect matrix multiplication
  955. GGML_API struct ggml_tensor * ggml_mul_mat_id(
  956. struct ggml_context * ctx,
  957. struct ggml_tensor * as,
  958. struct ggml_tensor * b,
  959. struct ggml_tensor * ids);
  960. // A: m columns, n rows,
  961. // B: p columns, n rows,
  962. // result is m columns, p rows
  963. GGML_API struct ggml_tensor * ggml_out_prod(
  964. struct ggml_context * ctx,
  965. struct ggml_tensor * a,
  966. struct ggml_tensor * b);
  967. //
  968. // operations on tensors without backpropagation
  969. //
  970. GGML_API struct ggml_tensor * ggml_scale(
  971. struct ggml_context * ctx,
  972. struct ggml_tensor * a,
  973. float s);
  974. // in-place, returns view(a)
  975. GGML_API struct ggml_tensor * ggml_scale_inplace(
  976. struct ggml_context * ctx,
  977. struct ggml_tensor * a,
  978. float s);
  979. // b -> view(a,offset,nb1,nb2,3), return modified a
  980. GGML_API struct ggml_tensor * ggml_set(
  981. struct ggml_context * ctx,
  982. struct ggml_tensor * a,
  983. struct ggml_tensor * b,
  984. size_t nb1,
  985. size_t nb2,
  986. size_t nb3,
  987. size_t offset); // in bytes
  988. // b -> view(a,offset,nb1,nb2,3), return view(a)
  989. GGML_API struct ggml_tensor * ggml_set_inplace(
  990. struct ggml_context * ctx,
  991. struct ggml_tensor * a,
  992. struct ggml_tensor * b,
  993. size_t nb1,
  994. size_t nb2,
  995. size_t nb3,
  996. size_t offset); // in bytes
  997. GGML_API struct ggml_tensor * ggml_set_1d(
  998. struct ggml_context * ctx,
  999. struct ggml_tensor * a,
  1000. struct ggml_tensor * b,
  1001. size_t offset); // in bytes
  1002. GGML_API struct ggml_tensor * ggml_set_1d_inplace(
  1003. struct ggml_context * ctx,
  1004. struct ggml_tensor * a,
  1005. struct ggml_tensor * b,
  1006. size_t offset); // in bytes
  1007. // b -> view(a,offset,nb1,nb2,3), return modified a
  1008. GGML_API struct ggml_tensor * ggml_set_2d(
  1009. struct ggml_context * ctx,
  1010. struct ggml_tensor * a,
  1011. struct ggml_tensor * b,
  1012. size_t nb1,
  1013. size_t offset); // in bytes
  1014. // b -> view(a,offset,nb1,nb2,3), return view(a)
  1015. GGML_API struct ggml_tensor * ggml_set_2d_inplace(
  1016. struct ggml_context * ctx,
  1017. struct ggml_tensor * a,
  1018. struct ggml_tensor * b,
  1019. size_t nb1,
  1020. size_t offset); // in bytes
  1021. // a -> b, return view(b)
  1022. GGML_API struct ggml_tensor * ggml_cpy(
  1023. struct ggml_context * ctx,
  1024. struct ggml_tensor * a,
  1025. struct ggml_tensor * b);
  1026. GGML_API struct ggml_tensor * ggml_cast(
  1027. struct ggml_context * ctx,
  1028. struct ggml_tensor * a,
  1029. enum ggml_type type);
  1030. // make contiguous
  1031. GGML_API struct ggml_tensor * ggml_cont(
  1032. struct ggml_context * ctx,
  1033. struct ggml_tensor * a);
  1034. // make contiguous, with new shape
  1035. GGML_API struct ggml_tensor * ggml_cont_1d(
  1036. struct ggml_context * ctx,
  1037. struct ggml_tensor * a,
  1038. int64_t ne0);
  1039. GGML_API struct ggml_tensor * ggml_cont_2d(
  1040. struct ggml_context * ctx,
  1041. struct ggml_tensor * a,
  1042. int64_t ne0,
  1043. int64_t ne1);
  1044. GGML_API struct ggml_tensor * ggml_cont_3d(
  1045. struct ggml_context * ctx,
  1046. struct ggml_tensor * a,
  1047. int64_t ne0,
  1048. int64_t ne1,
  1049. int64_t ne2);
  1050. GGML_API struct ggml_tensor * ggml_cont_4d(
  1051. struct ggml_context * ctx,
  1052. struct ggml_tensor * a,
  1053. int64_t ne0,
  1054. int64_t ne1,
  1055. int64_t ne2,
  1056. int64_t ne3);
  1057. // return view(a), b specifies the new shape
  1058. // TODO: when we start computing gradient, make a copy instead of view
  1059. GGML_API struct ggml_tensor * ggml_reshape(
  1060. struct ggml_context * ctx,
  1061. struct ggml_tensor * a,
  1062. struct ggml_tensor * b);
  1063. // return view(a)
  1064. // TODO: when we start computing gradient, make a copy instead of view
  1065. GGML_API struct ggml_tensor * ggml_reshape_1d(
  1066. struct ggml_context * ctx,
  1067. struct ggml_tensor * a,
  1068. int64_t ne0);
  1069. GGML_API struct ggml_tensor * ggml_reshape_2d(
  1070. struct ggml_context * ctx,
  1071. struct ggml_tensor * a,
  1072. int64_t ne0,
  1073. int64_t ne1);
  1074. // return view(a)
  1075. // TODO: when we start computing gradient, make a copy instead of view
  1076. GGML_API struct ggml_tensor * ggml_reshape_3d(
  1077. struct ggml_context * ctx,
  1078. struct ggml_tensor * a,
  1079. int64_t ne0,
  1080. int64_t ne1,
  1081. int64_t ne2);
  1082. GGML_API struct ggml_tensor * ggml_reshape_4d(
  1083. struct ggml_context * ctx,
  1084. struct ggml_tensor * a,
  1085. int64_t ne0,
  1086. int64_t ne1,
  1087. int64_t ne2,
  1088. int64_t ne3);
  1089. // offset in bytes
  1090. GGML_API struct ggml_tensor * ggml_view_1d(
  1091. struct ggml_context * ctx,
  1092. struct ggml_tensor * a,
  1093. int64_t ne0,
  1094. size_t offset);
  1095. GGML_API struct ggml_tensor * ggml_view_2d(
  1096. struct ggml_context * ctx,
  1097. struct ggml_tensor * a,
  1098. int64_t ne0,
  1099. int64_t ne1,
  1100. size_t nb1, // row stride in bytes
  1101. size_t offset);
  1102. GGML_API struct ggml_tensor * ggml_view_3d(
  1103. struct ggml_context * ctx,
  1104. struct ggml_tensor * a,
  1105. int64_t ne0,
  1106. int64_t ne1,
  1107. int64_t ne2,
  1108. size_t nb1, // row stride in bytes
  1109. size_t nb2, // slice stride in bytes
  1110. size_t offset);
  1111. GGML_API struct ggml_tensor * ggml_view_4d(
  1112. struct ggml_context * ctx,
  1113. struct ggml_tensor * a,
  1114. int64_t ne0,
  1115. int64_t ne1,
  1116. int64_t ne2,
  1117. int64_t ne3,
  1118. size_t nb1, // row stride in bytes
  1119. size_t nb2, // slice stride in bytes
  1120. size_t nb3,
  1121. size_t offset);
  1122. GGML_API struct ggml_tensor * ggml_permute(
  1123. struct ggml_context * ctx,
  1124. struct ggml_tensor * a,
  1125. int axis0,
  1126. int axis1,
  1127. int axis2,
  1128. int axis3);
  1129. // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
  1130. GGML_API struct ggml_tensor * ggml_transpose(
  1131. struct ggml_context * ctx,
  1132. struct ggml_tensor * a);
  1133. // supports 3D: a->ne[2] == b->ne[1]
  1134. GGML_API struct ggml_tensor * ggml_get_rows(
  1135. struct ggml_context * ctx,
  1136. struct ggml_tensor * a, // data
  1137. struct ggml_tensor * b); // row indices
  1138. GGML_API struct ggml_tensor * ggml_get_rows_back(
  1139. struct ggml_context * ctx,
  1140. struct ggml_tensor * a, // gradients of ggml_get_rows result
  1141. struct ggml_tensor * b, // row indices
  1142. struct ggml_tensor * c); // data for ggml_get_rows, only used for its shape
  1143. GGML_API struct ggml_tensor * ggml_diag(
  1144. struct ggml_context * ctx,
  1145. struct ggml_tensor * a);
  1146. // set elements above the diagonal to -INF
  1147. GGML_API struct ggml_tensor * ggml_diag_mask_inf(
  1148. struct ggml_context * ctx,
  1149. struct ggml_tensor * a,
  1150. int n_past);
  1151. // in-place, returns view(a)
  1152. GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
  1153. struct ggml_context * ctx,
  1154. struct ggml_tensor * a,
  1155. int n_past);
  1156. // set elements above the diagonal to 0
  1157. GGML_API struct ggml_tensor * ggml_diag_mask_zero(
  1158. struct ggml_context * ctx,
  1159. struct ggml_tensor * a,
  1160. int n_past);
  1161. // in-place, returns view(a)
  1162. GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
  1163. struct ggml_context * ctx,
  1164. struct ggml_tensor * a,
  1165. int n_past);
  1166. GGML_API struct ggml_tensor * ggml_soft_max(
  1167. struct ggml_context * ctx,
  1168. struct ggml_tensor * a);
  1169. // in-place, returns view(a)
  1170. GGML_API struct ggml_tensor * ggml_soft_max_inplace(
  1171. struct ggml_context * ctx,
  1172. struct ggml_tensor * a);
  1173. // fused soft_max(a*scale + mask*(ALiBi slope))
  1174. // mask is optional
  1175. // max_bias = 0.0f for no ALiBi
  1176. GGML_API struct ggml_tensor * ggml_soft_max_ext(
  1177. struct ggml_context * ctx,
  1178. struct ggml_tensor * a,
  1179. struct ggml_tensor * mask,
  1180. float scale,
  1181. float max_bias);
  1182. GGML_API struct ggml_tensor * ggml_soft_max_back(
  1183. struct ggml_context * ctx,
  1184. struct ggml_tensor * a,
  1185. struct ggml_tensor * b);
  1186. // in-place, returns view(a)
  1187. GGML_API struct ggml_tensor * ggml_soft_max_back_inplace(
  1188. struct ggml_context * ctx,
  1189. struct ggml_tensor * a,
  1190. struct ggml_tensor * b);
  1191. // rotary position embedding
  1192. // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
  1193. // if (mode & GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
  1194. //
  1195. // b is an int32 vector with size a->ne[2], it contains the positions
  1196. GGML_API struct ggml_tensor * ggml_rope(
  1197. struct ggml_context * ctx,
  1198. struct ggml_tensor * a,
  1199. struct ggml_tensor * b,
  1200. int n_dims,
  1201. int mode);
  1202. // in-place, returns view(a)
  1203. GGML_API struct ggml_tensor * ggml_rope_inplace(
  1204. struct ggml_context * ctx,
  1205. struct ggml_tensor * a,
  1206. struct ggml_tensor * b,
  1207. int n_dims,
  1208. int mode);
  1209. // custom RoPE
  1210. // c is freq factors (e.g. phi3-128k), (optional)
  1211. GGML_API struct ggml_tensor * ggml_rope_ext(
  1212. struct ggml_context * ctx,
  1213. struct ggml_tensor * a,
  1214. struct ggml_tensor * b,
  1215. struct ggml_tensor * c,
  1216. int n_dims,
  1217. int mode,
  1218. int n_ctx_orig,
  1219. float freq_base,
  1220. float freq_scale,
  1221. float ext_factor,
  1222. float attn_factor,
  1223. float beta_fast,
  1224. float beta_slow);
  1225. // in-place, returns view(a)
  1226. GGML_API struct ggml_tensor * ggml_rope_ext_inplace(
  1227. struct ggml_context * ctx,
  1228. struct ggml_tensor * a,
  1229. struct ggml_tensor * b,
  1230. struct ggml_tensor * c,
  1231. int n_dims,
  1232. int mode,
  1233. int n_ctx_orig,
  1234. float freq_base,
  1235. float freq_scale,
  1236. float ext_factor,
  1237. float attn_factor,
  1238. float beta_fast,
  1239. float beta_slow);
  1240. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom(
  1241. struct ggml_context * ctx,
  1242. struct ggml_tensor * a,
  1243. struct ggml_tensor * b,
  1244. int n_dims,
  1245. int mode,
  1246. int n_ctx_orig,
  1247. float freq_base,
  1248. float freq_scale,
  1249. float ext_factor,
  1250. float attn_factor,
  1251. float beta_fast,
  1252. float beta_slow),
  1253. "use ggml_rope_ext instead");
  1254. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
  1255. struct ggml_context * ctx,
  1256. struct ggml_tensor * a,
  1257. struct ggml_tensor * b,
  1258. int n_dims,
  1259. int mode,
  1260. int n_ctx_orig,
  1261. float freq_base,
  1262. float freq_scale,
  1263. float ext_factor,
  1264. float attn_factor,
  1265. float beta_fast,
  1266. float beta_slow),
  1267. "use ggml_rope_ext_inplace instead");
  1268. // compute correction dims for YaRN RoPE scaling
  1269. GGML_API void ggml_rope_yarn_corr_dims(
  1270. int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
  1271. // rotary position embedding backward, i.e compute dx from dy
  1272. // a - dy
  1273. GGML_API struct ggml_tensor * ggml_rope_back(
  1274. struct ggml_context * ctx,
  1275. struct ggml_tensor * a, // gradients of ggml_rope result
  1276. struct ggml_tensor * b, // positions
  1277. struct ggml_tensor * c, // freq factors
  1278. int n_dims,
  1279. int mode,
  1280. int n_ctx_orig,
  1281. float freq_base,
  1282. float freq_scale,
  1283. float ext_factor,
  1284. float attn_factor,
  1285. float beta_fast,
  1286. float beta_slow);
  1287. // clamp
  1288. // in-place, returns view(a)
  1289. GGML_API struct ggml_tensor * ggml_clamp(
  1290. struct ggml_context * ctx,
  1291. struct ggml_tensor * a,
  1292. float min,
  1293. float max);
  1294. // im2col
  1295. // converts data into a format that effectively results in a convolution when combined with matrix multiplication
  1296. GGML_API struct ggml_tensor * ggml_im2col(
  1297. struct ggml_context * ctx,
  1298. struct ggml_tensor * a, // convolution kernel
  1299. struct ggml_tensor * b, // data
  1300. int s0, // stride dimension 0
  1301. int s1, // stride dimension 1
  1302. int p0, // padding dimension 0
  1303. int p1, // padding dimension 1
  1304. int d0, // dilation dimension 0
  1305. int d1, // dilation dimension 1
  1306. bool is_2D,
  1307. enum ggml_type dst_type);
  1308. GGML_API struct ggml_tensor * ggml_im2col_back(
  1309. struct ggml_context * ctx,
  1310. struct ggml_tensor * a, // convolution kernel
  1311. struct ggml_tensor * b, // gradient of im2col output
  1312. int64_t * ne, // shape of im2col input
  1313. int s0, // stride dimension 0
  1314. int s1, // stride dimension 1
  1315. int p0, // padding dimension 0
  1316. int p1, // padding dimension 1
  1317. int d0, // dilation dimension 0
  1318. int d1, // dilation dimension 1
  1319. bool is_2D);
  1320. GGML_API struct ggml_tensor * ggml_conv_depthwise_2d(
  1321. struct ggml_context * ctx,
  1322. struct ggml_tensor * a, // convolution kernel
  1323. struct ggml_tensor * b, // data
  1324. int s0, // stride dimension 0
  1325. int s1, // stride dimension 1
  1326. int p0, // padding dimension 0
  1327. int p1, // padding dimension 1
  1328. int d0, // dilation dimension 0
  1329. int d1); // dilation dimension 1
  1330. GGML_API struct ggml_tensor * ggml_conv_1d(
  1331. struct ggml_context * ctx,
  1332. struct ggml_tensor * a, // convolution kernel
  1333. struct ggml_tensor * b, // data
  1334. int s0, // stride
  1335. int p0, // padding
  1336. int d0); // dilation
  1337. // conv_1d with padding = half
  1338. // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
  1339. GGML_API struct ggml_tensor* ggml_conv_1d_ph(
  1340. struct ggml_context * ctx,
  1341. struct ggml_tensor * a, // convolution kernel
  1342. struct ggml_tensor * b, // data
  1343. int s, // stride
  1344. int d); // dilation
  1345. GGML_API struct ggml_tensor * ggml_conv_transpose_1d(
  1346. struct ggml_context * ctx,
  1347. struct ggml_tensor * a, // convolution kernel
  1348. struct ggml_tensor * b, // data
  1349. int s0, // stride
  1350. int p0, // padding
  1351. int d0); // dilation
  1352. GGML_API struct ggml_tensor * ggml_conv_2d(
  1353. struct ggml_context * ctx,
  1354. struct ggml_tensor * a, // convolution kernel
  1355. struct ggml_tensor * b, // data
  1356. int s0, // stride dimension 0
  1357. int s1, // stride dimension 1
  1358. int p0, // padding dimension 0
  1359. int p1, // padding dimension 1
  1360. int d0, // dilation dimension 0
  1361. int d1); // dilation dimension 1
  1362. // kernel size is a->ne[0] x a->ne[1]
  1363. // stride is equal to kernel size
  1364. // padding is zero
  1365. // example:
  1366. // a: 16 16 3 768
  1367. // b: 1024 1024 3 1
  1368. // res: 64 64 768 1
  1369. // used in sam
  1370. GGML_API struct ggml_tensor * ggml_conv_2d_sk_p0(
  1371. struct ggml_context * ctx,
  1372. struct ggml_tensor * a,
  1373. struct ggml_tensor * b);
  1374. // kernel size is a->ne[0] x a->ne[1]
  1375. // stride is 1
  1376. // padding is half
  1377. // example:
  1378. // a: 3 3 256 256
  1379. // b: 64 64 256 1
  1380. // res: 64 64 256 1
  1381. // used in sam
  1382. GGML_API struct ggml_tensor * ggml_conv_2d_s1_ph(
  1383. struct ggml_context * ctx,
  1384. struct ggml_tensor * a,
  1385. struct ggml_tensor * b);
  1386. GGML_API struct ggml_tensor * ggml_conv_transpose_2d_p0(
  1387. struct ggml_context * ctx,
  1388. struct ggml_tensor * a,
  1389. struct ggml_tensor * b,
  1390. int stride);
  1391. enum ggml_op_pool {
  1392. GGML_OP_POOL_MAX,
  1393. GGML_OP_POOL_AVG,
  1394. GGML_OP_POOL_COUNT,
  1395. };
  1396. GGML_API struct ggml_tensor * ggml_pool_1d(
  1397. struct ggml_context * ctx,
  1398. struct ggml_tensor * a,
  1399. enum ggml_op_pool op,
  1400. int k0, // kernel size
  1401. int s0, // stride
  1402. int p0); // padding
  1403. // the result will have 2*p0 padding for the first dimension
  1404. // and 2*p1 padding for the second dimension
  1405. GGML_API struct ggml_tensor * ggml_pool_2d(
  1406. struct ggml_context * ctx,
  1407. struct ggml_tensor * a,
  1408. enum ggml_op_pool op,
  1409. int k0,
  1410. int k1,
  1411. int s0,
  1412. int s1,
  1413. float p0,
  1414. float p1);
  1415. GGML_API struct ggml_tensor * ggml_pool_2d_back(
  1416. struct ggml_context * ctx,
  1417. struct ggml_tensor * a,
  1418. struct ggml_tensor * af, // "a"/input used in forward pass
  1419. enum ggml_op_pool op,
  1420. int k0,
  1421. int k1,
  1422. int s0,
  1423. int s1,
  1424. float p0,
  1425. float p1);
  1426. // nearest interpolate
  1427. // multiplies ne0 and ne1 by scale factor
  1428. // used in stable-diffusion
  1429. GGML_API struct ggml_tensor * ggml_upscale(
  1430. struct ggml_context * ctx,
  1431. struct ggml_tensor * a,
  1432. int scale_factor);
  1433. // nearest interpolate
  1434. // nearest interpolate to specified dimensions
  1435. // used in tortoise.cpp
  1436. GGML_API struct ggml_tensor * ggml_upscale_ext(
  1437. struct ggml_context * ctx,
  1438. struct ggml_tensor * a,
  1439. int ne0,
  1440. int ne1,
  1441. int ne2,
  1442. int ne3);
  1443. // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
  1444. GGML_API struct ggml_tensor * ggml_pad(
  1445. struct ggml_context * ctx,
  1446. struct ggml_tensor * a,
  1447. int p0,
  1448. int p1,
  1449. int p2,
  1450. int p3);
  1451. // pad each dimension with reflection: [a, b, c, d] -> [b, a, b, c, d, c]
  1452. GGML_API struct ggml_tensor * ggml_pad_reflect_1d(
  1453. struct ggml_context * ctx,
  1454. struct ggml_tensor * a,
  1455. int p0,
  1456. int p1);
  1457. // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
  1458. // timesteps: [N,]
  1459. // return: [N, dim]
  1460. GGML_API struct ggml_tensor * ggml_timestep_embedding(
  1461. struct ggml_context * ctx,
  1462. struct ggml_tensor * timesteps,
  1463. int dim,
  1464. int max_period);
  1465. // sort rows
  1466. enum ggml_sort_order {
  1467. GGML_SORT_ORDER_ASC,
  1468. GGML_SORT_ORDER_DESC,
  1469. };
  1470. GGML_API struct ggml_tensor * ggml_argsort(
  1471. struct ggml_context * ctx,
  1472. struct ggml_tensor * a,
  1473. enum ggml_sort_order order);
  1474. GGML_API struct ggml_tensor * ggml_arange(
  1475. struct ggml_context * ctx,
  1476. float start,
  1477. float stop,
  1478. float step);
  1479. // top k elements per row
  1480. GGML_API struct ggml_tensor * ggml_top_k(
  1481. struct ggml_context * ctx,
  1482. struct ggml_tensor * a,
  1483. int k);
  1484. #define GGML_KQ_MASK_PAD 32
  1485. // q: [n_embd, n_batch, n_head, 1]
  1486. // k: [n_embd, n_kv, n_head_kv, 1]
  1487. // v: [n_embd, n_kv, n_head_kv, 1] !! not transposed !!
  1488. // mask: [n_kv, n_batch_pad, 1, 1] !! n_batch_pad = GGML_PAD(n_batch, GGML_KQ_MASK_PAD) !!
  1489. // res: [n_embd, n_head, n_batch, 1] !! permuted !!
  1490. GGML_API struct ggml_tensor * ggml_flash_attn_ext(
  1491. struct ggml_context * ctx,
  1492. struct ggml_tensor * q,
  1493. struct ggml_tensor * k,
  1494. struct ggml_tensor * v,
  1495. struct ggml_tensor * mask,
  1496. float scale,
  1497. float max_bias,
  1498. float logit_softcap);
  1499. GGML_API void ggml_flash_attn_ext_set_prec(
  1500. struct ggml_tensor * a,
  1501. enum ggml_prec prec);
  1502. GGML_API enum ggml_prec ggml_flash_attn_ext_get_prec(
  1503. const struct ggml_tensor * a);
  1504. // TODO: needs to be adapted to ggml_flash_attn_ext
  1505. GGML_API struct ggml_tensor * ggml_flash_attn_back(
  1506. struct ggml_context * ctx,
  1507. struct ggml_tensor * q,
  1508. struct ggml_tensor * k,
  1509. struct ggml_tensor * v,
  1510. struct ggml_tensor * d,
  1511. bool masked);
  1512. GGML_API struct ggml_tensor * ggml_ssm_conv(
  1513. struct ggml_context * ctx,
  1514. struct ggml_tensor * sx,
  1515. struct ggml_tensor * c);
  1516. GGML_API struct ggml_tensor * ggml_ssm_scan(
  1517. struct ggml_context * ctx,
  1518. struct ggml_tensor * s,
  1519. struct ggml_tensor * x,
  1520. struct ggml_tensor * dt,
  1521. struct ggml_tensor * A,
  1522. struct ggml_tensor * B,
  1523. struct ggml_tensor * C);
  1524. // partition into non-overlapping windows with padding if needed
  1525. // example:
  1526. // a: 768 64 64 1
  1527. // w: 14
  1528. // res: 768 14 14 25
  1529. // used in sam
  1530. GGML_API struct ggml_tensor * ggml_win_part(
  1531. struct ggml_context * ctx,
  1532. struct ggml_tensor * a,
  1533. int w);
  1534. // reverse of ggml_win_part
  1535. // used in sam
  1536. GGML_API struct ggml_tensor * ggml_win_unpart(
  1537. struct ggml_context * ctx,
  1538. struct ggml_tensor * a,
  1539. int w0,
  1540. int h0,
  1541. int w);
  1542. GGML_API struct ggml_tensor * ggml_unary(
  1543. struct ggml_context * ctx,
  1544. struct ggml_tensor * a,
  1545. enum ggml_unary_op op);
  1546. GGML_API struct ggml_tensor * ggml_unary_inplace(
  1547. struct ggml_context * ctx,
  1548. struct ggml_tensor * a,
  1549. enum ggml_unary_op op);
  1550. // used in sam
  1551. GGML_API struct ggml_tensor * ggml_get_rel_pos(
  1552. struct ggml_context * ctx,
  1553. struct ggml_tensor * a,
  1554. int qh,
  1555. int kh);
  1556. // used in sam
  1557. GGML_API struct ggml_tensor * ggml_add_rel_pos(
  1558. struct ggml_context * ctx,
  1559. struct ggml_tensor * a,
  1560. struct ggml_tensor * pw,
  1561. struct ggml_tensor * ph);
  1562. GGML_API struct ggml_tensor * ggml_add_rel_pos_inplace(
  1563. struct ggml_context * ctx,
  1564. struct ggml_tensor * a,
  1565. struct ggml_tensor * pw,
  1566. struct ggml_tensor * ph);
  1567. GGML_API struct ggml_tensor * ggml_rwkv_wkv6(
  1568. struct ggml_context * ctx,
  1569. struct ggml_tensor * k,
  1570. struct ggml_tensor * v,
  1571. struct ggml_tensor * r,
  1572. struct ggml_tensor * tf,
  1573. struct ggml_tensor * td,
  1574. struct ggml_tensor * state);
  1575. // custom operators
  1576. typedef void (*ggml_unary_op_f32_t) (const int, float *, const float *);
  1577. typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
  1578. typedef void (*ggml_custom1_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *);
  1579. typedef void (*ggml_custom2_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
  1580. typedef void (*ggml_custom3_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
  1581. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_f32(
  1582. struct ggml_context * ctx,
  1583. struct ggml_tensor * a,
  1584. ggml_unary_op_f32_t fun),
  1585. "use ggml_map_custom1 instead");
  1586. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_inplace_f32(
  1587. struct ggml_context * ctx,
  1588. struct ggml_tensor * a,
  1589. ggml_unary_op_f32_t fun),
  1590. "use ggml_map_custom1_inplace instead");
  1591. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_f32(
  1592. struct ggml_context * ctx,
  1593. struct ggml_tensor * a,
  1594. struct ggml_tensor * b,
  1595. ggml_binary_op_f32_t fun),
  1596. "use ggml_map_custom2 instead");
  1597. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_inplace_f32(
  1598. struct ggml_context * ctx,
  1599. struct ggml_tensor * a,
  1600. struct ggml_tensor * b,
  1601. ggml_binary_op_f32_t fun),
  1602. "use ggml_map_custom2_inplace instead");
  1603. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_f32(
  1604. struct ggml_context * ctx,
  1605. struct ggml_tensor * a,
  1606. ggml_custom1_op_f32_t fun),
  1607. "use ggml_map_custom1 instead");
  1608. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_inplace_f32(
  1609. struct ggml_context * ctx,
  1610. struct ggml_tensor * a,
  1611. ggml_custom1_op_f32_t fun),
  1612. "use ggml_map_custom1_inplace instead");
  1613. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_f32(
  1614. struct ggml_context * ctx,
  1615. struct ggml_tensor * a,
  1616. struct ggml_tensor * b,
  1617. ggml_custom2_op_f32_t fun),
  1618. "use ggml_map_custom2 instead");
  1619. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_inplace_f32(
  1620. struct ggml_context * ctx,
  1621. struct ggml_tensor * a,
  1622. struct ggml_tensor * b,
  1623. ggml_custom2_op_f32_t fun),
  1624. "use ggml_map_custom2_inplace instead");
  1625. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_f32(
  1626. struct ggml_context * ctx,
  1627. struct ggml_tensor * a,
  1628. struct ggml_tensor * b,
  1629. struct ggml_tensor * c,
  1630. ggml_custom3_op_f32_t fun),
  1631. "use ggml_map_custom3 instead");
  1632. GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_inplace_f32(
  1633. struct ggml_context * ctx,
  1634. struct ggml_tensor * a,
  1635. struct ggml_tensor * b,
  1636. struct ggml_tensor * c,
  1637. ggml_custom3_op_f32_t fun),
  1638. "use ggml_map_custom3_inplace instead");
  1639. // custom operators v2
  1640. typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
  1641. 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);
  1642. 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);
  1643. #define GGML_N_TASKS_MAX (-1)
  1644. // n_tasks == GGML_N_TASKS_MAX means to use max number of tasks
  1645. GGML_API struct ggml_tensor * ggml_map_custom1(
  1646. struct ggml_context * ctx,
  1647. struct ggml_tensor * a,
  1648. ggml_custom1_op_t fun,
  1649. int n_tasks,
  1650. void * userdata);
  1651. GGML_API struct ggml_tensor * ggml_map_custom1_inplace(
  1652. struct ggml_context * ctx,
  1653. struct ggml_tensor * a,
  1654. ggml_custom1_op_t fun,
  1655. int n_tasks,
  1656. void * userdata);
  1657. GGML_API struct ggml_tensor * ggml_map_custom2(
  1658. struct ggml_context * ctx,
  1659. struct ggml_tensor * a,
  1660. struct ggml_tensor * b,
  1661. ggml_custom2_op_t fun,
  1662. int n_tasks,
  1663. void * userdata);
  1664. GGML_API struct ggml_tensor * ggml_map_custom2_inplace(
  1665. struct ggml_context * ctx,
  1666. struct ggml_tensor * a,
  1667. struct ggml_tensor * b,
  1668. ggml_custom2_op_t fun,
  1669. int n_tasks,
  1670. void * userdata);
  1671. GGML_API struct ggml_tensor * ggml_map_custom3(
  1672. struct ggml_context * ctx,
  1673. struct ggml_tensor * a,
  1674. struct ggml_tensor * b,
  1675. struct ggml_tensor * c,
  1676. ggml_custom3_op_t fun,
  1677. int n_tasks,
  1678. void * userdata);
  1679. GGML_API struct ggml_tensor * ggml_map_custom3_inplace(
  1680. struct ggml_context * ctx,
  1681. struct ggml_tensor * a,
  1682. struct ggml_tensor * b,
  1683. struct ggml_tensor * c,
  1684. ggml_custom3_op_t fun,
  1685. int n_tasks,
  1686. void * userdata);
  1687. // loss function
  1688. GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
  1689. struct ggml_context * ctx,
  1690. struct ggml_tensor * a, // logits
  1691. struct ggml_tensor * b); // labels
  1692. GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
  1693. struct ggml_context * ctx,
  1694. struct ggml_tensor * a, // logits
  1695. struct ggml_tensor * b, // labels
  1696. struct ggml_tensor * c); // gradients of cross_entropy_loss result
  1697. // AdamW optimizer step
  1698. // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
  1699. // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
  1700. GGML_API struct ggml_tensor * ggml_opt_step_adamw(
  1701. struct ggml_context * ctx,
  1702. struct ggml_tensor * a,
  1703. struct ggml_tensor * grad,
  1704. struct ggml_tensor * m,
  1705. struct ggml_tensor * v,
  1706. struct ggml_tensor * adamw_params); // parameters such a the learning rate
  1707. //
  1708. // automatic differentiation
  1709. //
  1710. GGML_API void ggml_build_forward_expand(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
  1711. GGML_API void ggml_build_backward_expand(
  1712. struct ggml_context * ctx_static, // context for static gradients (loss + gradient accumulation)
  1713. struct ggml_context * ctx_compute, // context for gradient computation
  1714. struct ggml_cgraph * cgraph,
  1715. bool accumulate); // whether or not gradients should be accumulated, requires static allocation of tensors in ctx_static
  1716. // graph allocation in a context
  1717. GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false
  1718. GGML_API struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads);
  1719. GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph);
  1720. GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst);
  1721. GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
  1722. GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph);
  1723. GGML_API int ggml_graph_size (struct ggml_cgraph * cgraph);
  1724. GGML_API struct ggml_tensor * ggml_graph_node (struct ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
  1725. GGML_API struct ggml_tensor ** ggml_graph_nodes (struct ggml_cgraph * cgraph);
  1726. GGML_API int ggml_graph_n_nodes(struct ggml_cgraph * cgraph);
  1727. GGML_API void ggml_graph_add_node(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
  1728. GGML_API size_t ggml_graph_overhead(void);
  1729. GGML_API size_t ggml_graph_overhead_custom(size_t size, bool grads);
  1730. GGML_API struct ggml_tensor * ggml_graph_get_tensor (const struct ggml_cgraph * cgraph, const char * name);
  1731. GGML_API struct ggml_tensor * ggml_graph_get_grad (const struct ggml_cgraph * cgraph, const struct ggml_tensor * node);
  1732. GGML_API struct ggml_tensor * ggml_graph_get_grad_acc(const struct ggml_cgraph * cgraph, const struct ggml_tensor * node);
  1733. GGML_API void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname);
  1734. GGML_API struct ggml_cgraph * ggml_graph_import(const char * fname, struct ggml_context ** ctx_data, struct ggml_context ** ctx_eval);
  1735. // print info and performance information for the graph
  1736. GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
  1737. // dump the graph into a file using the dot format
  1738. GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
  1739. // TODO these functions were sandwiched in the old optimization interface, is there a better place for them?
  1740. typedef void (*ggml_log_callback)(enum ggml_log_level level, const char * text, void * user_data);
  1741. // Set callback for all future logging events.
  1742. // If this is not called, or NULL is supplied, everything is output on stderr.
  1743. GGML_API void ggml_log_set(ggml_log_callback log_callback, void * user_data);
  1744. GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
  1745. //
  1746. // quantization
  1747. //
  1748. // - ggml_quantize_init can be called multiple times with the same type
  1749. // it will only initialize the quantization tables for the first call or after ggml_quantize_free
  1750. // automatically called by ggml_quantize_chunk for convenience
  1751. //
  1752. // - ggml_quantize_free will free any memory allocated by ggml_quantize_init
  1753. // call this at the end of the program to avoid memory leaks
  1754. //
  1755. // note: these are thread-safe
  1756. //
  1757. GGML_API void ggml_quantize_init(enum ggml_type type);
  1758. GGML_API void ggml_quantize_free(void);
  1759. // some quantization type cannot be used without an importance matrix
  1760. GGML_API bool ggml_quantize_requires_imatrix(enum ggml_type type);
  1761. // calls ggml_quantize_init internally (i.e. can allocate memory)
  1762. GGML_API size_t ggml_quantize_chunk(
  1763. enum ggml_type type,
  1764. const float * src,
  1765. void * dst,
  1766. int64_t start,
  1767. int64_t nrows,
  1768. int64_t n_per_row,
  1769. const float * imatrix);
  1770. //
  1771. // gguf
  1772. //
  1773. enum gguf_type {
  1774. GGUF_TYPE_UINT8 = 0,
  1775. GGUF_TYPE_INT8 = 1,
  1776. GGUF_TYPE_UINT16 = 2,
  1777. GGUF_TYPE_INT16 = 3,
  1778. GGUF_TYPE_UINT32 = 4,
  1779. GGUF_TYPE_INT32 = 5,
  1780. GGUF_TYPE_FLOAT32 = 6,
  1781. GGUF_TYPE_BOOL = 7,
  1782. GGUF_TYPE_STRING = 8,
  1783. GGUF_TYPE_ARRAY = 9,
  1784. GGUF_TYPE_UINT64 = 10,
  1785. GGUF_TYPE_INT64 = 11,
  1786. GGUF_TYPE_FLOAT64 = 12,
  1787. GGUF_TYPE_COUNT, // marks the end of the enum
  1788. };
  1789. struct gguf_context;
  1790. struct gguf_init_params {
  1791. bool no_alloc;
  1792. // if not NULL, create a ggml_context and allocate the tensor data in it
  1793. struct ggml_context ** ctx;
  1794. };
  1795. GGML_API struct gguf_context * gguf_init_empty(void);
  1796. GGML_API struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params);
  1797. //GGML_API struct gguf_context * gguf_init_from_buffer(..);
  1798. GGML_API void gguf_free(struct gguf_context * ctx);
  1799. GGML_API const char * gguf_type_name(enum gguf_type type);
  1800. GGML_API int gguf_get_version (const struct gguf_context * ctx);
  1801. GGML_API size_t gguf_get_alignment (const struct gguf_context * ctx);
  1802. GGML_API size_t gguf_get_data_offset(const struct gguf_context * ctx);
  1803. GGML_API void * gguf_get_data (const struct gguf_context * ctx);
  1804. GGML_API int gguf_get_n_kv(const struct gguf_context * ctx);
  1805. GGML_API int gguf_find_key(const struct gguf_context * ctx, const char * key);
  1806. GGML_API const char * gguf_get_key (const struct gguf_context * ctx, int key_id);
  1807. GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int key_id);
  1808. GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int key_id);
  1809. // will abort if the wrong type is used for the key
  1810. GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int key_id);
  1811. GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int key_id);
  1812. GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int key_id);
  1813. GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int key_id);
  1814. GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int key_id);
  1815. GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int key_id);
  1816. GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int key_id);
  1817. GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int key_id);
  1818. GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int key_id);
  1819. GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int key_id);
  1820. GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int key_id);
  1821. GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int key_id);
  1822. GGML_API const void * gguf_get_val_data(const struct gguf_context * ctx, int key_id);
  1823. GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int key_id);
  1824. GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int key_id);
  1825. GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int key_id, int i);
  1826. GGML_API int gguf_get_n_tensors (const struct gguf_context * ctx);
  1827. GGML_API int gguf_find_tensor (const struct gguf_context * ctx, const char * name);
  1828. GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int i);
  1829. GGML_API char * gguf_get_tensor_name (const struct gguf_context * ctx, int i);
  1830. GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int i);
  1831. // removes key if it exists
  1832. GGML_API void gguf_remove_key(struct gguf_context * ctx, const char * key);
  1833. // overrides existing values or adds a new one
  1834. GGML_API void gguf_set_val_u8 (struct gguf_context * ctx, const char * key, uint8_t val);
  1835. GGML_API void gguf_set_val_i8 (struct gguf_context * ctx, const char * key, int8_t val);
  1836. GGML_API void gguf_set_val_u16 (struct gguf_context * ctx, const char * key, uint16_t val);
  1837. GGML_API void gguf_set_val_i16 (struct gguf_context * ctx, const char * key, int16_t val);
  1838. GGML_API void gguf_set_val_u32 (struct gguf_context * ctx, const char * key, uint32_t val);
  1839. GGML_API void gguf_set_val_i32 (struct gguf_context * ctx, const char * key, int32_t val);
  1840. GGML_API void gguf_set_val_f32 (struct gguf_context * ctx, const char * key, float val);
  1841. GGML_API void gguf_set_val_u64 (struct gguf_context * ctx, const char * key, uint64_t val);
  1842. GGML_API void gguf_set_val_i64 (struct gguf_context * ctx, const char * key, int64_t val);
  1843. GGML_API void gguf_set_val_f64 (struct gguf_context * ctx, const char * key, double val);
  1844. GGML_API void gguf_set_val_bool(struct gguf_context * ctx, const char * key, bool val);
  1845. GGML_API void gguf_set_val_str (struct gguf_context * ctx, const char * key, const char * val);
  1846. GGML_API void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_type type, const void * data, int n);
  1847. GGML_API void gguf_set_arr_str (struct gguf_context * ctx, const char * key, const char ** data, int n);
  1848. // set or add KV pairs from another context
  1849. GGML_API void gguf_set_kv(struct gguf_context * ctx, struct gguf_context * src);
  1850. // manage tensor info
  1851. GGML_API void gguf_add_tensor(struct gguf_context * ctx, const struct ggml_tensor * tensor);
  1852. GGML_API void gguf_set_tensor_type(struct gguf_context * ctx, const char * name, enum ggml_type type);
  1853. GGML_API void gguf_set_tensor_data(struct gguf_context * ctx, const char * name, const void * data, size_t size);
  1854. // writing gguf files can be done in 2 ways:
  1855. //
  1856. // - write the entire gguf_context to a binary file in a single pass:
  1857. //
  1858. // gguf_write_to_file(ctx, fname);
  1859. //
  1860. // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
  1861. //
  1862. // FILE * f = fopen(fname, "wb");
  1863. // fseek(f, gguf_get_meta_size(ctx), SEEK_SET);
  1864. // fwrite(f, ...);
  1865. // void * data = gguf_meta_get_meta_data(ctx);
  1866. // fseek(f, 0, SEEK_SET);
  1867. // fwrite(f, data, gguf_get_meta_size(ctx));
  1868. // free(data);
  1869. // fclose(f);
  1870. //
  1871. // write the entire context to a binary file
  1872. GGML_API void gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta);
  1873. // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
  1874. GGML_API size_t gguf_get_meta_size(const struct gguf_context * ctx);
  1875. GGML_API void gguf_get_meta_data(const struct gguf_context * ctx, void * data);
  1876. #ifdef __cplusplus
  1877. // restrict not standard in C++
  1878. #define GGML_RESTRICT
  1879. #else
  1880. #define GGML_RESTRICT restrict
  1881. #endif
  1882. typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
  1883. typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
  1884. struct ggml_type_traits {
  1885. const char * type_name;
  1886. int64_t blck_size;
  1887. int64_t blck_size_interleave; // interleave elements in blocks
  1888. size_t type_size;
  1889. bool is_quantized;
  1890. ggml_to_float_t to_float;
  1891. ggml_from_float_t from_float_ref;
  1892. };
  1893. GGML_API const struct ggml_type_traits * ggml_get_type_traits(enum ggml_type type);
  1894. // ggml threadpool
  1895. // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
  1896. // the goal should be to create an API that other backends can use move everything to the ggml base
  1897. // scheduling priorities
  1898. enum ggml_sched_priority {
  1899. GGML_SCHED_PRIO_NORMAL,
  1900. GGML_SCHED_PRIO_MEDIUM,
  1901. GGML_SCHED_PRIO_HIGH,
  1902. GGML_SCHED_PRIO_REALTIME
  1903. };
  1904. // threadpool params
  1905. // Use ggml_threadpool_params_default() or ggml_threadpool_params_init() to populate the defaults
  1906. struct ggml_threadpool_params {
  1907. bool cpumask[GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
  1908. int n_threads; // number of threads
  1909. enum ggml_sched_priority prio; // thread priority
  1910. uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
  1911. bool strict_cpu; // strict cpu placement
  1912. bool paused; // start in paused state
  1913. };
  1914. struct ggml_threadpool; // forward declaration, see ggml.c
  1915. typedef struct ggml_threadpool * ggml_threadpool_t;
  1916. GGML_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads);
  1917. GGML_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads);
  1918. GGML_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1);
  1919. #ifdef __cplusplus
  1920. }
  1921. #endif