cui-llama.rn 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +4 -23
  2. package/android/build.gradle +12 -3
  3. package/android/src/main/CMakeLists.txt +13 -7
  4. package/android/src/main/java/com/rnllama/LlamaContext.java +27 -20
  5. package/android/src/main/java/com/rnllama/RNLlama.java +5 -1
  6. package/android/src/main/jni.cpp +15 -12
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  11. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  12. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  14. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  15. package/cpp/README.md +1 -1
  16. package/cpp/common.cpp +158 -267
  17. package/cpp/common.h +46 -12
  18. package/cpp/ggml-alloc.c +1042 -1037
  19. package/cpp/ggml-backend-impl.h +255 -256
  20. package/cpp/ggml-backend-reg.cpp +582 -582
  21. package/cpp/ggml-backend.cpp +2002 -2002
  22. package/cpp/ggml-backend.h +354 -352
  23. package/cpp/ggml-common.h +1853 -1853
  24. package/cpp/ggml-cpp.h +39 -39
  25. package/cpp/ggml-cpu-aarch64.cpp +4247 -4247
  26. package/cpp/ggml-cpu-aarch64.h +8 -8
  27. package/cpp/ggml-cpu-impl.h +386 -386
  28. package/cpp/ggml-cpu-quants.c +10920 -10839
  29. package/cpp/ggml-cpu-traits.cpp +36 -36
  30. package/cpp/ggml-cpu-traits.h +38 -38
  31. package/cpp/ggml-cpu.c +329 -60
  32. package/cpp/ggml-cpu.cpp +10 -2
  33. package/cpp/ggml-cpu.h +135 -135
  34. package/cpp/ggml-impl.h +567 -567
  35. package/cpp/ggml-metal-impl.h +17 -17
  36. package/cpp/ggml-metal.m +4884 -4884
  37. package/cpp/ggml-quants.c +5238 -5238
  38. package/cpp/ggml-threading.h +14 -14
  39. package/cpp/ggml.c +6514 -6448
  40. package/cpp/ggml.h +2194 -2163
  41. package/cpp/gguf.cpp +1329 -1325
  42. package/cpp/gguf.h +202 -202
  43. package/cpp/json-schema-to-grammar.cpp +1045 -1045
  44. package/cpp/json-schema-to-grammar.h +8 -8
  45. package/cpp/json.hpp +24766 -24766
  46. package/cpp/llama-adapter.cpp +347 -346
  47. package/cpp/llama-adapter.h +74 -73
  48. package/cpp/llama-arch.cpp +1487 -1434
  49. package/cpp/llama-arch.h +400 -395
  50. package/cpp/llama-batch.cpp +368 -368
  51. package/cpp/llama-batch.h +88 -88
  52. package/cpp/llama-chat.cpp +578 -567
  53. package/cpp/llama-chat.h +52 -51
  54. package/cpp/llama-context.cpp +1775 -1771
  55. package/cpp/llama-context.h +128 -128
  56. package/cpp/llama-cparams.cpp +1 -1
  57. package/cpp/llama-cparams.h +37 -37
  58. package/cpp/llama-cpp.h +30 -30
  59. package/cpp/llama-grammar.cpp +1139 -1139
  60. package/cpp/llama-grammar.h +143 -143
  61. package/cpp/llama-hparams.cpp +71 -71
  62. package/cpp/llama-hparams.h +139 -140
  63. package/cpp/llama-impl.cpp +167 -167
  64. package/cpp/llama-impl.h +61 -61
  65. package/cpp/llama-kv-cache.cpp +718 -718
  66. package/cpp/llama-kv-cache.h +218 -218
  67. package/cpp/llama-mmap.cpp +2 -1
  68. package/cpp/llama-mmap.h +67 -67
  69. package/cpp/llama-model-loader.cpp +1124 -1011
  70. package/cpp/llama-model-loader.h +167 -158
  71. package/cpp/llama-model.cpp +3997 -2202
  72. package/cpp/llama-model.h +370 -391
  73. package/cpp/llama-sampling.cpp +2408 -2406
  74. package/cpp/llama-sampling.h +32 -48
  75. package/cpp/llama-vocab.cpp +3247 -1982
  76. package/cpp/llama-vocab.h +125 -182
  77. package/cpp/llama.cpp +416 -2886
  78. package/cpp/llama.h +1323 -1285
  79. package/cpp/log.cpp +401 -401
  80. package/cpp/log.h +121 -121
  81. package/cpp/rn-llama.cpp +822 -0
  82. package/cpp/rn-llama.h +123 -0
  83. package/cpp/rn-llama.hpp +18 -12
  84. package/cpp/sampling.cpp +505 -500
  85. package/cpp/sgemm.cpp +2597 -2597
  86. package/cpp/speculative.cpp +277 -274
  87. package/cpp/speculative.h +28 -28
  88. package/cpp/unicode.cpp +2 -3
  89. package/ios/CMakeLists.txt +99 -0
  90. package/ios/RNLlama.h +5 -1
  91. package/ios/RNLlama.mm +2 -2
  92. package/ios/RNLlamaContext.h +8 -1
  93. package/ios/RNLlamaContext.mm +15 -11
  94. package/ios/rnllama.xcframework/Info.plist +74 -0
  95. package/jest/mock.js +3 -2
  96. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  97. package/lib/commonjs/index.js +4 -2
  98. package/lib/commonjs/index.js.map +1 -1
  99. package/lib/module/NativeRNLlama.js.map +1 -1
  100. package/lib/module/index.js +4 -2
  101. package/lib/module/index.js.map +1 -1
  102. package/lib/typescript/NativeRNLlama.d.ts +5 -1
  103. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  104. package/lib/typescript/index.d.ts.map +1 -1
  105. package/llama-rn.podspec +8 -2
  106. package/package.json +5 -2
  107. package/src/NativeRNLlama.ts +5 -1
  108. package/src/index.ts +9 -2
package/cpp/gguf.h CHANGED
@@ -1,202 +1,202 @@
1
- // This file contains functionality related to "GGUF" files, the binary file format used by ggml.
2
- // GGUF files have the following structure:
3
- //
4
- // 1. File magic "GGUF" (4 bytes).
5
- // 2. File version (uint32_t).
6
- // 3. Number of ggml tensors in file (int64_t).
7
- // 4. Number of key-value-pairs in file (int64_t).
8
- // 5. For each KV pair:
9
- // 1. The key (string).
10
- // 2. The value type (lm_gguf_type).
11
- // 3a. If the value type is LM_GGUF_TYPE_ARRAY:
12
- // 1. The type of the array (lm_gguf_type).
13
- // 2. The number of elements in the array (uint64_t).
14
- // 3. The binary representation of each element in the array.
15
- // 3b. Otherwise:
16
- // 1. The binary representation of the value.
17
- // 6. For each ggml tensor:
18
- // 1. The tensor name (string).
19
- // 2. The number of dimensions of the tensor (uint32_t).
20
- // 3. For each dimension:
21
- // 1. The size of the tensor in the dimension (int64_t).
22
- // 4. The tensor data type (lm_ggml_type).
23
- // 5. The tensor data offset in the tensor data binary blob (uint64_t).
24
- // 7. The tensor data binary blob (optional, aligned).
25
- //
26
- // Strings are serialized as the string length (uint64_t) followed by the C string without the null terminator.
27
- // All enums are stored as int32_t.
28
- // All bool values are stored as int8_t.
29
- // If the special key "general.alignment" (uint32_t) is defined it is used for alignment,
30
- // otherwise LM_GGUF_DEFAULT_ALIGNMENT is used.
31
- //
32
- // Module maintainer: Johannes Gäßler (@JohannesGaessler, johannesg@5d6.de)
33
-
34
- #pragma once
35
-
36
- #include "ggml.h"
37
-
38
- #include <stdbool.h>
39
- #include <stdint.h>
40
-
41
- #define LM_GGUF_MAGIC "GGUF"
42
- #define LM_GGUF_VERSION 3
43
-
44
- #define LM_GGUF_KEY_GENERAL_ALIGNMENT "general.alignment"
45
-
46
- #define LM_GGUF_DEFAULT_ALIGNMENT 32
47
-
48
- #ifdef __cplusplus
49
- extern "C" {
50
- #endif
51
-
52
- // types that can be stored as GGUF KV data
53
- enum lm_gguf_type {
54
- LM_GGUF_TYPE_UINT8 = 0,
55
- LM_GGUF_TYPE_INT8 = 1,
56
- LM_GGUF_TYPE_UINT16 = 2,
57
- LM_GGUF_TYPE_INT16 = 3,
58
- LM_GGUF_TYPE_UINT32 = 4,
59
- LM_GGUF_TYPE_INT32 = 5,
60
- LM_GGUF_TYPE_FLOAT32 = 6,
61
- LM_GGUF_TYPE_BOOL = 7,
62
- LM_GGUF_TYPE_STRING = 8,
63
- LM_GGUF_TYPE_ARRAY = 9,
64
- LM_GGUF_TYPE_UINT64 = 10,
65
- LM_GGUF_TYPE_INT64 = 11,
66
- LM_GGUF_TYPE_FLOAT64 = 12,
67
- LM_GGUF_TYPE_COUNT, // marks the end of the enum
68
- };
69
-
70
- struct lm_gguf_context;
71
-
72
- struct lm_gguf_init_params {
73
- bool no_alloc;
74
-
75
- // if not NULL, create a lm_ggml_context and allocate the tensor data in it
76
- struct lm_ggml_context ** ctx;
77
- };
78
-
79
- LM_GGML_API struct lm_gguf_context * lm_gguf_init_empty(void);
80
- LM_GGML_API struct lm_gguf_context * lm_gguf_init_from_file(const char * fname, struct lm_gguf_init_params params);
81
- //LM_GGML_API struct lm_gguf_context * lm_gguf_init_from_buffer(..);
82
-
83
- LM_GGML_API void lm_gguf_free(struct lm_gguf_context * ctx);
84
-
85
- LM_GGML_API const char * lm_gguf_type_name(enum lm_gguf_type type);
86
-
87
- LM_GGML_API uint32_t lm_gguf_get_version (const struct lm_gguf_context * ctx);
88
- LM_GGML_API size_t lm_gguf_get_alignment (const struct lm_gguf_context * ctx);
89
- LM_GGML_API size_t lm_gguf_get_data_offset(const struct lm_gguf_context * ctx);
90
-
91
- LM_GGML_API int64_t lm_gguf_get_n_kv(const struct lm_gguf_context * ctx);
92
- LM_GGML_API int64_t lm_gguf_find_key(const struct lm_gguf_context * ctx, const char * key); // returns -1 if key is not found
93
- LM_GGML_API const char * lm_gguf_get_key (const struct lm_gguf_context * ctx, int64_t key_id);
94
-
95
- LM_GGML_API enum lm_gguf_type lm_gguf_get_kv_type (const struct lm_gguf_context * ctx, int64_t key_id);
96
- LM_GGML_API enum lm_gguf_type lm_gguf_get_arr_type(const struct lm_gguf_context * ctx, int64_t key_id);
97
-
98
- // will abort if the wrong type is used for the key
99
- LM_GGML_API uint8_t lm_gguf_get_val_u8 (const struct lm_gguf_context * ctx, int64_t key_id);
100
- LM_GGML_API int8_t lm_gguf_get_val_i8 (const struct lm_gguf_context * ctx, int64_t key_id);
101
- LM_GGML_API uint16_t lm_gguf_get_val_u16 (const struct lm_gguf_context * ctx, int64_t key_id);
102
- LM_GGML_API int16_t lm_gguf_get_val_i16 (const struct lm_gguf_context * ctx, int64_t key_id);
103
- LM_GGML_API uint32_t lm_gguf_get_val_u32 (const struct lm_gguf_context * ctx, int64_t key_id);
104
- LM_GGML_API int32_t lm_gguf_get_val_i32 (const struct lm_gguf_context * ctx, int64_t key_id);
105
- LM_GGML_API float lm_gguf_get_val_f32 (const struct lm_gguf_context * ctx, int64_t key_id);
106
- LM_GGML_API uint64_t lm_gguf_get_val_u64 (const struct lm_gguf_context * ctx, int64_t key_id);
107
- LM_GGML_API int64_t lm_gguf_get_val_i64 (const struct lm_gguf_context * ctx, int64_t key_id);
108
- LM_GGML_API double lm_gguf_get_val_f64 (const struct lm_gguf_context * ctx, int64_t key_id);
109
- LM_GGML_API bool lm_gguf_get_val_bool(const struct lm_gguf_context * ctx, int64_t key_id);
110
- LM_GGML_API const char * lm_gguf_get_val_str (const struct lm_gguf_context * ctx, int64_t key_id);
111
- LM_GGML_API const void * lm_gguf_get_val_data(const struct lm_gguf_context * ctx, int64_t key_id);
112
- LM_GGML_API size_t lm_gguf_get_arr_n (const struct lm_gguf_context * ctx, int64_t key_id);
113
-
114
- // get raw pointer to the first element of the array with the given key_id
115
- // for bool arrays, note that they are always stored as int8 on all platforms (usually this makes no difference)
116
- LM_GGML_API const void * lm_gguf_get_arr_data(const struct lm_gguf_context * ctx, int64_t key_id);
117
-
118
- // get ith C string from array with given key_id
119
- LM_GGML_API const char * lm_gguf_get_arr_str (const struct lm_gguf_context * ctx, int64_t key_id, size_t i);
120
-
121
- LM_GGML_API int64_t lm_gguf_get_n_tensors (const struct lm_gguf_context * ctx);
122
- LM_GGML_API int64_t lm_gguf_find_tensor (const struct lm_gguf_context * ctx, const char * name); // returns -1 if the tensor is not found
123
- LM_GGML_API size_t lm_gguf_get_tensor_offset(const struct lm_gguf_context * ctx, int64_t tensor_id);
124
- LM_GGML_API const char * lm_gguf_get_tensor_name (const struct lm_gguf_context * ctx, int64_t tensor_id);
125
- LM_GGML_API enum lm_ggml_type lm_gguf_get_tensor_type (const struct lm_gguf_context * ctx, int64_t tensor_id);
126
- LM_GGML_API size_t lm_gguf_get_tensor_size (const struct lm_gguf_context * ctx, int64_t tensor_id);
127
-
128
- // removes key if it exists, returns id that the key had prior to removal (-1 if it didn't exist)
129
- LM_GGML_API int64_t lm_gguf_remove_key(struct lm_gguf_context * ctx, const char * key);
130
-
131
- // overrides an existing KV pair or adds a new one, the new KV pair is always at the back
132
- LM_GGML_API void lm_gguf_set_val_u8 (struct lm_gguf_context * ctx, const char * key, uint8_t val);
133
- LM_GGML_API void lm_gguf_set_val_i8 (struct lm_gguf_context * ctx, const char * key, int8_t val);
134
- LM_GGML_API void lm_gguf_set_val_u16 (struct lm_gguf_context * ctx, const char * key, uint16_t val);
135
- LM_GGML_API void lm_gguf_set_val_i16 (struct lm_gguf_context * ctx, const char * key, int16_t val);
136
- LM_GGML_API void lm_gguf_set_val_u32 (struct lm_gguf_context * ctx, const char * key, uint32_t val);
137
- LM_GGML_API void lm_gguf_set_val_i32 (struct lm_gguf_context * ctx, const char * key, int32_t val);
138
- LM_GGML_API void lm_gguf_set_val_f32 (struct lm_gguf_context * ctx, const char * key, float val);
139
- LM_GGML_API void lm_gguf_set_val_u64 (struct lm_gguf_context * ctx, const char * key, uint64_t val);
140
- LM_GGML_API void lm_gguf_set_val_i64 (struct lm_gguf_context * ctx, const char * key, int64_t val);
141
- LM_GGML_API void lm_gguf_set_val_f64 (struct lm_gguf_context * ctx, const char * key, double val);
142
- LM_GGML_API void lm_gguf_set_val_bool(struct lm_gguf_context * ctx, const char * key, bool val);
143
- LM_GGML_API void lm_gguf_set_val_str (struct lm_gguf_context * ctx, const char * key, const char * val);
144
-
145
- // creates a new array with n elements of the given type and copies the corresponding number of bytes from data
146
- LM_GGML_API void lm_gguf_set_arr_data(struct lm_gguf_context * ctx, const char * key, enum lm_gguf_type type, const void * data, size_t n);
147
-
148
- // creates a new array with n strings and copies the corresponding strings from data
149
- LM_GGML_API void lm_gguf_set_arr_str (struct lm_gguf_context * ctx, const char * key, const char ** data, size_t n);
150
-
151
- // set or add KV pairs from another context
152
- LM_GGML_API void lm_gguf_set_kv(struct lm_gguf_context * ctx, const struct lm_gguf_context * src);
153
-
154
- // add tensor to GGUF context, tensor name must be unique
155
- LM_GGML_API void lm_gguf_add_tensor(struct lm_gguf_context * ctx, const struct lm_ggml_tensor * tensor);
156
-
157
- // after changing a tensor's type, the offsets of all tensors with higher indices are immediately recalculated
158
- // in such a way that the tensor data remains as one contiguous block (except for padding)
159
- LM_GGML_API void lm_gguf_set_tensor_type(struct lm_gguf_context * ctx, const char * name, enum lm_ggml_type type);
160
-
161
- // assumes that at least lm_gguf_get_tensor_size bytes can be read from data
162
- LM_GGML_API void lm_gguf_set_tensor_data(struct lm_gguf_context * ctx, const char * name, const void * data);
163
-
164
- // writing gguf files can be done in 3 ways:
165
- //
166
- // - write the entire lm_gguf_context to a binary file in a single pass:
167
- //
168
- // lm_gguf_write_to_file(ctx, fname, /*only_meta =*/ false);
169
- //
170
- // - write only the meta data to a file, then re-open the file and append the tensor data:
171
- //
172
- // lm_gguf_write_to_file(ctx, fname, /*only_meta =*/ true);
173
- // FILE * f = fopen(fname, "ab");
174
- // fwrite(f, ...); // write tensor data
175
- // fclose(f);
176
- //
177
- // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
178
- //
179
- // FILE * f = fopen(fname, "wb");
180
- // const size_t size_meta = lm_gguf_get_meta_size(ctx);
181
- // fseek(f, size_meta, SEEK_SET);
182
- // fwrite(f, ...); // write tensor data
183
- // void * data = malloc(size_meta);
184
- // lm_gguf_get_meta_data(ctx, data);
185
- // rewind(f);
186
- // fwrite(data, 1, data, f);
187
- // free(data);
188
- // fclose(f);
189
- //
190
-
191
- // write the entire context to a binary file
192
- LM_GGML_API bool lm_gguf_write_to_file(const struct lm_gguf_context * ctx, const char * fname, bool only_meta);
193
-
194
- // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
195
- LM_GGML_API size_t lm_gguf_get_meta_size(const struct lm_gguf_context * ctx);
196
-
197
- // writes the meta data to pointer "data"
198
- LM_GGML_API void lm_gguf_get_meta_data(const struct lm_gguf_context * ctx, void * data);
199
-
200
- #ifdef __cplusplus
201
- }
202
- #endif
1
+ // This file contains functionality related to "GGUF" files, the binary file format used by ggml.
2
+ // GGUF files have the following structure:
3
+ //
4
+ // 1. File magic "GGUF" (4 bytes).
5
+ // 2. File version (uint32_t).
6
+ // 3. Number of ggml tensors in file (int64_t).
7
+ // 4. Number of key-value-pairs in file (int64_t).
8
+ // 5. For each KV pair:
9
+ // 1. The key (string).
10
+ // 2. The value type (lm_gguf_type).
11
+ // 3a. If the value type is LM_GGUF_TYPE_ARRAY:
12
+ // 1. The type of the array (lm_gguf_type).
13
+ // 2. The number of elements in the array (uint64_t).
14
+ // 3. The binary representation of each element in the array.
15
+ // 3b. Otherwise:
16
+ // 1. The binary representation of the value.
17
+ // 6. For each ggml tensor:
18
+ // 1. The tensor name (string).
19
+ // 2. The number of dimensions of the tensor (uint32_t).
20
+ // 3. For each dimension:
21
+ // 1. The size of the tensor in the dimension (int64_t).
22
+ // 4. The tensor data type (lm_ggml_type).
23
+ // 5. The tensor data offset in the tensor data binary blob (uint64_t).
24
+ // 7. The tensor data binary blob (optional, aligned).
25
+ //
26
+ // Strings are serialized as the string length (uint64_t) followed by the C string without the null terminator.
27
+ // All enums are stored as int32_t.
28
+ // All bool values are stored as int8_t.
29
+ // If the special key "general.alignment" (uint32_t) is defined it is used for alignment,
30
+ // otherwise LM_GGUF_DEFAULT_ALIGNMENT is used.
31
+ //
32
+ // Module maintainer: Johannes Gäßler (@JohannesGaessler, johannesg@5d6.de)
33
+
34
+ #pragma once
35
+
36
+ #include "ggml.h"
37
+
38
+ #include <stdbool.h>
39
+ #include <stdint.h>
40
+
41
+ #define LM_GGUF_MAGIC "GGUF"
42
+ #define LM_GGUF_VERSION 3
43
+
44
+ #define LM_GGUF_KEY_GENERAL_ALIGNMENT "general.alignment"
45
+
46
+ #define LM_GGUF_DEFAULT_ALIGNMENT 32
47
+
48
+ #ifdef __cplusplus
49
+ extern "C" {
50
+ #endif
51
+
52
+ // types that can be stored as GGUF KV data
53
+ enum lm_gguf_type {
54
+ LM_GGUF_TYPE_UINT8 = 0,
55
+ LM_GGUF_TYPE_INT8 = 1,
56
+ LM_GGUF_TYPE_UINT16 = 2,
57
+ LM_GGUF_TYPE_INT16 = 3,
58
+ LM_GGUF_TYPE_UINT32 = 4,
59
+ LM_GGUF_TYPE_INT32 = 5,
60
+ LM_GGUF_TYPE_FLOAT32 = 6,
61
+ LM_GGUF_TYPE_BOOL = 7,
62
+ LM_GGUF_TYPE_STRING = 8,
63
+ LM_GGUF_TYPE_ARRAY = 9,
64
+ LM_GGUF_TYPE_UINT64 = 10,
65
+ LM_GGUF_TYPE_INT64 = 11,
66
+ LM_GGUF_TYPE_FLOAT64 = 12,
67
+ LM_GGUF_TYPE_COUNT, // marks the end of the enum
68
+ };
69
+
70
+ struct lm_gguf_context;
71
+
72
+ struct lm_gguf_init_params {
73
+ bool no_alloc;
74
+
75
+ // if not NULL, create a lm_ggml_context and allocate the tensor data in it
76
+ struct lm_ggml_context ** ctx;
77
+ };
78
+
79
+ LM_GGML_API struct lm_gguf_context * lm_gguf_init_empty(void);
80
+ LM_GGML_API struct lm_gguf_context * lm_gguf_init_from_file(const char * fname, struct lm_gguf_init_params params);
81
+ //LM_GGML_API struct lm_gguf_context * lm_gguf_init_from_buffer(..);
82
+
83
+ LM_GGML_API void lm_gguf_free(struct lm_gguf_context * ctx);
84
+
85
+ LM_GGML_API const char * lm_gguf_type_name(enum lm_gguf_type type);
86
+
87
+ LM_GGML_API uint32_t lm_gguf_get_version (const struct lm_gguf_context * ctx);
88
+ LM_GGML_API size_t lm_gguf_get_alignment (const struct lm_gguf_context * ctx);
89
+ LM_GGML_API size_t lm_gguf_get_data_offset(const struct lm_gguf_context * ctx);
90
+
91
+ LM_GGML_API int64_t lm_gguf_get_n_kv(const struct lm_gguf_context * ctx);
92
+ LM_GGML_API int64_t lm_gguf_find_key(const struct lm_gguf_context * ctx, const char * key); // returns -1 if key is not found
93
+ LM_GGML_API const char * lm_gguf_get_key (const struct lm_gguf_context * ctx, int64_t key_id);
94
+
95
+ LM_GGML_API enum lm_gguf_type lm_gguf_get_kv_type (const struct lm_gguf_context * ctx, int64_t key_id);
96
+ LM_GGML_API enum lm_gguf_type lm_gguf_get_arr_type(const struct lm_gguf_context * ctx, int64_t key_id);
97
+
98
+ // will abort if the wrong type is used for the key
99
+ LM_GGML_API uint8_t lm_gguf_get_val_u8 (const struct lm_gguf_context * ctx, int64_t key_id);
100
+ LM_GGML_API int8_t lm_gguf_get_val_i8 (const struct lm_gguf_context * ctx, int64_t key_id);
101
+ LM_GGML_API uint16_t lm_gguf_get_val_u16 (const struct lm_gguf_context * ctx, int64_t key_id);
102
+ LM_GGML_API int16_t lm_gguf_get_val_i16 (const struct lm_gguf_context * ctx, int64_t key_id);
103
+ LM_GGML_API uint32_t lm_gguf_get_val_u32 (const struct lm_gguf_context * ctx, int64_t key_id);
104
+ LM_GGML_API int32_t lm_gguf_get_val_i32 (const struct lm_gguf_context * ctx, int64_t key_id);
105
+ LM_GGML_API float lm_gguf_get_val_f32 (const struct lm_gguf_context * ctx, int64_t key_id);
106
+ LM_GGML_API uint64_t lm_gguf_get_val_u64 (const struct lm_gguf_context * ctx, int64_t key_id);
107
+ LM_GGML_API int64_t lm_gguf_get_val_i64 (const struct lm_gguf_context * ctx, int64_t key_id);
108
+ LM_GGML_API double lm_gguf_get_val_f64 (const struct lm_gguf_context * ctx, int64_t key_id);
109
+ LM_GGML_API bool lm_gguf_get_val_bool(const struct lm_gguf_context * ctx, int64_t key_id);
110
+ LM_GGML_API const char * lm_gguf_get_val_str (const struct lm_gguf_context * ctx, int64_t key_id);
111
+ LM_GGML_API const void * lm_gguf_get_val_data(const struct lm_gguf_context * ctx, int64_t key_id);
112
+ LM_GGML_API size_t lm_gguf_get_arr_n (const struct lm_gguf_context * ctx, int64_t key_id);
113
+
114
+ // get raw pointer to the first element of the array with the given key_id
115
+ // for bool arrays, note that they are always stored as int8 on all platforms (usually this makes no difference)
116
+ LM_GGML_API const void * lm_gguf_get_arr_data(const struct lm_gguf_context * ctx, int64_t key_id);
117
+
118
+ // get ith C string from array with given key_id
119
+ LM_GGML_API const char * lm_gguf_get_arr_str (const struct lm_gguf_context * ctx, int64_t key_id, size_t i);
120
+
121
+ LM_GGML_API int64_t lm_gguf_get_n_tensors (const struct lm_gguf_context * ctx);
122
+ LM_GGML_API int64_t lm_gguf_find_tensor (const struct lm_gguf_context * ctx, const char * name); // returns -1 if the tensor is not found
123
+ LM_GGML_API size_t lm_gguf_get_tensor_offset(const struct lm_gguf_context * ctx, int64_t tensor_id);
124
+ LM_GGML_API const char * lm_gguf_get_tensor_name (const struct lm_gguf_context * ctx, int64_t tensor_id);
125
+ LM_GGML_API enum lm_ggml_type lm_gguf_get_tensor_type (const struct lm_gguf_context * ctx, int64_t tensor_id);
126
+ LM_GGML_API size_t lm_gguf_get_tensor_size (const struct lm_gguf_context * ctx, int64_t tensor_id);
127
+
128
+ // removes key if it exists, returns id that the key had prior to removal (-1 if it didn't exist)
129
+ LM_GGML_API int64_t lm_gguf_remove_key(struct lm_gguf_context * ctx, const char * key);
130
+
131
+ // overrides an existing KV pair or adds a new one, the new KV pair is always at the back
132
+ LM_GGML_API void lm_gguf_set_val_u8 (struct lm_gguf_context * ctx, const char * key, uint8_t val);
133
+ LM_GGML_API void lm_gguf_set_val_i8 (struct lm_gguf_context * ctx, const char * key, int8_t val);
134
+ LM_GGML_API void lm_gguf_set_val_u16 (struct lm_gguf_context * ctx, const char * key, uint16_t val);
135
+ LM_GGML_API void lm_gguf_set_val_i16 (struct lm_gguf_context * ctx, const char * key, int16_t val);
136
+ LM_GGML_API void lm_gguf_set_val_u32 (struct lm_gguf_context * ctx, const char * key, uint32_t val);
137
+ LM_GGML_API void lm_gguf_set_val_i32 (struct lm_gguf_context * ctx, const char * key, int32_t val);
138
+ LM_GGML_API void lm_gguf_set_val_f32 (struct lm_gguf_context * ctx, const char * key, float val);
139
+ LM_GGML_API void lm_gguf_set_val_u64 (struct lm_gguf_context * ctx, const char * key, uint64_t val);
140
+ LM_GGML_API void lm_gguf_set_val_i64 (struct lm_gguf_context * ctx, const char * key, int64_t val);
141
+ LM_GGML_API void lm_gguf_set_val_f64 (struct lm_gguf_context * ctx, const char * key, double val);
142
+ LM_GGML_API void lm_gguf_set_val_bool(struct lm_gguf_context * ctx, const char * key, bool val);
143
+ LM_GGML_API void lm_gguf_set_val_str (struct lm_gguf_context * ctx, const char * key, const char * val);
144
+
145
+ // creates a new array with n elements of the given type and copies the corresponding number of bytes from data
146
+ LM_GGML_API void lm_gguf_set_arr_data(struct lm_gguf_context * ctx, const char * key, enum lm_gguf_type type, const void * data, size_t n);
147
+
148
+ // creates a new array with n strings and copies the corresponding strings from data
149
+ LM_GGML_API void lm_gguf_set_arr_str (struct lm_gguf_context * ctx, const char * key, const char ** data, size_t n);
150
+
151
+ // set or add KV pairs from another context
152
+ LM_GGML_API void lm_gguf_set_kv(struct lm_gguf_context * ctx, const struct lm_gguf_context * src);
153
+
154
+ // add tensor to GGUF context, tensor name must be unique
155
+ LM_GGML_API void lm_gguf_add_tensor(struct lm_gguf_context * ctx, const struct lm_ggml_tensor * tensor);
156
+
157
+ // after changing a tensor's type, the offsets of all tensors with higher indices are immediately recalculated
158
+ // in such a way that the tensor data remains as one contiguous block (except for padding)
159
+ LM_GGML_API void lm_gguf_set_tensor_type(struct lm_gguf_context * ctx, const char * name, enum lm_ggml_type type);
160
+
161
+ // assumes that at least lm_gguf_get_tensor_size bytes can be read from data
162
+ LM_GGML_API void lm_gguf_set_tensor_data(struct lm_gguf_context * ctx, const char * name, const void * data);
163
+
164
+ // writing gguf files can be done in 3 ways:
165
+ //
166
+ // - write the entire lm_gguf_context to a binary file in a single pass:
167
+ //
168
+ // lm_gguf_write_to_file(ctx, fname, /*only_meta =*/ false);
169
+ //
170
+ // - write only the meta data to a file, then re-open the file and append the tensor data:
171
+ //
172
+ // lm_gguf_write_to_file(ctx, fname, /*only_meta =*/ true);
173
+ // FILE * f = fopen(fname, "ab");
174
+ // fwrite(f, ...); // write tensor data
175
+ // fclose(f);
176
+ //
177
+ // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
178
+ //
179
+ // FILE * f = fopen(fname, "wb");
180
+ // const size_t size_meta = lm_gguf_get_meta_size(ctx);
181
+ // fseek(f, size_meta, SEEK_SET);
182
+ // fwrite(f, ...); // write tensor data
183
+ // void * data = malloc(size_meta);
184
+ // lm_gguf_get_meta_data(ctx, data);
185
+ // rewind(f);
186
+ // fwrite(data, 1, data, f);
187
+ // free(data);
188
+ // fclose(f);
189
+ //
190
+
191
+ // write the entire context to a binary file
192
+ LM_GGML_API bool lm_gguf_write_to_file(const struct lm_gguf_context * ctx, const char * fname, bool only_meta);
193
+
194
+ // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
195
+ LM_GGML_API size_t lm_gguf_get_meta_size(const struct lm_gguf_context * ctx);
196
+
197
+ // writes the meta data to pointer "data"
198
+ LM_GGML_API void lm_gguf_get_meta_data(const struct lm_gguf_context * ctx, void * data);
199
+
200
+ #ifdef __cplusplus
201
+ }
202
+ #endif