|
#pragma once |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef GGML_SHARED |
|
# if defined(_WIN32) && !defined(__MINGW32__) |
|
# ifdef GGML_BUILD |
|
# define GGML_API __declspec(dllexport) |
|
# else |
|
# define GGML_API __declspec(dllimport) |
|
# endif |
|
# else |
|
# define GGML_API __attribute__ ((visibility ("default"))) |
|
# endif |
|
#else |
|
# define GGML_API |
|
#endif |
|
|
|
#include <stdint.h> |
|
#include <stddef.h> |
|
#include <stdbool.h> |
|
|
|
#define GGML_FILE_MAGIC 0x67676d6c |
|
#define GGML_FILE_VERSION 1 |
|
|
|
#define GGML_QNT_VERSION 2 |
|
#define GGML_QNT_VERSION_FACTOR 1000 |
|
|
|
#define GGML_MAX_DIMS 4 |
|
#define GGML_MAX_NODES 4096 |
|
#define GGML_MAX_PARAMS 256 |
|
#define GGML_MAX_CONTEXTS 64 |
|
#define GGML_MAX_OPT 4 |
|
#define GGML_MAX_NAME 32 |
|
#define GGML_DEFAULT_N_THREADS 4 |
|
|
|
#define GGML_ASSERT(x) \ |
|
do { \ |
|
if (!(x)) { \ |
|
fprintf(stderr, "GGML_ASSERT: %s:%d: %s\n", __FILE__, __LINE__, #x); \ |
|
abort(); \ |
|
} \ |
|
} while (0) |
|
|
|
#ifdef __cplusplus |
|
extern "C" { |
|
#endif |
|
|
|
#ifdef __ARM_NEON |
|
|
|
typedef __fp16 ggml_fp16_t; |
|
#else |
|
typedef uint16_t ggml_fp16_t; |
|
#endif |
|
|
|
|
|
GGML_API float ggml_fp16_to_fp32(ggml_fp16_t x); |
|
GGML_API ggml_fp16_t ggml_fp32_to_fp16(float x); |
|
|
|
GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t * x, float * y, size_t n); |
|
GGML_API void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, size_t n); |
|
|
|
struct ggml_object; |
|
struct ggml_context; |
|
|
|
enum ggml_type { |
|
GGML_TYPE_F32 = 0, |
|
GGML_TYPE_F16 = 1, |
|
GGML_TYPE_Q4_0 = 2, |
|
GGML_TYPE_Q4_1 = 3, |
|
|
|
|
|
GGML_TYPE_Q5_0 = 6, |
|
GGML_TYPE_Q5_1 = 7, |
|
GGML_TYPE_Q8_0 = 8, |
|
GGML_TYPE_Q8_1 = 9, |
|
|
|
GGML_TYPE_Q2_K = 10, |
|
GGML_TYPE_Q3_K = 11, |
|
GGML_TYPE_Q4_K = 12, |
|
GGML_TYPE_Q5_K = 13, |
|
GGML_TYPE_Q6_K = 14, |
|
GGML_TYPE_Q8_K = 15, |
|
GGML_TYPE_I8, |
|
GGML_TYPE_I16, |
|
GGML_TYPE_I32, |
|
GGML_TYPE_COUNT, |
|
}; |
|
|
|
enum ggml_backend { |
|
GGML_BACKEND_CPU = 0, |
|
GGML_BACKEND_GPU = 10, |
|
GGML_BACKEND_GPU_SPLIT = 20, |
|
}; |
|
|
|
|
|
enum ggml_ftype { |
|
GGML_FTYPE_UNKNOWN = -1, |
|
GGML_FTYPE_ALL_F32 = 0, |
|
GGML_FTYPE_MOSTLY_F16 = 1, |
|
GGML_FTYPE_MOSTLY_Q4_0 = 2, |
|
GGML_FTYPE_MOSTLY_Q4_1 = 3, |
|
GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, |
|
GGML_FTYPE_MOSTLY_Q8_0 = 7, |
|
GGML_FTYPE_MOSTLY_Q5_0 = 8, |
|
GGML_FTYPE_MOSTLY_Q5_1 = 9, |
|
GGML_FTYPE_MOSTLY_Q2_K = 10, |
|
GGML_FTYPE_MOSTLY_Q3_K = 11, |
|
GGML_FTYPE_MOSTLY_Q4_K = 12, |
|
GGML_FTYPE_MOSTLY_Q5_K = 13, |
|
GGML_FTYPE_MOSTLY_Q6_K = 14, |
|
}; |
|
|
|
|
|
enum ggml_op { |
|
GGML_OP_NONE = 0, |
|
|
|
GGML_OP_DUP, |
|
GGML_OP_ADD, |
|
GGML_OP_ADD1, |
|
GGML_OP_ACC, |
|
GGML_OP_SUB, |
|
GGML_OP_MUL, |
|
GGML_OP_DIV, |
|
GGML_OP_SQR, |
|
GGML_OP_SQRT, |
|
GGML_OP_LOG, |
|
GGML_OP_SUM, |
|
GGML_OP_SUM_ROWS, |
|
GGML_OP_MEAN, |
|
GGML_OP_REPEAT, |
|
GGML_OP_REPEAT_BACK, |
|
GGML_OP_ABS, |
|
GGML_OP_SGN, |
|
GGML_OP_NEG, |
|
GGML_OP_STEP, |
|
GGML_OP_RELU, |
|
GGML_OP_GELU, |
|
GGML_OP_SILU, |
|
GGML_OP_SILU_BACK, |
|
GGML_OP_NORM, |
|
GGML_OP_RMS_NORM, |
|
GGML_OP_RMS_NORM_BACK, |
|
|
|
GGML_OP_MUL_MAT, |
|
GGML_OP_OUT_PROD, |
|
|
|
GGML_OP_SCALE, |
|
GGML_OP_SET, |
|
GGML_OP_CPY, |
|
GGML_OP_CONT, |
|
GGML_OP_RESHAPE, |
|
GGML_OP_VIEW, |
|
GGML_OP_PERMUTE, |
|
GGML_OP_TRANSPOSE, |
|
GGML_OP_GET_ROWS, |
|
GGML_OP_GET_ROWS_BACK, |
|
GGML_OP_DIAG, |
|
GGML_OP_DIAG_MASK_INF, |
|
GGML_OP_DIAG_MASK_ZERO, |
|
GGML_OP_SOFT_MAX, |
|
GGML_OP_SOFT_MAX_BACK, |
|
GGML_OP_ROPE, |
|
GGML_OP_ROPE_BACK, |
|
GGML_OP_ALIBI, |
|
GGML_OP_CLAMP, |
|
GGML_OP_CONV_1D_1S, |
|
GGML_OP_CONV_1D_2S, |
|
|
|
GGML_OP_FLASH_ATTN, |
|
GGML_OP_FLASH_FF, |
|
GGML_OP_FLASH_ATTN_BACK, |
|
|
|
GGML_OP_MAP_UNARY, |
|
GGML_OP_MAP_BINARY, |
|
|
|
GGML_OP_CROSS_ENTROPY_LOSS, |
|
GGML_OP_CROSS_ENTROPY_LOSS_BACK, |
|
|
|
GGML_OP_COUNT, |
|
}; |
|
|
|
|
|
|
|
struct ggml_object { |
|
size_t offs; |
|
size_t size; |
|
|
|
struct ggml_object * next; |
|
|
|
char padding[8]; |
|
}; |
|
|
|
static const size_t GGML_OBJECT_SIZE = sizeof(struct ggml_object); |
|
|
|
|
|
struct ggml_tensor { |
|
enum ggml_type type; |
|
enum ggml_backend backend; |
|
|
|
int n_dims; |
|
int64_t ne[GGML_MAX_DIMS]; |
|
size_t nb[GGML_MAX_DIMS]; |
|
|
|
|
|
|
|
|
|
|
|
enum ggml_op op; |
|
|
|
bool is_param; |
|
|
|
struct ggml_tensor * grad; |
|
struct ggml_tensor * src0; |
|
struct ggml_tensor * src1; |
|
struct ggml_tensor * opt[GGML_MAX_OPT]; |
|
|
|
|
|
int n_tasks; |
|
|
|
|
|
int perf_runs; |
|
int64_t perf_cycles; |
|
int64_t perf_time_us; |
|
|
|
void * data; |
|
|
|
char name[GGML_MAX_NAME]; |
|
|
|
void * extra; |
|
|
|
char padding[4]; |
|
}; |
|
|
|
static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor); |
|
|
|
|
|
struct ggml_cgraph { |
|
int n_nodes; |
|
int n_leafs; |
|
int n_threads; |
|
|
|
size_t work_size; |
|
struct ggml_tensor * work; |
|
|
|
struct ggml_tensor * nodes[GGML_MAX_NODES]; |
|
struct ggml_tensor * grads[GGML_MAX_NODES]; |
|
struct ggml_tensor * leafs[GGML_MAX_NODES]; |
|
|
|
|
|
int perf_runs; |
|
int64_t perf_cycles; |
|
int64_t perf_time_us; |
|
}; |
|
|
|
|
|
struct ggml_scratch { |
|
size_t offs; |
|
size_t size; |
|
void * data; |
|
}; |
|
|
|
struct ggml_init_params { |
|
|
|
size_t mem_size; |
|
void * mem_buffer; |
|
bool no_alloc; |
|
}; |
|
|
|
|
|
|
|
enum ggml_task_type { |
|
GGML_TASK_INIT = 0, |
|
GGML_TASK_COMPUTE, |
|
GGML_TASK_FINALIZE, |
|
}; |
|
|
|
struct ggml_compute_params { |
|
enum ggml_task_type type; |
|
|
|
|
|
int ith, nth; |
|
|
|
|
|
size_t wsize; |
|
void * wdata; |
|
}; |
|
|
|
|
|
|
|
GGML_API void ggml_time_init(void); |
|
GGML_API int64_t ggml_time_ms(void); |
|
GGML_API int64_t ggml_time_us(void); |
|
GGML_API int64_t ggml_cycles(void); |
|
GGML_API int64_t ggml_cycles_per_ms(void); |
|
|
|
GGML_API void ggml_print_object (const struct ggml_object * obj); |
|
GGML_API void ggml_print_objects(const struct ggml_context * ctx); |
|
|
|
GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor); |
|
GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor); |
|
GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor); |
|
GGML_API size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split); |
|
|
|
GGML_API int ggml_blck_size (enum ggml_type type); |
|
GGML_API size_t ggml_type_size (enum ggml_type type); |
|
GGML_API float ggml_type_sizef(enum ggml_type type); |
|
|
|
GGML_API const char * ggml_type_name(enum ggml_type type); |
|
GGML_API const char * ggml_op_name (enum ggml_op op); |
|
|
|
GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor); |
|
|
|
GGML_API bool ggml_is_quantized(enum ggml_type type); |
|
|
|
|
|
GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype); |
|
|
|
GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor); |
|
GGML_API bool ggml_is_contiguous(const struct ggml_tensor * tensor); |
|
GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor); |
|
|
|
|
|
GGML_API size_t ggml_tensor_overhead(void); |
|
|
|
|
|
|
|
GGML_API struct ggml_context * ggml_init(struct ggml_init_params params); |
|
GGML_API void ggml_free(struct ggml_context * ctx); |
|
|
|
GGML_API size_t ggml_used_mem(const struct ggml_context * ctx); |
|
|
|
GGML_API size_t ggml_set_scratch (struct ggml_context * ctx, struct ggml_scratch scratch); |
|
GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc); |
|
|
|
GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx); |
|
GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx); |
|
GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx); |
|
|
|
GGML_API struct ggml_tensor * ggml_new_tensor( |
|
struct ggml_context * ctx, |
|
enum ggml_type type, |
|
int n_dims, |
|
const int64_t *ne); |
|
|
|
GGML_API struct ggml_tensor * ggml_new_tensor_1d( |
|
struct ggml_context * ctx, |
|
enum ggml_type type, |
|
int64_t ne0); |
|
|
|
GGML_API struct ggml_tensor * ggml_new_tensor_2d( |
|
struct ggml_context * ctx, |
|
enum ggml_type type, |
|
int64_t ne0, |
|
int64_t ne1); |
|
|
|
GGML_API struct ggml_tensor * ggml_new_tensor_3d( |
|
struct ggml_context * ctx, |
|
enum ggml_type type, |
|
int64_t ne0, |
|
int64_t ne1, |
|
int64_t ne2); |
|
|
|
GGML_API struct ggml_tensor * ggml_new_tensor_4d( |
|
struct ggml_context * ctx, |
|
enum ggml_type type, |
|
int64_t ne0, |
|
int64_t ne1, |
|
int64_t ne2, |
|
int64_t ne3); |
|
|
|
GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value); |
|
GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value); |
|
|
|
GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src); |
|
GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, const struct ggml_tensor * src); |
|
|
|
GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name); |
|
|
|
GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor); |
|
GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value); |
|
GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value); |
|
|
|
GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i); |
|
GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value); |
|
|
|
GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i); |
|
GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value); |
|
|
|
GGML_API void * ggml_get_data (const struct ggml_tensor * tensor); |
|
GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor); |
|
|
|
GGML_API const char * ggml_get_name(const struct ggml_tensor * tensor); |
|
GGML_API void ggml_set_name(struct ggml_tensor * tensor, const char * name); |
|
|
|
|
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_dup( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_add( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_add_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_add1( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_add1_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_acc( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
size_t nb1, |
|
size_t nb2, |
|
size_t nb3, |
|
size_t offset); |
|
|
|
GGML_API struct ggml_tensor * ggml_acc_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
size_t nb1, |
|
size_t nb2, |
|
size_t nb3, |
|
size_t offset); |
|
|
|
GGML_API struct ggml_tensor * ggml_sub( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_mul( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_div( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_sqr( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_sqrt( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_log( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_log_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_sum( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_sum_rows( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_mean( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_repeat( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_repeat_back( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_abs( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_sgn( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_neg( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_step( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_relu( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_gelu( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_silu( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_silu_back( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_norm( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_rms_norm( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_rms_norm_back( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_mul_mat( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_out_prod( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_scale( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_scale_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_set( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
size_t nb1, |
|
size_t nb2, |
|
size_t nb3, |
|
size_t offset); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_set_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
size_t nb1, |
|
size_t nb2, |
|
size_t nb3, |
|
size_t offset); |
|
|
|
GGML_API struct ggml_tensor * ggml_set_1d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
size_t offset); |
|
|
|
GGML_API struct ggml_tensor * ggml_set_1d_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
size_t offset); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_set_2d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
size_t nb1, |
|
size_t offset); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_set_2d_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
size_t nb1, |
|
size_t offset); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_cpy( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_cont( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_reshape( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_reshape_1d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int64_t ne0); |
|
|
|
GGML_API struct ggml_tensor * ggml_reshape_2d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int64_t ne0, |
|
int64_t ne1); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_reshape_3d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int64_t ne0, |
|
int64_t ne1, |
|
int64_t ne2); |
|
|
|
GGML_API struct ggml_tensor * ggml_reshape_4d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int64_t ne0, |
|
int64_t ne1, |
|
int64_t ne2, |
|
int64_t ne3); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_view_1d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int64_t ne0, |
|
size_t offset); |
|
|
|
GGML_API struct ggml_tensor * ggml_view_2d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int64_t ne0, |
|
int64_t ne1, |
|
size_t nb1, |
|
size_t offset); |
|
|
|
GGML_API struct ggml_tensor * ggml_view_3d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int64_t ne0, |
|
int64_t ne1, |
|
int64_t ne2, |
|
size_t nb1, |
|
size_t nb2, |
|
size_t offset); |
|
|
|
GGML_API struct ggml_tensor * ggml_view_4d( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int64_t ne0, |
|
int64_t ne1, |
|
int64_t ne2, |
|
int64_t ne3, |
|
size_t nb1, |
|
size_t nb2, |
|
size_t nb3, |
|
size_t offset); |
|
|
|
GGML_API struct ggml_tensor * ggml_permute( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int axis0, |
|
int axis1, |
|
int axis2, |
|
int axis3); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_transpose( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_get_rows( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_get_rows_back( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
struct ggml_tensor * c); |
|
|
|
GGML_API struct ggml_tensor * ggml_diag( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_diag_mask_inf( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int n_past); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int n_past); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_diag_mask_zero( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int n_past); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int n_past); |
|
|
|
GGML_API struct ggml_tensor * ggml_soft_max( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_soft_max_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a); |
|
|
|
GGML_API struct ggml_tensor * ggml_soft_max_back( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_soft_max_back_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
|
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_rope( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int n_past, |
|
int n_dims, |
|
int mode); |
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_rope_inplace( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int n_past, |
|
int n_dims, |
|
int mode); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_rope_back( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int n_past, |
|
int n_dims, |
|
int mode); |
|
|
|
|
|
|
|
struct ggml_tensor * ggml_alibi( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
int n_past, |
|
int n_head, |
|
float bias_max); |
|
|
|
|
|
|
|
struct ggml_tensor * ggml_clamp( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
float min, |
|
float max); |
|
|
|
|
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_conv_1d_1s( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_conv_1d_2s( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_flash_attn( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * q, |
|
struct ggml_tensor * k, |
|
struct ggml_tensor * v, |
|
bool masked); |
|
|
|
GGML_API struct ggml_tensor * ggml_flash_attn_back( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * q, |
|
struct ggml_tensor * k, |
|
struct ggml_tensor * v, |
|
struct ggml_tensor * d, |
|
bool masked); |
|
|
|
GGML_API struct ggml_tensor * ggml_flash_ff( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b0, |
|
struct ggml_tensor * b1, |
|
struct ggml_tensor * c0, |
|
struct ggml_tensor * c1); |
|
|
|
|
|
typedef void (*ggml_unary_op_f32_t)(const int, float *, const float *); |
|
typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *); |
|
|
|
GGML_API struct ggml_tensor * ggml_map_unary_f32( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
ggml_unary_op_f32_t fun); |
|
|
|
GGML_API struct ggml_tensor * ggml_map_binary_f32( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
ggml_binary_op_f32_t fun); |
|
|
|
|
|
|
|
GGML_API struct ggml_tensor * ggml_cross_entropy_loss( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b); |
|
|
|
GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * a, |
|
struct ggml_tensor * b, |
|
struct ggml_tensor * c); |
|
|
|
|
|
|
|
|
|
|
|
GGML_API void ggml_set_param( |
|
struct ggml_context * ctx, |
|
struct ggml_tensor * tensor); |
|
|
|
GGML_API void ggml_build_forward_expand(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor); |
|
|
|
GGML_API struct ggml_cgraph ggml_build_forward (struct ggml_tensor * tensor); |
|
GGML_API struct ggml_cgraph ggml_build_backward(struct ggml_context * ctx, struct ggml_cgraph * gf, bool keep); |
|
|
|
GGML_API void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph); |
|
GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); |
|
|
|
GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name); |
|
|
|
GGML_API void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname); |
|
GGML_API struct ggml_cgraph ggml_graph_import(const char * fname, struct ggml_context ** ctx_data, struct ggml_context ** ctx_eval); |
|
|
|
|
|
GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph); |
|
|
|
|
|
GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename); |
|
|
|
|
|
|
|
|
|
|
|
|
|
enum ggml_opt_type { |
|
GGML_OPT_ADAM, |
|
GGML_OPT_LBFGS, |
|
}; |
|
|
|
|
|
enum ggml_linesearch { |
|
GGML_LINESEARCH_DEFAULT = 1, |
|
|
|
GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0, |
|
GGML_LINESEARCH_BACKTRACKING_WOLFE = 1, |
|
GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2, |
|
}; |
|
|
|
|
|
enum ggml_opt_result { |
|
GGML_OPT_OK = 0, |
|
GGML_OPT_DID_NOT_CONVERGE, |
|
GGML_OPT_NO_CONTEXT, |
|
GGML_OPT_INVALID_WOLFE, |
|
GGML_OPT_FAIL, |
|
|
|
GGML_LINESEARCH_FAIL = -128, |
|
GGML_LINESEARCH_MINIMUM_STEP, |
|
GGML_LINESEARCH_MAXIMUM_STEP, |
|
GGML_LINESEARCH_MAXIMUM_ITERATIONS, |
|
GGML_LINESEARCH_INVALID_PARAMETERS, |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct ggml_opt_params { |
|
enum ggml_opt_type type; |
|
|
|
int n_threads; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int past; |
|
float delta; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int max_no_improvement; |
|
|
|
bool print_forward_graph; |
|
bool print_backward_graph; |
|
|
|
|
|
struct { |
|
int n_iter; |
|
|
|
float sched; |
|
float decay; |
|
float alpha; |
|
float beta1; |
|
float beta2; |
|
float eps; |
|
float eps_f; |
|
float eps_g; |
|
} adam; |
|
|
|
|
|
struct { |
|
int m; |
|
int n_iter; |
|
int max_linesearch; |
|
|
|
float eps; |
|
float ftol; |
|
float wolfe; |
|
float min_step; |
|
float max_step; |
|
|
|
enum ggml_linesearch linesearch; |
|
} lbfgs; |
|
}; |
|
|
|
struct ggml_opt_context { |
|
struct ggml_context * ctx; |
|
struct ggml_opt_params params; |
|
|
|
int iter; |
|
int64_t nx; |
|
|
|
bool just_initialized; |
|
|
|
struct { |
|
struct ggml_tensor * x; |
|
struct ggml_tensor * g1; |
|
struct ggml_tensor * g2; |
|
struct ggml_tensor * m; |
|
struct ggml_tensor * v; |
|
struct ggml_tensor * mh; |
|
struct ggml_tensor * vh; |
|
struct ggml_tensor * pf; |
|
float fx_best; |
|
float fx_prev; |
|
int n_no_improvement; |
|
} adam; |
|
|
|
struct { |
|
struct ggml_tensor * x; |
|
struct ggml_tensor * xp; |
|
struct ggml_tensor * g; |
|
struct ggml_tensor * gp; |
|
struct ggml_tensor * d; |
|
struct ggml_tensor * pf; |
|
struct ggml_tensor * lmal; |
|
struct ggml_tensor * lmys; |
|
struct ggml_tensor * lms; |
|
struct ggml_tensor * lmy; |
|
float fx_best; |
|
float step; |
|
int j; |
|
int k; |
|
int end; |
|
int n_no_improvement; |
|
} lbfgs; |
|
}; |
|
|
|
GGML_API struct ggml_opt_params ggml_opt_default_params(enum ggml_opt_type type); |
|
|
|
|
|
GGML_API enum ggml_opt_result ggml_opt( |
|
struct ggml_context * ctx, |
|
struct ggml_opt_params params, |
|
struct ggml_tensor * f); |
|
|
|
|
|
GGML_API void ggml_opt_init( |
|
struct ggml_context * ctx, |
|
struct ggml_opt_context * opt, |
|
struct ggml_opt_params params, |
|
int64_t nx); |
|
|
|
|
|
GGML_API enum ggml_opt_result ggml_opt_resume( |
|
struct ggml_context * ctx, |
|
struct ggml_opt_context * opt, |
|
struct ggml_tensor * f); |
|
|
|
|
|
GGML_API enum ggml_opt_result ggml_opt_resume_g( |
|
struct ggml_context * ctx, |
|
struct ggml_opt_context * opt, |
|
struct ggml_tensor * f, |
|
struct ggml_cgraph * gf, |
|
struct ggml_cgraph * gb); |
|
|
|
|
|
|
|
|
|
|
|
GGML_API size_t ggml_quantize_q4_0(const float * src, void * dst, int n, int k, int64_t * hist); |
|
GGML_API size_t ggml_quantize_q4_1(const float * src, void * dst, int n, int k, int64_t * hist); |
|
GGML_API size_t ggml_quantize_q5_0(const float * src, void * dst, int n, int k, int64_t * hist); |
|
GGML_API size_t ggml_quantize_q5_1(const float * src, void * dst, int n, int k, int64_t * hist); |
|
GGML_API size_t ggml_quantize_q8_0(const float * src, void * dst, int n, int k, int64_t * hist); |
|
|
|
GGML_API size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, int start, int n, int64_t * hist); |
|
|
|
|
|
|
|
|
|
|
|
GGML_API int ggml_cpu_has_avx (void); |
|
GGML_API int ggml_cpu_has_avx2 (void); |
|
GGML_API int ggml_cpu_has_avx512 (void); |
|
GGML_API int ggml_cpu_has_avx512_vbmi(void); |
|
GGML_API int ggml_cpu_has_avx512_vnni(void); |
|
GGML_API int ggml_cpu_has_fma (void); |
|
GGML_API int ggml_cpu_has_neon (void); |
|
GGML_API int ggml_cpu_has_arm_fma (void); |
|
GGML_API int ggml_cpu_has_f16c (void); |
|
GGML_API int ggml_cpu_has_fp16_va (void); |
|
GGML_API int ggml_cpu_has_wasm_simd (void); |
|
GGML_API int ggml_cpu_has_blas (void); |
|
GGML_API int ggml_cpu_has_cublas (void); |
|
GGML_API int ggml_cpu_has_clblast (void); |
|
GGML_API int ggml_cpu_has_gpublas (void); |
|
GGML_API int ggml_cpu_has_sse3 (void); |
|
GGML_API int ggml_cpu_has_vsx (void); |
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus |
|
|
|
#define GGML_RESTRICT |
|
#else |
|
#define GGML_RESTRICT restrict |
|
#endif |
|
typedef void (*dequantize_row_q_t)(const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int k); |
|
typedef void (*quantize_row_q_t) (const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int k); |
|
typedef void (*vec_dot_q_t) (const int n, float * GGML_RESTRICT s, const void * GGML_RESTRICT x, const void * GGML_RESTRICT y); |
|
|
|
typedef struct { |
|
dequantize_row_q_t dequantize_row_q; |
|
quantize_row_q_t quantize_row_q; |
|
quantize_row_q_t quantize_row_q_reference; |
|
quantize_row_q_t quantize_row_q_dot; |
|
vec_dot_q_t vec_dot_q; |
|
enum ggml_type vec_dot_type; |
|
} quantize_fns_t; |
|
|
|
quantize_fns_t ggml_internal_get_quantize_fn(size_t i); |
|
|
|
#ifdef __cplusplus |
|
} |
|
#endif |
|
|