type ProgressCallback func(completed, total uint) bool It never runs on two threads at once, and it must not panic.
Ask your coding agent about this page, or add the docs server to it
curl -s 'https://www.unum.cloud/docs/uform/go/receivers-and-deferred-calls.md' | claude -p "How would this fit my code?"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.
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.
ProgressCallbackProgressCallback 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.type ProgressCallback func(completed, total uint) bool It never runs on two threads at once, and it must not panic.
DeferredDeferred 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.3type 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.
CloseClose blocks until the call is final, releases its pins, and reports the call's failure.func (deferred *Deferred[Result]) Close() error It is idempotent and safe after Wait.
StatusStatus reports StatusDeferred until the call is final, then its final status, without blocking.func (deferred *Deferred[Result]) Status() Status WaitWait blocks until the call is final, releases its pins, then hands back its result, the same every time it is asked.func (deferred *Deferred[Result]) Wait() (Result, error) ModelDeferredOnModelDeferredOn is the receiver whose verbs submit their calls to an executor and answer a Deferred, offering every verb of ModelOn with the same arguments.8type 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.
AudioEncodeAudioEncode submits ModelOn.AudioEncode.func (on ModelDeferredOn) AudioEncode(
recordings Tape,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *AudioEncodeConfig,
) (*Deferred[Encoded], error) AudioEncodeSamplesAudioEncodeSamples submits ModelOn.AudioEncodeSamples.func (on ModelDeferredOn) AudioEncodeSamples(
samples Tape,
sampleRate uint32,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *AudioEncodeConfig,
) (*Deferred[Encoded], error) DescribeDescribe submits ModelOn.Describe.func (on ModelDeferredOn) Describe() (*Deferred[ModelDescription], error) ImageEncodeImageEncode submits ModelOn.ImageEncode.func (on ModelDeferredOn) ImageEncode(
images Tape,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *ImageEncodeConfig,
) (*Deferred[Encoded], error) ImageEncodePixelsImageEncodePixels submits ModelOn.ImageEncodePixels.func (on ModelDeferredOn) ImageEncodePixels(
pixels Tape,
heights, widths []uint32,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *ImageEncodeConfig,
) (*Deferred[Encoded], error) TextChooseTextChoose submits ModelOn.TextChoose.func (on ModelDeferredOn) TextChoose(
texts, choices Tape,
arena *Arena,
config *TextChooseConfig,
) (*Deferred[TapeView], error) TextEncodeTextEncode submits ModelOn.TextEncode.func (on ModelDeferredOn) TextEncode(
texts Tape,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *TextEncodeConfig,
) (*Deferred[Encoded], error) VideoEncodeVideoEncode submits ModelOn.VideoEncode.func (on ModelDeferredOn) VideoEncode(
videos Tape,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *VideoEncodeConfig,
) (*Deferred[Encoded], error) ModelOnModelOn 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.10AudioEncodeAudioEncode decodes and encodes WAV, FLAC, MP3 or Opus recordings, resampled to mono.func (on ModelOn) AudioEncode(
recordings Tape,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *AudioEncodeConfig,
) (Encoded, error) Calls uform_audio_encode.
AudioEncodeSamplesAudioEncodeSamples encodes decoded mono ScalarF32 recordings sharing one sampleRate, resampled to the model's rate where it differs.func (on ModelOn) AudioEncodeSamples(
samples Tape,
sampleRate uint32,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *AudioEncodeConfig,
) (Encoded, error) Calls uform_audio_encode_samples.
DeferredDeferred is the receiver whose verbs submit their calls to executor, each answering a Deferred from the same arguments.DescribeDescribe reports the model's geometry, placement, classes and heads in one call.ImageEncodeImageEncode decodes and encodes JPEG, PNG or WebP files; an undecodable file fails the call with StatusMalformed.func (on ModelOn) ImageEncode(
images Tape,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *ImageEncodeConfig,
) (Encoded, error) Calls uform_image_encode.
ImageEncodePixelsImageEncodePixels 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.func (on ModelOn) ImageEncodePixels(
pixels Tape,
heights, widths []uint32,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *ImageEncodeConfig,
) (Encoded, error) Calls uform_image_encode_pixels.
OnOn is the receiver whose verbs block on executor, failing StatusContended while another call holds it.TextChooseTextChoose 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.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.
TextEncodeTextEncode 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.func (on ModelOn) TextEncode(
texts Tape,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *TextEncodeConfig,
) (Encoded, error) Calls uform_text_encode.
VideoEncodeVideoEncode decodes and encodes MP4 or WebM clips.func (on ModelOn) VideoEncode(
videos Tape,
outputs EncodeOutputs,
outputsScalar Scalar,
arena *Arena,
config *VideoEncodeConfig,
) (Encoded, error) Calls uform_video_encode.
MoodMood is what an executor's threads do between a drain and its next call.2type Mood int32 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. |
MoodFromNameMoodFromName reads a mood back from the spelling Mood.String renders.func MoodFromName(name string) (Mood, error) StringString renders the mood as the library spells it.func (mood Mood) String() string TrainerDeferredOnTrainerDeferredOn is the receiver whose verbs submit their calls and answer a Deferred, offering every verb of TrainerOn with the same arguments.1type TrainerDeferredOn struct {
// contains filtered or unexported fields
} Binding-only.
*× 2Save submits TrainerOn.Save.func (on TrainerDeferredOn) Save(
path string,
config *TrainerSaveConfig,
) (*Deferred[struct{}], error) | Symbol |
|---|
Save |
Step |