ggml.h 61 KB

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