# Executors

> An executor owns host threads or device streams, the activation scratch, and the pinned buffers staging passes through, grown to a high-water mark.

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

An executor owns host threads or device streams, the activation scratch, and the pinned buffers staging passes through, grown to a high-water mark. One executor serves any number of models of its residency.

```go
threads, err := uform.ExecutorForRam(nil, uform.CPUClassUnknown, 8)                  // eight threads, where zero takes 7/8 of the cores
nearby, err := uform.ExecutorForRam([]int32{1}, uform.CPUClassUnknown, 0)            // threads spawned near NUMA node 1
serial, err := uform.ExecutorForRam(nil, uform.CPUClassSerial, 0)                    // dispatches as the portable floor
streams, err := uform.ExecutorForVram([]int32{0, 1}, uform.GPUClassUnknown, nil, uform.CPUClassUnknown, nil, 0)
var local *uform.Executor                                                            // owns nothing, runs on the calling goroutine

err = threads.Drain(uform.MoodSpinning) // waits until no call holds it, then keeps spinning
```

The class arguments are ceilings: calls dispatch as the best class under them the hardware runs, and the zero class sets none. Naming one memory domain spawns the threads near it, while none or several interleave them across the machine. `ExecutorForVram` opens one stream per device, or adopts the caller’s streams, one per named device, and `Close` destroys them; naming no devices drives every listed device of the first vendor with a known class. `uform.GPUDevices(devices)` fills a slice of `GPUDevice` and answers how many GPUs the process can drive, and a device’s position there is the ordinal `ExecutorForVram` and `ModelLoadVram` name it by. An executor runs one call at a time, so two goroutines that genuinely overlap take one apiece. `Close` waits until no call holds the executor, and a closed one refuses every later verb with `StatusUninitialized`.

## API

### Types

#### `MeshDescription`

MeshDescription is what a mesh reports about itself.

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

```go
type MeshDescription struct {
    GPUsPerHostDomain      uint
    GPUsPerMultimemDomain  uint
    GPUsPerColocatedDomain uint
    HostDomainIndex        uint
    FirstGPUIndex          uint
    MultimemTier           MultimemTier
    GPUClass               GPUClass
}
```

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

#### `MeshOptions`

MeshOptions is where this process sits among the processes of a run, and how they meet.

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

```go
type MeshOptions struct {
    HostDomainIndex uint
    HostDomainCount uint
    GPUsPerMultimemDomain uint
    Directory string
    Job string
    TimeoutSeconds float64
}
```

Mirrors uform_mesh_options_t.

#### Fields

| `HostDomainIndex` | `uint` | HostDomainIndex is this process's index among HostDomainCount. |
| - | - | - |
| `HostDomainCount` | `uint` | HostDomainCount is the processes in the run; zero derives one. |
| `GPUsPerMultimemDomain` | `uint` | GPUsPerMultimemDomain is the GPUs one NVLink multicast domain spans; zero derives the host. |
| `Directory` | `string` | Directory is the path every host sees, where processes exchange their facts. |
| `Job` | `string` | Job is the run's unique name, naming one directory inside Directory. |
| `TimeoutSeconds` | `float64` | TimeoutSeconds is how long any wait for a peer lasts; zero derives 120. |

#### Enum `CPUClass`

CPUClass is a CPU family UForm ships tuned variants for, numbered as USearch numbers it; a RAM placement or executor may cap dispatch at one, and the zero value sets no ceiling.2

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

```go
type CPUClass int32
```

#### Values

| `CPUClassUnknown` | | CPUClassUnknown sets no ceiling, so dispatch may pick any class the machine runs. |
| - | - | - |
| `CPUClassSerial` | | CPUClassSerial is the portable floor, with no SIMD assumptions. |
| `CPUClassMonoversioned` | | CPUClassMonoversioned is the one class of a monoversioned build. |
| `CPUClassIntelHaswell` | | CPUClassIntelHaswell is Haswell-class x86: AVX2 and FMA. |
| `CPUClassIntelSierra` | | CPUClassIntelSierra is Sierra Forest-class x86: AVX-VNNI without AVX-512. |
| `CPUClassIntelSkylake` | | CPUClassIntelSkylake is Skylake-class x86: the AVX-512 foundation. |
| `CPUClassIntelIcelake` | | CPUClassIntelIcelake is Icelake-class x86: AVX-512 VNNI and VPOPCNT. |
| `CPUClassAMDGenoa` | | CPUClassAMDGenoa is Genoa-class x86: native bf16 dot products over AVX-512. |
| `CPUClassIntelSapphire` | | CPUClassIntelSapphire is Sapphire Rapids-class x86: AVX-512 FP16 and the AMX tiles. |
| `CPUClassIntelGranite` | | CPUClassIntelGranite is Granite Rapids-class x86: the AMX-FP16 tiles. |
| `CPUClassIntelDiamond` | | CPUClassIntelDiamond is Diamond Rapids-class x86: AVX10.2's FP8 conversions. |
| `CPUClassAWSG2` | | CPUClassAWSG2 is Graviton2-class Arm: the bare NEON floor. |
| `CPUClassAppleM2` | | CPUClassAppleM2 is Armv8.6 NEON with dot products and bf16. |
| `CPUClassAppleM4` | | CPUClassAppleM4 is Apple M4-class Arm: the SME outer-product tiles. |
| `CPUClassAWSG5` | | CPUClassAWSG5 is Graviton5-class Arm: Armv9.2. |
| `CPUClassNVIDIAVera` | | CPUClassNVIDIAVera is Vera-class Arm: FP8 dot products. |
| `CPUClassAlibabaC930` | | CPUClassAlibabaC930 is Alibaba C930-class RISC-V: RVA23 vectors. |
| `CPUClassIBMPower10` | | CPUClassIBMPower10 is IBM POWER10-class Power: VSX vectors and MMA accumulators. |
| `CPUClassWasm32V128` | | CPUClassWasm32V128 is WebAssembly with fixed-width simd128. |
| `CPUClassWasm64Extended` | | CPUClassWasm64Extended is WebAssembly with relaxed SIMD, threads and 64-bit addressing. |

##### `CPUClassFromName`

CPUClassFromName reads a CPU class back from the spelling [CPUClass.String](https://www.unum.cloud/docs/uform/go/errors.md#String) renders.

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

```go
func CPUClassFromName(name string) (CPUClass, error)
```

##### `*String`

× 2 String renders the class as the library spells it.

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

```go
func (class CPUClass) String() string
```

#### Struct `Executor`

Executor owns the host threads or device streams a call runs on, the activation scratch, and the pinned buffers staging passes through, apart from any model.4

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

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

One executor serves any number of models of its residency, while a model owns only its weights, which is what lets one pool serve several models and keeps the cost of spawning visible at the line that spawns it. Its kind is fixed by the constructor that produced it.

A nil \*Executor is the null executor: it owns nothing, and every call handed it runs on the calling goroutine. One call holds an executor at a time; a second call finding it held finishes StatusContended, and nothing queues.

##### `ExecutorForRam`

ExecutorForRam spawns host threads for RAM-resident models near the one memory domain memoryDomains names, or interleaved across the machine for none or several; the list is the one the models were loaded with, and an empty list derives it from the machine.

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

```go
func ExecutorForRam(
    memoryDomains []int32,
    cpuClassCeiling CPUClass,
    threads uint,
) (*Executor, error)
```

Zero threads takes seven eighths of the cores those domains carry. A cpuClassCeiling intersects with what the machine runs, so a class the machine lacks narrows rather than refuses.

##### `ExecutorForVram`

ExecutorForVram opens one submission stream on every named device, all driven by the one host thread that calls the verbs, beside host threads on memoryDomains for the decoding the devices leave to the host.

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

```go
func ExecutorForVram(
    devices []int32,
    gpuClassCeiling GPUClass,
    memoryDomains []int32,
    cpuClassCeiling CPUClass,
    streams []unsafe.Pointer,
    scratchLimit uint,
) (*Executor, error)
```

Streams is empty, or holds one stream per device in devices order: a CUDA or HIP stream becomes the executor's, which synchronizes and destroys it; a Metal queue is retained, the executor releasing its own reference while the caller keeps and releases theirs. Zero scratchLimit lets scratch grow as batches ask.

##### `Close`

Close waits out the call holding the executor, then releases its threads, rings or streams; every arena it made must be closed first, or it fails with [StatusContended](https://www.unum.cloud/docs/uform/go/errors.md#StatusContended) and frees nothing, so a later Close retries.

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

```go
func (executor *Executor) Close() error
```

It is idempotent; a closed executor must not be used again.

##### `Drain`

Drain waits until no call holds this executor, then adopts mood until the next call; it only waits and never runs a call itself.

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

```go
func (executor *Executor) Drain(mood Mood) error
```

A failure is the executor's own, such as a lost device, never one of a drained call.

#### Enum `GPUClass`

GPUClass is a GPU family UForm ships tuned variants for, numbered as USearch numbers it; a VRAM placement or executor may cap dispatch at one, and the zero value sets no ceiling.1

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

```go
type GPUClass int32
```

#### Values

| `GPUClassUnknown` | | GPUClassUnknown sets no ceiling, so dispatch may pick any class every named device loads. |
| - | - | - |
| `GPUClassNVIDIAAmpere` | | GPUClassNVIDIAAmpere is Ampere-class NVIDIA, and every newer GPU whose driver compiles its PTX. |
| `GPUClassNVIDIAAda` | | GPUClassNVIDIAAda is Ada-class NVIDIA: the fp8 conversions. |
| `GPUClassNVIDIAHopper` | | GPUClassNVIDIAHopper is Hopper-class NVIDIA. |
| `GPUClassNVIDIABlackwell` | | GPUClassNVIDIABlackwell is Blackwell-class NVIDIA. |
| `GPUClassNVIDIABlackwellRTX` | | GPUClassNVIDIABlackwellRTX is Consumer Blackwell NVIDIA. |
| `GPUClassNVIDIARubin` | | GPUClassNVIDIARubin is Rubin-class NVIDIA. |
| `GPUClassAMDCDNA3` | | GPUClassAMDCDNA3 is CDNA3 AMD: gfx942. |
| `GPUClassAMDCDNA4` | | GPUClassAMDCDNA4 is CDNA4 AMD: gfx950. |
| `GPUClassAMDCDNA5` | | GPUClassAMDCDNA5 is CDNA5 AMD: gfx1250. |
| `GPUClassAppleMetal7` | | GPUClassAppleMetal7 is Apple7-family GPUs, and every Apple GPU without a class of its own. |
| `GPUClassAppleMetal9` | | GPUClassAppleMetal9 is Apple9-family GPUs. |
| `GPUClassAppleMetal10` | | GPUClassAppleMetal10 is Apple10-family GPUs. |

##### `GPUClassFromName`

GPUClassFromName reads a GPU class back from the spelling [GPUClass.String](https://www.unum.cloud/docs/uform/go/errors.md#String) renders.

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

```go
func GPUClassFromName(name string) (GPUClass, error)
```

#### Struct `GPUDevice`

GPUDevice is one GPU as UForm numbers it: its position in what GPUDevices fills is the ordinal every VRAM call names.1

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

```go
type GPUDevice struct {
    Vendor GPUVendor
    VendorOrdinal uint32
    GPUClass GPUClass
    ComputeUnits uint32
    MemoryBytes uint64
    // contains filtered or unexported fields
}
```

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

#### Fields

| `Vendor` | `GPUVendor` | Vendor is the runtime that drives the device. |
| - | - | - |
| `VendorOrdinal` | `uint32` | VendorOrdinal is the device's ordinal in its vendor's runtime. |
| `GPUClass` | `GPUClass` | GPUClass is the newest class whose code runs on it, or GPUClassUnknown where none does. |
| `ComputeUnits` | `uint32` | ComputeUnits counts streaming multiprocessors or compute units. |
| `MemoryBytes` | `uint64` | MemoryBytes is the device memory. |

##### `Name`

Name reports the vendor's own name for the device.

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

```go
func (device GPUDevice) Name() string
```

#### Enum `GPUVendor`

GPUVendor is the runtime that drives a GPU.1

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

```go
type GPUVendor int32
```

#### Values

| `GPUVendorUnknown` | | GPUVendorUnknown names no vendor. |
| - | - | - |
| `GPUVendorNVIDIA` | | GPUVendorNVIDIA drives the device through the CUDA driver. |
| `GPUVendorAMD` | | GPUVendorAMD drives the device through the HIP runtime. |
| `GPUVendorApple` | | GPUVendorApple drives the device through Metal. |

##### `String`

String renders the vendor as the library spells it.

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

```go
func (vendor GPUVendor) String() string
```

#### Struct `Mesh`

Mesh is one process's place among the processes of a run, made once and referred to by every trainer, which must be closed first.3

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

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

Mirrors uform_mesh_t; Close calls uform_mesh_free.

##### `MeshBootstrap`

MeshBootstrap joins this process to the run's mesh over executor, a VRAM executor whose devices are this process's members; every process of the run calls it with the same options.

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

```go
func MeshBootstrap(
    executor *Executor,
    config *MeshOptions,
) (*Mesh, error)
```

Calls uform_mesh_bootstrap, blocking.

##### `Close`

Close leaves the mesh.

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

```go
func (mesh *Mesh) Close() error
```

It is idempotent, and every trainer made over the mesh must be closed first.

##### `Describe`

Describe reports the mesh's shape and this process's place in it.

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

```go
func (mesh *Mesh) Describe() (MeshDescription, error)
```

Calls uform_mesh_describe.

#### Enum `MultimemTier`

MultimemTier is how a mesh's NVLink domains reduce.2

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

```go
type MultimemTier int32
```

#### Values

| `MultimemTierSwitchReduced` | | MultimemTierSwitchReduced reduces through the NVLink switch's multicast memory. |
| - | - | - |
| `MultimemTierPeerRead` | | MultimemTierPeerRead reads every peer in a fixed order, where multicast is absent. |

##### `MultimemTierFromName`

MultimemTierFromName reads a multimem tier back from the spelling [MultimemTier.String](https://www.unum.cloud/docs/uform/go/errors.md#String) renders.

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

```go
func MultimemTierFromName(name string) (MultimemTier, error)
```

##### `String`

String renders the tier as the library spells it.

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

```go
func (tier MultimemTier) String() string
```

### Functions

#### `CapabilitiesAvailable`

CapabilitiesAvailable lists the hardware capabilities the current machine offers at runtime, comma separated.

[uform.go:1273 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/uform.go#L1273)

```go
func CapabilitiesAvailable() string
```

#### `CapabilitiesCompiled`

CapabilitiesCompiled lists the hardware capabilities the linked binary was compiled with, comma separated.

[uform.go:1265 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/uform.go#L1265)

```go
func CapabilitiesCompiled() string
```

#### `GPUDevices`

GPUDevices fills devices with the GPUs this process can drive, NVIDIA first, then AMD, then Apple, and reports how many there are, which may exceed len(devices).

[uform.go:770 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/uform.go#L770)

```go
func GPUDevices(devices []GPUDevice) (uint, error)
```
