Skip to content

Compile bug: ggml_graph_compute_with_ctx import error #15570

@piyushK52

Description

@piyushK52

Git commit

In the following code I am trying to run a simple mnist model and following the example given inside ggml.h I am using "ggml_graph_compute_with_ctx" but it errors out during compilation. I am attaching the code and the error below:

#include "ggml.h"
#include "gguf.h"
#include <iostream>
#include <vector>
#include <cassert>
#include <fstream>
#include <numeric>
#include <algorithm>

// Define the model architecture structure
struct MNIST_CNN {
    struct ggml_tensor *conv1_weight;
    struct ggml_tensor *conv1_bias;
    struct ggml_tensor *conv2_weight;
    struct ggml_tensor *conv2_bias;
    struct ggml_tensor *fc1_weight;
    struct ggml_tensor *fc1_bias;
    struct ggml_tensor *fc2_weight;
    struct ggml_tensor *fc2_bias;
};

int main() {
    struct ggml_context *ctx = NULL;

    // 1. Load the GGUF model file and tensors into the ggml context
    struct gguf_init_params params = {
        /*.no_alloc = */ false,
        /*.ctx      = */ &ctx,
    };
    struct gguf_context *ctx_gguf = gguf_init_from_file("mnist_cnn.gguf", params);
    if (!ctx_gguf) {
        fprintf(stderr, "Failed to load model: mnist_cnn.gguf\n");
        return 1;
    }

    // 2. Initialize the model struct
    MNIST_CNN model;
    model.conv1_weight = ggml_get_tensor(ctx, "conv1.weight");
    model.conv1_bias   = ggml_get_tensor(ctx, "conv1.bias");
    model.conv2_weight = ggml_get_tensor(ctx, "conv2.weight");
    model.conv2_bias   = ggml_get_tensor(ctx, "conv2.bias");
    model.fc1_weight   = ggml_get_tensor(ctx, "fc1.weight");
    model.fc1_bias     = ggml_get_tensor(ctx, "fc1.bias");
    model.fc2_weight   = ggml_get_tensor(ctx, "fc2.weight");
    model.fc2_bias     = ggml_get_tensor(ctx, "fc2.bias");

    // 3. Prepare the input data
    struct ggml_tensor *input = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 28, 28, 1);
    float * data = (float *) ggml_get_data(input);
    for (int i = 0; i < 28 * 28; ++i) {
        data[i] = 0.5f;
    }

    // 4. Build the computation graph
    struct ggml_cgraph *gf = ggml_new_graph(ctx);
    struct ggml_tensor *cur = ggml_conv_2d(ctx, model.conv1_weight, input, 1, 1, 1, 1, 1, 1);
    cur = ggml_add(ctx, cur, ggml_reshape_4d(ctx, model.conv1_bias, 1, 1, 32, 1));
    cur = ggml_relu(ctx, cur);
    cur = ggml_pool_2d(ctx, cur, GGML_OP_POOL_MAX, 2, 2, 0, 0, 2, 2);

    cur = ggml_conv_2d(ctx, model.conv2_weight, cur, 1, 1, 1, 1, 1, 1);
    cur = ggml_add(ctx, cur, ggml_reshape_4d(ctx, model.conv2_bias, 1, 1, 64, 1));
    cur = ggml_relu(ctx, cur);
    cur = ggml_pool_2d(ctx, cur, GGML_OP_POOL_MAX, 2, 2, 0, 0, 2, 2);

    cur = ggml_reshape_2d(ctx, cur, 64 * 7 * 7, 1);
    cur = ggml_mul_mat(ctx, model.fc1_weight, cur);
    cur = ggml_add(ctx, cur, model.fc1_bias);
    cur = ggml_relu(ctx, cur);

    cur = ggml_mul_mat(ctx, model.fc2_weight, cur);
    cur = ggml_add(ctx, cur, model.fc2_bias);

    ggml_build_forward_expand(gf, cur);

    // 5. Run the computation
    ggml_graph_compute_with_ctx(ctx, gf, 1);   // identifier "ggml_graph_compute_with_ctx" is undefined

    // 6. Get the result
    float *output = (float *)ggml_get_data(cur);
    int predicted_label = std::distance(output, std::max_element(output, output + 10));
    std::cout << "Predicted label: " << predicted_label << std::endl;

    // 7. Clean up
    ggml_free(ctx);
    gguf_free(ctx_gguf);

    return 0;
}

Error log

 error: use of undeclared identifier 'ggml_graph_compute_with_ctx'
   83 |     ggml_graph_compute_with_ctx(ctx, gf, 1);
      |     ^
1 error generated.
make[2]: *** [CMakeFiles/mnist_inference.dir/inference.cpp.o] Error 1
make[1]: *** [CMakeFiles/mnist_inference.dir/all] Error 2
make: *** [all] Error 2

Operating systems

Mac

GGML backends

Metal

Problem description & steps to reproduce

the only issue right now is the ggml_graph_compute_with_ctx import error, which is causing the build to fail.

First Bad Commit

No response

Compile command

I just used `cmake ..` with the following CMakeLists.txt


cmake_minimum_required(VERSION 3.13)
project(mnist_inference)

# llama.cpp as a subdirectory
add_subdirectory(llama.cpp)

add_executable(mnist_inference inference.cpp)

# include directories for ggml and llama
target_include_directories(mnist_inference PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/llama.cpp/ggml/include
    ${CMAKE_CURRENT_SOURCE_DIR}/llama.cpp/include
)

target_link_libraries(mnist_inference PRIVATE llama)

Relevant log output

error: use of undeclared identifier 'ggml_graph_compute_with_ctx'
   83 |     ggml_graph_compute_with_ctx(ctx, gf, 1);
      |     ^
1 error generated.
make[2]: *** [CMakeFiles/mnist_inference.dir/inference.cpp.o] Error 1
make[1]: *** [CMakeFiles/mnist_inference.dir/all] Error 2
make: *** [all] Error 2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions