C++ · Encoding

Encoding

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

One verb per input form, each spelled as in C, and each taking its inputs, the encode_outputs_t to keep, a config, and last the arena:

  • text_encode(texts, ..) takes UTF-8 texts as a tape_view<char>, a byte per token, since models are tokenizer-free.
  • image_encode(files, ..) takes JPEG, PNG or WebP files as a tape_view<std::uint8_t>.
  • image_encode_pixels(pixels, heights, widths, ..) takes interleaved 8-bit RGB, heights[i] rows of widths[i] pixels per item.
  • video_encode(files, ..) takes MP4 or WebM clips.
  • audio_encode(files, ..) takes WAV, FLAC, MP3 or Opus recordings.
  • audio_encode_samples(samples, sample_rate, ..) takes mono float samples as a tape_view<float>.

A tape_view is the elements and count + 1 offsets, which the library reads without a copy once the SDK checks the offsets stay inside the elements. Separate items gather into memory the caller owns with one copy, through gathered<char>(items, offsets, elements), which answers the view every verb takes, or status_code_t::invalid_argument_k when either span is too short:

std::string_view const joined = "first documentthe second";
std::size_t const offsets[] = {0, 14, 24};
auto [encoded, status] = model.text_encode({joined, offsets}, uf::encode_outputs_t::states_k, uform_scalar_bf16_k, arena);
std::span<std::size_t const> const items = encoded.states.offsets(); // item `i` spans rows `items[i]` up to `items[i + 1]`
uf::written_rows_t const second = encoded.states.item(1);              // the rows of one item

encode_outputs_t::embeddings_k, states_k and token_vectors_k join with |, and a surface left out answers an empty view and costs nothing past the final norm. Token vectors are the token head’s unit rows, one per state, which late-interaction rerankers score; a model without the head answers status_code_t::unsupported_k. A bare uform_scalar_t converts into the verb’s config, the scalar every output lands as; the config’s other fields reach the knobs:

uform_text_encode_stats_t stats {};
auto handler = [](std::size_t completed, std::size_t total) noexcept {
    return completed * 2 < total ? uform_continue_k : uform_cancel_k;
};
uf::text_encode_config_t const config {uform_scalar_f32_k, {handler}, &stats};
auto [encoded, status] = model.text_encode(texts, uf::encode_outputs_t::embeddings_k, config, arena);

written_rows_t holds the C view, its stride, columns and scalar, beside the rows written, and row<float>(i) reads one row, empty when the rows hold another scalar type. written_rows_tape_t adds the offsets() and an item(i) per input. A handler answering uform_cancel_k stops the call, which fails with status_code_t::cancelled_k and publishes nothing.

API

Types

outputsWhere a call writes what value_type_ is made of.
types.hpp:87 ↗
template <typename value_type_>
struct outputs

Fields

valuevalue_type_The answer, which the call writes in place.
outputs< void >A call that outputs nothing but its status.
types.hpp:97 ↗
template <>
struct outputs< void >

Fields

valuevoidThe answer, which the call writes in place.
bf16_tBit containers for the formats C++ has no arithmetic type for, so a call can still name them.
types.hpp:311 ↗
struct bf16_t

Fields

encodingstd::uint16_t
written_rows_tRows a call wrote packed into its arena, one per item, readable until the arena's next call; host code may read them only from a RAM arena.
types.hpp:455 ↗
struct written_rows_t

Fields

viewuform_rows_view_tThe rows as the call wrote them.
writtenstd::size_tRows written, one per item along the call's batch axis; zero for a surface the call did not keep.
written_rows_tape_tA ragged run of rows a call wrote into its arena, item i spanning rows offsets()[i] up to offsets()[i+1]; readable until the arena's next call.
types.hpp:475 ↗
struct written_rows_tape_t

Fields

viewuform_rows_tape_view_tThe rows and offsets as the call wrote them.
writtenstd::size_tItems written along the call's batch axis; zero for a surface the call did not keep.
encode_outputs_tThe surfaces an encode verb keeps from its one forward, named after the C fields they fill; operator| joins them.
encode.hpp:25 ↗
enum encode_outputs_t

Values

embeddings_k1One pooled embedding per item, past the attention pool and the projection head.
states_k2One final-norm state per token left after truncation.
token_vectors_k4One unit vector of the token head per token left after truncation; a model without the head fails the call with status_code_t::unsupported_k.
encoded_tWhat an encode verb wrote into the arena it was given: the surfaces its encode_outputs_t named, each empty when left out, and none normalized.
encode.hpp:50 ↗
struct encoded_t

Fields

embeddingswritten_rows_tOne pooled embedding of the model's dimensions per item.
stateswritten_rows_tape_tOne final-norm state per token left after truncation, item by item.
token_vectorswritten_rows_tape_tOne unit vector of the model's token width per token left after truncation, item by item.
encode_configThe knobs of an encode verb reporting stats_type_: the scalar every output lands as, which a bare uform_scalar_t converts into, a handler, and a report.
encode.hpp:83 ↗
template <typename stats_type_>
struct encode_config

Fields

outputs_scalaruform_scalar_tThe scalar type every output lands as.
progressprogress_tHears the items encoded, and may cancel the call.
statsstats_type_ *The report the call overwrites; nullptr skips it.
outputs< tape_view< float > >Where text_choose writes one probability per choice: the C tape and its count, from which the answer assembles a tape_view over the arena.
encode.hpp:127 ↗
template <>
struct outputs< tape_view< float > >

Fields

writtenuform_tape_view_t
countstd::size_t
valuetape_view<float>The answer, which the call writes in place.
Structtape_viewA ragged batch of element_type_ laid out as Apache Arrow lays one out: elements end to end, and one more offset than entries, entry i spanning two consecutive offsets.3
types.hpp:405 ↗
template <an_element element_type_>
struct tape_view

The caller owns both spans: as an input, it keeps them alive until the call is final; as an output, it reads the call's arena until the arena's next call.

countEntries along the batch axis, one fewer than the offsets.
types.hpp:410 ↗
std::size_t count() const noexcept;
entryThe elements of the entry at position, below count.
types.hpp:413 ↗
std::span<element_type_ const> entry(
    std::size_t position) const noexcept;
checkRefuses what only the binding sees: no offsets, or a last one past the elements.
types.hpp:418 ↗
status_t check() const noexcept;

Functions

bytes_viewViews text as the unterminated run of bytes every path, buffer and name arrives in.
types.hpp:388 ↗
uform_bytes_view_t bytes_view(std::string_view text) noexcept;
uform_bytes_view_t bytes_view(
    std::span<std::uint8_t const> bytes) noexcept;

Views bytes as the caller's run every checkpoint buffer arrives in.

gatheredGathers separate items of element_type_ into one tape laid out in the caller's offsets and elements, for callers whose items are not laid end to end already.
types.hpp:436 ↗
template <an_element element_type_, std::ranges::sized_range items_type_>
expected<tape_view<element_type_>> gathered(
    items_type_ const & items,
    std::span<std::size_t> offsets,
    std::span<element_type_> elements) noexcept;

Gathering copies every element once, which a caller already holding a tape skips by passing its tape_view directly. The answer views both spans, so they outlive every call it is passed to.

containsWhether every surface surface names is kept by outputs.
encode.hpp:43 ↗
bool contains(
    encode_outputs_t outputs,
    encode_outputs_t surface) noexcept;

Constants

scalar_ofThe scalar kind a C++ element type names, or uform_scalar_unknown_k for none.
types.hpp:341 ↗
uform_scalar_t scalar_of;
values_per_elementValues one element packs: eight for bit sets, two for nibbles, one otherwise.
types.hpp:379 ↗
std::size_t values_per_element;