C++ · Models

Models

#include <uform/model.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/models.md' | claude -p "How would this fit my code?"

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:

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

ClassmodelA model and its verbs, completed as receiver_type_ says: the owning handle blocks on the calling thread, on blocks on an executor, and both answer a status_t or an expected, while deferred submits to an executor and answers a deferred call.16
model.hpp:46 ↗
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 ↗
requires an_owning_receiver<receiver_type_>
model() noexcept=default;
handle_fieldThe output field a lifecycle verb writes the new handle into.
model.hpp:64 ↗
requires an_owning_receiver<receiver_type_>
uform_model_t * handle_field() noexcept;
onThe same handle, whose verbs block on executor.
model.hpp:71 ↗
model<caller_owned<uform_model_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.
model.hpp:76 ↗
model<caller_owned<uform_model_t, uform_completion_deferred_k>> deferred(
    executor_t const & executor) const noexcept;
describeReports the model's geometry, placement, classes and heads in one call.
model.hpp:82 ↗
deferrable<uform_model_description_t> describe() const noexcept;
text_encodeEncodes UTF-8 texts through one forward, a token per byte, keeping the surfaces kept names in arena.
model.hpp:93 ↗
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_chooseScores 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 ↗
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_encodeDecodes and encodes JPEG, PNG or WebP images; an undecodable file fails the call with status_code_t::malformed_k.
model.hpp:150 ↗
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_pixelsEncodes decoded images of interleaved 8-bit RGB, item i holding heights [i] rows of widths [i] pixels.
model.hpp:173 ↗
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_encodeDecodes and encodes MP4 or WebM videos.
model.hpp:202 ↗
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_encodeDecodes and encodes WAV, FLAC, MP3 or Opus recordings, resampled to mono.
model.hpp:224 ↗
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_samplesEncodes decoded mono samples sharing one sample_rate, resampled to the model's rate where it differs.
model.hpp:248 ↗
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_ramCopies a safetensors checkpoint file into host memory and packs it for the CPU, blocking.
model.hpp:273 ↗
requires an_owning_receiver<receiver_type_>
expected<model> load_ram(uform_model_load_ram_t arguments) noexcept;
load_ram_bufferCopies a checkpoint held in memory into host memory and packs it for the CPU, blocking.
model.hpp:281 ↗
requires an_owning_receiver<receiver_type_>
expected<model> load_ram_buffer(
    uform_model_load_ram_buffer_t arguments) noexcept;
load_vramCopies a checkpoint file onto the GPUs arguments names and packs it there, blocking.
model.hpp:289 ↗
requires an_owning_receiver<receiver_type_>
expected<model> load_vram(uform_model_load_vram_t arguments) noexcept;
load_vram_bufferCopies a checkpoint held in memory onto the GPUs arguments names, blocking.
model.hpp:297 ↗
requires an_owning_receiver<receiver_type_>
expected<model> load_vram_buffer(
    uform_model_load_vram_buffer_t arguments) noexcept;