# Encoding

> 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.

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

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:

```cpp
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:

```cpp
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

#### `outputs`

Where a call writes what `value_type_` is made of.

[types.hpp:87 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L87)

```cpp
template <typename value_type_>
struct outputs
```

#### Fields

| `value` | `value_type_` | The answer, which the call writes in place. |
| - | - | - |

#### `outputs< void >`

A call that outputs nothing but its status.

[types.hpp:97 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L97)

```cpp
template <>
struct outputs< void >
```

#### Fields

| `value` | `void` | The answer, which the call writes in place. |
| - | - | - |

#### `bf16_t`

Bit containers for the formats C++ has no arithmetic type for, so a call can still name them.

[types.hpp:311 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L311)

```cpp
struct bf16_t
```

#### Fields

| `encoding` | `std::uint16_t` | |
| - | - | - |

#### `written_rows_t`

Rows 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 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L455)

```cpp
struct written_rows_t
```

#### Fields

| `view` | `uform_rows_view_t` | The rows as the call wrote them. |
| - | - | - |
| `written` | `std::size_t` | Rows written, one per item along the call's batch axis; zero for a surface the call did not keep. |

#### `written_rows_tape_t`

A 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 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L475)

```cpp
struct written_rows_tape_t
```

#### Fields

| `view` | `uform_rows_tape_view_t` | The rows and offsets as the call wrote them. |
| - | - | - |
| `written` | `std::size_t` | Items written along the call's batch axis; zero for a surface the call did not keep. |

#### `encode_outputs_t`

The surfaces an encode verb keeps from its one forward, named after the C fields they fill; `operator|` joins them.

[encode.hpp:25 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/encode.hpp#L25)

```cpp
enum encode_outputs_t
```

#### Values

| `embeddings_k` | `1` | One pooled embedding per item, past the attention pool and the projection head. |
| - | - | - |
| `states_k` | `2` | One final-norm state per token left after truncation. |
| `token_vectors_k` | `4` | One 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`](https://www.unum.cloud/docs/uform/cpp/errors.md#unsupported_k). |

#### `encoded_t`

What 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 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/encode.hpp#L50)

```cpp
struct encoded_t
```

#### Fields

| `embeddings` | `written_rows_t` | One pooled embedding of the model's dimensions per item. |
| - | - | - |
| `states` | `written_rows_tape_t` | One final-norm state per token left after truncation, item by item. |
| `token_vectors` | `written_rows_tape_t` | One unit vector of the model's token width per token left after truncation, item by item. |

#### `encode_config`

The 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 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/encode.hpp#L83)

```cpp
template <typename stats_type_>
struct encode_config
```

#### Fields

| `outputs_scalar` | `uform_scalar_t` | The scalar type every output lands as. |
| - | - | - |
| `progress` | `progress_t` | Hears the items encoded, and may cancel the call. |
| `stats` | `stats_type_ *` | The report the call overwrites; `nullptr` skips it. |

#### `outputs< tape_view< float > >`

Where [`text_choose`](https://www.unum.cloud/docs/uform/cpp/models.md#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 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/encode.hpp#L127)

```cpp
template <>
struct outputs< tape_view< float > >
```

#### Fields

| `written` | `uform_tape_view_t` | |
| - | - | - |
| `count` | `std::size_t` | |
| `value` | `tape_view<float>` | The answer, which the call writes in place. |

#### Struct `tape_view`

A 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 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L405)

```cpp
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.

##### `count`

Entries along the batch axis, one fewer than the offsets.

[types.hpp:410 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L410)

```cpp
std::size_t count() const noexcept;
```

##### `entry`

The elements of the entry at `position`, below [`count`](#count).

[types.hpp:413 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L413)

```cpp
std::span<element_type_ const> entry(
    std::size_t position) const noexcept;
```

##### `check`

Refuses what only the binding sees: no offsets, or a last one past the elements.

[types.hpp:418 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L418)

```cpp
status_t check() const noexcept;
```

### Functions

#### `bytes_view`

Views `text` as the unterminated run of bytes every path, buffer and name arrives in.

[types.hpp:388 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L388)

```cpp
uform_bytes_view_t bytes_view(std::string_view text) noexcept;
```

```cpp
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.

#### `gathered`

Gathers 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 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L436)

```cpp
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.

#### `contains`

Whether every surface `surface` names is kept by [`outputs`](#outputs).

[encode.hpp:43 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/encode.hpp#L43)

```cpp
bool contains(
    encode_outputs_t outputs,
    encode_outputs_t surface) noexcept;
```

### Constants

#### `scalar_of`

The scalar kind a C++ element type names, or [`uform_scalar_unknown_k`](https://www.unum.cloud/docs/uform/c/encoding.md#uform_scalar_unknown_k) for none.

[types.hpp:341 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L341)

```cpp
uform_scalar_t scalar_of;
```

#### `values_per_element`

Values one element packs: eight for bit sets, two for nibbles, one otherwise.

[types.hpp:379 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L379)

```cpp
std::size_t values_per_element;
```
