From cfb820b3436b9ddaead16a21a3a9bda7fad0c150 Mon Sep 17 00:00:00 2001 From: Pierrick HYMBERT Date: Wed, 10 Apr 2024 23:23:22 +0200 Subject: [PATCH] ggml-debug: better tensor type support --- examples/ggml-debug/ggml-debug.cpp | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/examples/ggml-debug/ggml-debug.cpp b/examples/ggml-debug/ggml-debug.cpp index aa2df8c64..43be46ef0 100644 --- a/examples/ggml-debug/ggml-debug.cpp +++ b/examples/ggml-debug/ggml-debug.cpp @@ -13,7 +13,7 @@ * Later on we can for example add operation or tensor name filter from the CLI arg, or a file descriptor to dump the tensor. */ struct callback_data { - std::vector data; + std::vector data; }; static std::string ggml_ne_string(const ggml_tensor * t) { @@ -27,7 +27,9 @@ static std::string ggml_ne_string(const ggml_tensor * t) { return str; } -static void ggml_print_tensor(int8_t * data, const int64_t * ne, const size_t * nb, int64_t n) { +static void ggml_print_tensor(uint8_t * data, ggml_type type, const int64_t * ne, const size_t * nb, int64_t n) { + size_t bs = ggml_blck_size(type); + for (int64_t i3 = 0; i3 < ne[3]; i3++) { printf(" [\n"); for (int64_t i2 = 0; i2 < ne[2] && i2 < n; i2++) { @@ -35,8 +37,21 @@ static void ggml_print_tensor(int8_t * data, const int64_t * ne, const size_t * for (int64_t i1 = 0; i1 < ne[1] && i1 < n; i1++) { printf(" ["); for (int64_t i0 = 0; i0 < ne[0] && i0 < n; i0++) { - size_t i = i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 * nb[0]; - float v = *(float *)(data + i); + size_t i = i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 / bs * nb[0]; + float v; + if (type == GGML_TYPE_F16) { + v = ggml_fp16_to_fp32(*(ggml_fp16_t *) data + i); + } else if (type == GGML_TYPE_F32) { + v = *(float *) data + i; + } else if (type == GGML_TYPE_I32) { + v = (float) *(int32_t *) data + i; + } else if (type == GGML_TYPE_I16) { + v = (float) *(int16_t *) data + i; + } else if (type == GGML_TYPE_I8) { + v = (float) *(int8_t *) data + i; + } else { + GGML_ASSERT(false); + } printf("%8.4f", v); if (i0 < ne[0] - 1 && i0 < n - 1) printf(", "); } @@ -92,9 +107,9 @@ static bool ggml_debug(struct ggml_tensor * t, bool ask, void * user_data) { ggml_backend_tensor_get(t, cb_data->data.data(), 0, n_bytes); } - if (t->type == GGML_TYPE_F32 || t->type == GGML_TYPE_F16) { - int8_t * data = is_host ? (int8_t *) t->data : cb_data->data.data(); - ggml_print_tensor(data, t->ne, t->nb, 3); + if (!ggml_is_quantized(t->type)) { + uint8_t * data = is_host ? (uint8_t *) t->data : cb_data->data.data(); + ggml_print_tensor(data, t->type, t->ne, t->nb, 3); } return true;