gguf : basic type checking in gguf_get_*

This commit is contained in:
Cebtenzzre 2023-09-26 13:01:47 -04:00
parent 99115f3fa6
commit 1eb0cf5c15

18
ggml.c
View file

@ -20164,68 +20164,84 @@ enum gguf_type gguf_get_kv_type(const struct gguf_context * ctx, int i) {
} }
enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int i) { enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_ARRAY);
return ctx->kv[i].value.arr.type; return ctx->kv[i].value.arr.type;
} }
const void * gguf_get_arr_data(const struct gguf_context * ctx, int i) { const void * gguf_get_arr_data(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_ARRAY);
return ctx->kv[i].value.arr.data; return ctx->kv[i].value.arr.data;
} }
const char * gguf_get_arr_str(const struct gguf_context * ctx, int key_id, int i) { const char * gguf_get_arr_str(const struct gguf_context * ctx, int key_id, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_ARRAY);
struct gguf_kv * kv = &ctx->kv[key_id]; struct gguf_kv * kv = &ctx->kv[key_id];
struct gguf_str * str = &((struct gguf_str *) kv->value.arr.data)[i]; struct gguf_str * str = &((struct gguf_str *) kv->value.arr.data)[i];
return str->data; return str->data;
} }
int gguf_get_arr_n(const struct gguf_context * ctx, int i) { int gguf_get_arr_n(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_ARRAY);
return ctx->kv[i].value.arr.n; return ctx->kv[i].value.arr.n;
} }
uint8_t gguf_get_val_u8(const struct gguf_context * ctx, int i) { uint8_t gguf_get_val_u8(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_UINT8);
return ctx->kv[i].value.uint8; return ctx->kv[i].value.uint8;
} }
int8_t gguf_get_val_i8(const struct gguf_context * ctx, int i) { int8_t gguf_get_val_i8(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_INT8);
return ctx->kv[i].value.int8; return ctx->kv[i].value.int8;
} }
uint16_t gguf_get_val_u16(const struct gguf_context * ctx, int i) { uint16_t gguf_get_val_u16(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_UINT16);
return ctx->kv[i].value.uint16; return ctx->kv[i].value.uint16;
} }
int16_t gguf_get_val_i16(const struct gguf_context * ctx, int i) { int16_t gguf_get_val_i16(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_INT16);
return ctx->kv[i].value.int16; return ctx->kv[i].value.int16;
} }
uint32_t gguf_get_val_u32(const struct gguf_context * ctx, int i) { uint32_t gguf_get_val_u32(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_UINT32);
return ctx->kv[i].value.uint32; return ctx->kv[i].value.uint32;
} }
int32_t gguf_get_val_i32(const struct gguf_context * ctx, int i) { int32_t gguf_get_val_i32(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_INT32);
return ctx->kv[i].value.int32; return ctx->kv[i].value.int32;
} }
float gguf_get_val_f32(const struct gguf_context * ctx, int i) { float gguf_get_val_f32(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_FLOAT32);
return ctx->kv[i].value.float32; return ctx->kv[i].value.float32;
} }
uint64_t gguf_get_val_u64(const struct gguf_context * ctx, int i) { uint64_t gguf_get_val_u64(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_UINT64);
return ctx->kv[i].value.uint64; return ctx->kv[i].value.uint64;
} }
int64_t gguf_get_val_i64(const struct gguf_context * ctx, int i) { int64_t gguf_get_val_i64(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_INT64);
return ctx->kv[i].value.int64; return ctx->kv[i].value.int64;
} }
double gguf_get_val_f64(const struct gguf_context * ctx, int i) { double gguf_get_val_f64(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_FLOAT64);
return ctx->kv[i].value.float64; return ctx->kv[i].value.float64;
} }
bool gguf_get_val_bool(const struct gguf_context * ctx, int i) { bool gguf_get_val_bool(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_BOOL);
return ctx->kv[i].value.bool_; return ctx->kv[i].value.bool_;
} }
const char * gguf_get_val_str (const struct gguf_context * ctx, int i) { const char * gguf_get_val_str(const struct gguf_context * ctx, int i) {
GGML_ASSERT(ctx->kv[i].type == GGUF_TYPE_STRING);
return ctx->kv[i].value.str.data; return ctx->kv[i].value.str.data;
} }