Skip to content

Commit 16b8ecd

Browse files
committed
WIP
1 parent 98ba54e commit 16b8ecd

File tree

2 files changed

+218
-52
lines changed

2 files changed

+218
-52
lines changed

examples/eval-callback/eval-callback.cpp

Lines changed: 216 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,54 +27,189 @@ static std::string ggml_ne_string(const ggml_tensor * t) {
2727
return str;
2828
}
2929

30+
static std::string ggml_nb_string(const ggml_tensor * t) {
31+
std::string str;
32+
for (int i = 0; i < GGML_MAX_DIMS; ++i) {
33+
str += std::to_string((t->nb[i]/ggml_element_size(t)));
34+
if (i + 1 < GGML_MAX_DIMS) {
35+
str += ", ";
36+
}
37+
}
38+
return str;
39+
}
40+
41+
3042
static void ggml_print_tensor(uint8_t * data, ggml_type type, const int64_t * ne, const size_t * nb, int64_t n) {
3143
GGML_ASSERT(n > 0);
3244
float sum = 0;
33-
for (int64_t i3 = 0; i3 < ne[3]; i3++) {
34-
printf(" [\n");
35-
for (int64_t i2 = 0; i2 < ne[2]; i2++) {
36-
if (i2 == n && ne[2] > 2*n) {
37-
printf(" ..., \n");
38-
i2 = ne[2] - n;
39-
}
40-
printf(" [\n");
41-
for (int64_t i1 = 0; i1 < ne[1]; i1++) {
42-
if (i1 == n && ne[1] > 2*n) {
43-
printf(" ..., \n");
44-
i1 = ne[1] - n;
45-
}
46-
printf(" [");
47-
for (int64_t i0 = 0; i0 < ne[0]; i0++) {
48-
if (i0 == n && ne[0] > 2*n) {
49-
printf("..., ");
50-
i0 = ne[0] - n;
51-
}
52-
size_t i = i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 * nb[0];
53-
float v;
54-
if (type == GGML_TYPE_F16) {
55-
v = ggml_fp16_to_fp32(*(ggml_fp16_t *) data + i);
56-
} else if (type == GGML_TYPE_F32) {
57-
v = *(float *) data + i;
58-
} else if (type == GGML_TYPE_I32) {
59-
v = (float) *(int32_t *) data + i;
60-
} else if (type == GGML_TYPE_I16) {
61-
v = (float) *(int16_t *) data + i;
62-
} else if (type == GGML_TYPE_I8) {
63-
v = (float) *(int8_t *) data + i;
64-
} else {
65-
GGML_ASSERT(false);
66-
}
67-
printf("%12.4f", v);
68-
sum += v;
69-
if (i0 < ne[0] - 1) printf(", ");
70-
}
71-
printf("],\n");
72-
}
73-
printf(" ],\n");
45+
46+
for (int64_t i0 = 0; i0 < 3; i0++) {
47+
if (i0 == n && ne[0] > 2*n) {
48+
printf("..., ");
49+
i0 = ne[0] - n;
50+
}
51+
size_t i = i0;//i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 * nb[0];
52+
float v;
53+
if (type == GGML_TYPE_F16) {
54+
v = ggml_fp16_to_fp32(*(ggml_fp16_t *) data + i);
55+
} else if (type == GGML_TYPE_F32) {
56+
v = *(float *) data + i;
57+
} else if (type == GGML_TYPE_I32) {
58+
v = (float) *((int32_t *) data + i);
59+
} else if (type == GGML_TYPE_I16) {
60+
v = (float) *(int16_t *) data + i;
61+
} else if (type == GGML_TYPE_I8) {
62+
v = (float) *(int8_t *) data + i;
63+
} else {
64+
GGML_ASSERT(false);
65+
}
66+
printf("%12.4f", v);
67+
sum += v;
68+
}
69+
printf("\n");
70+
71+
72+
73+
74+
// for (int64_t i3 = 0; i3 < ne[3]; i3++) {
75+
// printf(" [\n");
76+
// for (int64_t i2 = 0; i2 < ne[2]; i2++) {
77+
// if (i2 == n && ne[2] > 2*n) {
78+
// printf(" ..., \n");
79+
// i2 = ne[2] - n;
80+
// }
81+
// printf(" [\n");
82+
// for (int64_t i1 = 0; i1 < ne[1]; i1++) {
83+
// if (i1 == n && ne[1] > 2*n) {
84+
// printf(" ..., \n");
85+
// i1 = ne[1] - n;
86+
// }
87+
// printf(" [");
88+
// for (int64_t i0 = 0; i0 < ne[0]; i0++) {
89+
// if (i0 == n && ne[0] > 2*n) {
90+
// printf("..., ");
91+
// i0 = ne[0] - n;
92+
// }
93+
// size_t i = i0;//i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 * nb[0];
94+
// float v;
95+
// if (type == GGML_TYPE_F16) {
96+
// v = ggml_fp16_to_fp32(*(ggml_fp16_t *) data + i);
97+
// } else if (type == GGML_TYPE_F32) {
98+
// v = *(float *) data + i;
99+
// } else if (type == GGML_TYPE_I32) {
100+
// v = (float) *((int32_t *) data + i);
101+
// } else if (type == GGML_TYPE_I16) {
102+
// v = (float) *(int16_t *) data + i;
103+
// } else if (type == GGML_TYPE_I8) {
104+
// v = (float) *(int8_t *) data + i;
105+
// } else {
106+
// GGML_ASSERT(false);
107+
// }
108+
// printf("%12.4f", v);
109+
// sum += v;
110+
// if (i0 < ne[0] - 1) printf(", ");
111+
// }
112+
// printf("],\n");
113+
// }
114+
// printf(" ],\n");
115+
// }
116+
// printf(" ]\n");
117+
// printf(" sum = %f\n", sum);
118+
// }
119+
}
120+
121+
float Sum(float *arr, int64_t N){
122+
float s = 0.0;
123+
for (int i = 0; i < N; i++){
124+
s += arr[i];
125+
}
126+
return s;
127+
}
128+
float PrintArr(const char * name, float * arr, int64_t N){
129+
float sum = 0.0;
130+
if (arr != NULL){
131+
sum = Sum(arr, N);
132+
printf("%s %d %10f \n",name, N, sum);
133+
} else {
134+
printf("%s %d %10f \n",name, 0, 0.0);
135+
}
136+
return sum;
137+
}
138+
139+
size_t get_nth_element(const int64_t *ne, const size_t *nb, int64_t nth) {
140+
size_t offset = 0;
141+
size_t divisor = 1;
142+
for (int i = 3; i >= 0; --i) {
143+
size_t index = size_t(floor(nth / divisor)) % ne[i];
144+
offset += index * nb[i]/4;
145+
divisor *= ne[i];
146+
}
147+
return offset;
148+
}
149+
150+
void print_tensor(const ggml_tensor * src0) {
151+
float sum = 0;
152+
153+
const int64_t * ne = src0->ne;
154+
int64_t n = 3;
155+
ggml_type type = src0->type;
156+
void * data = src0->data;
157+
158+
159+
char *buf = static_cast<char *>(malloc(sizeof(char)*ne[0]*8));
160+
161+
char *buf2 = buf;
162+
163+
for (int64_t i = 0; i < 1; i++) {
164+
if (i == n) {
165+
buf2 += sprintf(buf2, "..., ");
74166
}
75-
printf(" ]\n");
76-
printf(" sum = %f\n", sum);
167+
// int64_t offset = get_nth_element(src0->ne, src0->nb, i);
168+
// offset *= ggml_element_size(src0);
169+
int64_t offset = i;
170+
float v;
171+
if (type == GGML_TYPE_F16) {
172+
v = ggml_fp16_to_fp32(*(ggml_fp16_t *) data + offset);
173+
} else if (type == GGML_TYPE_F32) {
174+
v = *((float *) data + offset);
175+
} else if (type == GGML_TYPE_I32) {
176+
v = (float) *((int32_t *) data + offset);
177+
} else if (type == GGML_TYPE_I16) {
178+
v = (float) *(int16_t *) data + offset;
179+
} else if (type == GGML_TYPE_I8) {
180+
v = (float) *(int8_t *) data + offset;
181+
} else {
182+
GGML_ASSERT(false);
183+
}
184+
if (i < n) {
185+
buf2 += sprintf(buf2, "%12.4f", v);
186+
}
187+
sum += v;
77188
}
189+
190+
int i = 0;
191+
while (i < ggml_nbytes(src0)/4){
192+
float val = (((float *) src0->data)[i]);
193+
float diff = abs(val - 0.0022226818837225437164306640625);
194+
if (diff < 0.000001 ){
195+
printf("found %s: %d = %f\n", src0->name, i, val);
196+
}
197+
i += 1;
198+
}
199+
200+
int max_name_length = 15;
201+
int max_dim_length = 15;
202+
int max_str_length = 15;
203+
printf("%-*.15s [0]=%.15g dim={%-*.15s} str={%-*.15s} [addr]=%lu\n",
204+
max_name_length, src0->name,
205+
sum,
206+
max_dim_length, ggml_ne_string(src0).c_str(),
207+
max_str_length, ggml_nb_string(src0).c_str(),
208+
src0->data);
209+
210+
211+
212+
// printf("%s\n", buf);
78213
}
79214

80215
/**
@@ -98,15 +233,46 @@ static bool ggml_debug(struct ggml_tensor * t, bool ask, void * user_data) {
98233
}
99234

100235
char src1_str[128] = {0};
236+
// if (src1) {
237+
// sprintf(src1_str, "%s{%s}\n", src1->name, ggml_ne_string(src1).c_str());
238+
// }
239+
240+
if (src0) {
241+
print_tensor(src0);
242+
// printf("%s{%s} n=%d %f\n", src0->name, ggml_ne_string(src0).c_str(),src0->ne[0], Sum(static_cast<float *>(src0->data), src0->ne[0]));
243+
// printf("%s{%s}", src0->name, ggml_ne_string(src0).c_str());
244+
// enum ggml_type type = src0->name == "inp_tokens" ? GGML_TYPE_I32:src0->type;
245+
// ggml_print_tensor(static_cast<uint8_t *>(src0->data), src0->type, src0->ne, src0->nb, 3);
246+
// PrintArr(src0->name, static_cast<float *>(src0->data), src0->ne[0]);
247+
}
101248
if (src1) {
102-
sprintf(src1_str, "%s{%s}", src1->name, ggml_ne_string(src1).c_str());
249+
print_tensor(src1);
250+
// printf("%s{%s} n=%d %f\n", src1->name, ggml_ne_string(src1).c_str(),src0->ne[0], Sum(static_cast<float *>(src1->data), src1->ne[0]));
251+
// enum ggml_type type = src1->name == "inp_tokens" ? GGML_TYPE_I32:src1->type;
252+
// ggml_print_tensor(static_cast<uint8_t *>(src1->data), type, src1->ne, src1->nb, 3);
253+
// ggml_print_tensor(static_cast<uint8_t *>(src1->data), src1->type, src1->ne, src1->nb, 3);
254+
// PrintArr(src1->name, static_cast<float *>(src1->data), src1->ne[0]);
255+
}
256+
printf("%s ==\n", ggml_op_desc(t));
257+
if (t) {
258+
print_tensor(t);
259+
// printf("%s{%s} n=%d %f\n", t->name, ggml_ne_string(t).c_str(),src0->ne[0], Sum(static_cast<float *>(t->data), t->ne[0]));
260+
// printf("%s{%s}", t->name, ggml_ne_string(t).c_str());
261+
// PrintArr(t->name, static_cast<float *>(t->data), t->ne[0]);
262+
// ggml_print_tensor(static_cast<uint8_t *>(t->data), t->type, t->ne, t->nb, 3);
263+
// printf("\n == \n");
103264
}
265+
printf("\n\n");
266+
267+
268+
104269

105-
printf("%s: %24s = (%s) %10s(%s{%s}, %s}) = {%s}\n", __func__,
106-
t->name, ggml_type_name(t->type), ggml_op_desc(t),
107-
src0->name, ggml_ne_string(src0).c_str(),
108-
src1 ? src1_str : "",
109-
ggml_ne_string(t).c_str());
270+
// printf("%24s = (%s) %10s(%s{%s}, %s}) = {%s}\n",
271+
//
272+
//
273+
// t->name, ggml_op_desc(t), src0->name, ggml_ne_string(src0).c_str(),
274+
// src1 ? src1_str : "",
275+
// ggml_ne_string(t).c_str());
110276

111277

112278
// copy the data from the GPU memory if needed
@@ -120,7 +286,7 @@ static bool ggml_debug(struct ggml_tensor * t, bool ask, void * user_data) {
120286

121287
if (!ggml_is_quantized(t->type)) {
122288
uint8_t * data = is_host ? (uint8_t *) t->data : cb_data->data.data();
123-
ggml_print_tensor(data, t->type, t->ne, t->nb, 3);
289+
// ggml_print_tensor(data, t->type, t->ne, t->nb, 3);
124290
}
125291

126292
return true;

examples/split-test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
set(TARGET baby-llama)
2-
add_executable(${TARGET} baby-llama.cpp)
1+
set(TARGET split-test)
2+
add_executable(${TARGET} split-test.cpp)
33
install(TARGETS ${TARGET} RUNTIME)
44
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
55
target_compile_features(${TARGET} PRIVATE cxx_std_11)

0 commit comments

Comments
 (0)