# Encoding

> One verb per input form, each spelled as in C, and each taking its inputs, the EncodeOutputs to keep, the scalar every output lands as, the arena, and last a config, nil for none.

`import "github.com/unum-cloud/uform"` 4.0.0-rc.1 · c58749fe

One verb per input form, each spelled as in C, and each taking its inputs, the `EncodeOutputs` to keep, the scalar every output lands as, the arena, and last a config, nil for none:

- `TextEncode(texts, ..)` takes UTF-8 texts, a byte per token, since models are tokenizer-free.
- `ImageEncode(files, ..)` takes JPEG, PNG or WebP files.
- `ImageEncodePixels(pixels, heights, widths, ..)` takes interleaved 8-bit RGB, `heights[i]` rows of `widths[i]` pixels per item.
- `VideoEncode(files, ..)` takes MP4 or WebM clips.
- `AudioEncode(files, ..)` takes WAV, FLAC, MP3 or Opus recordings.
- `AudioEncodeSamples(samples, sampleRate, ..)` takes mono `float32` samples.

A `Tape` passes the elements and `count + 1` offsets to the library without a copy: `BytesTape(data, offsets)` for texts and files, and `SamplesTape(data, offsets)` for recordings. `TapeOf(items)` gathers separate strings or byte slices into a fresh tape with one copy, and `SamplesOf(recordings)` does so for samples:

```go
tape := uform.BytesTape([]byte("first documentthe second"), []uint{0, 14, 24})
encoded, err := model.TextEncode(tape, uform.EncodeOutputsStates, uform.ScalarBF16, arena, nil)
offsets := encoded.States.Offsets() // item `i` spans rows `offsets[i]` up to `offsets[i + 1]`
second, err := encoded.States.Item(1) // the rows of one item, as `Rows`
```

`EncodeOutputsEmbeddings`, `EncodeOutputsStates` and `EncodeOutputsTokenVectors` join with `|`, and a surface left out answers a zero `Count` 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 `StatusUnsupported`. The config reaches the knobs:

```go
stats := new(uform.TextEncodeStats)
config := &uform.TextEncodeConfig{
	Stats:    stats,
	Progress: func(completed, total uint) bool { return completed*2 < total },
}
encoded, err := model.TextEncode(texts, uform.EncodeOutputsEmbeddings, uform.ScalarF32, arena, config)
```

`Rows` reports its `Count`, `Columns`, `Stride` and `Scalar`, and `uform.RowValues[float32](rows, i)` views one row’s values, refused with `StatusInvalidArgument` when the rows hold another scalar type. `RowsTape` adds the `Offsets` and an `Item` per input. A handler answering false stops the call, which fails with `StatusCancelled` and publishes nothing.

## API

### Types

#### `AudioEncodeConfig`

AudioEncodeConfig holds the knobs of AudioEncode and AudioEncodeSamples; nil derives every field.

[types.go:162 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L162)

```go
type AudioEncodeConfig struct {
    Progress ProgressCallback
    ProgressSteps uint
    Stats *AudioEncodeStats
}
```

Mirrors the knobs of uform_audio_encode_t and uform_audio_encode_samples_t.

#### Fields

| `Progress` | `ProgressCallback` | Progress hears the items encoded and may stop the call. |
| - | - | - |
| `ProgressSteps` | `uint` | ProgressSteps is the reports the call makes at most; zero derives ten. |
| `Stats` | `*AudioEncodeStats` | Stats is the report the call overwrites, kept by the caller until it is final; nil skips it. |

#### `AudioEncodeStats`

AudioEncodeStats is what an audio call reports beside its outputs, overwritten by the call.

[types.go:108 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L108)

```go
type AudioEncodeStats struct {
    Truncated uint
    Tokens uint
}
```

Mirrors uform_audio_encode_stats_t.

#### Fields

| `Truncated` | `uint` | Truncated counts the items cut at the model's context in frames. |
| - | - | - |
| `Tokens` | `uint` | Tokens counts the frames the call encoded, after truncation. |

#### `E2M3Scalar`

E2M3Scalar holds the six occupied bits of a two-exponent minifloat.

[types.go:250 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L250)

```go
type E2M3Scalar uint8
```

#### `E3M2Scalar`

E3M2Scalar holds the six occupied bits of a three-exponent minifloat.

[types.go:248 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L248)

```go
type E3M2Scalar uint8
```

#### `E4M3Scalar`

E4M3Scalar holds the eight bits of a four-exponent minifloat.

[types.go:246 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L246)

```go
type E4M3Scalar uint8
```

#### `E5M2Scalar`

E5M2Scalar holds the eight bits of a five-exponent minifloat.

[types.go:244 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L244)

```go
type E5M2Scalar uint8
```

#### `Element`

Element constrains the element types rows and tapes may hold, ordered by shrinking dynamic range.

[types.go:260 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L260)

```go
type Element interface {
    float64 | float32 | BFloat16 | Float16 |
        E5M2Scalar | E4M3Scalar | E3M2Scalar | E2M3Scalar |
        int8 | I4x2Scalar | int32 | uint8 | U4x2Scalar | U1x8Scalar
}
```

#### `EncodeOutputs`

EncodeOutputs names the surfaces an encode verb keeps from its one forward, after the C fields they fill; | joins them, and a surface left out costs nothing past the final norm.

[types.go:29 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L29)

```go
type EncodeOutputs uint32
```

Binding-only.

#### Values

| `EncodeOutputsEmbeddings` | `1 &lt;&lt; iota` | EncodeOutputsEmbeddings keeps one pooled embedding per item, past the attention pool and the projection head. |
| - | - | - |
| `EncodeOutputsStates` | | EncodeOutputsStates keeps one final-norm state per token left after truncation. |
| `EncodeOutputsTokenVectors` | | EncodeOutputsTokenVectors keeps one unit vector of the token head per token left after truncation; a model without the head fails the call with StatusUnsupported. |

#### `Encoded`

Encoded is what an encode verb wrote into the arena it was given: the surfaces its EncodeOutputs named, each with a zero Count when left out, and none normalized.

[types.go:44 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L44)

```go
type Encoded struct {
    Embeddings Rows
    States RowsTape
    TokenVectors RowsTape
}
```

Binding-only.

#### Fields

| `Embeddings` | `Rows` | Embeddings holds one pooled embedding of the model's dimensions per item. |
| - | - | - |
| `States` | `RowsTape` | States holds one final-norm state per token left after truncation, item by item. |
| `TokenVectors` | `RowsTape` | TokenVectors holds one unit vector of the model's token width per token left after truncation, item by item. |

#### `Float16`

Float16 holds the sixteen bits of an IEEE half-precision value.

[types.go:242 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L242)

```go
type Float16 uint16
```

#### `I4x2Scalar`

I4x2Scalar holds two 4-bit signed integers.

[types.go:252 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L252)

```go
type I4x2Scalar int8
```

#### `ImageEncodeConfig`

ImageEncodeConfig holds the knobs of ImageEncode and ImageEncodePixels; nil derives every field.

[types.go:138 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L138)

```go
type ImageEncodeConfig struct {
    Progress ProgressCallback
    ProgressSteps uint
    Stats *ImageEncodeStats
}
```

Mirrors the knobs of uform_image_encode_t and uform_image_encode_pixels_t.

#### Fields

| `Progress` | `ProgressCallback` | Progress hears the items encoded and may stop the call. |
| - | - | - |
| `ProgressSteps` | `uint` | ProgressSteps is the reports the call makes at most; zero derives ten. |
| `Stats` | `*ImageEncodeStats` | Stats is the report the call overwrites, kept by the caller until it is final; nil skips it. |

#### `ImageEncodeStats`

ImageEncodeStats is what an image call reports beside its outputs, overwritten by the call.

[types.go:88 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L88)

```go
type ImageEncodeStats struct {
    Truncated uint
    Tokens uint
}
```

Mirrors uform_image_encode_stats_t.

#### Fields

| `Truncated` | `uint` | Truncated counts the items whose patch grid was cut at the model's context. |
| - | - | - |
| `Tokens` | `uint` | Tokens counts the patches the call encoded, after truncation. |

#### `TextEncodeConfig`

TextEncodeConfig holds the knobs of TextEncode; nil derives every field.

[types.go:126 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L126)

```go
type TextEncodeConfig struct {
    Progress ProgressCallback
    ProgressSteps uint
    Stats *TextEncodeStats
}
```

Mirrors the knobs of uform_text_encode_t.

#### Fields

| `Progress` | `ProgressCallback` | Progress hears the items encoded and may stop the call. |
| - | - | - |
| `ProgressSteps` | `uint` | ProgressSteps is the reports the call makes at most; zero derives ten. |
| `Stats` | `*TextEncodeStats` | Stats is the report the call overwrites, kept by the caller until it is final; nil skips it. |

#### `TextEncodeStats`

TextEncodeStats is what a text call reports beside its outputs, overwritten by the call.

[types.go:78 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L78)

```go
type TextEncodeStats struct {
    Truncated uint
    Tokens uint
}
```

Mirrors uform_text_encode_stats_t.

#### Fields

| `Truncated` | `uint` | Truncated counts the items cut at the model's context. |
| - | - | - |
| `Tokens` | `uint` | Tokens counts the tokens the call encoded, after truncation. |

#### `U1x8Scalar`

U1x8Scalar holds eight single-bit dimensions.

[types.go:256 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L256)

```go
type U1x8Scalar uint8
```

#### `U4x2Scalar`

U4x2Scalar holds two 4-bit unsigned integers.

[types.go:254 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L254)

```go
type U4x2Scalar uint8
```

#### `VideoEncodeConfig`

VideoEncodeConfig holds the knobs of VideoEncode; nil derives every field.

[types.go:150 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L150)

```go
type VideoEncodeConfig struct {
    Progress ProgressCallback
    ProgressSteps uint
    Stats *VideoEncodeStats
}
```

Mirrors the knobs of uform_video_encode_t.

#### Fields

| `Progress` | `ProgressCallback` | Progress hears the items encoded and may stop the call. |
| - | - | - |
| `ProgressSteps` | `uint` | ProgressSteps is the reports the call makes at most; zero derives ten. |
| `Stats` | `*VideoEncodeStats` | Stats is the report the call overwrites, kept by the caller until it is final; nil skips it. |

#### `VideoEncodeStats`

VideoEncodeStats is what a video call reports beside its outputs, overwritten by the call.

[types.go:98 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L98)

```go
type VideoEncodeStats struct {
    Truncated uint
    Tokens uint
}
```

Mirrors uform_video_encode_stats_t.

#### Fields

| `Truncated` | `uint` | Truncated counts the items cut at the model's context in tubes. |
| - | - | - |
| `Tokens` | `uint` | Tokens counts the tubes the call encoded, after truncation. |

#### Struct `Rows`

Rows is a run of rows a call wrote packed into its arena, one per item, readable until the arena serves another call, after which [RowValues](#RowValues) fails with [StatusContended](https://www.unum.cloud/docs/uform/go/errors.md#StatusContended).4

[types.go:312 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L312)

```go
type Rows struct {
    // contains filtered or unexported fields
}
```

A call into an arena the host may not read is refused before it runs.

Mirrors uform_rows_view_t, beside the count the call was given.

##### `Columns`

Columns reports the values per row, the model's dimensions or token width.

[types.go:322 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L322)

```go
func (rows Rows) Columns() uint
```

##### `Count`

Count reports how many rows the call wrote, zero for a surface it did not keep.

[types.go:319 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L319)

```go
func (rows Rows) Count() uint
```

##### `Scalar`

Scalar reports the scalar type the rows were written as.

[types.go:328 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L328)

```go
func (rows Rows) Scalar() Scalar
```

##### `Stride`

Stride reports the bytes between the starts of consecutive rows.

[types.go:325 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L325)

```go
func (rows Rows) Stride() uint
```

#### Struct `RowsTape`

RowsTape is a ragged run of rows a call wrote into its arena, item i spanning the rows between two consecutive [RowsTape.Offsets](#Offsets), readable until the arena serves another call.3

[types.go:352 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L352)

```go
type RowsTape struct {
    // contains filtered or unexported fields
}
```

Mirrors uform_rows_tape_view_t, beside the count the call was given.

##### `Count`

Count reports how many items the call wrote rows for, zero for a surface it did not keep.

[types.go:359 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L359)

```go
func (tape RowsTape) Count() uint
```

##### `Item`

Item views the rows of one item, refused with [StatusInvalidArgument](https://www.unum.cloud/docs/uform/go/errors.md#StatusInvalidArgument) when item is not below [RowsTape.Count](#Count).

[types.go:376 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L376)

```go
func (tape RowsTape) Item(item uint) (Rows, error)
```

##### `Offsets`

Offsets views the Count + 1 offsets into the rows, in rows.

[types.go:363 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L363)

```go
func (tape RowsTape) Offsets() []uint
```

It panics with [StatusContended](https://www.unum.cloud/docs/uform/go/errors.md#StatusContended) once a later call took the arena, as a slice bound does, and the slice is valid only until then.

#### Enum `Scalar`

Scalar names a computed or exchanged scalar type by width and encoding, numbered as USearch numbers it, so a run of embeddings is a run USearch indexes without conversion.2

[types.go:174 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L174)

```go
type Scalar int32
```

Sub-byte kinds pack several dimensions per byte, as their xN suffixes spell.

#### Values

| `ScalarUnknown` | | ScalarUnknown names no scalar: an unset field, which the library refuses. |
| - | - | - |
| `ScalarF64` | | ScalarF64 is 64-bit double precision. |
| `ScalarF32` | | ScalarF32 is 32-bit single precision. |
| `ScalarBF16` | | ScalarBF16 is the brain floating-point format. |
| `ScalarF16` | | ScalarF16 is IEEE half precision. |
| `ScalarE5M2` | | ScalarE5M2 is 8-bit floating point with five exponent bits. |
| `ScalarE4M3` | | ScalarE4M3 is 8-bit floating point with four exponent bits. |
| `ScalarE3M2` | | ScalarE3M2 is 6-bit floating point with three exponent bits. |
| `ScalarE2M3` | | ScalarE2M3 is 6-bit floating point with two exponent bits. |
| `ScalarI8` | | ScalarI8 is an 8-bit signed integer. |
| `ScalarI4x2` | | ScalarI4x2 packs two 4-bit signed integers per byte. |
| `ScalarI32` | | ScalarI32 is a 32-bit signed integer. |
| `ScalarU8` | | ScalarU8 is an 8-bit unsigned integer, the element of UTF-8 text and of encoded files. |
| `ScalarU4x2` | | ScalarU4x2 packs two 4-bit unsigned integers per byte. |
| `ScalarU1x8` | | ScalarU1x8 packs eight single-bit dimensions per byte, the sign of each embedding dimension. |

##### `ScalarFromName`

ScalarFromName reads a scalar back from the spelling [Scalar.String](https://www.unum.cloud/docs/uform/go/errors.md#String) renders.

[types.go:217 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L217)

```go
func ScalarFromName(name string) (Scalar, error)
```

##### `String`

String renders the scalar kind as the library spells it.

[types.go:212 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L212)

```go
func (scalar Scalar) String() string
```

#### Struct `Tape`

Tape views a ragged batch as Apache Arrow lays one out: elements end to end, and one more offset than entries around them, entry i spanning offsets\[i] up to offsets\[i+1].6

[types.go:400 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L400)

```go
type Tape struct {
    // contains filtered or unexported fields
}
```

An element is a byte of UTF-8 text or of an encoded file, or a sample of a recording.

A Tape views and never owns, and the binding pins both slices until each call it crosses is final, so neither may change until then.

##### `BytesTape`

BytesTape views bytes end to end around len(offsets) - 1 texts or files, without a copy.

[types.go:424 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L424)

```go
func BytesTape(data []byte, offsets []uint) Tape
```

Panics when the offsets overrun the bytes, which names a caller's arithmetic mistake rather than a condition the library could report.

##### `SamplesOf`

SamplesOf gathers separate mono recordings end to end into a fresh Tape, one entry apiece.

[types.go:427 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L427)

```go
func SamplesOf(recordings [][]float32) Tape
```

##### `SamplesTape`

SamplesTape views mono samples end to end around len(offsets) - 1 recordings, without a copy.

[types.go:442 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L442)

```go
func SamplesTape(data []float32, offsets []uint) Tape
```

Panics when the offsets overrun the samples.

##### `TapeOf`

TapeOf gathers separate texts or files end to end into a fresh Tape, one entry apiece, copying each once.

[types.go:408 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L408)

```go
func TapeOf[Item ~string | ~[]byte](items []Item) Tape
```

##### `Entries`

Entries reports how many entries the tape spans, zero for the zero Tape.

[types.go:456 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L456)

```go
func (tape Tape) Entries() uint
```

##### `Scalar`

Scalar reports the scalar type the elements hold.

[types.go:464 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L464)

```go
func (tape Tape) Scalar() Scalar
```

#### Struct `TapeView`

TapeView is a ragged batch a call wrote into its arena, readable until the arena serves another call: one probability per choice, item by item, for TextChoose.2

[types.go:483 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L483)

```go
type TapeView struct {
    // contains filtered or unexported fields
}
```

Mirrors uform_tape_view_t, beside the count the call was given.

##### `Entries`

Entries reports how many entries the call wrote.

[types.go:489 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L489)

```go
func (tape TapeView) Entries() uint
```

##### `Scalar`

Scalar reports the scalar type the elements were written as.

[types.go:492 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L492)

```go
func (tape TapeView) Scalar() Scalar
```

### Functions

#### `RowValues`

RowValues views one row's values in the arena, refused with StatusInvalidArgument when Value is not the scalar type they were written as or row is not below Count, and with StatusContended once a later call took the arena.

[uform.go:333 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/uform.go#L333)

```go
func RowValues[Value Element](
    rows Rows,
    row uint,
) ([]Value, error)
```

The slice is valid only until that call.

#### `TapeEntry`

TapeEntry views one entry's elements in the arena, refused with StatusInvalidArgument when Value is not the scalar type they were written as or entry is not below Entries.

[uform.go:496 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/uform.go#L496)

```go
func TapeEntry[Value Element](
    tape TapeView,
    entry uint,
) ([]Value, error)
```
