Implementation:Ollama Ollama Llama Common
| Knowledge Sources | |
|---|---|
| Domains | Inference, Utilities |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Implements the shared utility functions and parameter handling for the llama.cpp common library, including CPU detection, model initialization, tokenization helpers, and platform-specific utilities.
Description
Contains implementations for a wide range of helper functionality: CPU core detection and thread management (cpu_get_num_physical_cores, cpu_get_num_math), RAII-based time measurement (common_time_meas), model and context parameter initialization (common_model_params_to_llama, common_context_params_to_llama), batch creation, GGUF metadata reading, file I/O utilities, tokenization helpers, control vector loading, LoRA adapter management, and platform-specific code for Windows/Linux/macOS compatibility. Nearly all example programs and the Ollama integration depend on these common functions.
Usage
Use this when initializing models, creating contexts, handling parameters, tokenizing text, or performing any common utility operation in the llama.cpp ecosystem.
Code Reference
Source Location
- Repository: Ollama
- File: llama/llama.cpp/common/common.cpp
- Lines: 1-1847
Signature
int32_t cpu_get_num_physical_cores();
int32_t cpu_get_num_math();
common_time_meas::common_time_meas(int64_t & t_acc, bool disable = false);
common_time_meas::~common_time_meas();
struct common_init_result common_init_from_params(common_params & params);
struct llama_model_params common_model_params_to_llama (common_params & params);
struct llama_context_params common_context_params_to_llama(const common_params & params);
std::string common_params_get_system_info(const common_params & params);
Import
#include "common.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| params | common_params & | Yes | Master configuration parameters for model, context, and sampling |
Outputs
| Name | Type | Description |
|---|---|---|
| result | common_init_result | Initialized model and context ready for inference |
| num_cores | int32_t | Number of physical CPU cores or math-capable cores |
Usage Examples
#include "common.h"
// Detect CPU cores
int32_t cores = cpu_get_num_physical_cores();
// Initialize model from parameters
common_params params;
params.model.path = "model.gguf";
auto result = common_init_from_params(params);
// Convert common params to llama params
auto mparams = common_model_params_to_llama(params);
auto cparams = common_context_params_to_llama(params);
// Time measurement
{
int64_t elapsed = 0;
common_time_meas timer(elapsed);
// ... work ...
} // elapsed now contains duration in microseconds