C++ · Training

Training

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

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/uform/cpp/training.md' | claude -p "How would this fit my code?"

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

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_tThe 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 ↗
struct trainer_step_config_t

Fields

bagsstd::span<std::uint32_t const>The bag of each item, whose items land on one device; empty keeps every item its own bag.
temperature_groupsstd::span<std::uint32_t const>The temperature group of each item; empty puts every item in group zero.
sidesstd::span<uform_late_interaction_side_t const>The late-interaction side of each item; empty takes no item into late interaction.
choicestape_view<char>The legal choices of each item, one byte each, as text_choose takes them.
choice_targetstape_view<float>The target probability of each choice, in choices order, as text_choose answers.
relationsuform_relations_view_tThe attract, repel and ignore edges over the step's items.
arenaarena_t *Where the pooled embeddings the loss scored land; nullptr skips the copy.
outputs_scalaruform_scalar_tThe scalar type the embeddings land as.
statsuform_trainer_step_stats_t *The report the step overwrites; nullptr skips it and the readback of the losses.
ClasstrainerA trainer and its verbs, completed as receiver_type_ says: the owning handle and on block, while deferred answers deferred calls, whose every input stays the caller's until final.7
train.hpp:93 ↗
template <typename receiver_type_>
class trainer
trainer
train.hpp:104 ↗
requires an_owning_receiver<receiver_type_>
trainer() noexcept=default;
handle_fieldThe output field a lifecycle verb writes the new handle into.
train.hpp:111 ↗
requires an_owning_receiver<receiver_type_>
uform_trainer_t * handle_field() noexcept;
onThe same handle, whose verbs block on executor.
train.hpp:118 ↗
trainer<caller_owned<uform_trainer_t, uform_completion_blocking_k>> on(
    executor_t const & executor) const noexcept;
deferredThe same handle, whose verbs submit to executor, each answering a deferred call.
train.hpp:124 ↗
trainer<caller_owned<uform_trainer_t, uform_completion_deferred_k>> deferred(
    executor_t const & executor) const noexcept;
stepApplies 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 ↗
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;
saveWrites the weights and the step to a safetensors checkpoint at path, which only the process of index zero writes.
train.hpp:185 ↗
deferrable save(
    uform_bytes_view_t path,
    progress_t progress = {}) const noexcept;
init_vramBuilds 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 ↗
requires an_owning_receiver<receiver_type_>
expected<trainer> init_vram(
    uform_trainer_init_vram_t arguments) noexcept;