Skip to content

examples: refine tensor_sum_elements(tensor dump) in examples/benchmark/benchmark-matmult.cpp #7844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions examples/benchmark/benchmark-matmult.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "common.h"
#include "ggml.h"
#include "ggml-impl.h"

#include <locale.h>
#include <assert.h>
Expand Down Expand Up @@ -32,14 +33,33 @@ static void ggml_graph_compute_helper(std::vector<uint8_t> & 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<float> 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;
}

Expand Down
Loading