ggml-cpu.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. #include "ggml.h"
  3. #include "ggml-backend.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. // Scheduling priorities
  8. enum ggml_sched_priority {
  9. GGML_SCHED_PRIO_NORMAL,
  10. GGML_SCHED_PRIO_MEDIUM,
  11. GGML_SCHED_PRIO_HIGH,
  12. GGML_SCHED_PRIO_REALTIME
  13. };
  14. // Threadpool params
  15. // Use ggml_threadpool_params_default() or ggml_threadpool_params_init() to populate the defaults
  16. struct ggml_threadpool_params {
  17. bool cpumask[GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
  18. int n_threads; // number of threads
  19. enum ggml_sched_priority prio; // thread priority
  20. uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
  21. bool strict_cpu; // strict cpu placement
  22. bool paused; // start in paused state
  23. };
  24. struct ggml_threadpool; // forward declaration, see ggml.c
  25. typedef struct ggml_threadpool * ggml_threadpool_t;
  26. // the compute plan that needs to be prepared for ggml_graph_compute()
  27. // since https://github.com/ggerganov/ggml/issues/287
  28. struct ggml_cplan {
  29. size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
  30. uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`
  31. int n_threads;
  32. struct ggml_threadpool * threadpool;
  33. // abort ggml_graph_compute when true
  34. ggml_abort_callback abort_callback;
  35. void * abort_callback_data;
  36. };
  37. // numa strategies
  38. enum ggml_numa_strategy {
  39. GGML_NUMA_STRATEGY_DISABLED = 0,
  40. GGML_NUMA_STRATEGY_DISTRIBUTE = 1,
  41. GGML_NUMA_STRATEGY_ISOLATE = 2,
  42. GGML_NUMA_STRATEGY_NUMACTL = 3,
  43. GGML_NUMA_STRATEGY_MIRROR = 4,
  44. GGML_NUMA_STRATEGY_COUNT
  45. };
  46. GGML_BACKEND_API void ggml_numa_init(enum ggml_numa_strategy numa); // call once for better performance on NUMA systems
  47. GGML_BACKEND_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
  48. GGML_BACKEND_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
  49. GGML_BACKEND_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
  50. GGML_BACKEND_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
  51. GGML_BACKEND_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
  52. GGML_BACKEND_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
  53. GGML_BACKEND_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
  54. GGML_BACKEND_API int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
  55. GGML_BACKEND_API void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value);
  56. GGML_BACKEND_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
  57. GGML_BACKEND_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
  58. GGML_BACKEND_API float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
  59. GGML_BACKEND_API void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value);
  60. GGML_BACKEND_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads);
  61. GGML_BACKEND_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads);
  62. GGML_BACKEND_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1);
  63. GGML_BACKEND_API struct ggml_threadpool * ggml_threadpool_new (struct ggml_threadpool_params * params);
  64. GGML_BACKEND_API void ggml_threadpool_free (struct ggml_threadpool * threadpool);
  65. GGML_BACKEND_API int ggml_threadpool_get_n_threads(struct ggml_threadpool * threadpool);
  66. GGML_BACKEND_API void ggml_threadpool_pause (struct ggml_threadpool * threadpool);
  67. GGML_BACKEND_API void ggml_threadpool_resume (struct ggml_threadpool * threadpool);
  68. // ggml_graph_plan() has to be called before ggml_graph_compute()
  69. // when plan.work_size > 0, caller must allocate memory for plan.work_data
  70. GGML_BACKEND_API struct ggml_cplan ggml_graph_plan(
  71. const struct ggml_cgraph * cgraph,
  72. int n_threads, /* = GGML_DEFAULT_N_THREADS */
  73. struct ggml_threadpool * threadpool /* = NULL */ );
  74. GGML_BACKEND_API enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan);
  75. // same as ggml_graph_compute() but the work data is allocated as a part of the context
  76. // note: the drawback of this API is that you must have ensured that the context has enough memory for the work data
  77. GGML_BACKEND_API enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
  78. //
  79. // system info
  80. //
  81. // x86
  82. GGML_BACKEND_API int ggml_cpu_has_sse3 (void);
  83. GGML_BACKEND_API int ggml_cpu_has_ssse3 (void);
  84. GGML_BACKEND_API int ggml_cpu_has_avx (void);
  85. GGML_BACKEND_API int ggml_cpu_has_avx2 (void);
  86. GGML_BACKEND_API int ggml_cpu_has_f16c (void);
  87. GGML_BACKEND_API int ggml_cpu_has_fma (void);
  88. GGML_BACKEND_API int ggml_cpu_has_avx_vnni (void);
  89. GGML_BACKEND_API int ggml_cpu_has_avx512 (void);
  90. GGML_BACKEND_API int ggml_cpu_has_avx512_vbmi(void);
  91. GGML_BACKEND_API int ggml_cpu_has_avx512_vnni(void);
  92. GGML_BACKEND_API int ggml_cpu_has_avx512_bf16(void);
  93. GGML_BACKEND_API int ggml_cpu_has_amx_int8 (void);
  94. // ARM
  95. GGML_BACKEND_API int ggml_cpu_has_neon (void);
  96. GGML_BACKEND_API int ggml_cpu_has_arm_fma (void);
  97. GGML_BACKEND_API int ggml_cpu_has_fp16_va (void);
  98. GGML_BACKEND_API int ggml_cpu_has_matmul_int8(void);
  99. GGML_BACKEND_API int ggml_cpu_has_sve (void);
  100. GGML_BACKEND_API int ggml_cpu_get_sve_cnt (void); // sve vector length in bytes
  101. // other
  102. GGML_BACKEND_API int ggml_cpu_has_riscv_v (void);
  103. GGML_BACKEND_API int ggml_cpu_has_vsx (void);
  104. GGML_BACKEND_API int ggml_cpu_has_wasm_simd (void);
  105. GGML_BACKEND_API int ggml_cpu_has_llamafile (void);
  106. // Internal types and functions exposed for tests and benchmarks
  107. typedef void (*ggml_from_float_to_mat_t)
  108. (const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t nr, int64_t k, int64_t bs);
  109. typedef void (*ggml_vec_dot_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x, size_t bx,
  110. const void * GGML_RESTRICT y, size_t by, int nrc);
  111. typedef void (*ggml_gemv_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x,
  112. const void * GGML_RESTRICT y, int nr, int nc);
  113. typedef void (*ggml_gemm_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x,
  114. const void * GGML_RESTRICT y, int nr, int nc);
  115. struct ggml_type_traits_cpu {
  116. ggml_from_float_t from_float;
  117. ggml_from_float_to_mat_t from_float_to_mat;
  118. ggml_vec_dot_t vec_dot;
  119. enum ggml_type vec_dot_type;
  120. int64_t nrows; // number of rows to process simultaneously
  121. int64_t ncols; // number of columns to process simultaneously
  122. ggml_gemv_t gemv;
  123. ggml_gemm_t gemm;
  124. };
  125. GGML_BACKEND_API const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type);
  126. GGML_BACKEND_API void ggml_cpu_init(void);
  127. //
  128. // CPU backend
  129. //
  130. GGML_BACKEND_API ggml_backend_t ggml_backend_cpu_init(void);
  131. GGML_BACKEND_API bool ggml_backend_is_cpu (ggml_backend_t backend);
  132. GGML_BACKEND_API void ggml_backend_cpu_set_n_threads (ggml_backend_t backend_cpu, int n_threads);
  133. GGML_BACKEND_API void ggml_backend_cpu_set_threadpool (ggml_backend_t backend_cpu, ggml_threadpool_t threadpool);
  134. GGML_BACKEND_API void ggml_backend_cpu_set_abort_callback(ggml_backend_t backend_cpu, ggml_abort_callback abort_callback, void * abort_callback_data);
  135. GGML_BACKEND_API ggml_backend_reg_t ggml_backend_cpu_reg(void);
  136. #ifdef GGML_USE_CPU_HBM
  137. GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_cpu_hbm_buffer_type(void);
  138. #endif
  139. GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_cpu_aarch64_buffer_type(void);
  140. GGML_BACKEND_API bool ggml_backend_cpu_buft_is_aarch64(ggml_backend_buffer_type_t buft);
  141. #ifdef __cplusplus
  142. }
  143. #endif