# Models

> UForm for Rust

`Model::load_ram(path)` and `Model::load_vram(path)` copy a safetensors checkpoint into host memory or onto GPUs, and `load_ram_buffer` and `load_vram_buffer` take the checkpoint's bytes instead.
A bare path or buffer loads on the calling thread with `Scalar::BF16` blocks, and its config reaches the executor, the placement and its class pin, `Scalar::E4M3` quantization and a progress handler:

```rust
let model = Model::load_vram(ModelLoadVramConfig {
    executor: Some(&executor),
    devices: &[0],
    quantization: Scalar::E4M3,
    ..ModelLoadVramConfig::from("model.safetensors")
})?;
let description = model.describe()?;                          // geometry, placement, classes and heads
```

A description reports the `cpu_class` or `gpu_class` the model dispatched as, and the heads the checkpoint carries: a nonzero `token_width` for token vectors, and a nonzero `choice_temperature_groups` for choices.
`Model` carries no type parameter: quantization is the runtime `Scalar` passed at load, as in C.

`use uform::Model;`

`use uform::ModelDescribe;`

`use uform::ModelDescription;`

`use uform::ModelLoadRamBufferConfig;`

`use uform::ModelLoadRamConfig;`

`use uform::ModelLoadVramBufferConfig;`

`use uform::ModelLoadVramConfig;`

`use uform::ModelVerbs;`

`use uform::Residency;`

## Model

A loaded encoder: its weights, their packed copies, and on a GPU the session its attention runs on, whose verbs come from [`ModelVerbs`](#ModelVerbs) and [`EncodeVerbs`](https://www.unum.cloud/docs/uform/rust/encoding.md#EncodeVerbs).

```rust
pub struct Model
```

```
use uform::*;

let model = Model::load_ram("model.safetensors")?;
let executor = Executor::for_ram(&#x26;[], None, 0, 0)?;
let mut arena = Arena::new_in(&#x26;executor)?;
let encoded = model.on(&#x26;executor).text_encode(&#x26;["a caption"], Outputs::EMBEDDINGS, Scalar::F32, &#x26;mut arena)?;
if let Some(embeddings) = encoded.embeddings {
    println!("{} rows of {}", embeddings.count(), embeddings.columns());
}
```

Mirrors `uform_model_t`; dropping it calls `uform_model_free`.

Implements: `Debug`, `Send`, `Sync`, `Drop`, `[ModelVerbs](#ModelVerbs)<'model>`

## on

The receiver whose verbs block on `executor`, after the calls submitted there before them.

```rust
pub fn on<'model>(&'model self, executor: &'model Executor) -> On<'model, Model>
```

Rust-only.

## deferred

The receiver whose verbs submit their calls to `executor`, answering a [`Deferred`](https://www.unum.cloud/docs/uform/rust/errors.md#Deferred) from the same verbs given owned arguments.

```rust
pub fn deferred<'model>(&'model self, executor: &'model Executor) -> On<'model, Model, Deferring>
```

The library reads a deferred call's inputs after the verb returns, so lending borrowed texts
does not compile:

```
use uform::*;

let model = Model::load_ram("model.safetensors")?;
let executor = Executor::for_ram(&#x26;[], None, 0, 0)?;
let mut arena = Arena::new_in(&#x26;executor)?;
let texts = vec![String::from("a caption")];
let call = model.deferred(&#x26;executor).text_encode(&#x26;texts, Outputs::EMBEDDINGS, Scalar::F32, &#x26;mut arena)?;
```

Owned texts move into the call, and the arena is borrowed as a blocking call borrows it, and
so is the waited result:

```
use uform::*;

let model = Model::load_ram("model.safetensors")?;
let executor = Executor::for_ram(&#x26;[], None, 0, 0)?;
let mut arena = Arena::new_in(&#x26;executor)?;
let texts = vec![String::from("a caption")];
let call = model.deferred(&#x26;executor).text_encode(texts, Outputs::EMBEDDINGS, Scalar::F32, &#x26;mut arena)?;
let encoded = call.wait()?;
```

Rust-only.

## load_ram

Copies a checkpoint file into host memory and packs its products for the CPU; a bare path loads on the calling thread in BFloat16.

```rust
pub fn load_ram<'config>(config: impl Into<ModelLoadRamConfig<'config>>) -> Result<Self>
```

Calls `uform_model_load_ram`.

## load_ram_buffer

Copies a checkpoint held in memory into host memory and packs its products for the CPU.

```rust
pub fn load_ram_buffer<'config>(
    config: impl Into<ModelLoadRamBufferConfig<'config>>,
) -> Result<Self>
```

Calls `uform_model_load_ram_buffer`.

## load_vram

Copies a checkpoint file onto the named GPUs and packs its products there.

```rust
pub fn load_vram<'config>(config: impl Into<ModelLoadVramConfig<'config>>) -> Result<Self>
```

Calls `uform_model_load_vram`.

## load_vram_buffer

Copies a checkpoint held in memory onto the named GPUs and packs its products there.

```rust
pub fn load_vram_buffer<'config>(
    config: impl Into<ModelLoadVramBufferConfig<'config>>,
) -> Result<Self>
```

Calls `uform_model_load_vram_buffer`.

## *

The model this receiver reaches and the executor its calls run on.

```rust
fn handles(&self) -> Handles
```

## *

Reports the model's geometry and placement in one call.

```rust
fn describe(
    self,
) -> <<Self as ModelVerbs<'model>>::Completion as Completion>::Answer<'model, ModelDescribe>
```

Calls `uform_model_describe`.

## ModelDescribe

A report of the model's geometry; see [`ModelVerbs::describe`](#describe).

```rust
pub struct ModelDescribe
```

Calls `uform_model_describe`.

Implements: `Send`

## ModelDescription

What a loaded model reports about itself.

```rust
pub struct ModelDescription
```

Mirrors `uform_model_description_t`.

- `dimensions` `usize`: Scalars per embedding row; Matryoshka prefixes of it are embeddings too.
- `context` `usize`: Tokens one item may span before it is truncated: bytes of text, patches of an image, tubes
of a clip, frames of a recording.
- `quantization` `Scalar`: The scalar the transformer blocks run in.
- `memory_usage` `usize`: Bytes the model holds; activations are the executor's.
- `cpu_class` `Option<CPUClass>`: The class a RAM model's tower was packed for; [`None`](https://www.unum.cloud/docs/uform/rust/training.md#None) for a VRAM model.
- `gpu_class` `Option<GPUClass>`: The class every replica of a VRAM model runs as; [`None`](https://www.unum.cloud/docs/uform/rust/training.md#None) for a RAM model.
- `token_width` `usize`: Scalars per token vector; zero when the checkpoint carries no token head.
- `choice_temperature_groups` `usize`: Question types the choice temperatures were calibrated over: zero without the byte decoder,
one when uncalibrated.

Implements: `Clone`, `Copy`, `StructuralPartialEq`, `PartialEq`, `Eq`, `Debug`, `Hash`

## ModelLoadRamBufferConfig

A checkpoint held in memory and how it is copied into a new model in host memory; see [`Model::load_ram_buffer`](#load_ram_buffer).

```rust
pub struct ModelLoadRamBufferConfig<'config>
```

Mirrors `uform_model_load_ram_buffer_t`.

- `buffer` `&'config [u8]`: The safetensors checkpoint itself, copied before the load completes.
- `executor` `Option<&'config Executor>`: The executor the load runs on; [`None`](https://www.unum.cloud/docs/uform/rust/training.md#None) loads on the calling thread.
- `memory_domains` `&'config [i32]`: NUMA node IDs to place the weights in; empty derives every node the process may allocate on.
- `cpu_class` `Option<CPUClass>`: The class to pack and dispatch as, as for [`ModelLoadRamConfig::cpu_class`](#ModelLoadRamConfig).
- `quantization` `Scalar`: The scalar the transformer blocks run in, as for [`ModelLoadRamConfig::quantization`](#ModelLoadRamConfig).
- `progress` `Option<&'config mut ProgressCallback<'config>>`: Reports the tensors placed and stops wherever it says to; a stopped load creates no model.
- `progress_steps` `usize`: Reports the call makes at most, the last at completion; zero derives to ten.

Implements: `From<&'config [u8]>`, `From<&'config Bytes>`

## ModelLoadRamConfig

A checkpoint path and how it is copied into a new model in host memory; see [`Model::load_ram`](#load_ram).

```rust
pub struct ModelLoadRamConfig<'config>
```

Mirrors `uform_model_load_ram_t`.

- `path` `&'config Path`: A safetensors checkpoint, read in full and closed before the load completes.
- `executor` `Option<&'config Executor>`: The executor the load runs on; [`None`](https://www.unum.cloud/docs/uform/rust/training.md#None) loads on the calling thread.
- `memory_domains` `&'config [i32]`: NUMA node IDs to place the weights in; empty derives every node the process may allocate on.
- `cpu_class` `Option<CPUClass>`: The class to pack and dispatch as, admitting every rung whose features it holds; [`None`](https://www.unum.cloud/docs/uform/rust/training.md#None)
derives the CPU's own.
- `quantization` `Scalar`: The scalar the transformer blocks run in: [`Scalar::BF16`](https://www.unum.cloud/docs/uform/rust/encoding.md#BF16) as the checkpoint stores it, or
[`Scalar::E4M3`](https://www.unum.cloud/docs/uform/rust/encoding.md#E4M3) quantized at load.
- `progress` `Option<&'config mut ProgressCallback<'config>>`: Reports the tensors placed and stops wherever it says to; a stopped load creates no model.
- `progress_steps` `usize`: Reports the call makes at most, the last at completion; zero derives to ten.

Implements: `From<&'config Location>`

## ModelLoadVramBufferConfig

A checkpoint held in memory and how it is copied onto devices; see [`Model::load_vram_buffer`](#load_vram_buffer).

```rust
pub struct ModelLoadVramBufferConfig<'config>
```

Mirrors `uform_model_load_vram_buffer_t`.

- `buffer` `&'config [u8]`: The safetensors checkpoint itself, in host or device memory, copied before
the load completes.
- `executor` `Option<&'config Executor>`: The executor the upload runs on, one stream per device; [`None`](https://www.unum.cloud/docs/uform/rust/training.md#None) uploads on
the calling thread.
- `devices` `&'config [i32]`: Positions in what [`gpu_devices`](https://www.unum.cloud/docs/uform/rust/executors.md#gpu_devices) lists, each holding a replica; empty
derives one free device.
- `gpu_class` `Option<GPUClass>`: The class every device dispatches as, as for [`ModelLoadVramConfig::gpu_class`](https://www.unum.cloud/docs/uform/rust/executors.md#gpu_class).
- `quantization` `Scalar`: The scalar the transformer blocks run in, as for [`ModelLoadRamConfig::quantization`](#ModelLoadRamConfig).
- `progress` `Option<&'config mut ProgressCallback<'config>>`: Reports the tensors placed and stops wherever it says to; a stopped load creates no model.
- `progress_steps` `usize`: Reports the call makes at most, the last at completion; zero derives to ten.

Implements: `From<&'config [u8]>`, `From<&'config Bytes>`

## ModelLoadVramConfig

A checkpoint path and how it is copied onto devices; see [`Model::load_vram`](#load_vram).

```rust
pub struct ModelLoadVramConfig<'config>
```

Mirrors `uform_model_load_vram_t`.

- `path` `&'config Path`: A safetensors checkpoint, read in full and closed before the load completes.
- `executor` `Option<&'config Executor>`: The executor the upload runs on, one stream per device; [`None`](https://www.unum.cloud/docs/uform/rust/training.md#None) uploads on
the calling thread.
- `devices` `&'config [i32]`: Positions in what [`gpu_devices`](https://www.unum.cloud/docs/uform/rust/executors.md#gpu_devices) lists, each holding a replica; empty
derives one free device.
- `gpu_class` `Option<GPUClass>`: The class every device dispatches as; [`None`](https://www.unum.cloud/docs/uform/rust/training.md#None) derives the devices' own, which must agree.
- `quantization` `Scalar`: The scalar the transformer blocks run in, as for [`ModelLoadRamConfig::quantization`](#ModelLoadRamConfig).
- `progress` `Option<&'config mut ProgressCallback<'config>>`: Reports the tensors placed and stops wherever it says to; a stopped load creates no model.
- `progress_steps` `usize`: Reports the call makes at most, the last at completion; zero derives to ten.

Implements: `From<&'config Location>`

## ModelVerbs

Every verb of the model header, written once for every receiver: `&Model` blocks on the calling thread, and the [`On`](https://www.unum.cloud/docs/uform/rust/receivers-and-deferred-calls.md#On) that [`Model::on`](https://www.unum.cloud/docs/uform/rust/training.md#on) or [`Model::deferred`](https://www.unum.cloud/docs/uform/rust/training.md#deferred) makes runs on an executor.

```rust
pub trait ModelVerbs<'model>: Copy
```

A blocking receiver answers what the call produced; a deferring one answers a
[`Deferred`](https://www.unum.cloud/docs/uform/rust/errors.md#Deferred) that borrows the model. [`EncodeVerbs`](https://www.unum.cloud/docs/uform/rust/encoding.md#EncodeVerbs)
extends it with the forward passes.

Rust-only.

Implementors: - `impl<'model> [ModelVerbs](#ModelVerbs)<'model> for &'model [Model](#Model)`
- `impl<'model, Completion: Completion> [ModelVerbs](#ModelVerbs)<'model> for [On](https://www.unum.cloud/docs/uform/rust/receivers-and-deferred-calls.md#On)<'model, [Model](#Model), Completion>`

## Completion

[`Blocking`](https://www.unum.cloud/docs/uform/rust/receivers-and-deferred-calls.md#Blocking) or [`Deferring`](https://www.unum.cloud/docs/uform/rust/receivers-and-deferred-calls.md#Deferring).

```rust
type Completion: Completion;
```

## Residency

Where an executor computes, and with it where the arenas it makes keep their memory.

```rust
pub enum Residency
```

Mirrors `uform_residency_t`.

- `Ram` = `1`: Host pages, which the host reads directly.
- `Vram` = `2`: One GPU's memory, which only device code reads.

Implements: `Clone`, `Copy`, `StructuralPartialEq`, `PartialEq`, `Eq`, `Debug`, `Hash`

