ggml.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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_DEFAULT_N_THREADS 4
  196. #define GGML_ASSERT(x) \
  197. do { \
  198. if (!(x)) { \
  199. fprintf(stderr, "GGML_ASSERT: %s:%d: %s\n", __FILE__, __LINE__, #x); \
  200. abort(); \
  201. } \
  202. } while (0)
  203. #ifdef __cplusplus
  204. extern "C" {
  205. #endif
  206. #ifdef __ARM_NEON
  207. // we use the built-in 16-bit float type
  208. typedef __fp16 ggml_fp16_t;
  209. #else
  210. typedef uint16_t ggml_fp16_t;
  211. #endif
  212. // convert FP16 <-> FP32
  213. GGML_API float ggml_fp16_to_fp32(ggml_fp16_t x);
  214. GGML_API ggml_fp16_t ggml_fp32_to_fp16(float x);
  215. GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t * x, float * y, size_t n);
  216. GGML_API void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, size_t n);
  217. struct ggml_object;
  218. struct ggml_context;
  219. enum ggml_type {
  220. GGML_TYPE_F32 = 0,
  221. GGML_TYPE_F16 = 1,
  222. GGML_TYPE_Q4_0 = 2,
  223. GGML_TYPE_Q4_1 = 3,
  224. // GGML_TYPE_Q4_2 = 4, support has been removed
  225. // GGML_TYPE_Q4_3 (5) support has been removed
  226. GGML_TYPE_Q5_0 = 6,
  227. GGML_TYPE_Q5_1 = 7,
  228. GGML_TYPE_Q8_0 = 8,
  229. GGML_TYPE_Q8_1 = 9,
  230. GGML_TYPE_I8,
  231. GGML_TYPE_I16,
  232. GGML_TYPE_I32,
  233. GGML_TYPE_COUNT,
  234. };
  235. enum ggml_backend {
  236. GGML_BACKEND_CPU = 0,
  237. GGML_BACKEND_CUDA = 1,
  238. };
  239. // model file types
  240. enum ggml_ftype {
  241. GGML_FTYPE_UNKNOWN = -1,
  242. GGML_FTYPE_ALL_F32 = 0,
  243. GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
  244. GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
  245. GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
  246. GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
  247. GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
  248. GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
  249. GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
  250. };
  251. // available tensor operations:
  252. enum ggml_op {
  253. GGML_OP_NONE = 0,
  254. GGML_OP_DUP,
  255. GGML_OP_ADD,
  256. GGML_OP_ADD1,
  257. GGML_OP_ACC,
  258. GGML_OP_SUB,
  259. GGML_OP_MUL,
  260. GGML_OP_DIV,
  261. GGML_OP_SQR,
  262. GGML_OP_SQRT,
  263. GGML_OP_LOG,
  264. GGML_OP_SUM,
  265. GGML_OP_SUM_ROWS,
  266. GGML_OP_MEAN,
  267. GGML_OP_REPEAT,
  268. GGML_OP_ABS,
  269. GGML_OP_SGN,
  270. GGML_OP_NEG,
  271. GGML_OP_STEP,
  272. GGML_OP_RELU,
  273. GGML_OP_GELU,
  274. GGML_OP_SILU,
  275. GGML_OP_SILU_BACK,
  276. GGML_OP_NORM, // normalize
  277. GGML_OP_RMS_NORM,
  278. GGML_OP_RMS_NORM_BACK,
  279. GGML_OP_MUL_MAT,
  280. GGML_OP_SCALE,
  281. GGML_OP_SET,
  282. GGML_OP_CPY,
  283. GGML_OP_CONT,
  284. GGML_OP_RESHAPE,
  285. GGML_OP_VIEW,
  286. GGML_OP_PERMUTE,
  287. GGML_OP_TRANSPOSE,
  288. GGML_OP_GET_ROWS,
  289. GGML_OP_GET_ROWS_BACK,
  290. GGML_OP_DIAG,
  291. GGML_OP_DIAG_MASK_INF,
  292. GGML_OP_DIAG_MASK_ZERO,
  293. GGML_OP_SOFT_MAX,
  294. GGML_OP_ROPE,
  295. GGML_OP_ROPE_BACK,
  296. GGML_OP_ALIBI,
  297. GGML_OP_CLAMP,
  298. GGML_OP_CONV_1D_1S,
  299. GGML_OP_CONV_1D_2S,
  300. GGML_OP_FLASH_ATTN,
  301. GGML_OP_FLASH_FF,
  302. GGML_OP_MAP_UNARY,
  303. GGML_OP_MAP_BINARY,
  304. GGML_OP_COUNT,
  305. };
  306. // ggml object
  307. struct ggml_object {
  308. size_t offs;
  309. size_t size;
  310. struct ggml_object * next;
  311. char padding[8];
  312. };
  313. static const size_t GGML_OBJECT_SIZE = sizeof(struct ggml_object);
  314. // n-dimensional tensor
  315. struct ggml_tensor {
  316. enum ggml_type type;
  317. enum ggml_backend backend;
  318. int n_dims;
  319. int64_t ne[GGML_MAX_DIMS]; // number of elements
  320. size_t nb[GGML_MAX_DIMS]; // stride in bytes:
  321. // nb[0] = sizeof(type)
  322. // nb[1] = nb[0] * ne[0] + padding
  323. // nb[i] = nb[i-1] * ne[i-1]
  324. // compute data
  325. enum ggml_op op;
  326. bool is_param;
  327. struct ggml_tensor * grad;
  328. struct ggml_tensor * src0;
  329. struct ggml_tensor * src1;
  330. struct ggml_tensor * opt[GGML_MAX_OPT];
  331. // thread scheduling
  332. int n_tasks;
  333. // performance
  334. int perf_runs;
  335. int64_t perf_cycles;
  336. int64_t perf_time_us;
  337. void * data;
  338. char name[32];
  339. char padding[16];
  340. };
  341. // computation graph
  342. struct ggml_cgraph {
  343. int n_nodes;
  344. int n_leafs;
  345. int n_threads;
  346. size_t work_size;
  347. struct ggml_tensor * work;
  348. struct ggml_tensor * nodes[GGML_MAX_NODES];
  349. struct ggml_tensor * grads[GGML_MAX_NODES];
  350. struct ggml_tensor * leafs[GGML_MAX_NODES];
  351. // performance
  352. int perf_runs;
  353. int64_t perf_cycles;
  354. int64_t perf_time_us;
  355. };
  356. // scratch buffer
  357. struct ggml_scratch {
  358. size_t offs;
  359. size_t size;
  360. void * data;
  361. };
  362. struct ggml_init_params {
  363. // memory pool
  364. size_t mem_size; // bytes
  365. void * mem_buffer; // if NULL, memory will be allocated internally
  366. bool no_alloc; // don't allocate memory for the tensor data
  367. };
  368. // misc
  369. GGML_API void ggml_time_init(void); // call this once at the beginning of the program
  370. GGML_API int64_t ggml_time_ms(void);
  371. GGML_API int64_t ggml_time_us(void);
  372. GGML_API int64_t ggml_cycles(void);
  373. GGML_API int64_t ggml_cycles_per_ms(void);
  374. GGML_API void ggml_print_object (const struct ggml_object * obj);
  375. GGML_API void ggml_print_objects(const struct ggml_context * ctx);
  376. GGML_API int64_t ggml_nelements(const struct ggml_tensor * tensor);
  377. GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor);
  378. GGML_API int ggml_blck_size (enum ggml_type type);
  379. GGML_API size_t ggml_type_size (enum ggml_type type); // size in bytes for all elements in a block
  380. GGML_API float ggml_type_sizef(enum ggml_type type); // ggml_type_size()/ggml_blck_size() as float
  381. GGML_API const char * ggml_type_name(enum ggml_type type);
  382. GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
  383. GGML_API bool ggml_is_quantized(enum ggml_type type);
  384. // TODO: temporary until model loading of ggml examples is refactored
  385. GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
  386. // main
  387. GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
  388. GGML_API void ggml_free(struct ggml_context * ctx);
  389. GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
  390. GGML_API size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch);
  391. GGML_API struct ggml_tensor * ggml_new_tensor(
  392. struct ggml_context * ctx,
  393. enum ggml_type type,
  394. int n_dims,
  395. const int64_t *ne);
  396. GGML_API struct ggml_tensor * ggml_new_tensor_1d(
  397. struct ggml_context * ctx,
  398. enum ggml_type type,
  399. int64_t ne0);
  400. GGML_API struct ggml_tensor * ggml_new_tensor_2d(
  401. struct ggml_context * ctx,
  402. enum ggml_type type,
  403. int64_t ne0,
  404. int64_t ne1);
  405. GGML_API struct ggml_tensor * ggml_new_tensor_3d(
  406. struct ggml_context * ctx,
  407. enum ggml_type type,
  408. int64_t ne0,
  409. int64_t ne1,
  410. int64_t ne2);
  411. GGML_API struct ggml_tensor * ggml_new_tensor_4d(
  412. struct ggml_context * ctx,
  413. enum ggml_type type,
  414. int64_t ne0,
  415. int64_t ne1,
  416. int64_t ne2,
  417. int64_t ne3);
  418. GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
  419. GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
  420. GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
  421. GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, const struct ggml_tensor * src);
  422. GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
  423. GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
  424. GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
  425. GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
  426. GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
  427. GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
  428. GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
  429. GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
  430. GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
  431. GGML_API const char * ggml_get_name(const struct ggml_tensor * tensor);
  432. GGML_API void ggml_set_name(struct ggml_tensor * tensor, const char * name);
  433. //
  434. // operations on tensors with backpropagation
  435. //
  436. GGML_API struct ggml_tensor * ggml_dup(
  437. struct ggml_context * ctx,
  438. struct ggml_tensor * a);
  439. GGML_API struct ggml_tensor * ggml_add(
  440. struct ggml_context * ctx,
  441. struct ggml_tensor * a,
  442. struct ggml_tensor * b);
  443. GGML_API struct ggml_tensor * ggml_add_inplace(
  444. struct ggml_context * ctx,
  445. struct ggml_tensor * a,
  446. struct ggml_tensor * b);
  447. GGML_API struct ggml_tensor * ggml_add1(
  448. struct ggml_context * ctx,
  449. struct ggml_tensor * a,
  450. struct ggml_tensor * b);
  451. GGML_API struct ggml_tensor * ggml_acc(
  452. struct ggml_context * ctx,
  453. struct ggml_tensor * a,
  454. struct ggml_tensor * b,
  455. size_t nb1,
  456. size_t nb2,
  457. size_t nb3,
  458. size_t offset);
  459. GGML_API struct ggml_tensor * ggml_acc_inplace(
  460. struct ggml_context * ctx,
  461. struct ggml_tensor * a,
  462. struct ggml_tensor * b,
  463. size_t nb1,
  464. size_t nb2,
  465. size_t nb3,
  466. size_t offset);
  467. GGML_API struct ggml_tensor * ggml_sub(
  468. struct ggml_context * ctx,
  469. struct ggml_tensor * a,
  470. struct ggml_tensor * b);
  471. GGML_API struct ggml_tensor * ggml_mul(
  472. struct ggml_context * ctx,
  473. struct ggml_tensor * a,
  474. struct ggml_tensor * b);
  475. GGML_API struct ggml_tensor * ggml_div(
  476. struct ggml_context * ctx,
  477. struct ggml_tensor * a,
  478. struct ggml_tensor * b);
  479. GGML_API struct ggml_tensor * ggml_sqr(
  480. struct ggml_context * ctx,
  481. struct ggml_tensor * a);
  482. GGML_API struct ggml_tensor * ggml_sqrt(
  483. struct ggml_context * ctx,
  484. struct ggml_tensor * a);
  485. GGML_API struct ggml_tensor * ggml_log(
  486. struct ggml_context * ctx,
  487. struct ggml_tensor * a);
  488. GGML_API struct ggml_tensor * ggml_log_inplace(
  489. struct ggml_context * ctx,
  490. struct ggml_tensor * a);
  491. // return scalar
  492. GGML_API struct ggml_tensor * ggml_sum(
  493. struct ggml_context * ctx,
  494. struct ggml_tensor * a);
  495. // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
  496. GGML_API struct ggml_tensor * ggml_sum_rows(
  497. struct ggml_context * ctx,
  498. struct ggml_tensor * a);
  499. // mean along rows
  500. GGML_API struct ggml_tensor * ggml_mean(
  501. struct ggml_context * ctx,
  502. struct ggml_tensor * a);
  503. // if a is the same shape as b, and a is not parameter, return a
  504. // otherwise, return a new tensor: repeat(a) to fit in b
  505. GGML_API struct ggml_tensor * ggml_repeat(
  506. struct ggml_context * ctx,
  507. struct ggml_tensor * a,
  508. struct ggml_tensor * b);
  509. GGML_API struct ggml_tensor * ggml_abs(
  510. struct ggml_context * ctx,
  511. struct ggml_tensor * a);
  512. GGML_API struct ggml_tensor * ggml_sgn(
  513. struct ggml_context * ctx,
  514. struct ggml_tensor * a);
  515. GGML_API struct ggml_tensor * ggml_neg(
  516. struct ggml_context * ctx,
  517. struct ggml_tensor * a);
  518. GGML_API struct ggml_tensor * ggml_step(
  519. struct ggml_context * ctx,
  520. struct ggml_tensor * a);
  521. GGML_API struct ggml_tensor * ggml_relu(
  522. struct ggml_context * ctx,
  523. struct ggml_tensor * a);
  524. // TODO: double-check this computation is correct
  525. GGML_API struct ggml_tensor * ggml_gelu(
  526. struct ggml_context * ctx,
  527. struct ggml_tensor * a);
  528. GGML_API struct ggml_tensor * ggml_silu(
  529. struct ggml_context * ctx,
  530. struct ggml_tensor * a);
  531. // a - x
  532. // b - dy
  533. GGML_API struct ggml_tensor * ggml_silu_back(
  534. struct ggml_context * ctx,
  535. struct ggml_tensor * a,
  536. struct ggml_tensor * b);
  537. // normalize along rows
  538. // TODO: eps is hardcoded to 1e-5 for now
  539. GGML_API struct ggml_tensor * ggml_norm(
  540. struct ggml_context * ctx,
  541. struct ggml_tensor * a);
  542. GGML_API struct ggml_tensor * ggml_rms_norm(
  543. struct ggml_context * ctx,
  544. struct ggml_tensor * a);
  545. // a - x
  546. // b - dy
  547. GGML_API struct ggml_tensor * ggml_rms_norm_back(
  548. struct ggml_context * ctx,
  549. struct ggml_tensor * a,
  550. struct ggml_tensor * b);
  551. // A: m rows, n columns
  552. // B: p rows, n columns (i.e. we transpose it internally)
  553. // result is m columns, p rows
  554. GGML_API struct ggml_tensor * ggml_mul_mat(
  555. struct ggml_context * ctx,
  556. struct ggml_tensor * a,
  557. struct ggml_tensor * b);
  558. //
  559. // operations on tensors without backpropagation
  560. //
  561. GGML_API struct ggml_tensor * ggml_scale(
  562. struct ggml_context * ctx,
  563. struct ggml_tensor * a,
  564. struct ggml_tensor * b);
  565. // in-place, returns view(a)
  566. GGML_API struct ggml_tensor * ggml_scale_inplace(
  567. struct ggml_context * ctx,
  568. struct ggml_tensor * a,
  569. struct ggml_tensor * b);
  570. // b -> view(a,offset,nb1,nb2,3), return modified a
  571. GGML_API struct ggml_tensor * ggml_set(
  572. struct ggml_context * ctx,
  573. struct ggml_tensor * a,
  574. struct ggml_tensor * b,
  575. size_t nb1,
  576. size_t nb2,
  577. size_t nb3,
  578. size_t offset);
  579. // b -> view(a,offset,nb1,nb2,3), return view(a)
  580. GGML_API struct ggml_tensor * ggml_set_inplace(
  581. struct ggml_context * ctx,
  582. struct ggml_tensor * a,
  583. struct ggml_tensor * b,
  584. size_t nb1,
  585. size_t nb2,
  586. size_t nb3,
  587. size_t offset);
  588. GGML_API struct ggml_tensor * ggml_set_1d(
  589. struct ggml_context * ctx,
  590. struct ggml_tensor * a,
  591. struct ggml_tensor * b,
  592. size_t offset);
  593. GGML_API struct ggml_tensor * ggml_set_1d_inplace(
  594. struct ggml_context * ctx,
  595. struct ggml_tensor * a,
  596. struct ggml_tensor * b,
  597. size_t offset);
  598. // b -> view(a,offset,nb1,nb2,3), return modified a
  599. GGML_API struct ggml_tensor * ggml_set_2d(
  600. struct ggml_context * ctx,
  601. struct ggml_tensor * a,
  602. struct ggml_tensor * b,
  603. size_t nb1,
  604. size_t offset);
  605. // b -> view(a,offset,nb1,nb2,3), return view(a)
  606. GGML_API struct ggml_tensor * ggml_set_2d_inplace(
  607. struct ggml_context * ctx,
  608. struct ggml_tensor * a,
  609. struct ggml_tensor * b,
  610. size_t nb1,
  611. size_t offset);
  612. // a -> b, return view(b)
  613. GGML_API struct ggml_tensor * ggml_cpy(
  614. struct ggml_context * ctx,
  615. struct ggml_tensor * a,
  616. struct ggml_tensor * b);
  617. // make contiguous
  618. GGML_API struct ggml_tensor * ggml_cont(
  619. struct ggml_context * ctx,
  620. struct ggml_tensor * a);
  621. // return view(a), b specifies the new shape
  622. // TODO: when we start computing gradient, make a copy instead of view
  623. GGML_API struct ggml_tensor * ggml_reshape(
  624. struct ggml_context * ctx,
  625. struct ggml_tensor * a,
  626. struct ggml_tensor * b);
  627. // return view(a)
  628. // TODO: when we start computing gradient, make a copy instead of view
  629. GGML_API struct ggml_tensor * ggml_reshape_1d(
  630. struct ggml_context * ctx,
  631. struct ggml_tensor * a,
  632. int64_t ne0);
  633. GGML_API struct ggml_tensor * ggml_reshape_2d(
  634. struct ggml_context * ctx,
  635. struct ggml_tensor * a,
  636. int64_t ne0,
  637. int64_t ne1);
  638. // return view(a)
  639. // TODO: when we start computing gradient, make a copy instead of view
  640. GGML_API struct ggml_tensor * ggml_reshape_3d(
  641. struct ggml_context * ctx,
  642. struct ggml_tensor * a,
  643. int64_t ne0,
  644. int64_t ne1,
  645. int64_t ne2);
  646. GGML_API struct ggml_tensor * ggml_reshape_4d(
  647. struct ggml_context * ctx,
  648. struct ggml_tensor * a,
  649. int64_t ne0,
  650. int64_t ne1,
  651. int64_t ne2,
  652. int64_t ne3);
  653. // offset in bytes
  654. GGML_API struct ggml_tensor * ggml_view_1d(
  655. struct ggml_context * ctx,
  656. struct ggml_tensor * a,
  657. int64_t ne0,
  658. size_t offset);
  659. GGML_API struct ggml_tensor * ggml_view_2d(
  660. struct ggml_context * ctx,
  661. struct ggml_tensor * a,
  662. int64_t ne0,
  663. int64_t ne1,
  664. size_t nb1, // row stride in bytes
  665. size_t offset);
  666. GGML_API struct ggml_tensor * ggml_view_3d(
  667. struct ggml_context * ctx,
  668. struct ggml_tensor * a,
  669. int64_t ne0,
  670. int64_t ne1,
  671. int64_t ne2,
  672. size_t nb1, // row stride in bytes
  673. size_t nb2, // slice stride in bytes
  674. size_t offset);
  675. GGML_API struct ggml_tensor * ggml_view_4d(
  676. struct ggml_context * ctx,
  677. struct ggml_tensor * a,
  678. int64_t ne0,
  679. int64_t ne1,
  680. int64_t ne2,
  681. int64_t ne3,
  682. size_t nb1, // row stride in bytes
  683. size_t nb2, // slice stride in bytes
  684. size_t nb3,
  685. size_t offset);
  686. GGML_API struct ggml_tensor * ggml_permute(
  687. struct ggml_context * ctx,
  688. struct ggml_tensor * a,
  689. int axis0,
  690. int axis1,
  691. int axis2,
  692. int axis3);
  693. // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
  694. GGML_API struct ggml_tensor * ggml_transpose(
  695. struct ggml_context * ctx,
  696. struct ggml_tensor * a);
  697. GGML_API struct ggml_tensor * ggml_get_rows(
  698. struct ggml_context * ctx,
  699. struct ggml_tensor * a,
  700. struct ggml_tensor * b);
  701. GGML_API struct ggml_tensor * ggml_get_rows_back(
  702. struct ggml_context * ctx,
  703. struct ggml_tensor * a,
  704. struct ggml_tensor * b,
  705. struct ggml_tensor * c);
  706. GGML_API struct ggml_tensor * ggml_diag(
  707. struct ggml_context * ctx,
  708. struct ggml_tensor * a);
  709. // set elements above the diagonal to -INF
  710. GGML_API struct ggml_tensor * ggml_diag_mask_inf(
  711. struct ggml_context * ctx,
  712. struct ggml_tensor * a,
  713. int n_past);
  714. // in-place, returns view(a)
  715. GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
  716. struct ggml_context * ctx,
  717. struct ggml_tensor * a,
  718. int n_past);
  719. // set elements above the diagonal to 0
  720. GGML_API struct ggml_tensor * ggml_diag_mask_zero(
  721. struct ggml_context * ctx,
  722. struct ggml_tensor * a,
  723. int n_past);
  724. // in-place, returns view(a)
  725. GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
  726. struct ggml_context * ctx,
  727. struct ggml_tensor * a,
  728. int n_past);
  729. GGML_API struct ggml_tensor * ggml_soft_max(
  730. struct ggml_context * ctx,
  731. struct ggml_tensor * a);
  732. // in-place, returns view(a)
  733. GGML_API struct ggml_tensor * ggml_soft_max_inplace(
  734. struct ggml_context * ctx,
  735. struct ggml_tensor * a);
  736. // rotary position embedding
  737. // if mode & 1 == 1, skip n_past elements
  738. // if mode & 2 == 1, GPT-NeoX style
  739. // TODO: avoid creating a new tensor every time
  740. GGML_API struct ggml_tensor * ggml_rope(
  741. struct ggml_context * ctx,
  742. struct ggml_tensor * a,
  743. int n_past,
  744. int n_dims,
  745. int mode);
  746. // in-place, returns view(a)
  747. GGML_API struct ggml_tensor * ggml_rope_inplace(
  748. struct ggml_context * ctx,
  749. struct ggml_tensor * a,
  750. int n_past,
  751. int n_dims,
  752. int mode);
  753. // rotary position embedding backward, i.e compute dx from dy
  754. // a - dy
  755. GGML_API struct ggml_tensor * ggml_rope_back(
  756. struct ggml_context * ctx,
  757. struct ggml_tensor * a,
  758. int n_past,
  759. int n_dims,
  760. int mode);
  761. // alibi position embedding
  762. // in-place, returns view(a)
  763. struct ggml_tensor * ggml_alibi(
  764. struct ggml_context * ctx,
  765. struct ggml_tensor * a,
  766. int n_past,
  767. int n_head,
  768. float bias_max);
  769. // clamp
  770. // in-place, returns view(a)
  771. struct ggml_tensor * ggml_clamp(
  772. struct ggml_context * ctx,
  773. struct ggml_tensor * a,
  774. float min,
  775. float max);
  776. // padding = 1
  777. // TODO: we don't support extra parameters for now
  778. // that's why we are hard-coding the stride, padding, and dilation
  779. // not great ..
  780. GGML_API struct ggml_tensor * ggml_conv_1d_1s(
  781. struct ggml_context * ctx,
  782. struct ggml_tensor * a,
  783. struct ggml_tensor * b);
  784. GGML_API struct ggml_tensor * ggml_conv_1d_2s(
  785. struct ggml_context * ctx,
  786. struct ggml_tensor * a,
  787. struct ggml_tensor * b);
  788. GGML_API struct ggml_tensor * ggml_flash_attn(
  789. struct ggml_context * ctx,
  790. struct ggml_tensor * q,
  791. struct ggml_tensor * k,
  792. struct ggml_tensor * v,
  793. bool masked);
  794. GGML_API struct ggml_tensor * ggml_flash_ff(
  795. struct ggml_context * ctx,
  796. struct ggml_tensor * a,
  797. struct ggml_tensor * b0,
  798. struct ggml_tensor * b1,
  799. struct ggml_tensor * c0,
  800. struct ggml_tensor * c1);
  801. // Mapping operations
  802. typedef void (*ggml_unary_op_f32_t)(const int, float *, const float *);
  803. typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
  804. GGML_API struct ggml_tensor * ggml_map_unary_f32(
  805. struct ggml_context * ctx,
  806. struct ggml_tensor * a,
  807. ggml_unary_op_f32_t fun);
  808. GGML_API struct ggml_tensor * ggml_map_binary_f32(
  809. struct ggml_context * ctx,
  810. struct ggml_tensor * a,
  811. struct ggml_tensor * b,
  812. ggml_binary_op_f32_t fun);
  813. //
  814. // automatic differentiation
  815. //
  816. GGML_API void ggml_set_param(
  817. struct ggml_context * ctx,
  818. struct ggml_tensor * tensor);
  819. GGML_API void ggml_build_forward_expand(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
  820. GGML_API struct ggml_cgraph ggml_build_forward (struct ggml_tensor * tensor);
  821. GGML_API struct ggml_cgraph ggml_build_backward(struct ggml_context * ctx, struct ggml_cgraph * gf, bool keep);
  822. GGML_API void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph);
  823. GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph);
  824. // print info and performance information for the graph
  825. GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
  826. // dump the graph into a file using the dot format
  827. GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
  828. //
  829. // optimization
  830. //
  831. // optimization methods
  832. enum ggml_opt_type {
  833. GGML_OPT_ADAM,
  834. GGML_OPT_LBFGS,
  835. };
  836. // linesearch methods
  837. enum ggml_linesearch {
  838. GGML_LINESEARCH_DEFAULT = 1,
  839. GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0,
  840. GGML_LINESEARCH_BACKTRACKING_WOLFE = 1,
  841. GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2,
  842. };
  843. // optimization return values
  844. enum ggml_opt_result {
  845. GGML_OPT_OK = 0,
  846. GGML_OPT_DID_NOT_CONVERGE,
  847. GGML_OPT_NO_CONTEXT,
  848. GGML_OPT_INVALID_WOLFE,
  849. GGML_OPT_FAIL,
  850. GGML_LINESEARCH_FAIL = -128,
  851. GGML_LINESEARCH_MINIMUM_STEP,
  852. GGML_LINESEARCH_MAXIMUM_STEP,
  853. GGML_LINESEARCH_MAXIMUM_ITERATIONS,
  854. GGML_LINESEARCH_INVALID_PARAMETERS,
  855. };
  856. // optimization parameters
  857. //
  858. // see ggml.c (ggml_opt_default_params) for default values
  859. //
  860. struct ggml_opt_params {
  861. enum ggml_opt_type type;
  862. int n_threads;
  863. // delta-based convergence test
  864. //
  865. // if past == 0 - disabled
  866. // if past > 0:
  867. // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|)
  868. //
  869. int past;
  870. float delta;
  871. // maximum number of iterations without improvement
  872. //
  873. // if 0 - disabled
  874. // if > 0:
  875. // assume convergence if no cost improvement in this number of iterations
  876. //
  877. int max_no_improvement;
  878. bool print_forward_graph;
  879. bool print_backward_graph;
  880. // ADAM parameters
  881. struct {
  882. int n_iter;
  883. float alpha; // learning rate
  884. float beta1;
  885. float beta2;
  886. float eps; // epsilon for numerical stability
  887. float eps_f; // epsilon for convergence test
  888. float eps_g; // epsilon for convergence test
  889. } adam;
  890. // LBFGS parameters
  891. struct {
  892. int m; // number of corrections to approximate the inv. Hessian
  893. int n_iter;
  894. int max_linesearch;
  895. float eps; // convergence tolerance
  896. float ftol; // line search tolerance
  897. float wolfe;
  898. float min_step;
  899. float max_step;
  900. enum ggml_linesearch linesearch;
  901. } lbfgs;
  902. };
  903. GGML_API struct ggml_opt_params ggml_opt_default_params(enum ggml_opt_type type);
  904. // optimize the function defined by the tensor f
  905. GGML_API enum ggml_opt_result ggml_opt(
  906. struct ggml_context * ctx,
  907. struct ggml_opt_params params,
  908. struct ggml_tensor * f);
  909. //
  910. // quantization
  911. //
  912. GGML_API size_t ggml_quantize_q4_0(const float * src, void * dst, int n, int k, int64_t * hist);
  913. GGML_API size_t ggml_quantize_q4_1(const float * src, void * dst, int n, int k, int64_t * hist);
  914. GGML_API size_t ggml_quantize_q5_0(const float * src, void * dst, int n, int k, int64_t * hist);
  915. GGML_API size_t ggml_quantize_q5_1(const float * src, void * dst, int n, int k, int64_t * hist);
  916. GGML_API size_t ggml_quantize_q8_0(const float * src, void * dst, int n, int k, int64_t * hist);
  917. GGML_API size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, int start, int n, int64_t * hist);
  918. //
  919. // system info
  920. //
  921. GGML_API int ggml_cpu_has_avx (void);
  922. GGML_API int ggml_cpu_has_avx2 (void);
  923. GGML_API int ggml_cpu_has_avx512 (void);
  924. GGML_API int ggml_cpu_has_avx512_vbmi(void);
  925. GGML_API int ggml_cpu_has_avx512_vnni(void);
  926. GGML_API int ggml_cpu_has_fma (void);
  927. GGML_API int ggml_cpu_has_neon (void);
  928. GGML_API int ggml_cpu_has_arm_fma (void);
  929. GGML_API int ggml_cpu_has_f16c (void);
  930. GGML_API int ggml_cpu_has_fp16_va (void);
  931. GGML_API int ggml_cpu_has_wasm_simd (void);
  932. GGML_API int ggml_cpu_has_blas (void);
  933. GGML_API int ggml_cpu_has_cublas (void);
  934. GGML_API int ggml_cpu_has_clblast (void);
  935. GGML_API int ggml_cpu_has_gpublas (void);
  936. GGML_API int ggml_cpu_has_sse3 (void);
  937. GGML_API int ggml_cpu_has_vsx (void);
  938. //
  939. // Internal types and functions exposed for tests and benchmarks
  940. //
  941. #ifdef __cplusplus
  942. // restrict not standard in C++
  943. #define GGML_RESTRICT
  944. #else
  945. #define GGML_RESTRICT restrict
  946. #endif
  947. typedef void (*dequantize_row_q_t)(const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int k);
  948. typedef void (*quantize_row_q_t) (const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int k);
  949. typedef void (*vec_dot_q_t) (const int n, float * GGML_RESTRICT s, const void * GGML_RESTRICT x, const void * GGML_RESTRICT y);
  950. typedef struct {
  951. dequantize_row_q_t dequantize_row_q;
  952. quantize_row_q_t quantize_row_q;
  953. quantize_row_q_t quantize_row_q_reference;
  954. quantize_row_q_t quantize_row_q_dot;
  955. vec_dot_q_t vec_dot_q;
  956. enum ggml_type vec_dot_type;
  957. } quantize_fns_t;
  958. quantize_fns_t ggml_internal_get_quantize_fn(size_t i);
  959. #ifdef __cplusplus
  960. }
  961. #endif