Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] Error (core dumped) when running example code from blogpost #1031

Open
treya-lin opened this issue Nov 28, 2024 · 0 comments
Open

[Help] Error (core dumped) when running example code from blogpost #1031

treya-lin opened this issue Nov 28, 2024 · 0 comments

Comments

@treya-lin
Copy link

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)

#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;
}

other relevant codes

  1. examples/demo/CMakeLists.txt
set(TEST_TARGET demo)
add_executable(${TEST_TARGET} demo)
target_link_libraries(${TEST_TARGET} PRIVATE ggml)

  1. add this at the end of examples/CMakeLists.txt
add_subdirectory(demo)

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

my ggml code version

$ git log
commit 6fcbd60bc72ac3f7ad43f78c87e535f2e6206f58 (HEAD -> master, origin/master, origin/HEAD)
Author: M Refi D.A <[email protected]>
Date:   Thu Nov 21 06:39:37 2024 +0800

@treya-lin 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant