Go · Encoding

Encoding

import "github.com/unum-cloud/uform" 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/go/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 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:

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:

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

AudioEncodeConfigAudioEncodeConfig holds the knobs of AudioEncode and AudioEncodeSamples; nil derives every field.
types.go:162 ↗
type AudioEncodeConfig struct {
    Progress ProgressCallback
    ProgressSteps uint
    Stats *AudioEncodeStats
}

Mirrors the knobs of uform_audio_encode_t and uform_audio_encode_samples_t.

Fields

ProgressProgressCallbackProgress hears the items encoded and may stop the call.
ProgressStepsuintProgressSteps is the reports the call makes at most; zero derives ten.
Stats*AudioEncodeStatsStats is the report the call overwrites, kept by the caller until it is final; nil skips it.
AudioEncodeStatsAudioEncodeStats is what an audio call reports beside its outputs, overwritten by the call.
types.go:108 ↗
type AudioEncodeStats struct {
    Truncated uint
    Tokens uint
}

Mirrors uform_audio_encode_stats_t.

Fields

TruncateduintTruncated counts the items cut at the model's context in frames.
TokensuintTokens counts the frames the call encoded, after truncation.
E2M3ScalarE2M3Scalar holds the six occupied bits of a two-exponent minifloat.
types.go:250 ↗
type E2M3Scalar uint8
E3M2ScalarE3M2Scalar holds the six occupied bits of a three-exponent minifloat.
types.go:248 ↗
type E3M2Scalar uint8
E4M3ScalarE4M3Scalar holds the eight bits of a four-exponent minifloat.
types.go:246 ↗
type E4M3Scalar uint8
E5M2ScalarE5M2Scalar holds the eight bits of a five-exponent minifloat.
types.go:244 ↗
type E5M2Scalar uint8
ElementElement constrains the element types rows and tapes may hold, ordered by shrinking dynamic range.
types.go:260 ↗
type Element interface {
    float64 | float32 | BFloat16 | Float16 |
        E5M2Scalar | E4M3Scalar | E3M2Scalar | E2M3Scalar |
        int8 | I4x2Scalar | int32 | uint8 | U4x2Scalar | U1x8Scalar
}
EncodeOutputsEncodeOutputs 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 ↗
type EncodeOutputs uint32

Binding-only.

Values

EncodeOutputsEmbeddings1 &lt;&lt; iotaEncodeOutputsEmbeddings keeps one pooled embedding per item, past the attention pool and the projection head.
EncodeOutputsStatesEncodeOutputsStates keeps one final-norm state per token left after truncation.
EncodeOutputsTokenVectorsEncodeOutputsTokenVectors keeps one unit vector of the token head per token left after truncation; a model without the head fails the call with StatusUnsupported.
EncodedEncoded 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 ↗
type Encoded struct {
    Embeddings Rows
    States RowsTape
    TokenVectors RowsTape
}

Binding-only.

Fields

EmbeddingsRowsEmbeddings holds one pooled embedding of the model's dimensions per item.
StatesRowsTapeStates holds one final-norm state per token left after truncation, item by item.
TokenVectorsRowsTapeTokenVectors holds one unit vector of the model's token width per token left after truncation, item by item.
Float16Float16 holds the sixteen bits of an IEEE half-precision value.
types.go:242 ↗
type Float16 uint16
I4x2ScalarI4x2Scalar holds two 4-bit signed integers.
types.go:252 ↗
type I4x2Scalar int8
ImageEncodeConfigImageEncodeConfig holds the knobs of ImageEncode and ImageEncodePixels; nil derives every field.
types.go:138 ↗
type ImageEncodeConfig struct {
    Progress ProgressCallback
    ProgressSteps uint
    Stats *ImageEncodeStats
}

Mirrors the knobs of uform_image_encode_t and uform_image_encode_pixels_t.

Fields

ProgressProgressCallbackProgress hears the items encoded and may stop the call.
ProgressStepsuintProgressSteps is the reports the call makes at most; zero derives ten.
Stats*ImageEncodeStatsStats is the report the call overwrites, kept by the caller until it is final; nil skips it.
ImageEncodeStatsImageEncodeStats is what an image call reports beside its outputs, overwritten by the call.
types.go:88 ↗
type ImageEncodeStats struct {
    Truncated uint
    Tokens uint
}

Mirrors uform_image_encode_stats_t.

Fields

TruncateduintTruncated counts the items whose patch grid was cut at the model's context.
TokensuintTokens counts the patches the call encoded, after truncation.
TextEncodeConfigTextEncodeConfig holds the knobs of TextEncode; nil derives every field.
types.go:126 ↗
type TextEncodeConfig struct {
    Progress ProgressCallback
    ProgressSteps uint
    Stats *TextEncodeStats
}

Mirrors the knobs of uform_text_encode_t.

Fields

ProgressProgressCallbackProgress hears the items encoded and may stop the call.
ProgressStepsuintProgressSteps is the reports the call makes at most; zero derives ten.
Stats*TextEncodeStatsStats is the report the call overwrites, kept by the caller until it is final; nil skips it.
TextEncodeStatsTextEncodeStats is what a text call reports beside its outputs, overwritten by the call.
types.go:78 ↗
type TextEncodeStats struct {
    Truncated uint
    Tokens uint
}

Mirrors uform_text_encode_stats_t.

Fields

TruncateduintTruncated counts the items cut at the model's context.
TokensuintTokens counts the tokens the call encoded, after truncation.
U1x8ScalarU1x8Scalar holds eight single-bit dimensions.
types.go:256 ↗
type U1x8Scalar uint8
U4x2ScalarU4x2Scalar holds two 4-bit unsigned integers.
types.go:254 ↗
type U4x2Scalar uint8
VideoEncodeConfigVideoEncodeConfig holds the knobs of VideoEncode; nil derives every field.
types.go:150 ↗
type VideoEncodeConfig struct {
    Progress ProgressCallback
    ProgressSteps uint
    Stats *VideoEncodeStats
}

Mirrors the knobs of uform_video_encode_t.

Fields

ProgressProgressCallbackProgress hears the items encoded and may stop the call.
ProgressStepsuintProgressSteps is the reports the call makes at most; zero derives ten.
Stats*VideoEncodeStatsStats is the report the call overwrites, kept by the caller until it is final; nil skips it.
VideoEncodeStatsVideoEncodeStats is what a video call reports beside its outputs, overwritten by the call.
types.go:98 ↗
type VideoEncodeStats struct {
    Truncated uint
    Tokens uint
}

Mirrors uform_video_encode_stats_t.

Fields

TruncateduintTruncated counts the items cut at the model's context in tubes.
TokensuintTokens counts the tubes the call encoded, after truncation.
StructRowsRows 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 fails with StatusContended.4
types.go:312 ↗
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.

ColumnsColumns reports the values per row, the model's dimensions or token width.
types.go:322 ↗
func (rows Rows) Columns() uint
CountCount reports how many rows the call wrote, zero for a surface it did not keep.
types.go:319 ↗
func (rows Rows) Count() uint
ScalarScalar reports the scalar type the rows were written as.
types.go:328 ↗
func (rows Rows) Scalar() Scalar
StrideStride reports the bytes between the starts of consecutive rows.
types.go:325 ↗
func (rows Rows) Stride() uint
StructRowsTapeRowsTape is a ragged run of rows a call wrote into its arena, item i spanning the rows between two consecutive RowsTape.Offsets, readable until the arena serves another call.3
types.go:352 ↗
type RowsTape struct {
    // contains filtered or unexported fields
}

Mirrors uform_rows_tape_view_t, beside the count the call was given.

CountCount reports how many items the call wrote rows for, zero for a surface it did not keep.
types.go:359 ↗
func (tape RowsTape) Count() uint
ItemItem views the rows of one item, refused with StatusInvalidArgument when item is not below RowsTape.Count.
types.go:376 ↗
func (tape RowsTape) Item(item uint) (Rows, error)
OffsetsOffsets views the Count + 1 offsets into the rows, in rows.
types.go:363 ↗
func (tape RowsTape) Offsets() []uint

It panics with StatusContended once a later call took the arena, as a slice bound does, and the slice is valid only until then.

EnumScalarScalar 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 ↗
type Scalar int32

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

Values

ScalarUnknownScalarUnknown names no scalar: an unset field, which the library refuses.
ScalarF64ScalarF64 is 64-bit double precision.
ScalarF32ScalarF32 is 32-bit single precision.
ScalarBF16ScalarBF16 is the brain floating-point format.
ScalarF16ScalarF16 is IEEE half precision.
ScalarE5M2ScalarE5M2 is 8-bit floating point with five exponent bits.
ScalarE4M3ScalarE4M3 is 8-bit floating point with four exponent bits.
ScalarE3M2ScalarE3M2 is 6-bit floating point with three exponent bits.
ScalarE2M3ScalarE2M3 is 6-bit floating point with two exponent bits.
ScalarI8ScalarI8 is an 8-bit signed integer.
ScalarI4x2ScalarI4x2 packs two 4-bit signed integers per byte.
ScalarI32ScalarI32 is a 32-bit signed integer.
ScalarU8ScalarU8 is an 8-bit unsigned integer, the element of UTF-8 text and of encoded files.
ScalarU4x2ScalarU4x2 packs two 4-bit unsigned integers per byte.
ScalarU1x8ScalarU1x8 packs eight single-bit dimensions per byte, the sign of each embedding dimension.
ScalarFromNameScalarFromName reads a scalar back from the spelling Scalar.String renders.
types.go:217 ↗
func ScalarFromName(name string) (Scalar, error)
StringString renders the scalar kind as the library spells it.
types.go:212 ↗
func (scalar Scalar) String() string
StructTapeTape 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 ↗
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.

BytesTapeBytesTape views bytes end to end around len(offsets) - 1 texts or files, without a copy.
types.go:424 ↗
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.

SamplesOfSamplesOf gathers separate mono recordings end to end into a fresh Tape, one entry apiece.
types.go:427 ↗
func SamplesOf(recordings [][]float32) Tape
SamplesTapeSamplesTape views mono samples end to end around len(offsets) - 1 recordings, without a copy.
types.go:442 ↗
func SamplesTape(data []float32, offsets []uint) Tape

Panics when the offsets overrun the samples.

TapeOfTapeOf gathers separate texts or files end to end into a fresh Tape, one entry apiece, copying each once.
types.go:408 ↗
func TapeOf[Item ~string | ~[]byte](items []Item) Tape
EntriesEntries reports how many entries the tape spans, zero for the zero Tape.
types.go:456 ↗
func (tape Tape) Entries() uint
ScalarScalar reports the scalar type the elements hold.
types.go:464 ↗
func (tape Tape) Scalar() Scalar
StructTapeViewTapeView 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 ↗
type TapeView struct {
    // contains filtered or unexported fields
}

Mirrors uform_tape_view_t, beside the count the call was given.

EntriesEntries reports how many entries the call wrote.
types.go:489 ↗
func (tape TapeView) Entries() uint
ScalarScalar reports the scalar type the elements were written as.
types.go:492 ↗
func (tape TapeView) Scalar() Scalar

Functions

RowValuesRowValues 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 ↗
func RowValues[Value Element](
    rows Rows,
    row uint,
) ([]Value, error)

The slice is valid only until that call.

TapeEntryTapeEntry 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 ↗
func TapeEntry[Value Element](
    tape TapeView,
    entry uint,
) ([]Value, error)