# Encoding

> UForm for JavaScript

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

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

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

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

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

`import * as uform from 'uform';`

`import { Rows } from 'uform';`

`import { States } from 'uform';`

`import { EncodeVerbs } from 'uform';`

## Outputs

The surfaces an encode call keeps, combined with `|`.

```typescript
enum Outputs
```

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

- `Embeddings` = `1`: One pooled embedding per item.
- `States` = `2`: The final-norm states, one row per token kept after truncation.
- `TokenVectors` = `4`: The token head's unit vectors, one row per token kept, `tokenWidth` wide; `unsupported` without a token head.

## EncodeConfig

Optional per-call settings of every encode verb.

```typescript
interface EncodeConfig
```

- `outputs?` `number`: The surfaces to keep; absent keeps [`Outputs.Embeddings`](#Embeddings) alone.
- `outputsScalar?` `Scalar`: The scalar every output lands as; absent lands `f32`, which a `Float32Array` reads.
- `progress?` `ProgressCallback`: Hears 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.

## Pixels

One decoded image: `ImageData` as a canvas hands it out, or interleaved RGB or RGBA bytes with their shape.

```typescript
interface Pixels
```

- `data` `Uint8Array | Uint8ClampedArray`: Rows end to end, three or four bytes per pixel.
- `width` `number`: Pixels per row.
- `height` `number`: Rows.

## EncodeStats

What an encode call reports beside its outputs.

```typescript
interface EncodeStats
```

- `truncated` `number`: Items cut at the model's context.
- `tokens` `number`: Tokens the call encoded, after truncation.

## Encoded

The surfaces one encode call kept, present as they were asked for.

```typescript
interface Encoded
```

- `embeddings?` `Rows`: One pooled embedding per item.
- `states?` `States`: The final-norm states of every item.
- `tokenVectors?` `States`: The token head's unit vectors of every item, sharing the states' offsets.
- `stats` `EncodeStats`: Truncation and token counts.

## Tape

A ragged batch packed as the C interface reads it: elements end to end, and `count + 1` offsets into them.

```typescript
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`](https://www.unum.cloud/docs/uform/javascript/choices.md#Probabilities) answer is itself a tape, so it
trains a step's `choiceTargets` as it lands.

- `data` `Elements`
- `offsets` `Offsets`

## Offsets

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

```typescript
type Offsets = BigUint64Array | Uint32Array
```

## Scalar

The scalar kinds a model computes in and its outputs land as, numbered as USearch numbers them.

```typescript
enum Scalar
```

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

## Rows

Rows a call landed in its arena.

```typescript
class Rows extends ArenaView
```

Mirrors a `uform_rows_view_t` output.

[`data`](https://www.unum.cloud/docs/uform/javascript/choices.md#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.

## Rows

Made by a verb, never directly.

```typescript
new Rows(landed: CompiledRows)
```

## row

Borrows row [`row`](#row) without copying.

```typescript
row(row: number): ArrayBufferView
```

Binding-only.

## rows

Rows written.

```typescript
readonly rows: number
```

## columns

Values in each row, the model's dimensions.

```typescript
readonly columns: number
```

## stride

Bytes between consecutive rows.

```typescript
readonly stride: number
```

## scalar

The scalar kind the rows landed as.

```typescript
readonly scalar: Scalar
```

## data

Every row, laid [`stride`](#stride) bytes apart.

```typescript
readonly data: ArrayBufferView
```

## States

The 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]`.

```typescript
class States extends ArenaView
```

Mirrors `uform_rows_tape_view_t`.

## States

Made by a verb, never directly.

```typescript
new States(landed: CompiledStates)
```

## item

Borrows the rows of item [`item`](https://www.unum.cloud/docs/uform/javascript/choices.md#item) without copying.

```typescript
item(item: number): ArrayBufferView
```

Binding-only.

## rows

Every item's rows, end to end.

```typescript
readonly rows: Rows
```

## offsets

The `count + 1` row offsets, the last of them the total.

```typescript
readonly offsets: BigUint64Array
```

## EncodeVerbs

The encode verbs every receiver of a model shares, written once.

```typescript
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`](https://www.unum.cloud/docs/uform/javascript/models.md#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`](https://www.unum.cloud/docs/uform/javascript/errors.md#Deferred). Every verb lands its
outputs in `arena` and answers views into it.

## textEncode

Encodes texts through one forward; every UTF-8 byte is a token.

```typescript
textEncode(
    texts: readonly (string | Uint8Array)[] | Tape<Uint8Array>,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>
```

Calls `uform_text_encode`.

- `texts`: Strings, UTF-8 bytes already encoded, or a tape of them.
- `arena`: The memory the outputs land in, rewound by the call.
- `config`: The surfaces, their scalar, and a progress handler.

## textChoose

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

```typescript
textChoose(
    texts: readonly (string | Uint8Array)[] | Tape<Uint8Array>,
    choices: readonly (string | Uint8Array)[] | Tape<Uint8Array>,
    arena: Arena,
    config: ChooseConfig = {},
): Answer<Receiving, Chosen>
```

Calls `uform_text_choose`.

- `texts`: The states, each ending where its choice goes.
- `choices`: Each item's legal choices, one byte each, like "yn".
- `arena`: The memory the probabilities land in, rewound by the call.
- `config`: The question types and a progress handler.

## imageEncode

Decodes and encodes JPEG, PNG or WebP files; the browser build reports `unsupported`.

```typescript
imageEncode(
    images: readonly Uint8Array[] | Tape<Uint8Array>,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>
```

Calls `uform_image_encode`.

- `images`: One encoded file per item, or a tape of them.
- `arena`: The memory the outputs land in, rewound by the call.
- `config`: The surfaces, their scalar, and a progress handler.

## imageEncodePixels

Encodes decoded images, resized on the executor's side.

```typescript
imageEncodePixels(
    images: readonly Pixels[],
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>
```

Calls `uform_image_encode_pixels`.

- `images`: `ImageData`, or RGB or RGBA bytes with their width and height.
- `arena`: The memory the outputs land in, rewound by the call.
- `config`: The surfaces, their scalar, and a progress handler.

## videoEncode

Decodes and encodes MP4 or WebM clips; the browser build reports `unsupported`.

```typescript
videoEncode(
    videos: readonly Uint8Array[] | Tape<Uint8Array>,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>
```

Calls `uform_video_encode`.

- `videos`: One encoded file per item, or a tape of them.
- `arena`: The memory the outputs land in, rewound by the call.
- `config`: The surfaces, their scalar, and a progress handler.

## audioEncode

Decodes and encodes WAV, FLAC, MP3 or Opus files; the browser build reports `unsupported`.

```typescript
audioEncode(
    recordings: readonly Uint8Array[] | Tape<Uint8Array>,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>
```

Calls `uform_audio_encode`.

- `recordings`: One encoded file per item, or a tape of them.
- `arena`: The memory the outputs land in, rewound by the call.
- `config`: The surfaces, their scalar, and a progress handler.

## audioEncodeSamples

Encodes decoded mono recordings, resampled to the model's rate where `sampleRate` differs.

```typescript
audioEncodeSamples(
    recordings: readonly Float32Array[] | Tape<Float32Array>,
    sampleRate: number,
    arena: Arena,
    config: EncodeConfig = {},
): Answer<Receiving, Encoded>
```

Calls `uform_audio_encode_samples`.

- `recordings`: One run of samples per item, or a tape of them.
- `sampleRate`: Samples per second shared by every recording.
- `arena`: The memory the outputs land in, rewound by the call.
- `config`: The surfaces, their scalar, and a progress handler.

## executor

The executor the verbs run on; absent runs them on the calling thread.

```typescript
readonly executor: Executor | undefined
```

