Skip to content

Commit 3f373bd

Browse files
cmp-nctJohn
andauthored
Added ggml_tensor_printf() to debug tensors. (ggml-org#25)
* Added ggml_tensor_printf() to debug tensors. Not sure if all cases work, it was only tested a bit. Example for the ggml_repeat2 dst tensor after it was computed: +======================+======================+======================+======================+ | ggml_compute_forward_repeat2_f32:9497 | node_1233 +----------------------+----------------------+----------------------+----------------------+ | Dimensions | Quantization | Layer id | Backend | | 3 | f32 | 31 | CPU | +----------------------+----------------------+----------------------+----------------------+ | Elements | Src0 | Src1 | Operation | | 64 x 2 x 71 | 64 x 2 x 1 | 64 x 2 x 71 | REPEAT2 | +----------------------+----------------------+----------------------+----------------------+ | Src0 name: | node_1232 | | Src1 name: | leaf_17 | +----------------------+----------------------+----------------------+----------------------+ +-------------------------------------------------------------------------------------------+ | Content of src0 "node_1232" (3 dim) Layer 0 | -0.019758 0.772589 0.000000 | | 0.772589 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | +-------------------------------------------------------------------------------------------+ Layer 1 | 0.001423 -1.063233 0.000000 | | -1.063233 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | +-------------------------------------------------------------------------------------------+ Layer 2 | -0.042461 -0.936166 0.000000 | | -0.936166 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | +-------------------------------------------------------------------------------------------+ +-------------------------------------------------------------------------------------------+ | Content of src1 "leaf_17" (3 dim) Layer 0 | 0.000000 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | +-------------------------------------------------------------------------------------------+ Layer 1 | 0.000000 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | +-------------------------------------------------------------------------------------------+ Layer 2 | 0.000000 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | | 0.000000 0.000000 0.000000 | +-------------------------------------------------------------------------------------------+ +-------------------------------------------------------------------------------------------+ | Content of dst "node_1233" (3 dim) Layer 0 | -0.019758 -0.019758 -0.019758 | | 0.772589 0.772589 0.772589 | | -0.019758 -0.019758 -0.019758 | +-------------------------------------------------------------------------------------------+ Layer 1 | 0.001423 0.001423 0.001423 | | -1.063233 -1.063233 -1.063233 | | 0.001423 0.001423 0.001423 | +-------------------------------------------------------------------------------------------+ Layer 2 | -0.042461 -0.042461 -0.042461 | | -0.936166 -0.936166 -0.936166 | | -0.042461 -0.042461 -0.042461 | +-------------------------------------------------------------------------------------------+ +======================+======================+======================+======================+ * typo stride>n_elem - sample print is probably still bugged * added strides and boolean info flags --------- Co-authored-by: John <nolife+git@gmail.com>
1 parent e97d148 commit 3f373bd

2 files changed

Lines changed: 171 additions & 31 deletions

File tree

ggml.c

Lines changed: 168 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,36 +4195,6 @@ void ggml_scratch_load(struct ggml_context * ctx) {
41954195

41964196
////////////////////////////////////////////////////////////////////////////////
41974197

4198-
size_t ggml_sizeof_tensor_data_impl(enum ggml_type type, int n_dims, const int64_t* ne) {
4199-
size_t size_needed = 0;
4200-
size_needed += GGML_TYPE_SIZE[type]*(ne[0]/GGML_BLCK_SIZE[type]);
4201-
for (int i = 1; i < n_dims; i++) {
4202-
size_needed *= ne[i];
4203-
}
4204-
// align to GGML_MEM_ALIGN
4205-
size_needed = ((size_needed + GGML_MEM_ALIGN - 1)/GGML_MEM_ALIGN)*GGML_MEM_ALIGN;
4206-
return size_needed;
4207-
}
4208-
4209-
size_t ggml_sizeof_tensor_1d(enum ggml_type type, int64_t ne0) {
4210-
return ggml_tensor_overhead() + ggml_sizeof_tensor_data_impl(type, 1, &ne0);
4211-
}
4212-
4213-
size_t ggml_sizeof_tensor_2d(enum ggml_type type, int64_t ne0, int64_t ne1) {
4214-
const int64_t ne[2] = {ne0, ne1};
4215-
return ggml_tensor_overhead() + ggml_sizeof_tensor_data_impl(type, 2, &ne);
4216-
}
4217-
4218-
size_t ggml_sizeof_tensor_3d(enum ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2) {
4219-
const int64_t ne[3] = {ne0, ne1, ne2};
4220-
return ggml_tensor_overhead() + ggml_sizeof_tensor_data_impl(type, 3, &ne);
4221-
}
4222-
4223-
size_t ggml_sizeof_tensor_4d(enum ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3) {
4224-
const int64_t ne[4] = {ne0, ne1, ne2, ne3};
4225-
return ggml_tensor_overhead() + ggml_sizeof_tensor_data_impl(type, 4, &ne);
4226-
}
4227-
42284198
struct ggml_tensor * ggml_new_tensor_impl(
42294199
struct ggml_context * ctx,
42304200
enum ggml_type type,
@@ -9292,7 +9262,6 @@ static void ggml_compute_forward_repeat_back(
92929262
}
92939263

92949264
// ggml_compute_forward_repeat2
9295-
92969265
static void ggml_compute_forward_repeat2_f32(
92979266
const struct ggml_compute_params * params,
92989267
const struct ggml_tensor * src0,
@@ -9354,6 +9323,7 @@ static void ggml_compute_forward_repeat2_f32(
93549323
}
93559324
}
93569325
}
9326+
// ggml_tensor_printf(dst, __func__, __LINE__, true,true);
93579327
}
93589328

93599329
static void ggml_compute_forward_repeat2(
@@ -18297,6 +18267,173 @@ size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, i
1829718267
return result;
1829818268
}
1829918269

18270+
18271+
////////////////////////////////////////////////////////////////////////////////
18272+
18273+
void ggml_printTensorSample(char *prefix,const struct ggml_tensor * tensor) {
18274+
const char *sep = "+-------------------------------------------------------------------------------------------+\n";
18275+
printf("%s", sep);
18276+
printf("| Content of %s \"%s\" (%d dim)",prefix,tensor->name,tensor->n_dims);
18277+
printf("\n");
18278+
const int max_elements = 4;
18279+
18280+
if (tensor->n_dims == 1) {
18281+
printf("| ");
18282+
for(int i = 0; i < tensor->ne[0] && i < max_elements; i++){
18283+
printf("%-20f ", *(float *)((char *) tensor->data + i*tensor->nb[0]));
18284+
}
18285+
printf("|");
18286+
printf("\n");
18287+
printf("%s", sep);
18288+
}
18289+
else if (tensor->n_dims == 2) {
18290+
for(int i = 0; i < tensor->ne[0] && i < max_elements; i++){
18291+
printf("| ");
18292+
for(int j = 0; j < tensor->ne[1] && j < max_elements; j++){
18293+
printf("%-20f ", *(float *)((char *) tensor->data + i*tensor->nb[0] + j*tensor->nb[1]));
18294+
}
18295+
printf("|");
18296+
printf("\n");
18297+
}
18298+
printf("%s", sep);
18299+
}
18300+
else if(tensor->n_dims == 3) {
18301+
for(int i = 0; i < tensor->ne[0] && i < 3; i++){
18302+
printf("Layer %d\n", i);
18303+
for(int j = 0; j < tensor->ne[1] && j < max_elements; j++){
18304+
printf("| ");
18305+
for(int k = 0; k < tensor->ne[2] && k < max_elements; k++){
18306+
printf("%-20f ", *(float *)((char *) tensor->data + i*tensor->nb[0] + j*tensor->nb[1] + k*tensor->nb[2]));
18307+
}
18308+
printf("|\n");
18309+
}
18310+
printf("%s\n", sep);
18311+
}
18312+
}
18313+
else if(tensor->n_dims == 4){
18314+
for(int i = 0; i < tensor->ne[0] && i < 3; i++){
18315+
printf("Batch %d\n", i);
18316+
for(int j = 0; j < tensor->ne[1] && j < 3; j++){
18317+
printf("Layer %d\n", j);
18318+
for(int k = 0; k < tensor->ne[2] && k < max_elements; k++){
18319+
printf("| ");
18320+
for(int l = 0; l < tensor->ne[3] && l < 3; l++){
18321+
printf("%-20f ", *(float *)((char *) tensor->data + i*tensor->nb[0] + j*tensor->nb[1] + k*tensor->nb[2] + l*tensor->nb[3]));
18322+
}
18323+
printf("|\n");
18324+
}
18325+
printf("%s\n", sep);
18326+
}
18327+
}
18328+
}
18329+
}
18330+
18331+
void ggml_tensor_printf(const struct ggml_tensor *tensor, char *prefix, int line, bool extended, bool print_sample) {
18332+
char tmp_str[256] = {0};
18333+
const char *sep = "+----------------------+----------------------+----------------------+----------------------+";
18334+
const char *sep_border = "+======================+======================+======================+======================+";
18335+
printf("%s\n", sep_border);
18336+
printf("| %s:%d\n", prefix,line);
18337+
printf("| %-32s [%s type]\n", tensor->name, GGML_TYPE_NAME[tensor->type]);
18338+
printf("%s\n", sep);
18339+
char strides[256] = {0};
18340+
/**
18341+
// nb[0] = sizeof(type)
18342+
// nb[1] = nb[0] * ne[0] + padding
18343+
// nb[i] = nb[i-1] * ne[i-1]
18344+
*/
18345+
{
18346+
int pos = 0;
18347+
for (int i = 0; i < tensor->n_dims; i++) {
18348+
pos += snprintf(strides + pos, sizeof(strides) - pos, "%" PRId64, tensor->nb[i]);
18349+
if (i != tensor->n_dims - 1) {
18350+
pos += snprintf(strides + pos, sizeof(strides) - pos, "x");
18351+
}
18352+
}
18353+
}
18354+
18355+
18356+
printf("| %-20s | %-20s | %-20s | %-20s |\n", "Dimensions", "Strides", "Layer id", "Backend");
18357+
printf("| %-20d | %-20s | %-20d | %-20s |\n", tensor->n_dims, strides, tensor->meta.layer_id >= 0 ? tensor->meta.layer_id : -1, tensor->backend == GGML_BACKEND_CPU ? "CPU" : ((tensor->backend == GGML_BACKEND_GPU) ? "GPU" : "GPU_SPLIT"));
18358+
printf("%s\n", sep);
18359+
int pos = 0;
18360+
for (int i = 0; i < tensor->n_dims; i++) {
18361+
pos += snprintf(tmp_str + pos, sizeof(tmp_str) - pos, "%" PRId64, tensor->ne[i]);
18362+
if (i != tensor->n_dims - 1) {
18363+
pos += snprintf(tmp_str + pos, sizeof(tmp_str) - pos, " x ");
18364+
}
18365+
}
18366+
printf("| %-20s | %-20s | %-20s | %-20s |\n", "Elements", "Src0", "Src1","Operation");
18367+
printf("| %-20s |", tmp_str);
18368+
18369+
18370+
if (tensor->src0) {
18371+
char tmp_str[256] = {0};
18372+
int pos = 0;
18373+
for (int i = 0; i < tensor->src0->n_dims; i++) {
18374+
pos += snprintf(tmp_str + pos, sizeof(tmp_str) - pos, "%" PRId64, tensor->src0->ne[i]);
18375+
if (i != tensor->src0->n_dims - 1) {
18376+
pos += snprintf(tmp_str + pos, sizeof(tmp_str) - pos, " x ");
18377+
}
18378+
}
18379+
printf(" %-20s |", tmp_str);
18380+
} else
18381+
{
18382+
printf(" %-20s |", "N/A");
18383+
}
18384+
if (tensor->src1) {
18385+
int pos = 0;
18386+
for (int i = 0; i < tensor->src1->n_dims; i++) {
18387+
pos += snprintf(tmp_str + pos, sizeof(tmp_str) - pos, "%" PRId64, tensor->src1->ne[i]);
18388+
if (i != tensor->src1->n_dims - 1) {
18389+
pos += snprintf(tmp_str + pos, sizeof(tmp_str) - pos, " x ");
18390+
}
18391+
}
18392+
printf(" %-20s |", tmp_str);
18393+
} else {
18394+
printf(" %-20s |", "N/A");
18395+
}
18396+
printf(" %-20s |", tensor->op > 0?GGML_OP_NAME[tensor->op] : "N/A");
18397+
printf("\n");
18398+
printf("%s\n", sep);
18399+
18400+
if (extended)
18401+
{
18402+
bool is_transposed = ggml_is_transposed(tensor);
18403+
bool is_permuted = ggml_is_permuted(tensor);
18404+
bool is_cont = ggml_is_contiguous(tensor);
18405+
printf("| %-17s%s | %-17s%s | %-17s%s | %-6s%11.2f MB |\n", "Transposed:", is_transposed ? "Yes" : "No ", "Permuted:", is_permuted ? "Yes" : "No ", "Contiguous:", is_cont ? "Yes" : "No ","Size:", ggml_nbytes(tensor)/(1024.0*1024.0));
18406+
}
18407+
18408+
18409+
if (extended) {
18410+
if (tensor->src0 && strlen(tensor->src0->name)) {
18411+
printf("| %-20s | ", "Src0 name:");
18412+
printf("%-66s |\n", tensor->src0->name);
18413+
}
18414+
if (tensor->src1 && strlen(tensor->src1->name)) {
18415+
printf("| %-20s | ", "Src1 name:");
18416+
printf("%-66s |\n", tensor->src1->name);
18417+
}
18418+
printf("%s\n\n", sep);
18419+
}
18420+
18421+
18422+
if (print_sample) {
18423+
if (extended)
18424+
{
18425+
if (tensor->src0 && tensor->src0->ne[0]) {
18426+
ggml_printTensorSample("src0",tensor->src0);
18427+
}
18428+
if (tensor->src1 && tensor->src1->ne[0]) {
18429+
ggml_printTensorSample("src1",tensor->src1);
18430+
}
18431+
}
18432+
ggml_printTensorSample("dst",tensor);
18433+
}
18434+
printf("%s\n", sep_border);
18435+
}
18436+
1830018437
////////////////////////////////////////////////////////////////////////////////
1830118438

1830218439
int ggml_cpu_has_avx(void) {

ggml.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,9 @@ extern "C" {
11381138

11391139
// dump the graph into a file using the dot format
11401140
GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
1141+
1142+
// visualize the tensor - extended adds more information - when printing sample content extended will also print src0 and src1 content
1143+
void ggml_tensor_printf(const struct ggml_tensor *tensor, char *prefix, int line, bool extended, bool print_sample);
11411144

11421145
//
11431146
// optimization

0 commit comments

Comments
 (0)