class

Llama::Model

Inherits Reference / Object

Wrapper for the llama_model structure

Constructors

new(path : String, n_gpu_layers : Int32 = 0, use_mmap : Bool = true, use_mlock : Bool = false, vocab_only : Bool = false)

Creates a new Model instance by loading a model from a file.

Parameters:

  • path: Path to the model file (.gguf format).
  • n_gpu_layers: Number of layers to store in VRAM (default: 0). If 0, all layers are loaded to the CPU.
  • use_mmap: Use mmap if possible (default: true). Reduces memory usage.
  • use_mlock: Force the system to keep the model in RAM (default: false). May improve performance but increases memory usage.
  • vocab_only: Only load the vocabulary, no weights (default: false). Useful for inspecting the vocabulary.

Raises:

  • Llama::Model::Error if the model cannot be loaded.
Source

Instance methods

chat_template(name : String | Nil = nil) : String | Nil

Gets the default chat template for this model

Parameters:

  • name: Optional template name (nil for default)

Returns:

  • The chat template string, or nil if not available
Source
classifier?

Returns whether this model is a classifier model

Returns:

  • true if the model has classifier outputs, false otherwise
Source
cls_label(i : UInt32) : String | Nil

Returns the classifier label by index

Parameters:

  • i: Index of the classifier output (must be < n_cls_out)

Returns:

  • The classifier label, or nil if index is out of bounds or no label is provided
Source
context(*args, **options) : Context

Creates a new Context for this model

This method delegates to Context.new, passing self as the model parameter and forwarding all other arguments.

Returns:

  • A new Context instance

Raises:

  • Llama::Context::Error if the context cannot be created
Source
decoder_start_token

Returns the token that must be provided to the decoder to start generating output For encoder-decoder models, returns the decoder start token For other models, returns -1

Source
description

Gets a string describing the model type

Returns:

  • A description of the model
Source
diffusion?

Returns whether the model is diffusion-based (e.g., LLaDA, Dream, etc.)

Source
finalize

Frees the resources associated with this model

Source
has_decoder?

Returns whether the model contains a decoder

Source
has_encoder?

Returns whether the model contains an encoder

Source
hybrid?

Returns whether the model is a hybrid (e.g., Jamba, Granite, etc.)

Source
metadata

Gets all metadata as a hash

Returns:

  • A hash mapping metadata keys to values
Source
metadata_count

Gets the number of metadata key/value pairs

Returns:

  • The number of metadata entries
Source
metadata_key_at(i : Int32) : String | Nil

Gets a metadata key name by index

Parameters:

  • i: The index of the metadata entry

Returns:

  • The key name, or nil if the index is out of bounds
Source
metadata_value(key : String) : String | Nil

Gets a metadata value as a string by key name

Parameters:

  • key: The metadata key to look up

Returns:

  • The metadata value as a string, or nil if not found
Source
metadata_value_at(i : Int32) : String | Nil

Gets a metadata value as a string by index

Parameters:

  • i: The index of the metadata entry

Returns:

  • The value as a string, or nil if the index is out of bounds
Source
model_size

Returns the total size of all the tensors in the model in bytes

Returns:

  • The total size of all tensors in the model (in bytes)
Source
n_cls_out

Returns the number of classifier outputs (only valid for classifier models) Returns 0 for non-classifier models

Source
n_embd

Returns the number of embedding dimensions in the model

Source
n_embd_inp

Returns the input embedding dimension used by the model

Source
n_embd_out

Returns the output embedding dimension used by the model

Source
n_head

Returns the number of attention heads in the model

Source
n_layer

Returns the number of layers in the model

Source
n_params

Returns the number of parameters in the model

Source
n_swa

Returns the number of SWA (sliding window attention) layers in the model

Source
recurrent?

Returns whether the model is recurrent (like Mamba, RWKV, etc.)

Source
rope_freq_scale_train

Returns the model's RoPE frequency scaling factor

Source
to_unsafe

Returns the raw pointer to the underlying llama_model structure

Source
vocab

Returns the vocabulary associated with this model

Source

Nested types