# Encoding

> UForm for Swift

Every verb runs one forward and keeps the surfaces `outputs` names, deciding which C output pointers the binding passes:

- `.embeddings`, the default, lands one pooled row per item as `Rows`;
- `.states` lands the final-norm states, one row per token each item kept after truncation, as `RowsTape`;
- `.tokenVectors` lands the token head's unit vectors, `tokenWidth` wide, on the same offsets as the states, and a model without a token head refuses it with `unsupported`;
- naming none is refused with `invalidArgument`.

```swift
let encoded = try model.textEncode(texts, outputs: [.embeddings, .states], arena: arena)
let pooled = encoded.embeddings!                  // pooled[i] is item i's row
let states = encoded.states!                      // states[i] is item i's rows, one per byte of its text
print(pooled.columns, pooled.stride, pooled.scalar, states[0].count, encoded.stats.truncated)
```

The six verbs take one input form each:

```swift
try model.textEncode(["a caption"], arena: arena)                                     // UTF-8, a byte per token
try model.imageEncode([jpeg, png], arena: arena)                                      // JPEG, PNG or WebP files
try model.imageEncodePixels([rgb], heights: [480], widths: [640], arena: arena)       // interleaved 8-bit RGB
try model.videoEncode([mp4], arena: arena)                                            // MP4 or WebM files
try model.audioEncode([wav], arena: arena)                                            // WAV, FLAC, MP3 or Opus
try model.audioEncodeSamples([samples], sampleRate: 16_000, arena: arena)             // mono Float samples
```

`EncodeConfig` carries the rest: `outputsScalar`, `f32` by default, and a `progress` handler with its `progressSteps`.
Rows of `f32`, `bf16` and `f16` read as `Float`, and `withUnsafeBytes` hands any scalar on as stored, such as to a USearch index.

```swift
let config = EncodeConfig(outputsScalar: .bf16, progress: { done, total in done < limit ? .continue : .cancel })
let narrow = try model.textEncode(texts, arena: arena, config: config)
narrow.embeddings!.withUnsafeBytes { bytes in index.add(bytes) }
```

A handler never runs on two threads at once, but may run on whichever thread advanced the work, hence `@Sendable`; returning `.cancel` stops the call with `cancelled`.

`import UForm`

## Scalar

A scalar type a model computes in and its outputs land as, numbered as USearch numbers it, carrying the C enumerator.

```swift
struct Scalar
```

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

Conforms to: `Hashable`, `Sendable`, `CustomStringConvertible`, `Equatable`

## unknown

No scalar named - an unset field.

```swift
static let unknown: Scalar
```

## f64

IEEE-754 binary64.

```swift
static let f64: Scalar
```

## f32

IEEE-754 binary32.

```swift
static let f32: Scalar
```

## bf16

The brain floating-point format.

```swift
static let bf16: Scalar
```

## f16

IEEE-754 binary16.

```swift
static let f16: Scalar
```

## e5m2

8-bit floating point with five exponent bits.

```swift
static let e5m2: Scalar
```

## e4m3

8-bit floating point with four exponent bits.

```swift
static let e4m3: Scalar
```

## e3m2

6-bit floating point with three exponent bits.

```swift
static let e3m2: Scalar
```

## e2m3

6-bit floating point with two exponent bits.

```swift
static let e2m3: Scalar
```

## i8

An 8-bit signed integer.

```swift
static let i8: Scalar
```

## i4x2

Two 4-bit signed integers per byte.

```swift
static let i4x2: Scalar
```

## i32

A 32-bit signed integer.

```swift
static let i32: Scalar
```

## u8

An 8-bit unsigned integer, the element of UTF-8 text and of encoded files.

```swift
static let u8: Scalar
```

## u4x2

Two 4-bit unsigned integers per byte.

```swift
static let u4x2: Scalar
```

## u1x8

Eight single-bit dimensions per byte.

```swift
static let u1x8: Scalar
```

## description

The kind as the library spells it.

```swift
var description: String { get }
```

Calls `uform_scalar_name`.

## init

Reads a spelling back, and nil for one no kind carries.

```swift
init?(name: String)
```

Calls `uform_scalar_from_name`.

## Rows

A run of rows a call wrote packed into its arena, one per item, read in place.

```swift
struct Rows
```

Mirrors `uform_rows_view_t`.

Rows of [`f32`](#f32), [`bf16`](#bf16) and [`f16`](#f16) read as `Float`; [`withUnsafeBytes`](#withUnsafeBytes) hands any scalar on as
stored, such as to a USearch index.

Conforms to: `RandomAccessCollection`, `BidirectionalCollection`, `Collection`, `Sequence`

## stride

Bytes between the starts of consecutive rows.

```swift
let stride: Int
```

## columns

Values per row, the model's embedding dimensions.

```swift
let columns: Int
```

## scalar

The scalar type the rows hold.

```swift
let scalar: Scalar
```

## endIndex

One past the last row.

```swift
let endIndex: Int
```

## startIndex

The first row's position, which a collection walk starts from.

```swift
var startIndex: Int { get }
```

## subscript

One row's values.

```swift
subscript(row: Int) -> Row { get }
```

## withUnsafeBytes

Lends the rows' bytes as stored to `body`, which must not keep the buffer past its return.

```swift
func withUnsafeBytes<Result, Failure>(
    _ body: (UnsafeRawBufferPointer) throws(Failure) -> Result
) throws(Failure) -> Result where Failure : Error
```

## Rows.Row

One row of an arena's values, read in place and widened to `Float`.

```swift
struct Row
```

Conforms to: `RandomAccessCollection`, `BidirectionalCollection`, `Collection`, `Sequence`

## endIndex

One past the last value in the row.

```swift
let endIndex: Int
```

## startIndex

The first value's position, which a collection walk starts from.

```swift
var startIndex: Int { get }
```

## subscript

The value at a position, widened from the row's scalar.

```swift
subscript(position: Int) -> Float { get }
```

## RowsTape

A ragged run of rows a call wrote into its arena, as many per item as the item had tokens left after truncation, read in place.

```swift
struct RowsTape
```

Mirrors `uform_rows_tape_view_t`.

Conforms to: `RandomAccessCollection`, `BidirectionalCollection`, `Collection`, `Sequence`

## rows

Every item's rows, end to end.

```swift
let rows: Rows
```

## endIndex

One past the last item.

```swift
let endIndex: Int
```

## startIndex

The first item's position, which a collection walk starts from.

```swift
var startIndex: Int { get }
```

## subscript

One item's rows, one per token it kept.

```swift
subscript(item: Int) -> Rows { get }
```

## Tape

A ragged run of [`f32`](#f32) values a call wrote into its arena, as many per item as the item asked for, read in place.

```swift
struct Tape
```

Mirrors `uform_tape_view_t`.

Conforms to: `RandomAccessCollection`, `BidirectionalCollection`, `Collection`, `Sequence`

## endIndex

One past the last item.

```swift
let endIndex: Int
```

## startIndex

The first item's position, which a collection walk starts from.

```swift
var startIndex: Int { get }
```

## subscript

One item's values.

```swift
subscript(item: Int) -> Rows.Row { get }
```

## Outputs

The surfaces an encode call keeps from its one forward, each deciding whether the binding passes the C output pointer of the same name; `.embeddings` alone by default.

```swift
struct Outputs
```

Conforms to: `OptionSet`, `Sendable`, `SetAlgebra`, `RawRepresentable`, `Equatable`, `ExpressibleByArrayLiteral`

## rawValue

The bits, one per C output pointer the set passes.

```swift
let rawValue: UInt8
```

## init

Wraps bits, one per C output pointer.

```swift
init(rawValue: UInt8)
```

## embeddings

One pooled embedding per item, read past the attention pool and the projection head.

```swift
static let embeddings: Outputs
```

## states

The final-norm states, one row per token each item kept after truncation.

```swift
static let states: Outputs
```

## tokenVectors

The token head's unit vectors, one row per token each item kept, its prefix included; a model without a token head reports [`unsupported`](https://www.unum.cloud/docs/uform/swift/errors.md#unsupported).

```swift
static let tokenVectors: Outputs
```

## EncodeStats

What an encode call reports beside its outputs.

```swift
struct EncodeStats
```

Mirrors every `uform_*_encode_stats_t`.

Conforms to: `Equatable`, `Sendable`

## truncated

Items cut at the model's context.

```swift
var truncated: Int
```

## tokens

Tokens the call encoded, after truncation: bytes of text, patches, tubes or frames.

```swift
var tokens: Int
```

## EncodeConfig

The knobs every encode verb takes beside its inputs, outputs and arena.

```swift
struct EncodeConfig
```

## outputsScalar

The scalar every output lands as.

```swift
var outputsScalar: Scalar
```

## progress

Called with items encoded.

```swift
var progress: Progress?
```

## progressSteps

Reports the call makes at most, the last at completion; zero derives to ten.

```swift
var progressSteps: Int
```

## init

Takes every knob by name; a zero [`progressSteps`](#progressSteps) derives ten.

```swift
init(outputsScalar: Scalar = .f32, progress: Progress? = nil, progressSteps: Int = 0)
```

## Encoded

What one encode call wrote into its arena, read in place until the arena's next call.

```swift
struct Encoded
```

## embeddings

One row per item, or nil when `outputs` left the embeddings out.

```swift
let embeddings: Rows?
```

## states

One run of rows per item, one row per token kept, or nil when `outputs` left the states out.

```swift
let states: RowsTape?
```

## tokenVectors

One run of token-width rows per item, one row per token kept, or nil when `outputs` left them out.

```swift
let tokenVectors: RowsTape?
```

## stats

What the call counted on the way.

```swift
let stats: EncodeStats
```

