Skip to content

Conversation

@pwilkin
Copy link
Collaborator

@pwilkin pwilkin commented Dec 10, 2025

I used my callback function from my Qwen3Next testing days, it seems like it works more cleanly than the previous one which was causing some problems with the scheduler / buffers.

Copy link
Collaborator

@ngxson ngxson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to go a step ahead, I would suggest using ggml_backend_sched_set_eval_callback to make it works the same way as libllama. This will be a cleaner solution

std::string t_name = std::string(name) + "_" + std::to_string(il);
ggml_tensor * args[] = { t };
ggml_tensor * res = ggml_custom_4d(ctx0, t->type, t->ne[0], t->ne[1], t->ne[2], t->ne[3], args, 1, print_debug, 1, nullptr);
strcpy(res->name, t_name.c_str());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ggml_set_name instead

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even better, ggml_format_name

ggml_tensor * args[] = { t };
ggml_tensor * res = ggml_custom_4d(ctx0, t->type, t->ne[0], t->ne[1], t->ne[2], t->ne[3], args, 1, print_debug, 1, nullptr);
strcpy(res->name, t_name.c_str());
ggml_build_forward_expand(gf, res);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should guard the whole thing under ctx->debug_graph. seems like it's got removed by mistake?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah :>

#include "ggml-cpp.h"
#include "ggml-alloc.h"
#include "ggml-backend.h"
#include "ggml/src/ggml-impl.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed - we cannot include internal header from ggml

#include <cstring>
#include <fstream>
#include <map>
#include <memory>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some of these are already included by clip-impl.h - do we really need to include them again here?

@pwilkin
Copy link
Collaborator Author

pwilkin commented Dec 10, 2025

All right, based on the convo with @ngxson I've decided to tackle this properly:

  • I moved the common debugging functions to llama-debug.cpp, added their headers to llama.h or llama-cpp.h depending on whether they use C or C++ APIs.
  • I plugged eval-callback to those new common functions
  • I modified mtmd's cb() to do the same thing as the llm_graph_builder's one, which is basically to just set the tensor name. The entire tensor dump is set via ggml_backend_sched_set_eval_callback
  • The added bonus is I created a template version of the ggml_debug function, so you can now set in the template whether NaNs should abort execution or not (default: no)

@pwilkin
Copy link
Collaborator Author

pwilkin commented Dec 10, 2025

I would very much like to extend the callback procedure to (a) make it also possible in other clients (such as llama-cli) (b) make it configurable via args (c) add a couple of standard debug callbacks, for example in addition to the printout also dumping selected tensors to a file, computing some diagnostic functions on the tensors and so on (but of course not within this PR).

@pwilkin pwilkin changed the title Restore clip's cb() to its rightful glory Restore clip's cb() to its rightful glory - extract common debugging elements in llama Dec 10, 2025
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be inside common/debug.h instead. There is no internal libllama components using these functions

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, fair point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants