ggml.h 55 KB

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