Skip to content

Commit 34b607c

Browse files
committed
make clipcb working again
1 parent 4df6e85 commit 34b607c

File tree

1 file changed

+63
-20
lines changed

1 file changed

+63
-20
lines changed

tools/mtmd/clip.cpp

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
#include "ggml-cpp.h"
99
#include "ggml-alloc.h"
1010
#include "ggml-backend.h"
11+
#include "ggml/src/ggml-impl.h"
1112
#include "gguf.h"
1213

14+
#include <algorithm>
1315
#include <cassert>
1416
#include <cmath>
1517
#include <cstdlib>
1618
#include <cstring>
1719
#include <fstream>
1820
#include <map>
21+
#include <memory>
1922
#include <stdexcept>
23+
#include <string>
2024
#include <unordered_set>
2125
#include <vector>
2226
#include <cinttypes>
@@ -2004,16 +2008,67 @@ struct clip_graph {
20042008
//
20052009
// utility functions
20062010
//
2011+
static float ggml_get_float_value(uint8_t * data, ggml_type type, const size_t * nb, size_t i0, size_t i1, size_t i2, size_t i3) {
2012+
size_t i = i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 * nb[0];
2013+
float v;
2014+
if (type == GGML_TYPE_F16) {
2015+
v = ggml_fp16_to_fp32(*(ggml_fp16_t *) &data[i]);
2016+
} else if (type == GGML_TYPE_F32) {
2017+
v = *(float *) &data[i];
2018+
} else if (type == GGML_TYPE_I64) {
2019+
v = (float) *(int64_t *) &data[i];
2020+
} else if (type == GGML_TYPE_I32) {
2021+
v = (float) *(int32_t *) &data[i];
2022+
} else if (type == GGML_TYPE_I16) {
2023+
v = (float) *(int16_t *) &data[i];
2024+
} else if (type == GGML_TYPE_I8) {
2025+
v = (float) *(int8_t *) &data[i];
2026+
} else if (type == GGML_TYPE_BF16) {
2027+
v = ggml_bf16_to_fp32(*(ggml_bf16_t *) &data[i]);
2028+
} else {
2029+
GGML_ABORT("fatal error");
2030+
}
2031+
return v;
2032+
}
20072033

2008-
void cb(ggml_tensor * cur0, const char * name, int il) const {
2009-
if (ctx->debug_graph) {
2010-
ggml_tensor * cur = ggml_cpy(ctx0, cur0, ggml_dup_tensor(ctx0, cur0));
2011-
std::string cur_name = il >= 0 ? std::string(name) + "_" + std::to_string(il) : name;
2012-
ggml_set_name(cur, cur_name.c_str());
2013-
ggml_set_output(cur);
2014-
ggml_build_forward_expand(gf, cur);
2015-
ctx->debug_print_tensors.push_back(cur);
2034+
static void print_debug(ggml_tensor * dst, int ith, int nth, void * userdata) {
2035+
GGML_UNUSED(nth);
2036+
GGML_UNUSED(userdata);
2037+
if (ith != 0) {
2038+
return;
20162039
}
2040+
2041+
ggml_tensor * t = dst->src[0];
2042+
uint8_t * data = (uint8_t *) t->data;
2043+
2044+
print_tensor_shape(t);
2045+
print_tensor_data(t, data, 3);
2046+
2047+
double sum = 0;
2048+
float lowest = 0;
2049+
float highest = 0;
2050+
for (int64_t i3 = 0; i3 < t->ne[3]; i3++) {
2051+
for (int64_t i2 = 0; i2 < t->ne[2]; i2++) {
2052+
for (int64_t i1 = 0; i1 < t->ne[1]; i1++) {
2053+
for (int64_t i0 = 0; i0 < t->ne[0]; i0++) {
2054+
const float v = ggml_get_float_value(data, t->type, t->nb, i0, i1, i2, i3);
2055+
sum += v;
2056+
lowest = std::min(v, lowest);
2057+
highest = std::max(v, highest);
2058+
}
2059+
}
2060+
}
2061+
}
2062+
2063+
printf("sum = %.6f, max = %.6f, min = %.6f\n", sum, highest, lowest);
2064+
}
2065+
2066+
void cb(ggml_tensor * t, const char * name, int il) const {
2067+
std::string t_name = std::string(name) + "_" + std::to_string(il);
2068+
ggml_tensor * args[] = { t };
2069+
ggml_tensor * res = ggml_custom_4d(ctx0, t->type, t->ne[0], t->ne[1], t->ne[2], t->ne[3], args, 1, print_debug, 1, nullptr);
2070+
strcpy(res->name, t_name.c_str());
2071+
ggml_build_forward_expand(gf, res);
20172072
}
20182073

20192074
// siglip2 naflex
@@ -4986,18 +5041,6 @@ bool clip_image_batch_encode(clip_ctx * ctx, const int n_threads, const clip_ima
49865041
return false;
49875042
}
49885043

4989-
// print debug nodes
4990-
if (ctx->debug_graph) {
4991-
LOG_INF("\n\n---\n\n");
4992-
LOG_INF("\n\nDebug graph:\n\n");
4993-
for (ggml_tensor * t : ctx->debug_print_tensors) {
4994-
std::vector<uint8_t> data(ggml_nbytes(t));
4995-
ggml_backend_tensor_get(t, data.data(), 0, ggml_nbytes(t));
4996-
print_tensor_shape(t);
4997-
print_tensor_data(t, data.data(), 3);
4998-
}
4999-
}
5000-
50015044
// the last node is the embedding tensor
50025045
ggml_tensor * embeddings = ggml_graph_node(gf, -1);
50035046

0 commit comments

Comments
 (0)