# Models

> model_t::load_ram and model_t::load_vram copy a safetensors checkpoint into host memory or onto GPUs, and load_ram_buffer and load_vram_buffer take the checkpoint's bytes instead.

`#include <uform/model.hpp>` 4.0.0-rc.1 · c58749fe

`model_t::load_ram` and `model_t::load_vram` copy a safetensors checkpoint into host memory or onto GPUs, and `load_ram_buffer` and `load_vram_buffer` take the checkpoint’s bytes instead. Each takes the C argument struct, which reaches the executor, the placement and its class ceiling, the quantization and a progress handler, and always loads blocking:

```cpp
std::int32_t const device = 0;
auto [model, model_status] = uf::model_t::load_vram({
    .executor = executor.handle(),
    .path = uf::bytes_view("model.safetensors"),
    .vram = {.devices = &device, .devices_count = 1},
    .quantization = uform_scalar_e4m3_k,
});
auto [description, describe_status] = model.on(executor).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. Blocks run in `uform_scalar_bf16_k`, as the checkpoint stores them, or `uform_scalar_e4m3_k` quantized at load, and a zero quantization is refused.

## API

### Class `model`

A model and its verbs, completed as `receiver_type_` says: the owning handle blocks on the calling thread, [`on`](https://www.unum.cloud/docs/uform/cpp/training.md#on) blocks on an executor, and both answer a `status_t` or an [`expected`](https://www.unum.cloud/docs/uform/cpp/errors.md#expected), while [`deferred`](https://www.unum.cloud/docs/uform/cpp/training.md#deferred) submits to an executor and answers a [`deferred`](https://www.unum.cloud/docs/uform/cpp/training.md#deferred) call.16

[model.hpp:46 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L46)

```cpp
template <typename receiver_type_>
class model
```

Each verb is written once, filling its C struct with the result as its output. Every input, handler, report and arena a deferred call names stays the caller's until it is final.

#### `model`

[model.hpp:57 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L57)

```cpp
requires an_owning_receiver<receiver_type_>
model() noexcept=default;
```

#### `handle_field`

The output field a lifecycle verb writes the new handle into.

[model.hpp:64 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L64)

```cpp
requires an_owning_receiver<receiver_type_>
uform_model_t * handle_field() noexcept;
```

#### `on`

The same handle, whose verbs block on `executor`.

[model.hpp:71 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L71)

```cpp
model<caller_owned<uform_model_t, uform_completion_blocking_k>> on(
    executor_t const & executor) const noexcept;
```

#### `deferred`

The same handle, whose verbs submit to `executor`, each answering a [`deferred`](https://www.unum.cloud/docs/uform/cpp/training.md#deferred) call.

[model.hpp:76 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L76)

```cpp
model<caller_owned<uform_model_t, uform_completion_deferred_k>> deferred(
    executor_t const & executor) const noexcept;
```

#### `describe`

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

[model.hpp:82 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L82)

```cpp
deferrable<uform_model_description_t> describe() const noexcept;
```

#### `text_encode`

Encodes UTF-8 `texts` through one forward, a token per byte, keeping the surfaces `kept` names in `arena`.

[model.hpp:93 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L93)

```cpp
deferrable<encoded_t> text_encode(
    tape_view<char> texts,
    encode_outputs_t kept,
    text_encode_config_t const & config,
    arena_t & arena) const noexcept;
```

#### `text_choose`

Scores each item's legal `choices`, one byte each, after its UTF-8 state in `texts`, answering one probability per choice in `arena`, a softmax over the item's choices.

[model.hpp:116 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L116)

```cpp
deferrable<tape_view<float>> text_choose(
    tape_view<char> texts,
    tape_view<char> choices,
    text_choose_config_t const & config,
    arena_t & arena) const noexcept;
```

#### `image_encode`

Decodes and encodes JPEG, PNG or WebP `images`; an undecodable file fails the call with [`status_code_t::malformed_k`](https://www.unum.cloud/docs/uform/cpp/errors.md#malformed_k).

[model.hpp:150 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L150)

```cpp
deferrable<encoded_t> image_encode(
    tape_view<std::uint8_t> images,
    encode_outputs_t kept,
    image_encode_config_t const & config,
    arena_t & arena) const noexcept;
```

#### `image_encode_pixels`

Encodes decoded images of interleaved 8-bit RGB, item `i` holding `heights` \[i] rows of `widths` \[i] pixels.

[model.hpp:173 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L173)

```cpp
deferrable<encoded_t> image_encode_pixels(
    tape_view<std::uint8_t> pixels,
    std::span<std::uint32_t const> heights,
    std::span<std::uint32_t const> widths,
    encode_outputs_t kept,
    image_encode_config_t const & config,
    arena_t & arena) const noexcept;
```

#### `video_encode`

Decodes and encodes MP4 or WebM `videos`.

[model.hpp:202 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L202)

```cpp
deferrable<encoded_t> video_encode(
    tape_view<std::uint8_t> videos,
    encode_outputs_t kept,
    video_encode_config_t const & config,
    arena_t & arena) const noexcept;
```

#### `audio_encode`

Decodes and encodes WAV, FLAC, MP3 or Opus `recordings`, resampled to mono.

[model.hpp:224 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L224)

```cpp
deferrable<encoded_t> audio_encode(
    tape_view<std::uint8_t> recordings,
    encode_outputs_t kept,
    audio_encode_config_t const & config,
    arena_t & arena) const noexcept;
```

#### `audio_encode_samples`

Encodes decoded mono `samples` sharing one `sample_rate`, resampled to the model's rate where it differs.

[model.hpp:248 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L248)

```cpp
deferrable<encoded_t> audio_encode_samples(
    tape_view<float> samples,
    std::uint32_t sample_rate,
    encode_outputs_t kept,
    audio_encode_config_t const & config,
    arena_t & arena) const noexcept;
```

#### `load_ram`

Copies a safetensors checkpoint file into host memory and packs it for the CPU, blocking.

[model.hpp:273 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L273)

```cpp
requires an_owning_receiver<receiver_type_>
expected<model> load_ram(uform_model_load_ram_t arguments) noexcept;
```

#### `load_ram_buffer`

Copies a checkpoint held in memory into host memory and packs it for the CPU, blocking.

[model.hpp:281 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L281)

```cpp
requires an_owning_receiver<receiver_type_>
expected<model> load_ram_buffer(
    uform_model_load_ram_buffer_t arguments) noexcept;
```

#### `load_vram`

Copies a checkpoint file onto the GPUs `arguments` names and packs it there, blocking.

[model.hpp:289 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L289)

```cpp
requires an_owning_receiver<receiver_type_>
expected<model> load_vram(uform_model_load_vram_t arguments) noexcept;
```

#### `load_vram_buffer`

Copies a checkpoint held in memory onto the GPUs `arguments` names, blocking.

[model.hpp:297 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/model.hpp#L297)

```cpp
requires an_owning_receiver<receiver_type_>
expected<model> load_vram_buffer(
    uform_model_load_vram_buffer_t arguments) noexcept;
```
