Python · Encoding

Encoding

import uformfrom uform import RowsTape, Encoded, TextEncodeStats, … 4.0.0-rc.1 · 3ee5e534

One verb per input form, each spelled as in C:

  • text_encode(texts, arena) takes UTF-8 texts, a byte per token, since models are tokenizer-free.
  • image_encode(files, arena) takes JPEG, PNG or WebP files.
  • image_encode_pixels(pixels, arena) takes C-contiguous [height, width, 3] uint8 buffers.
  • video_encode(files, arena) takes MP4 or WebM clips.
  • audio_encode(files, arena) takes WAV, FLAC, MP3 or Opus recordings.
  • audio_encode_samples(samples, sample_rate, arena) takes 1-D float32 buffers.

A batch is a sequence of separate objects, str or bytes-like for texts and bytes-like for files, gathered into one tape with one copy. A (data, offsets) tuple of buffers, the data and count + 1 size_t offsets, is lent to the library as a tape without a copy:

data = b"first documentthe second"
offsets = np.array([0, 14, 24], dtype=np.uint64)
encoded = model.text_encode((data, offsets), arena)

outputs names the surfaces a call keeps, and decides which C output pointers the binding passes:

encoded = model.text_encode(texts, arena, outputs=uform.Outputs.EMBEDDINGS | uform.Outputs.STATES)
embeddings, states, token_vectors = encoded  # an `ArenaBuffer` of (count, dimensions), a `RowsTape`, and None
offsets = np.asarray(states.offsets)  # item `i` spans rows `offsets[i]` up to `offsets[i + 1]`

Outputs.EMBEDDINGS is the default, and a surface left out answers None and costs nothing past the final norm. Outputs.TOKEN_VECTORS keeps the token head’s unit vectors, a RowsTape of (tokens, token_width) on the states’ offsets, for late-interaction reranking; a model whose describe().token_width is zero raises UnsupportedError. outputs_scalar picks the scalar every output lands as, Scalar.F32 by default; Scalar.BF16 outputs export as raw bytes, a (count, row_bytes) run of 'B'. stats=uform.TextEncodeStats() and its siblings are overwritten with the truncated items and the encoded tokens. progress=handler is called with (completed, total) items, and a handler returning False stops the call, which raises uform.CancelledError; one that raises stops it too, and its own exception reaches the caller.

API

Types

ScalarA scalar type a model computes in or its outputs land as, valued as uform_scalar_t.
__init__.py:72 ↗
class Scalar(IntEnum)

Values

F641
F322
BF163
F164
E5M25
E4M36
E3M27
E2M38
I810
I4X211
I3212
U820
U4X221
U1X822
OutputsWhich surfaces an encoding keeps, deciding which of the C output pointers the binding passes.
__init__.py:177 ↗
class Outputs(IntFlag)

Values

EMBEDDINGS1
STATES2
TOKEN_VECTORS4
ClassRowsTapeA ragged run of rows in an Arena, one per token each item kept or one value per choice, split by offsets.1
lib.pyi:368 ↗
class RowsTape(ArenaBuffer)
offsets(count + 1,) Apache Arrow offsets into the rows.
lib.pyi:373 ↗
offsets: ArenaBuffer
ClassEncodedWhat an encoding wrote into its arena, unpacking as embeddings, states, token_vectors = encoded.3
lib.pyi:460 ↗
class Encoded(structseq[Any], tuple[ArenaBuffer | None, RowsTape | None, RowsTape | None])
embeddingsThe (count, dimensions) pooled embeddings, or None when not asked for.
lib.pyi:467 ↗
embeddings: ArenaBuffer | None
statesThe (tokens, dimensions) final-norm states and their offsets, or None when not asked for.
lib.pyi:472 ↗
states: RowsTape | None
token_vectorsThe (tokens, token_width) unit vectors of the token head, offset as the states, or None.
lib.pyi:477 ↗
token_vectors: RowsTape | None
ClassTextEncodeStatsWhat text_encode and text_choose report, overwritten by every call it is passed to.2
lib.pyi:494 ↗
class TextEncodeStats
truncatedItems cut at the model's context.
lib.pyi:485 ↗

Inherited from _EncodeStats

truncated: int
tokensTokens the call encoded, after truncation.
lib.pyi:490 ↗

Inherited from _EncodeStats

tokens: int
ClassImageEncodeStatsWhat image_encode and image_encode_pixels report, overwritten by every call it is passed to.2
lib.pyi:498 ↗
class ImageEncodeStats
truncatedItems cut at the model's context.
lib.pyi:485 ↗

Inherited from _EncodeStats

truncated: int
tokensTokens the call encoded, after truncation.
lib.pyi:490 ↗

Inherited from _EncodeStats

tokens: int
ClassVideoEncodeStatsWhat video_encode reports, overwritten by every call it is passed to.2
lib.pyi:502 ↗
class VideoEncodeStats
truncatedItems cut at the model's context.
lib.pyi:485 ↗

Inherited from _EncodeStats

truncated: int
tokensTokens the call encoded, after truncation.
lib.pyi:490 ↗

Inherited from _EncodeStats

tokens: int
ClassAudioEncodeStatsWhat audio_encode and audio_encode_samples report, overwritten by every call it is passed to.2
lib.pyi:506 ↗
class AudioEncodeStats
truncatedItems cut at the model's context.
lib.pyi:485 ↗

Inherited from _EncodeStats

truncated: int
tokensTokens the call encoded, after truncation.
lib.pyi:490 ↗

Inherited from _EncodeStats

tokens: int