ggml.h 48 KB

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