Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Llama Impl

From Leeroopedia
Knowledge Sources
Domains LLM Inference, Utilities
Last Updated 2025-02-15 00:00 GMT

Overview

Implements core internal utilities: the logging system with configurable callbacks, string formatting helpers, and GGUF metadata value formatting.

Description

Manages a global llama_logger_state with a configurable log callback (defaulting to stderr output). llama_log_internal formats log messages with vsnprintf using a stack buffer for short messages and heap allocation for longer ones. Provides replace_all for string substitution, format for printf-style string formatting, llama_format_tensor_shape for human-readable tensor dimensions, and gguf_kv_to_str for converting GGUF metadata entries to string representations. The time_meas RAII class measures elapsed time.

Usage

Foundation utility layer used by virtually every other source file in llama.cpp via the LLAMA_LOG_* macros. The logging callback system is how Ollama redirects llama.cpp log output to Go's slog.

Code Reference

Source Location

  • Repository: Ollama
  • File: llama/llama.cpp/src/llama-impl.cpp
  • Lines: 1-171

Signature

struct llama_logger_state {
    ggml_log_callback log_callback = llama_log_callback_default;
    void * log_callback_user_data = nullptr;
};

time_meas::time_meas(int64_t & t_acc, bool disable);
time_meas::~time_meas();

void llama_log_get(ggml_log_callback * log_callback, void ** user_data);
void llama_log_set(ggml_log_callback log_callback, void * user_data);
void llama_log_internal(ggml_log_level level, const char * format, ...);
void llama_log_callback_default(ggml_log_level level, const char * text, void * user_data);

void replace_all(std::string & s, const std::string & search, const std::string & replace);
std::string format(const char * fmt, ...);
std::string llama_format_tensor_shape(const std::vector<int64_t> & ne);
std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i);

Import

#include "llama-impl.h"

I/O Contract

Inputs

Name Type Required Description
level ggml_log_level Yes Log severity level
format const char * Yes Printf-style format string
log_callback ggml_log_callback No Custom log callback function

Outputs

Name Type Description
formatted string std::string Formatted output string
log output void Log messages dispatched to callback

Usage Examples

#include "llama-impl.h"

// Logging (typically via macros):
LLAMA_LOG_INFO("Loading model with %d layers\n", n_layers);

// String formatting:
std::string desc = format("tensor %s: shape %s", name, shape.c_str());

// Tensor shape formatting:
std::string shape = llama_format_tensor_shape(tensor);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment