ggml.h 37 KB

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