# `LlamaCppEx.Model`
[🔗](https://github.com/nyo16/llama_cpp_ex/blob/main/lib/llama_cpp_ex/model.ex#L1)

Model loading and introspection.

# `t`

```elixir
@type t() :: %LlamaCppEx.Model{load_mtp: boolean(), ref: reference()}
```

# `chat_template`

```elixir
@spec chat_template(t()) :: String.t() | nil
```

Returns the chat template string embedded in the model, or `nil` if none.

# `desc`

```elixir
@spec desc(t()) :: String.t()
```

Returns a human-readable description of the model.

# `devices`

```elixir
@spec devices(t()) :: [String.t()]
```

Names of the devices the model's layers were placed on, in placement order —
e.g. `["CUDA0", "RPC0"]` for a model split across a local GPU and an RPC
worker. Empty for a CPU-only load.

# `load`

```elixir
@spec load(
  String.t(),
  keyword()
) :: {:ok, t()} | {:error, String.t()}
```

Loads a GGUF model from the given file path.

## Options

  * `:n_gpu_layers` - Number of layers to offload to GPU. Use `-1` for all layers.
    Defaults to `99` (offload all layers).
  * `:use_mmap` - Memory-map the model file (`true`) or read it (`false`).
    Unset by default, which leaves the choice to llama.cpp — see "Load mode"
    below.
  * `:main_gpu` - GPU device index for single-GPU mode. Defaults to `0`.
  * `:split_mode` - How to split the model across devices: `:none`, `:layer`,
    `:row` or `:tensor`. Defaults to `:none`. Only `:none` and `:layer` are
    generally usable at this llama.cpp version — see the note below.
  * `:tensor_split` - List of floats specifying the proportion of work per GPU
    (e.g. `[0.5, 0.5]` for two GPUs). Defaults to `[]`.
  * `:use_mlock` - Pin model memory in RAM to prevent swapping. Combined with
    `:use_mmap` unless that is `false`. Defaults to `false`.
  * `:use_direct_io` - Bypass page cache when loading (takes precedence over mmap).
    Defaults to `false`.
  * `:vocab_only` - Load vocabulary and metadata only, skip weights. Defaults to `false`.
  * `:check_tensors` - Validate model tensor data on load. Defaults to `false`,
    because the check walks every tensor and costs real time on a large model.
  * `:load_mtp` - Load the Multi-Token Prediction head's layers, for use with
    `LlamaCppEx.MTP`. Defaults to `false`, matching upstream, so that callers
    who are not doing speculative decoding do not pay for the extra tensors.
    Required for `LlamaCppEx.MTP.init/2`, which refuses a model loaded without
    it — the layers cannot be added after the fact.
  * `:rpc_servers` - Endpoints (`"host:port"`) to register before loading, so
    their remote devices can hold part of the model. Defaults to `[]`. Requires
    a build with `LLAMA_RPC=1`. See `LlamaCppEx.RPC`. Note that llama.cpp puts
    remote devices **first** in its automatic placement list — which is not the
    order `LlamaCppEx.devices/0` reports — so `tensor_split: [0.25, 0.75]`
    gives 25% to the first remote endpoint. Pass `:devices` to avoid guessing.
  * `:devices` - Device names, e.g. `["CUDA0", "RPC0"]`, used **verbatim** as
    the placement list: no reordering, no dedup, no CPU filtering. Defaults to
    `[]`, which lets llama.cpp build the list itself. Set this whenever more
    than one device is in play, because the automatic list is **not** the order
    `LlamaCppEx.devices/0` reports — it puts RPC devices first — so
    `:tensor_split` and `:main_gpu` would index a list you never saw. With
    `:devices` set, they index this one.

> #### Split modes at llama.cpp b10944 (`b6b003d2`) {: .warning}
>
> `:layer` splits contiguous layer ranges across devices, one KV cache per
> device, and is the only mode that works across hosts.
>
> `:row` throws at load time on CUDA: ggml-cuda no longer exports
> `ggml_backend_split_buffer_type`, so `llama_model_load` raises
> `device CUDA0 does not support split buffers`. It is kept mapped to its
> upstream value rather than removed, because the enum is upstream's, but do
> not build on it. Only SYCL still declares a split buffer type.
>
> `:tensor` is real tensor parallelism via a Meta device, added in
> llama.cpp #19378. It forces flash attention on, refuses some architectures,
> disables backend sampling, and its CUDA all-reduce is `ncclCommInitAll` —
> a single-process, all-local-GPUs API. **It cannot span hosts**, so it is not
> "tp=2 across two machines". See `docs/dgx-spark.md` for the measurements.
> With an RPC device in the set it runs, far slower than `:layer`, and at
> llama.cpp `a691b2345` its first decode overflows a dirty scheduler's
> default stack (320 KB) while the RPC backend serializes the graph, which
> kills the VM with a segfault. Start the VM with `+sssdcpu 256` (2 MB,
> e.g. `ELIXIR_ERL_OPTIONS="+sssdcpu 256"`) if you run it anyway.

> #### Load mode {: .info}
>
> llama.cpp collapsed its three loading booleans into one `load_mode` enum, so
> these options resolve to a single mode. `:use_direct_io` takes precedence
> over everything and selects `dio`; otherwise `:use_mlock` selects
> `mmap_mlock`, or `mlock` (read into anonymous memory, no mapping) with
> `use_mmap: false`; `use_mmap: true` alone selects `mmap` and
> `use_mmap: false` alone `none`. With none of the three given, the mode is
> upstream's `auto`: mmap, except on devices that do not support it — an
> integrated GPU such as DGX Spark's GB10 — where the file is read instead
> (llama.cpp #26081). On GB10 a forced `mmap` left most loads of
> gpt-oss-120b decoding ~15% slower (48.5 against 57 tok/s), so leave these
> unset unless you have measured otherwise.

> #### Untrusted models {: .warning}
>
> GGUF parsing happens in llama.cpp's C++ loader, and `:check_tensors` defaults
> to `false` for every source — including files fetched by
> `LlamaCppEx.Hub.download/3`, which verifies a download against the SHA-256
> HuggingFace publishes but cannot vouch for what the repository owner uploaded.
> `load/2` receives a bare path and has no notion of provenance, so it cannot
> raise that default on its own: pass `check_tensors: true` explicitly for any
> model whose publisher you do not trust.

## Examples

    {:ok, model} = LlamaCppEx.Model.load("path/to/model.gguf", n_gpu_layers: -1)
    {:ok, model} = LlamaCppEx.Model.load("path/to/model.gguf", split_mode: :layer, tensor_split: [0.5, 0.5])
    {:ok, model} = LlamaCppEx.Model.load("path/to/model.gguf", vocab_only: true)

# `n_ctx_train`

```elixir
@spec n_ctx_train(t()) :: integer()
```

Returns the training context size of the model.

# `n_embd`

```elixir
@spec n_embd(t()) :: integer()
```

Returns the hidden width of the model.

This is not always the length of the vectors `LlamaCppEx.embed/3` returns;
see `n_embd_out/1`.

# `n_embd_out`

```elixir
@spec n_embd_out(t()) :: integer()
```

Returns the output-side embedding width: the length of the vectors
`LlamaCppEx.embed/3` returns, and the row width an MTP draft head consumes.

Usually equal to `n_embd/1`, but not always: embeddinggemma-2 projects its
512-wide hidden state to 768-wide embeddings, and `gemma4-assistant` has a
small `n_embd` with the target's hidden width here. `LlamaCppEx.MTP` matches
it across the target and a separate drafter GGUF.

# `n_layer_nextn`

```elixir
@spec n_layer_nextn(t()) :: non_neg_integer()
```

Returns the number of MTP / "next-N" prediction layers in the checkpoint, or
`0` when it carries no MTP head. Note this reports what the *file* contains;
the layers are only actually loaded when the model was opened with
`load_mtp: true`.

# `n_params`

```elixir
@spec n_params(t()) :: integer()
```

Returns the number of model parameters.

# `size`

```elixir
@spec size(t()) :: integer()
```

Returns the model file size in bytes.

# `structural_option_keys`

```elixir
@spec structural_option_keys() :: [atom()]
```

Options a caller must set explicitly rather than forward blindly.

`:vocab_only` in particular must never be forwarded into a server — it would
load a model with no weights.

# `tuning_option_keys`

```elixir
@spec tuning_option_keys() :: [atom()]
```

Options that are safe for a caller to forward from user-supplied opts.

`LlamaCppEx.Server` selects its model options with this function rather than
keeping its own copy of the list.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
