# Receivers and Deferred Calls

> The receiver decides where a verb runs and how it completes, while the verb and its arguments stay the same.

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

The receiver decides where a verb runs and how it completes, while the verb and its arguments stay the same:

- `model.TextEncode(..)` runs on the calling goroutine, through the `ModelOn` a `Model` embeds.
- `model.On(executor).TextEncode(..)` blocks on the executor, failing `StatusContended` while another call holds it.
- `model.Deferred(executor).TextEncode(..)` submits the call and answers a `*Deferred[Encoded]`.

`ModelOn` and `ModelDeferredOn` offer every model verb with the same arguments, as `TrainerOn` and `TrainerDeferredOn` do for a trainer. A blocking verb answers `(T, error)` directly, so nothing about asynchrony reaches a caller who never asks for it.

```go
call, err := model.Deferred(executor).TextEncode(texts, uform.EncodeOutputsEmbeddings, uform.ScalarF32, arena, nil)
if err != nil {
	return err // refused before anything was submitted
}
if call.Status() == uform.StatusDeferred {
	// other work
}
encoded, err := call.Wait()
```

A deferred verb answers `(*Deferred[T], error)`, so a refusal the binding catches arrives at once, as a blocking one would, and nothing is submitted. `Status` answers `StatusDeferred` until the call is final, then its final status, and `Wait` blocks the goroutine in cgo until then, answering the same result every time. A goroutine calling `Wait` overlaps the wait, and a `select` reaches it through a channel that goroutine closes; the package starts no goroutine of its own. `Close` waits the same way and drops the result, and a `Deferred` dropped without either is released by the collector once final, never blocking it.

The library reads a deferred call’s inputs after the verb returns, so the tapes, slices, handlers and reports a deferred verb names must not change until the call is final; the binding pins every one of them. An executor without threads of its own may run a deferred call at submission, final when the verb returns.

## API

### Types

#### `ProgressCallback`

ProgressCallback is called with the units a long call finished so far and the units it finishes unless stopped; false stops it, and the call fails with [StatusCancelled](https://www.unum.cloud/docs/uform/go/errors.md#StatusCancelled).

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

```go
type ProgressCallback func(completed, total uint) bool
```

It never runs on two threads at once, and it must not panic.

#### Struct `Deferred`

Deferred is a submitted call, which holds its outcome and inputs until it is final, owned by the one goroutine that polls or waits on it.3

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

```go
type Deferred[Result any] struct {
    // contains filtered or unexported fields
}
```

Status polls without blocking, Wait blocks until the call is final and hands back its result, and Close waits the same way and drops it. A Deferred dropped without Wait or Close is released by the collector once the call is final, never blocking on it: until then every later collection checks it again.

##### `Close`

Close blocks until the call is final, releases its pins, and reports the call's failure.

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

```go
func (deferred *Deferred[Result]) Close() error
```

It is idempotent and safe after Wait.

##### `Status`

Status reports StatusDeferred until the call is final, then its final status, without blocking.

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

```go
func (deferred *Deferred[Result]) Status() Status
```

##### `Wait`

Wait blocks until the call is final, releases its pins, then hands back its result, the same every time it is asked.

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

```go
func (deferred *Deferred[Result]) Wait() (Result, error)
```

#### Struct `ModelDeferredOn`

ModelDeferredOn is the receiver whose verbs submit their calls to an executor and answer a Deferred, offering every verb of ModelOn with the same arguments.8

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

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

Binding-only.

The library reads a deferred call's inputs until the call is final, so the tapes and slices a verb is handed must not change until then; its arena stays in use until then, as with a blocking verb.

##### `AudioEncode`

AudioEncode submits ModelOn.AudioEncode.

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

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

##### `AudioEncodeSamples`

AudioEncodeSamples submits ModelOn.AudioEncodeSamples.

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

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

##### `Describe`

Describe submits ModelOn.Describe.

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

```go
func (on ModelDeferredOn) Describe() (*Deferred[ModelDescription], error)
```

##### `ImageEncode`

ImageEncode submits ModelOn.ImageEncode.

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

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

##### `ImageEncodePixels`

ImageEncodePixels submits ModelOn.ImageEncodePixels.

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

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

##### `TextChoose`

TextChoose submits ModelOn.TextChoose.

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

```go
func (on ModelDeferredOn) TextChoose(
    texts, choices Tape,
    arena *Arena,
    config *TextChooseConfig,
) (*Deferred[TapeView], error)
```

##### `TextEncode`

TextEncode submits ModelOn.TextEncode.

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

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

##### `VideoEncode`

VideoEncode submits ModelOn.VideoEncode.

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

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

#### Struct `ModelOn`

ModelOn is the receiver whose verbs block until their call is final: on the calling goroutine when embedded in a Model, and on an executor when Model.On makes it.10

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

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

Binding-only.

##### `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)

```go
func (on ModelOn) 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)

```go
func (on ModelOn) 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)

```go
func (on ModelOn) 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)

```go
func (on ModelOn) 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)

```go
func (on ModelOn) 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)

```go
func (on ModelOn) 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)

```go
func (on ModelOn) 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)

```go
func (on ModelOn) 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)

```go
func (on ModelOn) 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)

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

Calls uform_video_encode.

#### Enum `Mood`

Mood is what an executor's threads do between a drain and its next call.2

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

```go
type Mood int32
```

#### Values

| `MoodParked` | | MoodParked sleeps in the kernel until the next call, freeing the cores. |
| - | - | - |
| `MoodSpinning` | | MoodSpinning keeps polling, trading the cores for the lowest wake-up latency. |

##### `MoodFromName`

MoodFromName reads a mood back from the spelling [Mood.String](https://www.unum.cloud/docs/uform/go/errors.md#String) renders.

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

```go
func MoodFromName(name string) (Mood, error)
```

##### `String`

String renders the mood as the library spells it.

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

```go
func (mood Mood) String() string
```

#### Struct `TrainerDeferredOn`

TrainerDeferredOn is the receiver whose verbs submit their calls and answer a Deferred, offering every verb of TrainerOn with the same arguments.1

[train.go:667 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/train.go#L667)

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

Binding-only.

##### `*`

× 2 Save submits TrainerOn.Save.

[train.go:711 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/train.go#L711)

```go
func (on TrainerDeferredOn) Save(
    path string,
    config *TrainerSaveConfig,
) (*Deferred[struct{}], error)
```

| Symbol |
| - |
| `Save` |
| `Step` |
