ggml.h 54 KB

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