You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I made examples/demo and tried compiling demo. The multiplication went ok and the result matrix can be printed. but then I added the graph print command as intructed in the blogpost, and it threw segment fault (core dumped).
here is my code
examples/demo/demo.cpp
(if I comment the ggml_graph_print(gf);, it can be compiled and run.
if I uncomment it and recompile, it can be compiled but when I run it it threw core dumped error)
#include "ggml.h"
#include "ggml-cpu.h"
#include <string.h>
#include <stdio.h>
int main(void) {
// initialize data of matrices
const int rows_A = 4, cols_A = 2;
float matrix_A[rows_A * cols_A] = {
2, 8,
5, 1,
4, 2,
8, 6
};
const int rows_B = 3, cols_B = 2;
float matrix_B[rows_B * cols_B] = {
10, 5,
9, 9,
5, 4
};
// 1. Allocate `ggml_context` to store tensor data
// Calculate the size needed to allocate
size_t ctx_size = 0;
ctx_size += rows_A * cols_A * ggml_type_size(GGML_TYPE_F32); // tensor a
ctx_size += rows_B * cols_B * ggml_type_size(GGML_TYPE_F32); // tensor b
ctx_size += rows_A * rows_B * ggml_type_size(GGML_TYPE_F32); // result
ctx_size += 3 * ggml_tensor_overhead(); // metadata for 3 tensors
ctx_size += ggml_graph_overhead(); // compute graph
ctx_size += 1024; //some overhead
// 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);
// 2. Create tensors and set data
struct ggml_tensor * tensor_a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, cols_A, rows_A);
struct ggml_tensor * tensor_b = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, cols_B, rows_B);
memcpy(tensor_a->data, matrix_A, ggml_nbytes(tensor_a));
memcpy(tensor_b->data, matrix_B, ggml_nbytes(tensor_b));
// 3. Create a `ggml_cgraph` for mul_mat operation
struct ggml_cgraph * gf = ggml_new_graph(ctx);
// result = a*b^T
// Pay attention: ggml_mul_mat(A,B) ===> B will be transposed internally
// the result is transposed
struct ggml_tensor * result = ggml_mul_mat(ctx, tensor_a, tensor_b);
// Mark the "result" tensor to be computed
ggml_build_forward_expand(gf, result);
// Print the graph
ggml_graph_print(gf);
// ggml_graph_dump_dot(gf, NULL, "debug.dot");
// 4. Run the computation
int n_threads = 1; // Optional: number of threads to perform some operations with multi-threading
ggml_graph_compute_with_ctx(ctx, gf, n_threads);
// 5. Retrieve results (output tensors)
float * result_data = (float *) result->data;
printf("mul mat (%d x %d)) (transposed result):\n[", (int) result->ne[0], (int) result->ne[1]);
for (int j = 0; j < result->ne[1]/* rows */; j++) {
if (j > 0) {
printf("\n");
}
for (int i = 0; i < result->ne[0]/* cols */; i++) {
printf(" %.2f", result_data[j * result->ne[0] + i]);
}
}
printf(" ]\n");
// 6. Free memory and exit
ggml_free(ctx);
return 0;
}
I dont have much experience with cpp and had trouble debugging. I tried asking some AI tools but it does work very well so I am very puzzled now. Any advice can help!
I am working on a linux server and the env info is:
$ uname -r
6.5.0-35-generic
$ cat /proc/version
Linux version 6.5.0-35-generic (buildd@lcy02-amd64-079) (x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2
The text was updated successfully, but these errors were encountered:
treya-lin
changed the title
[Help] Error when running example code from blogpost
[Help] Error (core dumped) when running example code from blogpost
Nov 28, 2024
Hello! I hope it is the right place to ask. I am learning about this repo from this blogpost: https://huggingface.co/blog/introduction-to-ggml
I made examples/demo and tried compiling demo. The multiplication went ok and the result matrix can be printed. but then I added the graph print command as intructed in the blogpost, and it threw
segment fault (core dumped)
.here is my code
examples/demo/demo.cpp
(if I comment the
ggml_graph_print(gf);
, it can be compiled and run.if I uncomment it and recompile, it can be compiled but when I run it it threw core dumped error)
other relevant codes
examples/CMakeLists.txt
I dont have much experience with cpp and had trouble debugging. I tried asking some AI tools but it does work very well so I am very puzzled now. Any advice can help!
I am working on a linux server and the env info is:
my ggml code version
The text was updated successfully, but these errors were encountered: