Skip to content

Commit c56b715

Browse files
prusnakunbounded
andauthored
Expose type name from ggml (#970)
Avoid duplication of type names in utils Co-authored-by: Håkon H. Hitland <haakon@likedan.net>
1 parent f4d277a commit c56b715

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

Diff for: examples/quantize-stats/quantize-stats.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
#include <unordered_map>
1717
#include <vector>
1818

19-
static const char * type_strs[] = { "q4_0", "q4_1", "i8", "i16", "i32", "f16", "f32" };
20-
static_assert(sizeof(type_strs) == GGML_TYPE_COUNT * sizeof(char *), "Incomplete type list");
21-
2219
struct quantize_stats_params {
2320
std::string model = "models/7B/ggml-model-f16.bin";
2421
bool verbose = false;
@@ -224,7 +221,7 @@ int main(int argc, char ** argv) {
224221
break;
225222
}
226223
int j;
227-
for (j = 0; j < GGML_TYPE_COUNT && strcmp(argv[i], type_strs[j]) != 0; j++) {
224+
for (j = 0; j < GGML_TYPE_COUNT && strcmp(argv[i], ggml_type_name((ggml_type) i)) != 0; j++) {
228225
// find match
229226
}
230227
if (j < GGML_TYPE_COUNT) {
@@ -279,7 +276,7 @@ int main(int argc, char ** argv) {
279276
continue;
280277
}
281278
if (params.verbose) {
282-
printf("%s: type %s, size %" PRId64 "\n", kv_tensor.first.c_str(), type_strs[kv_tensor.second->type], ggml_nelements(kv_tensor.second));
279+
printf("%s: type %s, size %" PRId64 "\n", kv_tensor.first.c_str(), ggml_type_name(kv_tensor.second->type), ggml_nelements(kv_tensor.second));
283280
}
284281
if (kv_tensor.second->type == GGML_TYPE_F16) {
285282
is_f16 = true;
@@ -304,13 +301,14 @@ int main(int argc, char ** argv) {
304301

305302
// loop throught quantization types
306303
for (int i = 0; i < GGML_TYPE_COUNT; i++) {
304+
const ggml_type type = (ggml_type) i;
307305
if (!params.include_types.empty() && std::find(params.include_types.begin(), params.include_types.end(), i) == params.include_types.end()) {
308306
continue;
309307
}
310308
quantize_fns_t qfns = ggml_internal_get_quantize_fn(i);
311309
if (qfns.quantize_row_q && qfns.dequantize_row_q) {
312310
if (params.verbose) {
313-
printf("testing %s ...\n", type_strs[i]);
311+
printf("testing %s ...\n", ggml_type_name(type));
314312
}
315313

316314
error_stats global_stats {};
@@ -322,7 +320,7 @@ int main(int argc, char ** argv) {
322320
if (params.verbose) {
323321
printf(" %s ...\n", kv_tensor.first.c_str());
324322
}
325-
std::string layer_name { type_strs[i] };
323+
std::string layer_name { ggml_type_name(type) };
326324
layer_name += "::" + kv_tensor.first;
327325
test_roundtrip_on_layer(
328326
layer_name,
@@ -337,7 +335,7 @@ int main(int argc, char ** argv) {
337335
);
338336
}
339337

340-
print_error_stats(type_strs[i], global_stats, params.print_histogram);
338+
print_error_stats(ggml_type_name(type), global_stats, params.print_histogram);
341339
}
342340
}
343341

Diff for: ggml.c

+17
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,18 @@ static const size_t GGML_TYPE_SIZE[GGML_TYPE_COUNT] = {
26712671
};
26722672
static_assert(GGML_TYPE_COUNT == 7, "GGML_TYPE_SIZE is outdated");
26732673

2674+
2675+
static const char * GGML_TYPE_NAME[GGML_TYPE_COUNT] = {
2676+
[GGML_TYPE_F32] = "f32",
2677+
[GGML_TYPE_F16] = "f16",
2678+
[GGML_TYPE_Q4_0] = "q4_0",
2679+
[GGML_TYPE_Q4_1] = "q4_1",
2680+
[GGML_TYPE_I8] = "i8",
2681+
[GGML_TYPE_I16] = "i16",
2682+
[GGML_TYPE_I32] = "i32",
2683+
};
2684+
static_assert(GGML_TYPE_COUNT == 7, "GGML_TYPE_NAME is outdated");
2685+
26742686
static const char * GGML_OP_LABEL[GGML_OP_COUNT] = {
26752687
"NONE",
26762688

@@ -2895,6 +2907,11 @@ float ggml_type_sizef(enum ggml_type type) {
28952907
return ((float)(GGML_TYPE_SIZE[type]))/GGML_BLCK_SIZE[type];
28962908
}
28972909

2910+
const char * ggml_type_name(enum ggml_type type) {
2911+
return GGML_TYPE_NAME[type];
2912+
}
2913+
2914+
28982915
size_t ggml_element_size(const struct ggml_tensor * tensor) {
28992916
return GGML_TYPE_SIZE[tensor->type];
29002917
}

Diff for: ggml.h

+2
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ int ggml_blck_size (enum ggml_type type);
354354
size_t ggml_type_size (enum ggml_type type); // size in bytes for all elements in a block
355355
float ggml_type_sizef(enum ggml_type type); // ggml_type_size()/ggml_blck_size() as float
356356

357+
const char * ggml_type_name(enum ggml_type type);
358+
357359
size_t ggml_element_size(const struct ggml_tensor * tensor);
358360

359361
struct ggml_context * ggml_init(struct ggml_init_params params);

Diff for: llama.cpp

+2-12
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,6 @@ static std::string llama_format_tensor_shape(const std::vector<uint32_t> & ne) {
269269
return ret;
270270
}
271271

272-
static const char * llama_format_type(enum ggml_type type) {
273-
switch (type) {
274-
case GGML_TYPE_F32: return "f32";
275-
case GGML_TYPE_F16: return "f16";
276-
case GGML_TYPE_Q4_0: return "q4_0";
277-
case GGML_TYPE_Q4_1: return "q4_1";
278-
default: LLAMA_ASSERT(false);
279-
}
280-
}
281-
282272
static size_t llama_calc_tensor_size(const std::vector<uint32_t> & ne, enum ggml_type type) {
283273
size_t size = ggml_type_size(type);
284274
for (uint32_t dim : ne) {
@@ -1582,7 +1572,7 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
15821572
printf("[%zu/%zu] %36s - %s, type = %6s, ",
15831573
++idx, model_loader->tensors_map.tensors.size(),
15841574
tensor.name.c_str(), llama_format_tensor_shape(tensor.ne).c_str(),
1585-
llama_format_type(tensor.type));
1575+
ggml_type_name(tensor.type));
15861576

15871577
// This used to be a regex, but <regex> has an extreme cost to compile times.
15881578
bool quantize = tensor.name.rfind("weight") == tensor.name.size() - 6; // ends with 'weight'?
@@ -1615,7 +1605,7 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
16151605
f32_data[i] = ggml_fp16_to_fp32(f16_data[i]);
16161606
}
16171607
} else {
1618-
throw format("type %s unsupported for integer quantization", llama_format_type(tensor.type));
1608+
throw format("type %s unsupported for integer quantization", ggml_type_name(tensor.type));
16191609
}
16201610

16211611
printf("quantizing .. ");

0 commit comments

Comments
 (0)