MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / gguf_ex_write

Function gguf_ex_write

examples/gguf/gguf.cpp:23–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21}
22
23static bool gguf_ex_write(const std::string & fname) {
24 struct gguf_context * ctx = gguf_init_empty();
25
26 gguf_set_val_u8 (ctx, "some.parameter.uint8", 0x12);
27 gguf_set_val_i8 (ctx, "some.parameter.int8", -0x13);
28 gguf_set_val_u16 (ctx, "some.parameter.uint16", 0x1234);
29 gguf_set_val_i16 (ctx, "some.parameter.int16", -0x1235);
30 gguf_set_val_u32 (ctx, "some.parameter.uint32", 0x12345678);
31 gguf_set_val_i32 (ctx, "some.parameter.int32", -0x12345679);
32 gguf_set_val_f32 (ctx, "some.parameter.float32", 0.123456789f);
33 gguf_set_val_u64 (ctx, "some.parameter.uint64", 0x123456789abcdef0ull);
34 gguf_set_val_i64 (ctx, "some.parameter.int64", -0x123456789abcdef1ll);
35 gguf_set_val_f64 (ctx, "some.parameter.float64", 0.1234567890123456789);
36 gguf_set_val_bool(ctx, "some.parameter.bool", true);
37 gguf_set_val_str (ctx, "some.parameter.string", "hello world");
38
39 gguf_set_arr_data(ctx, "some.parameter.arr.i16", GGUF_TYPE_INT16, std::vector<int16_t>{ 1, 2, 3, 4, }.data(), 4);
40 gguf_set_arr_data(ctx, "some.parameter.arr.f32", GGUF_TYPE_FLOAT32, std::vector<float>{ 3.145f, 2.718f, 1.414f, }.data(), 3);
41 gguf_set_arr_str (ctx, "some.parameter.arr.str", std::vector<const char *>{ "hello", "world", "!" }.data(), 3);
42
43 struct ggml_init_params params = {
44 /*.mem_size =*/ 128ull*1024ull*1024ull,
45 /*.mem_buffer =*/ NULL,
46 /*.no_alloc =*/ false,
47 };
48
49 struct ggml_context * ctx_data = ggml_init(params);
50
51 const int n_tensors = 10;
52
53 // tensor infos
54 for (int i = 0; i < n_tensors; ++i) {
55 const std::string name = "tensor_" + to_string(i);
56
57 int64_t ne[GGML_MAX_DIMS] = { 1 };
58 int32_t n_dims = rand() % GGML_MAX_DIMS + 1;
59
60 for (int j = 0; j < n_dims; ++j) {
61 ne[j] = rand() % 10 + 1;
62 }
63
64 struct ggml_tensor * cur = ggml_new_tensor(ctx_data, GGML_TYPE_F32, n_dims, ne);
65 ggml_set_name(cur, name.c_str());
66
67 {
68 float * data = (float *) cur->data;
69 for (int j = 0; j < ggml_nelements(cur); ++j) {
70 data[j] = 100 + i;
71 }
72 }
73
74 gguf_add_tensor(ctx, cur);
75 }
76
77 gguf_write_to_file(ctx, fname.c_str(), false);
78
79 printf("%s: wrote file '%s;\n", __func__, fname.c_str());
80

Callers 1

mainFunction · 0.70

Calls 15

printfFunction · 0.85
to_stringFunction · 0.70
gguf_init_emptyFunction · 0.50
gguf_set_val_u8Function · 0.50
gguf_set_val_i8Function · 0.50
gguf_set_val_u16Function · 0.50
gguf_set_val_i16Function · 0.50
gguf_set_val_u32Function · 0.50
gguf_set_val_i32Function · 0.50
gguf_set_val_f32Function · 0.50
gguf_set_val_u64Function · 0.50
gguf_set_val_i64Function · 0.50

Tested by

no test coverage detected