|
8 | 8 | #include "ggml-cpp.h" |
9 | 9 | #include "ggml-alloc.h" |
10 | 10 | #include "ggml-backend.h" |
| 11 | +#include "ggml/src/ggml-impl.h" |
11 | 12 | #include "gguf.h" |
12 | 13 |
|
| 14 | +#include <algorithm> |
13 | 15 | #include <cassert> |
14 | 16 | #include <cmath> |
15 | 17 | #include <cstdlib> |
16 | 18 | #include <cstring> |
17 | 19 | #include <fstream> |
18 | 20 | #include <map> |
| 21 | +#include <memory> |
19 | 22 | #include <stdexcept> |
| 23 | +#include <string> |
20 | 24 | #include <unordered_set> |
21 | 25 | #include <vector> |
22 | 26 | #include <cinttypes> |
@@ -2004,16 +2008,67 @@ struct clip_graph { |
2004 | 2008 | // |
2005 | 2009 | // utility functions |
2006 | 2010 | // |
| 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 | + } |
2007 | 2033 |
|
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; |
2016 | 2039 | } |
| 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); |
2017 | 2072 | } |
2018 | 2073 |
|
2019 | 2074 | // siglip2 naflex |
@@ -4986,18 +5041,6 @@ bool clip_image_batch_encode(clip_ctx * ctx, const int n_threads, const clip_ima |
4986 | 5041 | return false; |
4987 | 5042 | } |
4988 | 5043 |
|
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 | | - |
5001 | 5044 | // the last node is the embedding tensor |
5002 | 5045 | ggml_tensor * embeddings = ggml_graph_node(gf, -1); |
5003 | 5046 |
|
|
0 commit comments