@@ -29,7 +29,7 @@ void gguf_ex_write_u64(std::ofstream & fout, size_t val) {
29
29
}
30
30
31
31
template <typename T>
32
- void gguf_ex_write_param (std::ofstream & fout, const std::string & key, enum gguf_type type, const T & val) {
32
+ void gguf_ex_write_val (std::ofstream & fout, const std::string & key, enum gguf_type type, const T & val) {
33
33
gguf_ex_write_str (fout, key);
34
34
fout.write ((const char *) &type, sizeof (type));
35
35
fout.write ((const char *) &val, sizeof (val));
@@ -38,13 +38,65 @@ void gguf_ex_write_param(std::ofstream & fout, const std::string & key, enum ggu
38
38
}
39
39
40
40
template <>
41
- void gguf_ex_write_param <std::string>(std::ofstream & fout, const std::string & key, enum gguf_type type, const std::string & val) {
41
+ void gguf_ex_write_val <std::string>(std::ofstream & fout, const std::string & key, enum gguf_type type, const std::string & val) {
42
42
gguf_ex_write_str (fout, key);
43
43
fout.write ((const char *) &type, sizeof (type));
44
44
45
45
const int32_t n = val.size ();
46
46
fout.write ((const char *) &n, sizeof (n));
47
47
fout.write (val.c_str (), n);
48
+
49
+ fprintf (stdout, " %s: write param: %s = %s\n " , __func__, key.c_str (), val.c_str ());
50
+ }
51
+
52
+ template <typename T>
53
+ void gguf_ex_write_arr (std::ofstream & fout, const std::string & key, enum gguf_type type, const std::vector<T> & val) {
54
+ gguf_ex_write_str (fout, key);
55
+ {
56
+ const enum gguf_type tarr = GGUF_TYPE_ARRAY;
57
+ fout.write ((const char *) &tarr, sizeof (tarr));
58
+ }
59
+
60
+ const int32_t n = val.size ();
61
+ fout.write ((const char *) &type, sizeof (type));
62
+ fout.write ((const char *) &n, sizeof (n));
63
+ fout.write ((const char *) val.data (), n * sizeof (T));
64
+
65
+ fprintf (stdout, " %s: write param: %s = [" , __func__, key.c_str ());
66
+ for (int i = 0 ; i < n; ++i) {
67
+ fprintf (stdout, " %s" , to_string (val[i]).c_str ());
68
+ if (i < n - 1 ) {
69
+ fprintf (stdout, " , " );
70
+ }
71
+ }
72
+ fprintf (stdout, " ]\n " );
73
+ }
74
+
75
+ template <>
76
+ void gguf_ex_write_arr<std::string>(std::ofstream & fout, const std::string & key, enum gguf_type type, const std::vector<std::string> & val) {
77
+ gguf_ex_write_str (fout, key);
78
+ {
79
+ const enum gguf_type tarr = GGUF_TYPE_ARRAY;
80
+ fout.write ((const char *) &tarr, sizeof (tarr));
81
+ }
82
+
83
+ const int32_t n = val.size ();
84
+ fout.write ((const char *) &type, sizeof (type));
85
+ fout.write ((const char *) &n, sizeof (n));
86
+ for (int i = 0 ; i < n; ++i) {
87
+ const int32_t nstr = val[i].size ();
88
+ fout.write ((const char *) &nstr, sizeof (nstr));
89
+ fout.write (val[i].c_str (), nstr);
90
+ }
91
+
92
+ fprintf (stdout, " %s: write param: %s = [" , __func__, key.c_str ());
93
+ for (int i = 0 ; i < n; ++i) {
94
+ fprintf (stdout, " %s" , val[i].c_str ());
95
+ if (i < n - 1 ) {
96
+ fprintf (stdout, " , " );
97
+ }
98
+ }
99
+ fprintf (stdout, " ]\n " );
48
100
}
49
101
50
102
bool gguf_ex_write (const std::string & fname) {
@@ -60,8 +112,9 @@ bool gguf_ex_write(const std::string & fname) {
60
112
fout.write ((const char *) &version, sizeof (version));
61
113
}
62
114
115
+ // NOTE: these have to match the output below!
63
116
const int n_tensors = 10 ;
64
- const int n_kv = 9 ;
117
+ const int n_kv = 12 ;
65
118
66
119
fout.write ((const char *) &n_tensors, sizeof (n_tensors));
67
120
fout.write ((const char *) &n_kv, sizeof (n_kv));
@@ -70,17 +123,21 @@ bool gguf_ex_write(const std::string & fname) {
70
123
71
124
// kv data
72
125
{
73
- gguf_ex_write_param < uint8_t >(fout, " some.parameter.uint8" , GGUF_TYPE_UINT8, 0x12 );
74
- gguf_ex_write_param < int8_t >(fout, " some.parameter.int8" , GGUF_TYPE_INT8, -0x13 );
75
- gguf_ex_write_param <uint16_t >(fout, " some.parameter.uint16" , GGUF_TYPE_UINT16, 0x1234 );
76
- gguf_ex_write_param < int16_t >(fout, " some.parameter.int16" , GGUF_TYPE_INT16, -0x1235 );
77
- gguf_ex_write_param <uint32_t >(fout, " some.parameter.uint32" , GGUF_TYPE_UINT32, 0x12345678 );
78
- gguf_ex_write_param < int32_t >(fout, " some.parameter.int32" , GGUF_TYPE_INT32, -0x12345679 );
126
+ gguf_ex_write_val < uint8_t >(fout, " some.parameter.uint8" , GGUF_TYPE_UINT8, 0x12 );
127
+ gguf_ex_write_val < int8_t >(fout, " some.parameter.int8" , GGUF_TYPE_INT8, -0x13 );
128
+ gguf_ex_write_val <uint16_t >(fout, " some.parameter.uint16" , GGUF_TYPE_UINT16, 0x1234 );
129
+ gguf_ex_write_val < int16_t >(fout, " some.parameter.int16" , GGUF_TYPE_INT16, -0x1235 );
130
+ gguf_ex_write_val <uint32_t >(fout, " some.parameter.uint32" , GGUF_TYPE_UINT32, 0x12345678 );
131
+ gguf_ex_write_val < int32_t >(fout, " some.parameter.int32" , GGUF_TYPE_INT32, -0x12345679 );
79
132
80
- gguf_ex_write_param <float > (fout, " some.parameter.float32" , GGUF_TYPE_FLOAT32, 0 .123456789f );
81
- gguf_ex_write_param <bool > (fout, " some.parameter.bool" , GGUF_TYPE_BOOL, true );
133
+ gguf_ex_write_val <float > (fout, " some.parameter.float32" , GGUF_TYPE_FLOAT32, 0 .123456789f );
134
+ gguf_ex_write_val <bool > (fout, " some.parameter.bool" , GGUF_TYPE_BOOL, true );
82
135
83
- gguf_ex_write_param<std::string>(fout, " some.parameter.string" , GGUF_TYPE_STRING, " hello world" );
136
+ gguf_ex_write_val<std::string>(fout, " some.parameter.string" , GGUF_TYPE_STRING, " hello world" );
137
+
138
+ gguf_ex_write_arr<int16_t > (fout, " some.parameter.arr.i16" , GGUF_TYPE_INT16, { 1 , 2 , 3 , 4 , });
139
+ gguf_ex_write_arr<float > (fout, " some.parameter.arr.f32" , GGUF_TYPE_FLOAT32, { 3 .145f , 2 .718f , 1 .414f , });
140
+ gguf_ex_write_arr<std::string>(fout, " some.parameter.arr.str" , GGUF_TYPE_STRING, { " hello" , " world" , " !" });
84
141
}
85
142
86
143
uint64_t offset_tensor = 0 ;
@@ -203,13 +260,15 @@ bool gguf_ex_read_0(const std::string & fname) {
203
260
fprintf (stdout, " %s: n_tensors: %d\n " , __func__, n_tensors);
204
261
205
262
for (int i = 0 ; i < n_tensors; ++i) {
206
- const char * name = gguf_get_tensor_name (ctx, i);
263
+ const char * name = gguf_get_tensor_name (ctx, i);
207
264
const size_t offset = gguf_get_tensor_offset (ctx, i);
208
265
209
266
fprintf (stdout, " %s: tensor[%d]: name = %s, offset = %zu\n " , __func__, i, name, offset);
210
267
}
211
268
}
212
269
270
+ gguf_free (ctx);
271
+
213
272
return true ;
214
273
}
215
274
@@ -248,7 +307,7 @@ bool gguf_ex_read_1(const std::string & fname) {
248
307
fprintf (stdout, " %s: n_tensors: %d\n " , __func__, n_tensors);
249
308
250
309
for (int i = 0 ; i < n_tensors; ++i) {
251
- const char * name = gguf_get_tensor_name (ctx, i);
310
+ const char * name = gguf_get_tensor_name (ctx, i);
252
311
const size_t offset = gguf_get_tensor_offset (ctx, i);
253
312
254
313
fprintf (stdout, " %s: tensor[%d]: name = %s, offset = %zu\n " , __func__, i, name, offset);
0 commit comments