Skip to content

Commit 1f36d19

Browse files
revise GGUF API data types
1 parent 38746d3 commit 1f36d19

File tree

4 files changed

+135
-134
lines changed

4 files changed

+135
-134
lines changed

examples/gguf-split/gguf-split.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <algorithm>
77
#include <cmath>
8+
#include <cinttypes>
89
#include <cstdlib>
910
#include <fstream>
1011
#include <string>
@@ -299,7 +300,7 @@ struct split_strategy {
299300
total_size += ggml_nbytes(t);
300301
}
301302
total_size = total_size / 1000 / 1000; // convert to megabytes
302-
printf("split %05d: n_tensors = %d, total_size = %zuM\n", i_split + 1, gguf_get_n_tensors(ctx_out), total_size);
303+
printf("split %05d: n_tensors = %" PRIi64 ", total_size = %zuM\n", i_split + 1, gguf_get_n_tensors(ctx_out), total_size);
303304
i_split++;
304305
}
305306
}

ggml/include/gguf.h

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//
44
// 1. File magic "GGUF" (4 bytes).
55
// 2. File version (uint32_t).
6-
// 3. Number of ggml tensors in file (uint64_t).
7-
// 4. Number of key-value-pairs in file (uint64_t).
6+
// 3. Number of ggml tensors in file (int64_t).
7+
// 4. Number of key-value-pairs in file (int64_t).
88
// 5. For each KV pair:
99
// 1. The key (string).
1010
// 2. The value type (gguf_type).
@@ -84,51 +84,51 @@ extern "C" {
8484

8585
GGML_API const char * gguf_type_name(enum gguf_type type);
8686

87-
GGML_API int gguf_get_version (const struct gguf_context * ctx);
88-
GGML_API size_t gguf_get_alignment (const struct gguf_context * ctx);
89-
GGML_API size_t gguf_get_data_offset(const struct gguf_context * ctx);
87+
GGML_API uint32_t gguf_get_version (const struct gguf_context * ctx);
88+
GGML_API size_t gguf_get_alignment (const struct gguf_context * ctx);
89+
GGML_API size_t gguf_get_data_offset(const struct gguf_context * ctx);
9090

91-
GGML_API int gguf_get_n_kv(const struct gguf_context * ctx);
92-
GGML_API int gguf_find_key(const struct gguf_context * ctx, const char * key);
93-
GGML_API const char * gguf_get_key (const struct gguf_context * ctx, int key_id);
91+
GGML_API int64_t gguf_get_n_kv(const struct gguf_context * ctx);
92+
GGML_API int64_t gguf_find_key(const struct gguf_context * ctx, const char * key); // returns -1 if key is not found
93+
GGML_API const char * gguf_get_key (const struct gguf_context * ctx, int64_t key_id);
9494

95-
GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int key_id);
96-
GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int key_id);
95+
GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int64_t key_id);
96+
GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int64_t key_id);
9797

9898
// will abort if the wrong type is used for the key
99-
GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int key_id);
100-
GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int key_id);
101-
GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int key_id);
102-
GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int key_id);
103-
GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int key_id);
104-
GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int key_id);
105-
GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int key_id);
106-
GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int key_id);
107-
GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int key_id);
108-
GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int key_id);
109-
GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int key_id);
110-
GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int key_id);
111-
GGML_API const void * gguf_get_val_data(const struct gguf_context * ctx, int key_id);
112-
GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int key_id);
99+
GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int64_t key_id);
100+
GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int64_t key_id);
101+
GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int64_t key_id);
102+
GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int64_t key_id);
103+
GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int64_t key_id);
104+
GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int64_t key_id);
105+
GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int64_t key_id);
106+
GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int64_t key_id);
107+
GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int64_t key_id);
108+
GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int64_t key_id);
109+
GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int64_t key_id);
110+
GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int64_t key_id);
111+
GGML_API const void * gguf_get_val_data(const struct gguf_context * ctx, int64_t key_id);
112+
GGML_API size_t gguf_get_arr_n (const struct gguf_context * ctx, int64_t key_id);
113113

114114
// get raw pointer to the first element of the array with the given key_id
115115
// for bool arrays, note that they are always stored as int8 on all platforms (usually this makes no difference)
116-
GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int key_id);
116+
GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int64_t key_id);
117117

118118
// get ith C string from array with given key_id
119-
GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int key_id, int i);
119+
GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int64_t key_id, size_t i);
120120

121-
GGML_API int gguf_get_n_tensors (const struct gguf_context * ctx);
122-
GGML_API int gguf_find_tensor (const struct gguf_context * ctx, const char * name);
123-
GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int i);
124-
GGML_API const char * gguf_get_tensor_name (const struct gguf_context * ctx, int i);
125-
GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int i);
126-
GGML_API size_t gguf_get_tensor_size (const struct gguf_context * ctx, int i);
121+
GGML_API int64_t gguf_get_n_tensors (const struct gguf_context * ctx);
122+
GGML_API int64_t gguf_find_tensor (const struct gguf_context * ctx, const char * name); // returns -1 if the tensor is not found
123+
GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int64_t tensor_id);
124+
GGML_API const char * gguf_get_tensor_name (const struct gguf_context * ctx, int64_t tensor_id);
125+
GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int64_t tensor_id);
126+
GGML_API size_t gguf_get_tensor_size (const struct gguf_context * ctx, int64_t tensor_id);
127127

128-
// removes key if it exists
129-
GGML_API void gguf_remove_key(struct gguf_context * ctx, const char * key);
128+
// removes key if it exists, returns id that the key had prior to removal (-1 if it didn't exist)
129+
GGML_API int64_t gguf_remove_key(struct gguf_context * ctx, const char * key);
130130

131-
// overrides existing values or adds a new one
131+
// overrides an existing KV pair or adds a new one, the new KV pair is always at the back
132132
GGML_API void gguf_set_val_u8 (struct gguf_context * ctx, const char * key, uint8_t val);
133133
GGML_API void gguf_set_val_i8 (struct gguf_context * ctx, const char * key, int8_t val);
134134
GGML_API void gguf_set_val_u16 (struct gguf_context * ctx, const char * key, uint16_t val);
@@ -143,10 +143,10 @@ extern "C" {
143143
GGML_API void gguf_set_val_str (struct gguf_context * ctx, const char * key, const char * val);
144144

145145
// creates a new array with n elements of the given type and copies the corresponding number of bytes from data
146-
GGML_API void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_type type, const void * data, int n);
146+
GGML_API void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_type type, const void * data, size_t n);
147147

148148
// creates a new array with n strings and copies the corresponding strings from data
149-
GGML_API void gguf_set_arr_str (struct gguf_context * ctx, const char * key, const char ** data, int n);
149+
GGML_API void gguf_set_arr_str (struct gguf_context * ctx, const char * key, const char ** data, size_t n);
150150

151151
// set or add KV pairs from another context
152152
GGML_API void gguf_set_kv(struct gguf_context * ctx, const struct gguf_context * src);

0 commit comments

Comments
 (0)