How to print a GGML tensor prettily?

// common-ggml.cpp ... void print_ggml_tensor(const char *name, const struct ggml_tensor *tensor, bool use_backend, int precision) { std::vector<float> data(ggml_nelements(tensor)); if (use_backend) { ggml_backend_tensor_get(tensor, data.data(), 0, ggml_nbytes(tensor)); } else { memcpy(data.data(), ggml_get_data_f32(tensor), ggml_nbytes(tensor)); } std::vector<int64_t> effective_shape; for (int i = 0; i < GGML_MAX_DIMS && tensor->ne[i] > 1; i++) { effective_shape.push_back(tensor->ne[i]); } int effective_dims = effective_shape.size(); printf("%s:\n", name); // Recursive function to print the tensor elements with proper indentation std::function<void(size_t, size_t &)> print_recursive = [&](size_t dim, size_t &offset) { if (dim == effective_dims) { printf("%.*f", precision, data[offset++]); } else { printf("["); for (int64_t i = 0; i < effective_shape[dim]; i++) { if (i > 0) { printf(dim == effective_dims - 1 ? ", " : ",\n%*s", static_cast<int>(dim) + 1, ""); } print_recursive(dim + 1, offset); } printf("]"); } }; size_t offset = 0; print_recursive(0, offset); printf("\n"); }
Β 
Β 
/* 1. Allocate `ggml_context` to store tensor data 2. Create tensors and set data 3. Create a ggml_cgraph for mul_mat operation 4. Run the computation 5. Retrieve results (output tensors) 6. Free memory and exit */ #include "ggml.h" #include "common-ggml.h" #include <string.h> #include <stdio.h> #include <vector> #include <functional> #include <iostream> #include <iomanip> int main(void) { // initialize data of matrices to perform matrix multiplication const int rows_A = 4, cols_A = 2, in = 3; float matrix_A[rows_A * cols_A * in] = { 2, 8, 8, 5, 1, 1, 4, 2, 2, 8, 6, 6, 2, 8, 8, 5, 1, 1, 4, 2, 2, 8, 6, 6, }; // 1. Allocate `ggml_context` to store tensor data // Calculate the size needed to allocate size_t ctx_size = 0; ctx_size = 1 * 1024 * 1024; // 1 MB // Allocate `ggml_context` to store tensor data struct ggml_init_params params = { /*.mem_size =*/ ctx_size, /*.mem_buffer =*/ NULL, /*.no_alloc =*/ false, }; struct ggml_context * ctx = ggml_init(params); // ctx manages memory allocation for these tensors. // 2. Create tensors and set data struct ggml_tensor * tensor_a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, cols_A, rows_A, in); // initialize tensor A memcpy(tensor_a->data, matrix_A, ggml_nbytes(tensor_a)); // copy the data from the pre-defined arrays matrix_A to tensor_a print_ggml_tensor("tensor_a", tensor_a, false); // print a tensor with 2 x 3 const int rows_B = 2, cols_B = 3; float matrix_B[rows_B * cols_B] = { 1, 2, 3, 4, 5, 6, }; struct ggml_tensor * tensor_b = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, rows_B, cols_B); memcpy(tensor_b->data, matrix_B, ggml_nbytes(tensor_b)); print_ggml_tensor("tensor_b", tensor_b); // 6. Free memory and exit ggml_free(ctx); return 0; }