ggml.h 39 KB

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