diff --git a/examples/benchmark/benchmark-matmult.cpp b/examples/benchmark/benchmark-matmult.cpp index 47cb16c69d53..424504375f17 100644 --- a/examples/benchmark/benchmark-matmult.cpp +++ b/examples/benchmark/benchmark-matmult.cpp @@ -1,5 +1,6 @@ #include "common.h" #include "ggml.h" +#include "ggml-impl.h" #include #include @@ -32,14 +33,33 @@ static void ggml_graph_compute_helper(std::vector & buf, ggml_cgraph * } static float tensor_sum_elements(const ggml_tensor * tensor) { - double sum = 0; - if (tensor->type == GGML_TYPE_F32) { + double sum = 0.0; + float floatvalue = 0.0f; + unsigned short shortvalue = 0; + + if (tensor->type == GGML_TYPE_F16) { for (int j = 0; j < tensor->ne[1]; j++) { for (int k = 0; k < tensor->ne[0]; k++) { - sum += ((float *) tensor->data)[j*tensor->ne[0] + k]; + shortvalue = ((unsigned short *) tensor->data)[j * tensor->ne[0] + k]; + floatvalue = GGML_FP16_TO_FP32(shortvalue); + sum += floatvalue; } } + } else if (tensor->type == GGML_TYPE_F32) { + for (int j = 0; j < tensor->ne[1]; j++) { + for (int k = 0; k < tensor->ne[0]; k++) { + sum += ((float *) tensor->data)[j * tensor->ne[0] + k]; + } + } + } else if (ggml_is_quantized(tensor->type)) { + std::vector f32out(ggml_nelements(tensor)); + ggml_type_traits_t qtype = ggml_internal_get_type_traits(tensor->type); + qtype.to_float((void *)tensor->data, f32out.data(), f32out.size()); + for (const float & value : f32out) { + sum += value; + } } + return sum; }