Skip to content

Commit 33cf5b3

Browse files
author
katsu560
committed
read data from kv string
1 parent ecf8043 commit 33cf5b3

File tree

3 files changed

+104
-289
lines changed

3 files changed

+104
-289
lines changed

examples/yolo/yolov3-tiny.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,13 @@ static bool load_labels(const char * filename, std::vector<std::string> & labels
159159

160160
static bool load_labels_kv(const struct gguf_context * ctx, const char * filename, std::vector<std::string> & labels)
161161
{
162-
struct gguf_nobj nobj = gguf_find_name_nobj(ctx, filename);
163-
if (nobj.n == 0) {
162+
int key_id = gguf_find_key(ctx, filename);
163+
if (key_id == -1) {
164164
return false;
165165
}
166-
membuf buf(nobj.data, nobj.data + nobj.n);
166+
const char * data = gguf_get_val_str(ctx, key_id);
167+
uint64_t n = gguf_get_val_str_len(ctx, key_id);
168+
membuf buf(data, data + n);
167169
std::istream file_in(&buf);
168170
if (!file_in) {
169171
return false;
@@ -198,13 +200,15 @@ static bool load_alphabet_kv(const struct gguf_context * ctx, std::vector<yolo_i
198200
for (int j = 0; j < 8; j++) {
199201
for (int i = 32; i < 127; i++) {
200202
char fname[256];
201-
sprintf(fname, "data/labels/%d_%d.png", i, j);
202-
struct gguf_nobj nobj = gguf_find_name_nobj(ctx, fname);
203-
if (nobj.n == 0) {
203+
sprintf(fname, "/data/labels/%d_%d.png", i, j);
204+
int key_id = gguf_find_key(ctx, fname);
205+
if (key_id == -1) {
204206
fprintf(stderr, "Cannot find '%s'\n", fname);
205207
return false;
206208
}
207-
if (!load_image_from_memory(nobj.data, nobj.n, alphabet[j*128 + i])) {
209+
const char * data = gguf_get_val_str(ctx, key_id);
210+
uint64_t n = gguf_get_val_str_len(ctx, key_id);
211+
if (!load_image_from_memory(data, n, alphabet[j*128 + i])) {
208212
fprintf(stderr, "Cannot load '%s'\n", fname);
209213
return false;
210214
}
@@ -586,8 +590,8 @@ int main(int argc, char *argv[])
586590
return 1;
587591
}
588592
std::vector<std::string> labels;
589-
if (!load_labels_kv(model.ggufctx, "data/coco.names", labels)) {
590-
fprintf(stderr, "%s: failed to load labels from 'data/coco.names' in model\n", __func__);
593+
if (!load_labels_kv(model.ggufctx, "/data/coco.names", labels)) {
594+
fprintf(stderr, "%s: failed to load labels from '/data/coco.names' in model\n", __func__);
591595
if (!load_labels("data/coco.names", labels)) {
592596
fprintf(stderr, "%s: failed to load labels from 'data/coco.names'\n", __func__);
593597
return 1;

include/ggml/ggml.h

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,30 +2258,22 @@ extern "C" {
22582258
//
22592259

22602260
enum gguf_type {
2261-
GGUF_TYPE_UINT8 = 0,
2262-
GGUF_TYPE_INT8 = 1,
2263-
GGUF_TYPE_UINT16 = 2,
2264-
GGUF_TYPE_INT16 = 3,
2265-
GGUF_TYPE_UINT32 = 4,
2266-
GGUF_TYPE_INT32 = 5,
2267-
GGUF_TYPE_FLOAT32 = 6,
2268-
GGUF_TYPE_BOOL = 7,
2269-
GGUF_TYPE_STRING = 8,
2270-
GGUF_TYPE_ARRAY = 9,
2271-
GGUF_TYPE_UINT64 = 10,
2272-
GGUF_TYPE_INT64 = 11,
2273-
GGUF_TYPE_FLOAT64 = 12,
2274-
GGUF_TYPE_NAMEDOBJECT = 13,
2261+
GGUF_TYPE_UINT8 = 0,
2262+
GGUF_TYPE_INT8 = 1,
2263+
GGUF_TYPE_UINT16 = 2,
2264+
GGUF_TYPE_INT16 = 3,
2265+
GGUF_TYPE_UINT32 = 4,
2266+
GGUF_TYPE_INT32 = 5,
2267+
GGUF_TYPE_FLOAT32 = 6,
2268+
GGUF_TYPE_BOOL = 7,
2269+
GGUF_TYPE_STRING = 8,
2270+
GGUF_TYPE_ARRAY = 9,
2271+
GGUF_TYPE_UINT64 = 10,
2272+
GGUF_TYPE_INT64 = 11,
2273+
GGUF_TYPE_FLOAT64 = 12,
22752274
GGUF_TYPE_COUNT, // marks the end of the enum
22762275
};
22772276

2278-
struct gguf_nobj {
2279-
uint64_t nname;
2280-
char * name;
2281-
uint64_t n;
2282-
char * data;
2283-
};
2284-
22852277
struct gguf_context;
22862278

22872279
struct gguf_init_params {
@@ -2311,27 +2303,24 @@ extern "C" {
23112303
GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int key_id);
23122304
GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int key_id);
23132305

2314-
GGML_API struct gguf_nobj gguf_find_name_nobj(const struct gguf_context * ctx, const char * name);
2315-
23162306
// will abort if the wrong type is used for the key
2317-
GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int key_id);
2318-
GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int key_id);
2319-
GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int key_id);
2320-
GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int key_id);
2321-
GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int key_id);
2322-
GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int key_id);
2323-
GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int key_id);
2324-
GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int key_id);
2325-
GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int key_id);
2326-
GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int key_id);
2327-
GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int key_id);
2328-
GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int key_id);
2329-
GGML_API struct gguf_nobj gguf_get_val_nobj(const struct gguf_context * ctx, int key_id);
2330-
GGML_API const void * gguf_get_val_data(const struct gguf_context * ctx, int key_id);
2331-
GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int key_id);
2332-
GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int key_id);
2333-
GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int key_id, int i);
2334-
GGML_API struct gguf_nobj gguf_get_arr_nobj(const struct gguf_context * ctx, int key_id, int i);
2307+
GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int key_id);
2308+
GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int key_id);
2309+
GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int key_id);
2310+
GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int key_id);
2311+
GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int key_id);
2312+
GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int key_id);
2313+
GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int key_id);
2314+
GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int key_id);
2315+
GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int key_id);
2316+
GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int key_id);
2317+
GGML_API bool gguf_get_val_bool (const struct gguf_context * ctx, int key_id);
2318+
GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int key_id);
2319+
GGML_API uint64_t gguf_get_val_str_len(const struct gguf_context * ctx, int key_id);
2320+
GGML_API const void * gguf_get_val_data (const struct gguf_context * ctx, int key_id);
2321+
GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int key_id);
2322+
GGML_API const void * gguf_get_arr_data (const struct gguf_context * ctx, int key_id);
2323+
GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int key_id, int i);
23352324

23362325
GGML_API int gguf_get_n_tensors (const struct gguf_context * ctx);
23372326
GGML_API int gguf_find_tensor (const struct gguf_context * ctx, const char * name);
@@ -2355,10 +2344,9 @@ extern "C" {
23552344
GGML_API void gguf_set_val_f64 (struct gguf_context * ctx, const char * key, double val);
23562345
GGML_API void gguf_set_val_bool(struct gguf_context * ctx, const char * key, bool val);
23572346
GGML_API void gguf_set_val_str (struct gguf_context * ctx, const char * key, const char * val);
2358-
GGML_API void gguf_set_val_nobj(struct gguf_context * ctx, const char * key, const char * name, const int len, const void * val);
2347+
GGML_API void gguf_set_val_data(struct gguf_context * ctx, const char * key, const char * val, int n);
23592348
GGML_API void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_type type, const void * data, int n);
23602349
GGML_API void gguf_set_arr_str (struct gguf_context * ctx, const char * key, const char ** data, int n);
2361-
GGML_API void gguf_set_arr_nobj(struct gguf_context * ctx, const char * key, const struct gguf_nobj * data, int n);
23622350

23632351
// set or add KV pairs from another context
23642352
GGML_API void gguf_set_kv(struct gguf_context * ctx, struct gguf_context * src);

0 commit comments

Comments
 (0)