Skip to main content

Loading a Model

The primary function for loading models is llama_model_load_from_file:
const char *
Path to the GGUF model file. For split models, use the naming pattern: <name>-%05d-of-%05d.gguf
struct llama_model_params
Model loading parameters (see below)
llama_model *
Returns pointer to loaded model, or NULL on failure

Example

Model Parameters

Structure Definition

Parameter Details

int32_t
default:"0"
Number of model layers to offload to GPU. Use -1 to offload all layers. Set to 0 for CPU-only inference.
enum llama_split_mode
default:"LLAMA_SPLIT_MODE_LAYER"
How to distribute the model across multiple GPUs:
  • LLAMA_SPLIT_MODE_NONE: Single GPU
  • LLAMA_SPLIT_MODE_LAYER: Split layers and KV cache across GPUs
  • LLAMA_SPLIT_MODE_ROW: Split layers and KV cache, use tensor parallelism if supported
int32_t
default:"0"
The GPU device ID to use when split_mode is LLAMA_SPLIT_MODE_NONE.
bool
default:"false"
Load only the vocabulary without model weights. Useful for tokenization-only applications.
bool
default:"true"
Use memory mapping to load the model. This can improve loading speed and reduce memory usage.
bool
default:"false"
Force the system to keep the model in RAM, preventing swapping to disk. Requires sufficient RAM.
bool
default:"false"
Validate model tensor data during loading. Useful for debugging corrupted models.

Loading Split Models

For models split across multiple files with custom naming:
const char **
Array of file paths in the correct order
size_t
Number of split files

Example

Progress Callback

Monitor model loading progress:
float
Loading progress from 0.0 to 1.0
void *
User-provided context pointer
bool
Return true to continue loading, false to abort

Example

Model Metadata

Access model metadata from GGUF files:

Example

Model Properties

Query model architecture and capabilities:

Freeing Models

Free model memory when done:
Always free models before calling llama_backend_free(). All contexts created from the model must be freed before freeing the model.

Example

Saving Models

Save a loaded model back to a file:

Fitting Parameters to Memory

Automatically adjust parameters to fit available device memory:
enum llama_params_fit_status
  • LLAMA_PARAMS_FIT_STATUS_SUCCESS: Parameters adjusted successfully
  • LLAMA_PARAMS_FIT_STATUS_FAILURE: Could not find fitting allocations
  • LLAMA_PARAMS_FIT_STATUS_ERROR: Hard error (e.g., model not found)
This function modifies the global logger state and is not thread-safe. Only parameters matching defaults are modified, except context size which is modified if equal to 0.

Complete Example

Next Steps

Inference

Learn how to create contexts and run inference

Sampling

Configure token sampling strategies