# Training

> Training is GPU-only.

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

Training is GPU-only. The configuration arrives as the C structs themselves, so a field left out is zero, which the library derives:

```cpp
auto [executor, executor_status] = uf::executor_t::for_vram({.vram = {.devices = devices, .devices_count = 2}});
auto [mesh, mesh_status] = uf::mesh_t::bootstrap({
    .executor = executor.handle(),
    .options = {.host_domain_index = rank, .host_domain_count = nodes,
                .directory = uf::bytes_view("/shared/exchange"), .job = uf::bytes_view("run-17")},
});
auto [trainer, trainer_status] = uf::trainer_t::init_vram({
    .executor = executor.handle(),
    .depth = 12,
    .hidden = 512,
    .batch = {.max_rows_per_device = 64, .max_segments_per_device = 256, .max_columns_per_row = 1024,
              .max_bags_per_device = 128, .image = {.patch_side = 16}},
    .objective = uform_objective_joint_k,
    .schedule = {.steps = 100000},
    .mesh = mesh.handle(),
    .seed = 42,
});

std::size_t const attract_offsets[] = {0, 1, 2, 3, 4};
std::uint32_t const attract_ids[] = {1, 0, 3, 2}, bags[] = {0, 0, 1, 1};
uform_trainer_step_stats_t stats {};
uf::trainer_step_config_t const config {
    .bags = bags, .relations = {.attract = {attract_offsets, attract_ids, nullptr}}, .stats = &stats};
auto [embeddings, step_status] = trainer.on(executor).step(modalities, contents, config);
uf::status_t const saved = trainer.on(executor).save(uf::bytes_view("checkpoint.safetensors"));
```

`trainer_step_config_t` gathers a step’s per-item inputs: `bags`, `temperature_groups`, `sides`, the `choices` and `choice_targets` tapes `text_choose` takes and answers, the `relations` edge sets, and an optional `arena` for the pooled embeddings the loss scored. The SDK checks every per-item span and tape against the step’s items before the call, and leaves the edge sets to the library. `step` answers the pooled embeddings when its config names an arena, and an empty `written_rows_t` otherwise. A trainer refers to its mesh, so the mesh outlives every trainer made over it, and `mesh.describe()` answers a `uform_mesh_description_t`.

## API

### Types

#### `trainer_step_config_t`

The inputs and knobs of a step beside its modalities and contents; every empty span and tape is left out of the call.

[train.hpp:60 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L60)

```cpp
struct trainer_step_config_t
```

#### Fields

| `bags` | `std::span<std::uint32_t const>` | The bag of each item, whose items land on one device; empty keeps every item its own bag. |
| - | - | - |
| `temperature_groups` | `std::span<std::uint32_t const>` | The temperature group of each item; empty puts every item in group zero. |
| `sides` | `std::span<uform_late_interaction_side_t const>` | The late-interaction side of each item; empty takes no item into late interaction. |
| `choices` | `tape_view<char>` | The legal choices of each item, one byte each, as [`text_choose`](https://www.unum.cloud/docs/uform/cpp/models.md#text_choose) takes them. |
| `choice_targets` | `tape_view<float>` | The target probability of each choice, in `choices` order, as [`text_choose`](https://www.unum.cloud/docs/uform/cpp/models.md#text_choose) answers. |
| `relations` | `uform_relations_view_t` | The attract, repel and ignore edges over the step's items. |
| `arena` | `arena_t *` | Where the pooled embeddings the loss scored land; `nullptr` skips the copy. |
| `outputs_scalar` | `uform_scalar_t` | The scalar type the embeddings land as. |
| `stats` | `uform_trainer_step_stats_t *` | The report the step overwrites; `nullptr` skips it and the readback of the losses. |

#### Class `trainer`

A trainer and its verbs, completed as `receiver_type_` says: the owning handle and [`on`](#on) block, while [`deferred`](#deferred) answers deferred calls, whose every input stays the caller's until final.7

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

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

##### `trainer`

[train.hpp:104 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L104)

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

##### `handle_field`

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

[train.hpp:111 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L111)

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

##### `on`

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

[train.hpp:118 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L118)

```cpp
trainer<caller_owned<uform_trainer_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`](#deferred) call.

[train.hpp:124 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L124)

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

##### `step`

Applies one optimizer step over this process's items, tagged by `modalities`, and every peer's, answering the pooled embeddings the loss scored when `config` names an arena.

[train.hpp:132 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L132)

```cpp
deferrable<written_rows_t> step(
    std::span<uform_modality_t const> modalities,
    tape_view<std::uint8_t> contents,
    trainer_step_config_t const & config = {}) const noexcept;
```

##### `save`

Writes the weights and the step to a safetensors checkpoint at `path`, which only the process of index zero writes.

[train.hpp:185 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L185)

```cpp
deferrable save(
    uform_bytes_view_t path,
    progress_t progress = {}) const noexcept;
```

##### `init_vram`

Builds one replica per device of the executor `arguments` names and starts or resumes the run, blocking until every process of the run has.

[train.hpp:195 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L195)

```cpp
requires an_owning_receiver<receiver_type_>
expected<trainer> init_vram(
    uform_trainer_init_vram_t arguments) noexcept;
```
