JavaScript · Encoding

Encoding

import * as uform from 'uform';import { Rows } from 'uform';import { States } from 'uform';import { EncodeVerbs } from 'uform'; 4.0.0-rc.1 · 3ee5e534

Every verb keeps the pooled embeddings by default. Ask for the final-norm states too, one row per token kept, with the outputs flags:

const { embeddings, states, stats } = model.textEncode(texts, arena, {
  outputs: Outputs.Embeddings | Outputs.States,
  outputsScalar: Scalar.BF16,
});
const tokensOfFirst = states.item(0); // rows from states.offsets[0] up to states.offsets[1]
console.log(stats.truncated, stats.tokens);

A checkpoint with a token head also lands unit token vectors for late-interaction reranking, tokenWidth wide and sharing the states’ offsets:

const { tokenWidth } = model.describe(); // zero without a token head, which answers `unsupported`
const { tokenVectors } = model.textEncode(documents, arena, { outputs: Outputs.TokenVectors });

Images, clips and recordings arrive encoded, as JPEG, PNG, WebP, MP4, WebM, WAV, FLAC, MP3 or Opus files:

import fs from "node:fs/promises";

const images = model.imageEncode([await fs.readFile("panda.jpg")], arena);
const clips = model.videoEncode([await fs.readFile("panda.mp4")], arena);
const recordings = model.audioEncode([await fs.readFile("panda.wav")], arena);

Or decoded, as RGB or RGBA pixels with their shape, and as mono samples with their rate:

const pixels = model.imageEncodePixels([{ data: rgb, width: 640, height: 480 }], arena);
const samples = model.audioEncodeSamples([new Float32Array(16000)], 16000, arena);

API

Types

OutputsThe surfaces an encode call keeps, combined with |.
uform.ts:28 ↗
enum Outputs

The C verb fills whichever of embeddings, states and token_vectors is non-NULL, and these flags decide which it is handed.

Values

Embeddings1One pooled embedding per item.
States2The final-norm states, one row per token kept after truncation.
TokenVectors4The token head's unit vectors, one row per token kept, tokenWidth wide; unsupported without a token head.
EncodeConfigOptional per-call settings of every encode verb.
uform.ts:39 ↗
interface EncodeConfig

Fields

outputs?numberThe surfaces to keep; absent keeps Outputs.Embeddings alone.
outputsScalar?ScalarThe scalar every output lands as; absent lands f32, which a Float32Array reads.
progress?ProgressCallbackHears the items encoded so far and may stop the call by answering false; only a model's own verbs, which run on the calling thread, take one.
PixelsOne decoded image: ImageData as a canvas hands it out, or interleaved RGB or RGBA bytes with their shape.
uform.ts:51 ↗
interface Pixels

Fields

dataUint8Array | Uint8ClampedArrayRows end to end, three or four bytes per pixel.
widthnumberPixels per row.
heightnumberRows.
EncodeStatsWhat an encode call reports beside its outputs.
uform.ts:61 ↗
interface EncodeStats

Fields

truncatednumberItems cut at the model's context.
tokensnumberTokens the call encoded, after truncation.
EncodedThe surfaces one encode call kept, present as they were asked for.
uform.ts:144 ↗
interface Encoded

Fields

embeddings?RowsOne pooled embedding per item.
states?StatesThe final-norm states of every item.
tokenVectors?StatesThe token head's unit vectors of every item, sharing the states' offsets.
statsEncodeStatsTruncation and token counts.
TapeA ragged batch packed as the C interface reads it: elements end to end, and count + 1 offsets into them.
uform.ts:227 ↗
interface Tape<Elements extends Uint8Array | Float32Array>

Mirrors uform_tape_view_t.

Every verb taking an array of entries also takes a packed tape, which the Node addon lends to the call as it is when its offsets are a BigUint64Array; an array of entries is first gathered into one tape, the one copy that path makes. A Probabilities answer is itself a tape, so it trains a step's choiceTargets as it lands.

Fields

dataElements
offsetsOffsets
OffsetsThe count + 1 offsets of a tape: a BigUint64Array, which the Node addon lends as size_t without a copy, or a Uint32Array, which it widens and the WebAssembly module, whose size_t is 32 bits wide, lends as it is.
uform.ts:30 ↗
type Offsets = BigUint64Array | Uint32Array
ScalarThe scalar kinds a model computes in and its outputs land as, numbered as USearch numbers them.
uform.ts:64 ↗
enum Scalar

Values

F64&quot;f64&quot;
F32&quot;f32&quot;
BF16&quot;bf16&quot;
F16&quot;f16&quot;
E5M2&quot;e5m2&quot;
E4M3&quot;e4m3&quot;
E3M2&quot;e3m2&quot;
E2M3&quot;e2m3&quot;
I8&quot;i8&quot;
I4x2&quot;i4x2&quot;
I32&quot;i32&quot;
U8&quot;u8&quot;
U4x2&quot;u4x2&quot;
U1x8&quot;u1x8&quot;
ClassRowsRows a call landed in its arena.7
encode.ts:74 ↗
class Rows extends ArenaView

Mirrors a uform_rows_view_t output.

data is a Float32Array for f32, a Float16Array for f16 where the runtime has one, a Uint16Array for bf16, and raw bytes for the kinds JavaScript spells no element for.

RowsMade by a verb, never directly.
encode.ts:86 ↗
new Rows(landed: CompiledRows)
rowBorrows row row without copying.
encode.ts:101 ↗
row(row: number): ArrayBufferView

Binding-only.

rowsRows written.
encode.ts:76 ↗
readonly rows: number
columnsValues in each row, the model's dimensions.
encode.ts:78 ↗
readonly columns: number
strideBytes between consecutive rows.
encode.ts:80 ↗
readonly stride: number
scalarThe scalar kind the rows landed as.
encode.ts:82 ↗
readonly scalar: Scalar
dataEvery row, laid stride bytes apart.
encode.ts:96 ↗
readonly data: ArrayBufferView
ClassStatesThe final-norm states or the token vectors a call landed in its arena: rows end to end, item i spanning rows offsets[i] up to offsets[i + 1].4
encode.ts:114 ↗
class States extends ArenaView

Mirrors uform_rows_tape_view_t.

StatesMade by a verb, never directly.
encode.ts:120 ↗
new States(landed: CompiledStates)
itemBorrows the rows of item item without copying.
encode.ts:132 ↗
item(item: number): ArrayBufferView

Binding-only.

rowsEvery item's rows, end to end.
encode.ts:116 ↗
readonly rows: Rows
offsetsThe count + 1 row offsets, the last of them the total.
encode.ts:127 ↗
readonly offsets: BigUint64Array
ClassEncodeVerbsThe encode verbs every receiver of a model shares, written once.8
encode.ts:322 ↗
abstract class EncodeVerbs<Receiving extends "blocking" | "deferred">

The receiver chooses where a verb runs and how it completes, and the verb stays the same: a Model runs it on the calling thread, model.on(executor) blocks on an executor, and model.deferred(executor) submits it there and answers a Deferred. Every verb lands its outputs in arena and answers views into it.

textEncodeEncodes texts through one forward; every UTF-8 byte is a token.
encode.ts:368 ↗
textEncode(
    texts: readonly (string | Uint8Array)[] | Tape<Uint8Array>,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>

Calls uform_text_encode.

Parameters

textsreadonly (string | Uint8Array)[] | Tape<Uint8Array>Strings, UTF-8 bytes already encoded, or a tape of them.
arenaArenaThe memory the outputs land in, rewound by the call.
configEncodeConfigThe surfaces, their scalar, and a progress handler.

Returns

The outputs, viewed inside arena.
textChooseScores each item's legal choices through one forward, a softmax over them alone at the item's group's calibrated inverse temperature; a model without the byte decoder reports unsupported.
encode.ts:393 ↗
textChoose(
    texts: readonly (string | Uint8Array)[] | Tape<Uint8Array>,
    choices: readonly (string | Uint8Array)[] | Tape<Uint8Array>,
    arena: Arena,
    config: ChooseConfig = {},
): Answer<Receiving, Chosen>

Calls uform_text_choose.

Parameters

textsreadonly (string | Uint8Array)[] | Tape<Uint8Array>The states, each ending where its choice goes.
choicesreadonly (string | Uint8Array)[] | Tape<Uint8Array>Each item's legal choices, one byte each, like "yn".
arenaArenaThe memory the probabilities land in, rewound by the call.
configChooseConfigThe question types and a progress handler.

Returns

The probabilities, viewed inside arena.
imageEncodeDecodes and encodes JPEG, PNG or WebP files; the browser build reports unsupported.
encode.ts:433 ↗
imageEncode(
    images: readonly Uint8Array[] | Tape<Uint8Array>,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>

Calls uform_image_encode.

Parameters

imagesreadonly Uint8Array[] | Tape<Uint8Array>One encoded file per item, or a tape of them.
arenaArenaThe memory the outputs land in, rewound by the call.
configEncodeConfigThe surfaces, their scalar, and a progress handler.
imageEncodePixelsEncodes decoded images, resized on the executor's side.
encode.ts:454 ↗
imageEncodePixels(
    images: readonly Pixels[],
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>

Calls uform_image_encode_pixels.

Parameters

imagesreadonly Pixels[]ImageData, or RGB or RGBA bytes with their width and height.
arenaArenaThe memory the outputs land in, rewound by the call.
configEncodeConfigThe surfaces, their scalar, and a progress handler.
videoEncodeDecodes and encodes MP4 or WebM clips; the browser build reports unsupported.
encode.ts:481 ↗
videoEncode(
    videos: readonly Uint8Array[] | Tape<Uint8Array>,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>

Calls uform_video_encode.

Parameters

videosreadonly Uint8Array[] | Tape<Uint8Array>One encoded file per item, or a tape of them.
arenaArenaThe memory the outputs land in, rewound by the call.
configEncodeConfigThe surfaces, their scalar, and a progress handler.
audioEncodeDecodes and encodes WAV, FLAC, MP3 or Opus files; the browser build reports unsupported.
encode.ts:503 ↗
audioEncode(
    recordings: readonly Uint8Array[] | Tape<Uint8Array>,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>

Calls uform_audio_encode.

Parameters

recordingsreadonly Uint8Array[] | Tape<Uint8Array>One encoded file per item, or a tape of them.
arenaArenaThe memory the outputs land in, rewound by the call.
configEncodeConfigThe surfaces, their scalar, and a progress handler.
audioEncodeSamplesEncodes decoded mono recordings, resampled to the model's rate where sampleRate differs.
encode.ts:526 ↗
audioEncodeSamples(
    recordings: readonly Float32Array[] | Tape<Float32Array>,
    sampleRate: number,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>

Calls uform_audio_encode_samples.

Parameters

recordingsreadonly Float32Array[] | Tape<Float32Array>One run of samples per item, or a tape of them.
sampleRatenumberSamples per second shared by every recording.
arenaArenaThe memory the outputs land in, rewound by the call.
configEncodeConfigThe surfaces, their scalar, and a progress handler.
executorThe executor the verbs run on; absent runs them on the calling thread.
encode.ts:326 ↗
readonly executor: Executor | undefined