Skip to content

Commit f6ecd15

Browse files
committed
gguf : initial write API ready + example
1 parent 85ebfb8 commit f6ecd15

File tree

2 files changed

+20
-85
lines changed

2 files changed

+20
-85
lines changed

examples/gguf/gguf.cpp

Lines changed: 19 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -107,48 +107,24 @@ void gguf_ex_write_arr<std::string>(std::ofstream & fout, const std::string & ke
107107
}
108108

109109
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();
111111

112112
{
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", "!" });
115126
}
116127

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-
152128
struct ggml_init_params params = {
153129
/*.mem_size =*/ 128ull*1024ull*1024ull,
154130
/*.mem_buffer =*/ NULL,
@@ -157,6 +133,8 @@ bool gguf_ex_write(const std::string & fname) {
157133

158134
struct ggml_context * ctx_data = ggml_init(params);
159135

136+
const int n_tensors = 10;
137+
160138
// tensor infos
161139
for (int i = 0; i < n_tensors; ++i) {
162140
const std::string name = "tensor_" + to_string(i);
@@ -178,58 +156,15 @@ bool gguf_ex_write(const std::string & fname) {
178156
}
179157
}
180158

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);
196160
}
197161

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());
229163

230164
fprintf(stdout, "%s: wrote file '%s;\n", __func__, fname.c_str());
231165

232166
ggml_free(ctx_data);
167+
gguf_free(ctx);
233168

234169
return true;
235170
}

ggml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19281,7 +19281,7 @@ void gguf_add_tensor(struct gguf_context * ctx, const struct ggml_tensor * tenso
1928119281
ctx->infos[idx].tensor = tensor;
1928219282

1928319283
if (ctx->header.n_tensors > 0) {
19284-
ctx->infos[idx].offset = ctx->infos[idx - 1].offset + GGML_PAD(ggml_nbytes(tensor), ctx->alignment);
19284+
ctx->infos[idx].offset = ctx->infos[idx - 1].offset + GGML_PAD(ggml_nbytes(ctx->infos[idx - 1].tensor), ctx->alignment);
1928519285
}
1928619286

1928719287
ctx->header.n_tensors++;

0 commit comments

Comments
 (0)