@@ -107,48 +107,24 @@ void gguf_ex_write_arr<std::string>(std::ofstream & fout, const std::string & ke
107
107
}
108
108
109
109
bool gguf_ex_write (const std::string & fname) {
110
- std::ofstream fout (fname. c_str (), std::ios::binary );
110
+ struct gguf_context * ctx = gguf_init_empty ( );
111
111
112
112
{
113
- const int32_t magic = GGUF_MAGIC;
114
- fout.write ((const char *) &magic, sizeof (magic));
113
+ gguf_set_val_u8 (ctx, " some.parameter.uint8" , 0x12 );
114
+ gguf_set_val_i8 (ctx, " some.parameter.int8" , -0x13 );
115
+ gguf_set_val_u16 (ctx, " some.parameter.uint16" , 0x1234 );
116
+ gguf_set_val_i16 (ctx, " some.parameter.int16" , -0x1235 );
117
+ gguf_set_val_u32 (ctx, " some.parameter.uint32" , 0x12345678 );
118
+ gguf_set_val_i32 (ctx, " some.parameter.int32" , -0x12345679 );
119
+ gguf_set_val_f32 (ctx, " some.parameter.float32" , 0 .123456789f );
120
+ gguf_set_val_bool (ctx, " some.parameter.bool" , true );
121
+ gguf_set_val_str (ctx, " some.parameter.string" , " hello world" );
122
+
123
+ // gguf_set_arr_data(ctx, "some.parameter.arr.i16", GGUF_TYPE_INT16, std::vector<int16_t>{ 1, 2, 3, 4, }.data(), 4);
124
+ // gguf_set_arr_data(ctx, "some.parameter.arr.f32", GGUF_TYPE_FLOAT32, std::vector<float>{ 3.145f, 2.718f, 1.414f, }.data(), 3);
125
+ // gguf_ex_write_arr<std::string>(fout, "some.parameter.arr.str", GGUF_TYPE_STRING, { "hello", "world", "!" });
115
126
}
116
127
117
- {
118
- const int32_t version = GGUF_VERSION;
119
- fout.write ((const char *) &version, sizeof (version));
120
- }
121
-
122
- // NOTE: these have to match the output below!
123
- const int n_tensors = 10 ;
124
- const int n_kv = 12 ;
125
-
126
- fout.write ((const char *) &n_tensors, sizeof (n_tensors));
127
- fout.write ((const char *) &n_kv, sizeof (n_kv));
128
-
129
- fprintf (stdout, " %s: write header\n " , __func__);
130
-
131
- // kv data
132
- {
133
- gguf_ex_write_val< uint8_t >(fout, " some.parameter.uint8" , GGUF_TYPE_UINT8, 0x12 );
134
- gguf_ex_write_val< int8_t >(fout, " some.parameter.int8" , GGUF_TYPE_INT8, -0x13 );
135
- gguf_ex_write_val<uint16_t >(fout, " some.parameter.uint16" , GGUF_TYPE_UINT16, 0x1234 );
136
- gguf_ex_write_val< int16_t >(fout, " some.parameter.int16" , GGUF_TYPE_INT16, -0x1235 );
137
- gguf_ex_write_val<uint32_t >(fout, " some.parameter.uint32" , GGUF_TYPE_UINT32, 0x12345678 );
138
- gguf_ex_write_val< int32_t >(fout, " some.parameter.int32" , GGUF_TYPE_INT32, -0x12345679 );
139
-
140
- gguf_ex_write_val<float > (fout, " some.parameter.float32" , GGUF_TYPE_FLOAT32, 0 .123456789f );
141
- gguf_ex_write_val<bool > (fout, " some.parameter.bool" , GGUF_TYPE_BOOL, true );
142
-
143
- gguf_ex_write_val<std::string>(fout, " some.parameter.string" , GGUF_TYPE_STRING, " hello world" );
144
-
145
- gguf_ex_write_arr<int16_t > (fout, " some.parameter.arr.i16" , GGUF_TYPE_INT16, { 1 , 2 , 3 , 4 , });
146
- gguf_ex_write_arr<float > (fout, " some.parameter.arr.f32" , GGUF_TYPE_FLOAT32, { 3 .145f , 2 .718f , 1 .414f , });
147
- gguf_ex_write_arr<std::string>(fout, " some.parameter.arr.str" , GGUF_TYPE_STRING, { " hello" , " world" , " !" });
148
- }
149
-
150
- uint64_t offset_tensor = 0 ;
151
-
152
128
struct ggml_init_params params = {
153
129
/* .mem_size =*/ 128ull *1024ull *1024ull ,
154
130
/* .mem_buffer =*/ NULL ,
@@ -157,6 +133,8 @@ bool gguf_ex_write(const std::string & fname) {
157
133
158
134
struct ggml_context * ctx_data = ggml_init (params);
159
135
136
+ const int n_tensors = 10 ;
137
+
160
138
// tensor infos
161
139
for (int i = 0 ; i < n_tensors; ++i) {
162
140
const std::string name = " tensor_" + to_string (i);
@@ -178,58 +156,15 @@ bool gguf_ex_write(const std::string & fname) {
178
156
}
179
157
}
180
158
181
- fprintf (stdout, " %s: tensor: %s, %d dims, ne = [" , __func__, name.c_str (), n_dims);
182
- for (int j = 0 ; j < 4 ; ++j) {
183
- fprintf (stdout, " %s%3d" , j == 0 ? " " : " , " , (int ) cur->ne [j]);
184
- }
185
- fprintf (stdout, " ], offset_tensor = %6" PRIu64 " \n " , offset_tensor);
186
-
187
- gguf_ex_write_str (fout, name);
188
- gguf_ex_write_i32 (fout, n_dims);
189
- for (int j = 0 ; j < n_dims; ++j) {
190
- gguf_ex_write_i32 (fout, cur->ne [j]);
191
- }
192
- gguf_ex_write_i32 (fout, cur->type );
193
- gguf_ex_write_u64 (fout, offset_tensor);
194
-
195
- offset_tensor += GGML_PAD (ggml_nbytes (cur), GGUF_DEFAULT_ALIGNMENT);
159
+ gguf_add_tensor (ctx, cur);
196
160
}
197
161
198
- const uint64_t offset_data = GGML_PAD ((uint64_t ) fout.tellp (), GGUF_DEFAULT_ALIGNMENT);
199
-
200
- fprintf (stdout, " %s: data offset = %" PRIu64 " \n " , __func__, offset_data);
201
-
202
- {
203
- const size_t pad = offset_data - fout.tellp ();
204
-
205
- for (size_t j = 0 ; j < pad; ++j) {
206
- fout.put (0 );
207
- }
208
- }
209
-
210
- for (int i = 0 ; i < n_tensors; ++i) {
211
- fprintf (stdout, " %s: writing tensor %d data\n " , __func__, i);
212
-
213
- const std::string name = " tensor_" + to_string (i);
214
-
215
- struct ggml_tensor * cur = ggml_get_tensor (ctx_data, name.c_str ());
216
-
217
- fout.write ((const char *) cur->data , ggml_nbytes (cur));
218
-
219
- {
220
- const size_t pad = GGML_PAD (ggml_nbytes (cur), GGUF_DEFAULT_ALIGNMENT) - ggml_nbytes (cur);
221
-
222
- for (size_t j = 0 ; j < pad; ++j) {
223
- fout.put (0 );
224
- }
225
- }
226
- }
227
-
228
- fout.close ();
162
+ gguf_write_to_file (ctx, fname.c_str ());
229
163
230
164
fprintf (stdout, " %s: wrote file '%s;\n " , __func__, fname.c_str ());
231
165
232
166
ggml_free (ctx_data);
167
+ gguf_free (ctx);
233
168
234
169
return true ;
235
170
}
0 commit comments