ggml-cpu.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_API void ggml_numa_init(enum ggml_numa_strategy numa); // call once for better performance on NUMA systems
  47. GGML_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
  48. GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
  49. GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
  50. GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
  51. GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
  52. GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
  53. GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
  54. GGML_API int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
  55. GGML_API void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value);
  56. GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
  57. GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
  58. GGML_API float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
  59. GGML_API void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value);
  60. GGML_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads);
  61. GGML_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads);
  62. GGML_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1);
  63. GGML_API struct ggml_threadpool * ggml_threadpool_new (struct ggml_threadpool_params * params);
  64. GGML_API void ggml_threadpool_free (struct ggml_threadpool * threadpool);
  65. GGML_API int ggml_threadpool_get_n_threads(struct ggml_threadpool * threadpool);
  66. GGML_API void ggml_threadpool_pause (struct ggml_threadpool * threadpool);
  67. GGML_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_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_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_API enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
  78. // TODO: move to backend interface
  79. GGML_API int ggml_cpu_has_neon (void);
  80. GGML_API int ggml_cpu_has_sve (void);
  81. GGML_API int ggml_cpu_has_matmul_int8(void);
  82. // get the sve vector length in bytes
  83. GGML_API int ggml_cpu_get_sve_cnt(void);
  84. // Internal types and functions exposed for tests and benchmarks
  85. typedef void (*ggml_from_float_to_mat_t)
  86. (const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t nr, int64_t k, int64_t bs);
  87. typedef void (*ggml_vec_dot_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x, size_t bx,
  88. const void * GGML_RESTRICT y, size_t by, int nrc);
  89. typedef void (*ggml_gemv_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x,
  90. const void * GGML_RESTRICT y, int nr, int nc);
  91. typedef void (*ggml_gemm_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x,
  92. const void * GGML_RESTRICT y, int nr, int nc);
  93. struct ggml_type_traits_cpu {
  94. ggml_from_float_to_mat_t from_float_to_mat;
  95. ggml_vec_dot_t vec_dot;
  96. enum ggml_type vec_dot_type;
  97. int64_t nrows; // number of rows to process simultaneously
  98. int64_t ncols; // number of columns to process simultaneously
  99. ggml_gemv_t gemv;
  100. ggml_gemm_t gemm;
  101. };
  102. GGML_API const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type);
  103. GGML_API void ggml_cpu_init(void);
  104. //
  105. // CPU backend
  106. //
  107. GGML_API ggml_backend_t ggml_backend_cpu_init(void);
  108. GGML_API bool ggml_backend_is_cpu (ggml_backend_t backend);
  109. GGML_API void ggml_backend_cpu_set_n_threads (ggml_backend_t backend_cpu, int n_threads);
  110. GGML_API void ggml_backend_cpu_set_threadpool (ggml_backend_t backend_cpu, ggml_threadpool_t threadpool);
  111. GGML_API void ggml_backend_cpu_set_abort_callback(ggml_backend_t backend_cpu, ggml_abort_callback abort_callback, void * abort_callback_data);
  112. GGML_API ggml_backend_reg_t ggml_backend_cpu_reg(void);
  113. #ifdef GGML_USE_CPU_HBM
  114. GGML_API ggml_backend_buffer_type_t ggml_backend_cpu_hbm_buffer_type(void);
  115. #endif
  116. #ifdef __cplusplus
  117. }
  118. #endif