# Models

> ModelLoadRam(path, config) and ModelLoadVram(path, config) copy a safetensors checkpoint into host memory or onto GPUs, and ModelLoadRamBuffer and ModelLoadVramBuffer take the checkpoint's bytes instead.

`import "github.com/unum-cloud/uform"` 4.0.0-rc.1 · c58749fe

`ModelLoadRam(path, config)` and `ModelLoadVram(path, config)` copy a safetensors checkpoint into host memory or onto GPUs, and `ModelLoadRamBuffer` and `ModelLoadVramBuffer` take the checkpoint’s bytes instead. A nil config loads on the calling goroutine with BFloat16 blocks, and the config reaches the executor, the placement and its class ceiling, `ScalarE4M3` quantization and a progress handler:

```go
model, err := uform.ModelLoadVram("model.safetensors", &uform.ModelLoadVramConfig{
	Executor:     executor,
	Devices:      []int32{0},
	Quantization: uform.ScalarE4M3,
})
description, err := model.Describe() // geometry, placement, classes and heads
```

A description reports the `CPUClass` or `GPUClass` the model dispatched as, and the heads the checkpoint carries: a nonzero `TokenWidth` for token vectors, and a nonzero `ChoiceTemperatureGroups` for choices. Loads always block, and `Close` waits for every deferred call naming the model.

## API

### Types

#### `ModelDescription`

ModelDescription is what a loaded model reports about itself.

[types.go:29 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L29)

```go
type ModelDescription struct {
    Dimensions uint
    Context uint
    Quantization Scalar
    MemoryUsage uint
    CPUClass CPUClass
    GPUClass GPUClass
    TokenWidth uint
    ChoiceTemperatureGroups uint
}
```

Mirrors uform_model_description_t field for field, so the library writes it in place.

#### Fields

| `Dimensions` | `uint` | Dimensions is the scalars per embedding row; Matryoshka prefixes of it are embeddings too. |
| - | - | - |
| `Context` | `uint` | Context is the tokens one item may span before it is truncated: bytes of text, patches of an image, tubes of a clip, frames of a recording. |
| `Quantization` | `Scalar` | Quantization is the scalar the transformer blocks run in. |
| `MemoryUsage` | `uint` | MemoryUsage is the bytes the model holds; activations are the executor's. |
| `CPUClass` | `CPUClass` | CPUClass is the class a RAM model's tower was packed for, CPUClassUnknown for a VRAM model. |
| `GPUClass` | `GPUClass` | GPUClass is the class every replica of a VRAM model runs as, GPUClassUnknown for a RAM model. |
| `TokenWidth` | `uint` | TokenWidth is the scalars per token vector, zero when the checkpoint carries no token head. |
| `ChoiceTemperatureGroups` | `uint` | ChoiceTemperatureGroups counts the question types the choice temperatures were calibrated over: zero without the byte decoder, one when uncalibrated. |

#### `ModelLoadRamConfig`

ModelLoadRamConfig is how ModelLoadRam and ModelLoadRamBuffer copy a checkpoint into host memory; nil derives every field.

[types.go:54 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L54)

```go
type ModelLoadRamConfig struct {
    Executor *Executor
    MemoryDomains []int32
    CPUClassCeiling CPUClass
    Quantization Scalar
    Progress ProgressCallback
    ProgressSteps uint
}
```

Mirrors uform_model_load_ram_t and uform_model_load_ram_buffer_t beside their path or buffer.

#### Fields

| `Executor` | `*Executor` | Executor is the one the load runs on; nil loads on the calling goroutine. |
| - | - | - |
| `MemoryDomains` | `[]int32` | MemoryDomains are the NUMA nodes to place the weights in; empty derives every node. |
| `CPUClassCeiling` | `CPUClass` | CPUClassCeiling is the newest class to pack and dispatch as; zero sets no ceiling. |
| `Quantization` | `Scalar` | Quantization is the scalar the transformer blocks run in: ScalarBF16 as the checkpoint stores it, or ScalarE4M3 quantized at load; zero keeps ScalarBF16. |
| `Progress` | `ProgressCallback` | Progress hears the tensors placed and may stop the load, which then creates no model. |
| `ProgressSteps` | `uint` | ProgressSteps is the reports the load makes at most; zero derives ten. |

#### `ModelLoadVramConfig`

ModelLoadVramConfig is how ModelLoadVram and ModelLoadVramBuffer copy a checkpoint onto GPUs; nil derives every field.

[types.go:74 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L74)

```go
type ModelLoadVramConfig struct {
    Executor *Executor
    Devices []int32
    GPUClassCeiling GPUClass
    Quantization Scalar
    Progress ProgressCallback
    ProgressSteps uint
}
```

Mirrors uform_model_load_vram_t and uform_model_load_vram_buffer_t beside their path or buffer.

#### Fields

| `Executor` | `*Executor` | Executor is the one the upload runs on, one stream per device; nil uploads on the calling goroutine. |
| - | - | - |
| `Devices` | `[]int32` | Devices are the ordinals GPUDevices lists, each holding a replica; empty places one on every device of the executor. |
| `GPUClassCeiling` | `GPUClass` | GPUClassCeiling is the newest class every device dispatches as; zero sets no ceiling. |
| `Quantization` | `Scalar` | Quantization is the scalar the transformer blocks run in, as for ModelLoadRamConfig. |
| `Progress` | `ProgressCallback` | Progress hears the tensors placed and may stop the load, which then creates no model. |
| `ProgressSteps` | `uint` | ProgressSteps is the reports the load makes at most; zero derives ten. |

#### `Residency`

Residency is where an arena's outputs live, as the executor that made it decided.

[types.go:564 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L564)

```go
type Residency int32
```

#### Values

| `ResidencyUnknown` | | ResidencyUnknown names no residency. |
| - | - | - |
| `ResidencyRam` | | ResidencyRam is host pages, which Go code reads directly. |
| `ResidencyVram` | | ResidencyVram is one GPU's memory, which Go code reads only where HostAccess lets it. |

#### Struct `Model`

Model is a loaded encoder: its weights, their packed copies, and on a GPU the session its attention runs on, shared by every executor of its residency.15

[model.go:111 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L111)

```go
type Model struct {
    ModelOn
    // contains filtered or unexported fields
}
```

Its embedded ModelOn runs every verb on the calling goroutine.

Mirrors uform_model_t; Close calls uform_model_free.

##### `ModelLoadRam`

ModelLoadRam copies a safetensors checkpoint file into host memory and packs it for the CPU.

[model.go:130 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L130)

```go
func ModelLoadRam(
    path string,
    config *ModelLoadRamConfig,
) (*Model, error)
```

The checkpoint carries the model's geometry.

Calls uform_model_load_ram, blocking.

##### `ModelLoadRamBuffer`

ModelLoadRamBuffer copies a checkpoint held in memory into host memory and packs it for the CPU; nothing of buffer is kept.

[model.go:155 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L155)

```go
func ModelLoadRamBuffer(
    buffer []byte,
    config *ModelLoadRamConfig,
) (*Model, error)
```

Calls uform_model_load_ram_buffer, blocking.

##### `ModelLoadVram`

ModelLoadVram copies a checkpoint file onto the named GPUs and packs it there, replicated on each, since a model is small next to the activations it serves.

[model.go:180 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L180)

```go
func ModelLoadVram(
    path string,
    config *ModelLoadVramConfig,
) (*Model, error)
```

Calls uform_model_load_vram, blocking.

##### `ModelLoadVramBuffer`

ModelLoadVramBuffer copies a checkpoint held in memory onto the named GPUs and packs it there.

[model.go:204 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L204)

```go
func ModelLoadVramBuffer(
    buffer []byte,
    config *ModelLoadVramConfig,
) (*Model, error)
```

Calls uform_model_load_vram_buffer, blocking.

##### `Close`

Close frees the model, waiting for every deferred call naming it.

[model.go:227 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L227)

```go
func (model *Model) Close() error
```

It is idempotent, and a closed model refuses every later verb with StatusUninitialized.

##### `AudioEncode`

AudioEncode decodes and encodes WAV, FLAC, MP3 or Opus recordings, resampled to mono.

[model.go:276 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L276)

Inherited from `ModelOn`

```go
func (on Model) AudioEncode(
    recordings Tape,
    outputs EncodeOutputs,
    outputsScalar Scalar,
    arena *Arena,
    config *AudioEncodeConfig,
) (Encoded, error)
```

Calls uform_audio_encode.

##### `AudioEncodeSamples`

AudioEncodeSamples encodes decoded mono ScalarF32 recordings sharing one sampleRate, resampled to the model's rate where it differs.

[model.go:293 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L293)

Inherited from `ModelOn`

```go
func (on Model) AudioEncodeSamples(
    samples Tape,
    sampleRate uint32,
    outputs EncodeOutputs,
    outputsScalar Scalar,
    arena *Arena,
    config *AudioEncodeConfig,
) (Encoded, error)
```

Calls uform_audio_encode_samples.

##### `Deferred`

Deferred is the receiver whose verbs submit their calls to executor, each answering a Deferred from the same arguments.

[model.go:270 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L270)

Inherited from `ModelOn`

```go
func (on Model) Deferred(executor *Executor) ModelDeferredOn
```

Binding-only.

##### `Describe`

Describe reports the model's geometry, placement, classes and heads in one call.

[model.go:278 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L278)

Inherited from `ModelOn`

```go
func (on Model) Describe() (ModelDescription, error)
```

Calls uform_model_describe.

##### `ImageEncode`

ImageEncode decodes and encodes JPEG, PNG or WebP files; an undecodable file fails the call with StatusMalformed.

[model.go:227 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L227)

Inherited from `ModelOn`

```go
func (on Model) ImageEncode(
    images Tape,
    outputs EncodeOutputs,
    outputsScalar Scalar,
    arena *Arena,
    config *ImageEncodeConfig,
) (Encoded, error)
```

Calls uform_image_encode.

##### `ImageEncodePixels`

ImageEncodePixels encodes decoded images of interleaved 8-bit RGB, item i holding heights\[i] rows of widths\[i] pixels; an item of another length fails with StatusShapeMismatch.

[model.go:244 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L244)

Inherited from `ModelOn`

```go
func (on Model) ImageEncodePixels(
    pixels Tape,
    heights, widths []uint32,
    outputs EncodeOutputs,
    outputsScalar Scalar,
    arena *Arena,
    config *ImageEncodeConfig,
) (Encoded, error)
```

Calls uform_image_encode_pixels.

##### `On`

On is the receiver whose verbs block on executor, failing StatusContended while another call holds it.

[model.go:263 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L263)

Inherited from `ModelOn`

```go
func (on Model) On(executor *Executor) ModelOn
```

Binding-only.

##### `TextChoose`

TextChoose scores each item's legal choices, one byte each, after its UTF-8 state in texts, through one forward, answering one ScalarF32 probability per choice in arena, a softmax over the item's choices alone.

[model.go:212 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L212)

Inherited from `ModelOn`

```go
func (on Model) TextChoose(
    texts, choices Tape,
    arena *Arena,
    config *TextChooseConfig,
) (TapeView, error)
```

A model without the byte decoder fails it with StatusUnsupported.

Calls uform_text_choose.

##### `TextEncode`

TextEncode encodes UTF-8 texts through one forward, a token per byte, since models are tokenizer-free, keeping the surfaces outputs names as outputsScalar in arena.

[model.go:194 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L194)

Inherited from `ModelOn`

```go
func (on Model) TextEncode(
    texts Tape,
    outputs EncodeOutputs,
    outputsScalar Scalar,
    arena *Arena,
    config *TextEncodeConfig,
) (Encoded, error)
```

Calls uform_text_encode.

##### `VideoEncode`

VideoEncode decodes and encodes MP4 or WebM clips.

[model.go:260 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/model.go#L260)

Inherited from `ModelOn`

```go
func (on Model) VideoEncode(
    videos Tape,
    outputs EncodeOutputs,
    outputsScalar Scalar,
    arena *Arena,
    config *VideoEncodeConfig,
) (Encoded, error)
```

Calls uform_video_encode.
