Skip to content

Commit 2db94d9

Browse files
authored
gguf : basic type checking in gguf_get_* (#3346)
1 parent ecf90b1 commit 2db94d9

File tree

2 files changed

+70
-54
lines changed

2 files changed

+70
-54
lines changed

ggml.c

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20211,78 +20211,94 @@ int gguf_find_key(const struct gguf_context * ctx, const char * key) {
2021120211
return keyfound;
2021220212
}
2021320213

20214-
const char * gguf_get_key(const struct gguf_context * ctx, int i) {
20215-
return ctx->kv[i].key.data;
20214+
const char * gguf_get_key(const struct gguf_context * ctx, int key_id) {
20215+
return ctx->kv[key_id].key.data;
2021620216
}
2021720217

20218-
enum gguf_type gguf_get_kv_type(const struct gguf_context * ctx, int i) {
20219-
return ctx->kv[i].type;
20218+
enum gguf_type gguf_get_kv_type(const struct gguf_context * ctx, int key_id) {
20219+
return ctx->kv[key_id].type;
2022020220
}
2022120221

20222-
enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int i) {
20223-
return ctx->kv[i].value.arr.type;
20222+
enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int key_id) {
20223+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_ARRAY);
20224+
return ctx->kv[key_id].value.arr.type;
2022420225
}
2022520226

20226-
const void * gguf_get_arr_data(const struct gguf_context * ctx, int i) {
20227-
return ctx->kv[i].value.arr.data;
20227+
const void * gguf_get_arr_data(const struct gguf_context * ctx, int key_id) {
20228+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_ARRAY);
20229+
return ctx->kv[key_id].value.arr.data;
2022820230
}
2022920231

2023020232
const char * gguf_get_arr_str(const struct gguf_context * ctx, int key_id, int i) {
20233+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_ARRAY);
2023120234
struct gguf_kv * kv = &ctx->kv[key_id];
2023220235
struct gguf_str * str = &((struct gguf_str *) kv->value.arr.data)[i];
2023320236
return str->data;
2023420237
}
2023520238

20236-
int gguf_get_arr_n(const struct gguf_context * ctx, int i) {
20237-
return ctx->kv[i].value.arr.n;
20239+
int gguf_get_arr_n(const struct gguf_context * ctx, int key_id) {
20240+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_ARRAY);
20241+
return ctx->kv[key_id].value.arr.n;
2023820242
}
2023920243

20240-
uint8_t gguf_get_val_u8(const struct gguf_context * ctx, int i) {
20241-
return ctx->kv[i].value.uint8;
20244+
uint8_t gguf_get_val_u8(const struct gguf_context * ctx, int key_id) {
20245+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_UINT8);
20246+
return ctx->kv[key_id].value.uint8;
2024220247
}
2024320248

20244-
int8_t gguf_get_val_i8(const struct gguf_context * ctx, int i) {
20245-
return ctx->kv[i].value.int8;
20249+
int8_t gguf_get_val_i8(const struct gguf_context * ctx, int key_id) {
20250+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_INT8);
20251+
return ctx->kv[key_id].value.int8;
2024620252
}
2024720253

20248-
uint16_t gguf_get_val_u16(const struct gguf_context * ctx, int i) {
20249-
return ctx->kv[i].value.uint16;
20254+
uint16_t gguf_get_val_u16(const struct gguf_context * ctx, int key_id) {
20255+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_UINT16);
20256+
return ctx->kv[key_id].value.uint16;
2025020257
}
2025120258

20252-
int16_t gguf_get_val_i16(const struct gguf_context * ctx, int i) {
20253-
return ctx->kv[i].value.int16;
20259+
int16_t gguf_get_val_i16(const struct gguf_context * ctx, int key_id) {
20260+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_INT16);
20261+
return ctx->kv[key_id].value.int16;
2025420262
}
2025520263

20256-
uint32_t gguf_get_val_u32(const struct gguf_context * ctx, int i) {
20257-
return ctx->kv[i].value.uint32;
20264+
uint32_t gguf_get_val_u32(const struct gguf_context * ctx, int key_id) {
20265+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_UINT32);
20266+
return ctx->kv[key_id].value.uint32;
2025820267
}
2025920268

20260-
int32_t gguf_get_val_i32(const struct gguf_context * ctx, int i) {
20261-
return ctx->kv[i].value.int32;
20269+
int32_t gguf_get_val_i32(const struct gguf_context * ctx, int key_id) {
20270+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_INT32);
20271+
return ctx->kv[key_id].value.int32;
2026220272
}
2026320273

20264-
float gguf_get_val_f32(const struct gguf_context * ctx, int i) {
20265-
return ctx->kv[i].value.float32;
20274+
float gguf_get_val_f32(const struct gguf_context * ctx, int key_id) {
20275+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_FLOAT32);
20276+
return ctx->kv[key_id].value.float32;
2026620277
}
2026720278

20268-
uint64_t gguf_get_val_u64(const struct gguf_context * ctx, int i) {
20269-
return ctx->kv[i].value.uint64;
20279+
uint64_t gguf_get_val_u64(const struct gguf_context * ctx, int key_id) {
20280+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_UINT64);
20281+
return ctx->kv[key_id].value.uint64;
2027020282
}
2027120283

20272-
int64_t gguf_get_val_i64(const struct gguf_context * ctx, int i) {
20273-
return ctx->kv[i].value.int64;
20284+
int64_t gguf_get_val_i64(const struct gguf_context * ctx, int key_id) {
20285+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_INT64);
20286+
return ctx->kv[key_id].value.int64;
2027420287
}
2027520288

20276-
double gguf_get_val_f64(const struct gguf_context * ctx, int i) {
20277-
return ctx->kv[i].value.float64;
20289+
double gguf_get_val_f64(const struct gguf_context * ctx, int key_id) {
20290+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_FLOAT64);
20291+
return ctx->kv[key_id].value.float64;
2027820292
}
2027920293

20280-
bool gguf_get_val_bool(const struct gguf_context * ctx, int i) {
20281-
return ctx->kv[i].value.bool_;
20294+
bool gguf_get_val_bool(const struct gguf_context * ctx, int key_id) {
20295+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_BOOL);
20296+
return ctx->kv[key_id].value.bool_;
2028220297
}
2028320298

20284-
const char * gguf_get_val_str (const struct gguf_context * ctx, int i) {
20285-
return ctx->kv[i].value.str.data;
20299+
const char * gguf_get_val_str(const struct gguf_context * ctx, int key_id) {
20300+
GGML_ASSERT(ctx->kv[key_id].type == GGUF_TYPE_STRING);
20301+
return ctx->kv[key_id].value.str.data;
2028620302
}
2028720303

2028820304
int gguf_get_n_tensors(const struct gguf_context * ctx) {

ggml.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,26 +1916,26 @@ extern "C" {
19161916

19171917
GGML_API int gguf_get_n_kv(const struct gguf_context * ctx);
19181918
GGML_API int gguf_find_key(const struct gguf_context * ctx, const char * key);
1919-
GGML_API const char * gguf_get_key (const struct gguf_context * ctx, int i);
1920-
1921-
GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int i);
1922-
GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int i);
1923-
1924-
// results are undefined if the wrong type is used for the key
1925-
GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int i);
1926-
GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int i);
1927-
GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int i);
1928-
GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int i);
1929-
GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int i);
1930-
GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int i);
1931-
GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int i);
1932-
GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int i);
1933-
GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int i);
1934-
GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int i);
1935-
GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int i);
1936-
GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int i);
1937-
GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int i);
1938-
GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int i);
1919+
GGML_API const char * gguf_get_key (const struct gguf_context * ctx, int key_id);
1920+
1921+
GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int key_id);
1922+
GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int key_id);
1923+
1924+
// will abort if the wrong type is used for the key
1925+
GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int key_id);
1926+
GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int key_id);
1927+
GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int key_id);
1928+
GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int key_id);
1929+
GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int key_id);
1930+
GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int key_id);
1931+
GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int key_id);
1932+
GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int key_id);
1933+
GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int key_id);
1934+
GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int key_id);
1935+
GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int key_id);
1936+
GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int key_id);
1937+
GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int key_id);
1938+
GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int key_id);
19391939
GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int key_id, int i);
19401940

19411941
GGML_API int gguf_get_n_tensors (const struct gguf_context * ctx);

0 commit comments

Comments
 (0)