@@ -21,91 +21,6 @@ static std::string to_string(const T & val) {
21
21
return ss.str ();
22
22
}
23
23
24
- void gguf_ex_write_str (std::ofstream & fout, const std::string & val) {
25
- const int32_t n = val.size ();
26
- fout.write ((const char *) &n, sizeof (n));
27
- fout.write (val.c_str (), n);
28
- }
29
-
30
- void gguf_ex_write_i32 (std::ofstream & fout, int32_t val) {
31
- fout.write ((const char *) &val, sizeof (val));
32
- }
33
-
34
- void gguf_ex_write_u64 (std::ofstream & fout, size_t val) {
35
- fout.write ((const char *) &val, sizeof (val));
36
- }
37
-
38
- template <typename T>
39
- void gguf_ex_write_val (std::ofstream & fout, const std::string & key, enum gguf_type type, const T & val) {
40
- gguf_ex_write_str (fout, key);
41
- fout.write ((const char *) &type, sizeof (type));
42
- fout.write ((const char *) &val, sizeof (val));
43
-
44
- fprintf (stdout, " %s: write param: %s = %s\n " , __func__, key.c_str (), to_string (val).c_str ());
45
- }
46
-
47
- template <>
48
- void gguf_ex_write_val<std::string>(std::ofstream & fout, const std::string & key, enum gguf_type type, const std::string & val) {
49
- gguf_ex_write_str (fout, key);
50
- fout.write ((const char *) &type, sizeof (type));
51
-
52
- const int32_t n = val.size ();
53
- fout.write ((const char *) &n, sizeof (n));
54
- fout.write (val.c_str (), n);
55
-
56
- fprintf (stdout, " %s: write param: %s = %s\n " , __func__, key.c_str (), val.c_str ());
57
- }
58
-
59
- template <typename T>
60
- void gguf_ex_write_arr (std::ofstream & fout, const std::string & key, enum gguf_type type, const std::vector<T> & val) {
61
- gguf_ex_write_str (fout, key);
62
- {
63
- const enum gguf_type tarr = GGUF_TYPE_ARRAY;
64
- fout.write ((const char *) &tarr, sizeof (tarr));
65
- }
66
-
67
- const int32_t n = val.size ();
68
- fout.write ((const char *) &type, sizeof (type));
69
- fout.write ((const char *) &n, sizeof (n));
70
- fout.write ((const char *) val.data (), n * sizeof (T));
71
-
72
- fprintf (stdout, " %s: write param: %s = [" , __func__, key.c_str ());
73
- for (int i = 0 ; i < n; ++i) {
74
- fprintf (stdout, " %s" , to_string (val[i]).c_str ());
75
- if (i < n - 1 ) {
76
- fprintf (stdout, " , " );
77
- }
78
- }
79
- fprintf (stdout, " ]\n " );
80
- }
81
-
82
- template <>
83
- void gguf_ex_write_arr<std::string>(std::ofstream & fout, const std::string & key, enum gguf_type type, const std::vector<std::string> & val) {
84
- gguf_ex_write_str (fout, key);
85
- {
86
- const enum gguf_type tarr = GGUF_TYPE_ARRAY;
87
- fout.write ((const char *) &tarr, sizeof (tarr));
88
- }
89
-
90
- const int32_t n = val.size ();
91
- fout.write ((const char *) &type, sizeof (type));
92
- fout.write ((const char *) &n, sizeof (n));
93
- for (int i = 0 ; i < n; ++i) {
94
- const int32_t nstr = val[i].size ();
95
- fout.write ((const char *) &nstr, sizeof (nstr));
96
- fout.write (val[i].c_str (), nstr);
97
- }
98
-
99
- fprintf (stdout, " %s: write param: %s = [" , __func__, key.c_str ());
100
- for (int i = 0 ; i < n; ++i) {
101
- fprintf (stdout, " %s" , val[i].c_str ());
102
- if (i < n - 1 ) {
103
- fprintf (stdout, " , " );
104
- }
105
- }
106
- fprintf (stdout, " ]\n " );
107
- }
108
-
109
24
bool gguf_ex_write (const std::string & fname) {
110
25
struct gguf_context * ctx = gguf_init_empty ();
111
26
@@ -118,11 +33,11 @@ bool gguf_ex_write(const std::string & fname) {
118
33
gguf_set_val_i32 (ctx, " some.parameter.int32" , -0x12345679 );
119
34
gguf_set_val_f32 (ctx, " some.parameter.float32" , 0 .123456789f );
120
35
gguf_set_val_bool (ctx, " some.parameter.bool" , true );
121
- gguf_set_val_str (ctx, " some.parameter.string" , " hello world" );
36
+ gguf_set_val_str (ctx, " some.parameter.string" , " hello world" );
122
37
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", "!" });
38
+ gguf_set_arr_data (ctx, " some.parameter.arr.i16" , GGUF_TYPE_INT16, std::vector<int16_t >{ 1 , 2 , 3 , 4 , }.data (), 4 );
39
+ gguf_set_arr_data (ctx, " some.parameter.arr.f32" , GGUF_TYPE_FLOAT32, std::vector<float >{ 3 .145f , 2 .718f , 1 .414f , }.data (), 3 );
40
+ gguf_set_arr_str (ctx , " some.parameter.arr.str" , std::vector< const char *> { " hello" , " world" , " !" }. data (), 3 );
126
41
}
127
42
128
43
struct ggml_init_params params = {
0 commit comments