# Executors

> UForm for Swift

A model says where its weights live; an `Executor` says who runs the calls on them.
One executor serves any number of models of its residency, so several models share one machine's cores or devices.

```swift
let hostThreads = try Executor.forRam(memoryDomains: [0, 1], threads: 0)
let deviceStreams = try Executor.forVram(devices: [0, 1], streams: nil, scratchLimit: 1 << 30)
try hostThreads.drain(mood: .parked)
```

Each factory takes every argument its C constructor has, and only those.
A thread count of zero takes 7/8 of the cores the memory domains carry, and `forVram` takes no thread count at all, since the submitting thread drives one stream per device.
`Executor.onPool(memoryDomains:cpuClass:pool:)` mirrors the C verb that borrows a ForkUnion pool, and reports `unsupported` until the library wires borrowed pools.
`drain(mood:)` waits until no call holds the executor and its devices are idle, then picks whether its threads park or spin.

`import UForm`

## CPUClass

A CPU family UForm ships tuned kernels for, carrying the C enumerator; dispatch reports its pick in this vocabulary, and a RAM placement may pin one.

```swift
struct CPUClass
```

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

## serial

The portable floor, with no SIMD assumptions.

```swift
static let serial: CPUClass
```

## intelHaswell

Haswell-class x86: AVX2 and FMA.

```swift
static let intelHaswell: CPUClass
```

## intelSierra

Sierra Forest-class x86: AVX-VNNI without AVX-512.

```swift
static let intelSierra: CPUClass
```

## intelSkylake

Skylake-class x86: the AVX-512 foundation.

```swift
static let intelSkylake: CPUClass
```

## intelIcelake

Icelake-class x86: AVX-512 VNNI and VPOPCNT.

```swift
static let intelIcelake: CPUClass
```

## amdGenoa

Genoa-class x86: native bf16 dot products over AVX-512.

```swift
static let amdGenoa: CPUClass
```

## intelSapphire

Sapphire Rapids-class x86: AVX-512 FP16 and the AMX tiles.

```swift
static let intelSapphire: CPUClass
```

## intelGranite

Granite Rapids-class x86: the AMX-FP16 tiles.

```swift
static let intelGranite: CPUClass
```

## intelDiamond

Diamond Rapids-class x86: AVX10.2's FP8 conversions.

```swift
static let intelDiamond: CPUClass
```

## awsG2

Graviton2-class Arm: the bare NEON floor.

```swift
static let awsG2: CPUClass
```

## appleM2

Armv8.6 NEON with dot products and bf16.

```swift
static let appleM2: CPUClass
```

## appleM4

Apple M4-class Arm: the SME outer-product tiles.

```swift
static let appleM4: CPUClass
```

## awsG5

Graviton5-class Arm: Armv9.2.

```swift
static let awsG5: CPUClass
```

## nvidiaVera

Vera-class Arm: FP8 dot products.

```swift
static let nvidiaVera: CPUClass
```

## alibabaC930

Alibaba C930-class RISC-V: RVA23 vectors.

```swift
static let alibabaC930: CPUClass
```

## wasm32V128

WebAssembly with fixed-width simd128.

```swift
static let wasm32V128: CPUClass
```

## wasm64Extended

WebAssembly with relaxed SIMD, threads and 64-bit addressing.

```swift
static let wasm64Extended: CPUClass
```

## monoversioned

The one class of a monoversioned build, with every capability it compiled.

```swift
static let monoversioned: CPUClass
```

## description

The class as the library spells it, like `intel-haswell`.

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

Calls `uform_cpu_class_name`.

## init

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

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

Calls `uform_cpu_class_from_name`.

## GPUClass

A GPU family UForm ships tuned kernels for, carrying the C enumerator; dispatch reports its pick in this vocabulary, and a VRAM placement may pin one.

```swift
struct GPUClass
```

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

## nvidiaAmpere

Ampere-class NVIDIA, and every NVIDIA GPU without a class of its own.

```swift
static let nvidiaAmpere: GPUClass
```

## nvidiaAda

Ada-class NVIDIA: the fp8 conversions.

```swift
static let nvidiaAda: GPUClass
```

## nvidiaHopper

Hopper-class NVIDIA.

```swift
static let nvidiaHopper: GPUClass
```

## nvidiaBlackwell

Blackwell-class NVIDIA.

```swift
static let nvidiaBlackwell: GPUClass
```

## nvidiaBlackwellRTX

Consumer Blackwell NVIDIA.

```swift
static let nvidiaBlackwellRTX: GPUClass
```

## nvidiaRubin

Rubin-class NVIDIA.

```swift
static let nvidiaRubin: GPUClass
```

## amdCDNA3

CDNA3 AMD: gfx942.

```swift
static let amdCDNA3: GPUClass
```

## amdCDNA4

CDNA4 AMD: gfx950.

```swift
static let amdCDNA4: GPUClass
```

## amdCDNA5

CDNA5 AMD: gfx1250.

```swift
static let amdCDNA5: GPUClass
```

## appleMetal7

Apple7-family GPUs, and every Apple GPU without a class of its own.

```swift
static let appleMetal7: GPUClass
```

## appleMetal9

Apple9-family GPUs.

```swift
static let appleMetal9: GPUClass
```

## appleMetal10

Apple10-family GPUs.

```swift
static let appleMetal10: GPUClass
```

## description

The class as the library spells it, like `nvidia-hopper`.

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

Calls `uform_gpu_class_name`.

## init

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

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

Calls `uform_gpu_class_from_name`.

## GPUVendor

The vendor whose runtime drives a GPU, carrying the C enumerator.

```swift
struct GPUVendor
```

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

## unknown

A vendor this binding has no name for.

```swift
static let unknown: GPUVendor
```

## nvidia

NVIDIA, through the CUDA driver.

```swift
static let nvidia: GPUVendor
```

## amd

AMD, through the HIP runtime.

```swift
static let amd: GPUVendor
```

## apple

Apple, through Metal.

```swift
static let apple: GPUVendor
```

## GPUDevice

One GPU this process can drive; its position in `gpuDevices()` is the ordinal every VRAM placement names it by.

```swift
struct GPUDevice
```

Mirrors `uform_gpu_device_t`.

Conforms to: `Sendable`

## vendor

The vendor whose runtime drives the device.

```swift
var vendor: GPUVendor
```

## vendorOrdinal

The device's ordinal in its vendor's runtime.

```swift
var vendorOrdinal: UInt32
```

## gpuClass

The class whose kernels run on it, or nil for none.

```swift
var gpuClass: GPUClass?
```

## computeUnits

Streaming multiprocessors or compute units.

```swift
var computeUnits: UInt32
```

## memoryBytes

Device memory in bytes.

```swift
var memoryBytes: UInt64
```

## name

The vendor's own name for the device.

```swift
var name: String
```

## Executor

The host threads or device streams a call runs on, with the activation scratch and the staging they use.

```swift
final class Executor
```

One executor serves any number of models of its residency, so several models share one machine's
cores or devices; a mismatch reports [`device`](https://www.unum.cloud/docs/uform/swift/errors.md#device). Calls on one executor run in submission order.

Conforms to: `Sendable`

## forRam

Spawns host threads for RAM-resident models, interleaved across the compute domains local to `memoryDomains`; an empty list derives every node the process may allocate on.

```swift
static func forRam(
    memoryDomains: [Int32] = [],
    cpuClass: CPUClass? = nil,
    threads: Int = 0,
    deferredLimit: Int = 0
) throws(UFormError) -> Executor
```

Zero
`threads` takes 7/8 of the cores those domains carry, and zero `deferredLimit` sizes the
deferred table by default. A [`cpuClass`](https://www.unum.cloud/docs/uform/swift/models.md#cpuClass) dispatches as if the CPU were of that class; nil
derives its own. Calls `uform_executor_for_ram`.

## onPool

Creates a RAM executor over a borrowed ForkUnion `fu_pool_t`, which must outlive it.

```swift
static func onPool(
    memoryDomains: [Int32] = [],
    cpuClass: CPUClass? = nil,
    pool: UnsafeMutableRawPointer,
    deferredLimit: Int = 0
) throws(UFormError) -> Executor
```

Reports [`unsupported`](https://www.unum.cloud/docs/uform/swift/errors.md#unsupported) until the library wires borrowed pools. A [`cpuClass`](https://www.unum.cloud/docs/uform/swift/models.md#cpuClass) pins as [`forRam`](#forRam)
does. Calls `uform_executor_on_pool`.

## forVram

Opens one submission stream on every named device, or borrows the caller's `streams`, one per device in `devices` order; an empty list derives one free device.

```swift
static func forVram(
    devices: [Int32] = [],
    gpuClass: GPUClass? = nil,
    memoryDomains: [Int32] = [],
    cpuClass: CPUClass? = nil,
    streams: [UnsafeMutableRawPointer?]? = nil,
    scratchLimit: Int = 0,
    deferredLimit: Int = 0
) throws(UFormError) -> Executor
```

A zero
`scratchLimit` lets per-device scratch grow as batches ask. A [`gpuClass`](https://www.unum.cloud/docs/uform/swift/models.md#gpuClass) dispatches
every device as that class; nil derives theirs. The host decoders take 7/8 of the
cores local to `memoryDomains` and dispatch as [`cpuClass`](https://www.unum.cloud/docs/uform/swift/models.md#cpuClass), where nil derives the CPU's
own. Calls `uform_executor_for_vram`.

## drain

Waits for every call submitted before it, then adopts `mood` whenever the executor next goes idle; a failure is the executor's own, never one of a drained call.

```swift
func drain(mood: Mood = .parked) throws(UFormError)
```

Calls `uform_executor_drain`.

## MultimemTier

How a mesh's NVLink domains reduce, carrying the C enumerator.

```swift
struct MultimemTier
```

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

## switchReduced

The NVLink switch reduces through multicast memory.

```swift
static let switchReduced: MultimemTier
```

## peerRead

Every member reads its peers in a fixed order, where multicast is absent.

```swift
static let peerRead: MultimemTier
```

## description

The tier as the library spells it.

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

Calls `uform_multimem_tier_name`.

## init

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

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

Calls `uform_multimem_tier_from_name`.

## MeshOptions

Where this process sits among the processes training one model, and how they meet.

```swift
struct MeshOptions
```

Mirrors
`uform_mesh_options_t`, every zero deriving as the C field documents.

Conforms to: `Sendable`

## hostDomainIndex

This process's index among [`hostDomainCount`](#hostDomainCount).

```swift
var hostDomainIndex: Int
```

## hostDomainCount

Processes in the run, one per host; zero derives one, a run with no peers.

```swift
var hostDomainCount: Int
```

## gpusPerMultimemDomain

GPUs one NVLink multicast domain spans; zero derives the host.

```swift
var gpusPerMultimemDomain: Int
```

## directory

The directory every host sees, where processes exchange their facts; required with peers.

```swift
var directory: String
```

## job

The run's unique name inside [`directory`](#directory); required with peers.

```swift
var job: String
```

## timeoutSeconds

Seconds any wait for a peer lasts before the call fails; zero derives 120.

```swift
var timeoutSeconds: Double
```

## init

Takes every field by name; zeros and empty strings derive as their fields document.

```swift
init(hostDomainIndex: Int = 0, hostDomainCount: Int = 0, gpusPerMultimemDomain: Int = 0, directory: String = "", job: String = "", timeoutSeconds: Double = 0)
```

## MeshDescription

What a mesh reports about itself.

```swift
struct MeshDescription
```

Mirrors `uform_mesh_description_t`.

Conforms to: `Sendable`

## gpusPerHostDomain

GPUs one process drives.

```swift
var gpusPerHostDomain: Int
```

## gpusPerMultimemDomain

GPUs one NVLink domain spans.

```swift
var gpusPerMultimemDomain: Int
```

## gpusPerColocatedDomain

GPUs in the whole run.

```swift
var gpusPerColocatedDomain: Int
```

## hostDomainIndex

This process's index among the run's processes.

```swift
var hostDomainIndex: Int
```

## firstGpuIndex

The run-wide index of this process's first GPU, where its share of a sharded dataset begins.

```swift
var firstGpuIndex: Int
```

## multimemTier

How its NVLink domains reduce.

```swift
var multimemTier: MultimemTier
```

## gpuClass

The class every GPU of the run dispatches as: the executor's pin, or the devices' own.

```swift
var gpuClass: GPUClass?
```

## Mesh

One process's place among the processes of a run, made once and borrowed by every trainer.

```swift
final class Mesh
```

Conforms to: `Sendable`

## bootstrap

Joins this process to the run's mesh over the devices of `executor`; every process of the run calls it with the same count, multicast extent, directory and job.

```swift
static func bootstrap(
    executor: Executor,
    options: MeshOptions = MeshOptions()
) throws(UFormError) -> Mesh
```

Calls `uform_mesh_bootstrap`.

## describe

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

```swift
func describe() throws(UFormError) -> MeshDescription
```

Calls `uform_mesh_describe`.

## gpuDevices

Every GPU this process can drive, NVIDIA devices first, then AMD; empty on a machine without GPUs.

```swift
func gpuDevices() throws(UFormError) -> [GPUDevice]
```

Calls `uform_gpu_devices`.

## capabilitiesCompiled

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

```swift
func capabilitiesCompiled() -> String
```

Calls
`uform_capabilities_compiled`.

## capabilitiesAvailable

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

```swift
func capabilitiesAvailable() -> String
```

Calls
`uform_capabilities_available`.

