Swift · Executors

Executors

import UForm 4.0.0-rc.1 · 3ee5e534

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.

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.

API

StructCPUClassA 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.20
Types.swift:179 ↗
struct CPUClass

Conforms to

Hashable, Sendable, CustomStringConvertible, Equatable
serialThe portable floor, with no SIMD assumptions.
Types.swift:184 ↗
static let serial: CPUClass
intelHaswellHaswell-class x86: AVX2 and FMA.
Types.swift:186 ↗
static let intelHaswell: CPUClass
intelSierraSierra Forest-class x86: AVX-VNNI without AVX-512.
Types.swift:188 ↗
static let intelSierra: CPUClass
intelSkylakeSkylake-class x86: the AVX-512 foundation.
Types.swift:190 ↗
static let intelSkylake: CPUClass
intelIcelakeIcelake-class x86: AVX-512 VNNI and VPOPCNT.
Types.swift:192 ↗
static let intelIcelake: CPUClass
amdGenoaGenoa-class x86: native bf16 dot products over AVX-512.
Types.swift:194 ↗
static let amdGenoa: CPUClass
intelSapphireSapphire Rapids-class x86: AVX-512 FP16 and the AMX tiles.
Types.swift:196 ↗
static let intelSapphire: CPUClass
intelGraniteGranite Rapids-class x86: the AMX-FP16 tiles.
Types.swift:198 ↗
static let intelGranite: CPUClass
intelDiamondDiamond Rapids-class x86: AVX10.2's FP8 conversions.
Types.swift:200 ↗
static let intelDiamond: CPUClass
awsG2Graviton2-class Arm: the bare NEON floor.
Types.swift:202 ↗
static let awsG2: CPUClass
appleM2Armv8.6 NEON with dot products and bf16.
Types.swift:204 ↗
static let appleM2: CPUClass
appleM4Apple M4-class Arm: the SME outer-product tiles.
Types.swift:206 ↗
static let appleM4: CPUClass
awsG5Graviton5-class Arm: Armv9.2.
Types.swift:208 ↗
static let awsG5: CPUClass
nvidiaVeraVera-class Arm: FP8 dot products.
Types.swift:210 ↗
static let nvidiaVera: CPUClass
alibabaC930Alibaba C930-class RISC-V: RVA23 vectors.
Types.swift:212 ↗
static let alibabaC930: CPUClass
wasm32V128WebAssembly with fixed-width simd128.
Types.swift:214 ↗
static let wasm32V128: CPUClass
wasm64ExtendedWebAssembly with relaxed SIMD, threads and 64-bit addressing.
Types.swift:216 ↗
static let wasm64Extended: CPUClass
monoversionedThe one class of a monoversioned build, with every capability it compiled.
Types.swift:218 ↗
static let monoversioned: CPUClass
descriptionThe class as the library spells it, like intel-haswell.
Types.swift:221 ↗
var description: String { get }

Calls uform_cpu_class_name.

initReads a spelling back, and nil for one no class carries.
Types.swift:224 ↗
init?(name: String)

Calls uform_cpu_class_from_name.

StructGPUClassA 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.14
Types.swift:242 ↗
struct GPUClass

Conforms to

Hashable, Sendable, CustomStringConvertible, Equatable
nvidiaAmpereAmpere-class NVIDIA, and every NVIDIA GPU without a class of its own.
Types.swift:247 ↗
static let nvidiaAmpere: GPUClass
nvidiaAdaAda-class NVIDIA: the fp8 conversions.
Types.swift:249 ↗
static let nvidiaAda: GPUClass
nvidiaHopperHopper-class NVIDIA.
Types.swift:251 ↗
static let nvidiaHopper: GPUClass
nvidiaBlackwellBlackwell-class NVIDIA.
Types.swift:253 ↗
static let nvidiaBlackwell: GPUClass
nvidiaBlackwellRTXConsumer Blackwell NVIDIA.
Types.swift:255 ↗
static let nvidiaBlackwellRTX: GPUClass
nvidiaRubinRubin-class NVIDIA.
Types.swift:257 ↗
static let nvidiaRubin: GPUClass
amdCDNA3CDNA3 AMD: gfx942.
Types.swift:259 ↗
static let amdCDNA3: GPUClass
amdCDNA4CDNA4 AMD: gfx950.
Types.swift:261 ↗
static let amdCDNA4: GPUClass
amdCDNA5CDNA5 AMD: gfx1250.
Types.swift:263 ↗
static let amdCDNA5: GPUClass
appleMetal7Apple7-family GPUs, and every Apple GPU without a class of its own.
Types.swift:265 ↗
static let appleMetal7: GPUClass
appleMetal9Apple9-family GPUs.
Types.swift:267 ↗
static let appleMetal9: GPUClass
appleMetal10Apple10-family GPUs.
Types.swift:269 ↗
static let appleMetal10: GPUClass
descriptionThe class as the library spells it, like nvidia-hopper.
Types.swift:272 ↗
var description: String { get }

Calls uform_gpu_class_name.

initReads a spelling back, and nil for one no class carries.
Types.swift:275 ↗
init?(name: String)

Calls uform_gpu_class_from_name.

StructGPUVendorThe vendor whose runtime drives a GPU, carrying the C enumerator.4
Types.swift:292 ↗
struct GPUVendor

Conforms to

Hashable, Sendable, Equatable
unknownA vendor this binding has no name for.
Types.swift:297 ↗
static let unknown: GPUVendor
nvidiaNVIDIA, through the CUDA driver.
Types.swift:299 ↗
static let nvidia: GPUVendor
amdAMD, through the HIP runtime.
Types.swift:301 ↗
static let amd: GPUVendor
appleApple, through Metal.
Types.swift:303 ↗
static let apple: GPUVendor
StructGPUDeviceOne GPU this process can drive; its position in gpuDevices() is the ordinal every VRAM placement names it by.6
Types.swift:308 ↗
struct GPUDevice

Mirrors uform_gpu_device_t.

Conforms to

Sendable
vendorThe vendor whose runtime drives the device.
Types.swift:310 ↗
var vendor: GPUVendor
vendorOrdinalThe device's ordinal in its vendor's runtime.
Types.swift:312 ↗
var vendorOrdinal: UInt32
gpuClassThe class whose kernels run on it, or nil for none.
Types.swift:314 ↗
var gpuClass: GPUClass?
computeUnitsStreaming multiprocessors or compute units.
Types.swift:316 ↗
var computeUnits: UInt32
memoryBytesDevice memory in bytes.
Types.swift:318 ↗
var memoryBytes: UInt64
nameThe vendor's own name for the device.
Types.swift:320 ↗
var name: String
ClassExecutorThe host threads or device streams a call runs on, with the activation scratch and the staging they use.4
Types.swift:627 ↗
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. Calls on one executor run in submission order.

Conforms to

Sendable
forRamSpawns 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.
Types.swift:640 ↗
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 dispatches as if the CPU were of that class; nil derives its own. Calls uform_executor_for_ram.

onPoolCreates a RAM executor over a borrowed ForkUnion fu_pool_t, which must outlive it.
Types.swift:662 ↗
static func onPool(
    memoryDomains: [Int32] = [],
    cpuClass: CPUClass? = nil,
    pool: UnsafeMutableRawPointer,
    deferredLimit: Int = 0
) throws(UFormError) -> Executor

Reports unsupported until the library wires borrowed pools. A cpuClass pins as forRam does. Calls uform_executor_on_pool.

forVramOpens 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.
Types.swift:687 ↗
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 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, where nil derives the CPU's own. Calls uform_executor_for_vram.

drainWaits 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.
Types.swift:722 ↗
func drain(mood: Mood = .parked) throws(UFormError)

Calls uform_executor_drain.

StructMultimemTierHow a mesh's NVLink domains reduce, carrying the C enumerator.4
Train.swift:89 ↗
struct MultimemTier

Conforms to

Hashable, Sendable, CustomStringConvertible, Equatable
switchReducedThe NVLink switch reduces through multicast memory.
Train.swift:94 ↗
static let switchReduced: MultimemTier
peerReadEvery member reads its peers in a fixed order, where multicast is absent.
Train.swift:96 ↗
static let peerRead: MultimemTier
descriptionThe tier as the library spells it.
Train.swift:99 ↗
var description: String { get }

Calls uform_multimem_tier_name.

initReads a spelling back, and nil for one no tier carries.
Train.swift:103 ↗
init?(name: String)

Calls uform_multimem_tier_from_name.

StructMeshOptionsWhere this process sits among the processes training one model, and how they meet.7
Train.swift:146 ↗
struct MeshOptions

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

Conforms to

Sendable
hostDomainIndexThis process's index among hostDomainCount.
Train.swift:148 ↗
var hostDomainIndex: Int
hostDomainCountProcesses in the run, one per host; zero derives one, a run with no peers.
Train.swift:150 ↗
var hostDomainCount: Int
gpusPerMultimemDomainGPUs one NVLink multicast domain spans; zero derives the host.
Train.swift:152 ↗
var gpusPerMultimemDomain: Int
directoryThe directory every host sees, where processes exchange their facts; required with peers.
Train.swift:154 ↗
var directory: String
jobThe run's unique name inside directory; required with peers.
Train.swift:156 ↗
var job: String
timeoutSecondsSeconds any wait for a peer lasts before the call fails; zero derives 120.
Train.swift:158 ↗
var timeoutSeconds: Double
initTakes every field by name; zeros and empty strings derive as their fields document.
Train.swift:161 ↗
init(hostDomainIndex: Int = 0, hostDomainCount: Int = 0, gpusPerMultimemDomain: Int = 0, directory: String = "", job: String = "", timeoutSeconds: Double = 0)
StructMeshDescriptionWhat a mesh reports about itself.7
Train.swift:179 ↗
struct MeshDescription

Mirrors uform_mesh_description_t.

Conforms to

Sendable
gpusPerHostDomainGPUs one process drives.
Train.swift:181 ↗
var gpusPerHostDomain: Int
gpusPerMultimemDomainGPUs one NVLink domain spans.
Train.swift:183 ↗
var gpusPerMultimemDomain: Int
gpusPerColocatedDomainGPUs in the whole run.
Train.swift:185 ↗
var gpusPerColocatedDomain: Int
hostDomainIndexThis process's index among the run's processes.
Train.swift:187 ↗
var hostDomainIndex: Int
firstGpuIndexThe run-wide index of this process's first GPU, where its share of a sharded dataset begins.
Train.swift:189 ↗
var firstGpuIndex: Int
multimemTierHow its NVLink domains reduce.
Train.swift:191 ↗
var multimemTier: MultimemTier
gpuClassThe class every GPU of the run dispatches as: the executor's pin, or the devices' own.
Train.swift:193 ↗
var gpuClass: GPUClass?
ClassMeshOne process's place among the processes of a run, made once and borrowed by every trainer.2
Train.swift:207 ↗
final class Mesh

Conforms to

Sendable
bootstrapJoins 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.
Train.swift:224 ↗
static func bootstrap(
    executor: Executor,
    options: MeshOptions = MeshOptions()
) throws(UFormError) -> Mesh

Calls uform_mesh_bootstrap.

describeReports the mesh's shape and this process's place in it.
Train.swift:246 ↗
func describe() throws(UFormError) -> MeshDescription

Calls uform_mesh_describe.

Functions

gpuDevicesEvery GPU this process can drive, NVIDIA devices first, then AMD; empty on a machine without GPUs.
Types.swift:336 ↗
func gpuDevices() throws(UFormError) -> [GPUDevice]

Calls uform_gpu_devices.

capabilitiesCompiledThe hardware capabilities the linked binary was compiled with, comma separated.
Types.swift:961 ↗
func capabilitiesCompiled() -> String

Calls uform_capabilities_compiled.

capabilitiesAvailableThe hardware capabilities the current machine offers at runtime, comma separated.
Types.swift:965 ↗
func capabilitiesAvailable() -> String

Calls uform_capabilities_available.