# Executors

> UForm for JavaScript

A model on GPUs takes a VRAM executor and lands its outputs in device memory, which no host view reads:

```javascript
const gpus = Executor.forVram({ devices: [0, 1] });
const resident = Model.loadVram("model.safetensors", { executor: gpus, devices: [0, 1] });
```

Device ordinals are positions in `gpuDevices()`, NVIDIA devices first, then AMD.

`import * as uform from 'uform';`

`import { Mesh } from 'uform';`

`import { Executor } from 'uform';`

## MeshOptions

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

```typescript
interface MeshOptions
```

Mirrors `uform_mesh_options_t`.

- `hostDomainIndex?` `number`: This process's index among `hostDomainCount`.
- `hostDomainCount?` `number`: Processes in the run, one per host; absent derives one, a run with no peers.
- `gpusPerMultimemDomain?` `number`: GPUs one NVLink multicast domain spans; absent derives the host.
- `directory?` `string`: The path of the directory every host sees, crossing as its UTF-8 bytes; required with peers.
- `job?` `string`: The run's unique name, naming one directory inside `directory`; required with peers.
- `timeoutSeconds?` `number`: Seconds any wait for a peer lasts before the call fails; absent derives 120.

## MeshDescription

What a mesh reports about itself.

```typescript
interface MeshDescription extends Omit<CompiledMeshDescription, "multimemTier">
```

Mirrors `uform_mesh_description_t`.

- `multimemTier` `MultimemTier`: How its NVLink domains reduce.

## MultimemTier

How a mesh's NVLink domains reduce.

```typescript
enum MultimemTier
```

- `SwitchReduced` = `&quot;switch-reduced&quot;`
- `PeerRead` = `&quot;peer-read&quot;`

## GpuVendor

The runtime that drives a GPU, numbered as `uform_gpu_vendor_t` numbers them.

```typescript
enum GpuVendor
```

- `Unknown` = `0`
- `Nvidia` = `1`
- `Amd` = `2`
- `Apple` = `3`

## GpuDevice

One GPU this process can drive.

```typescript
interface GpuDevice
```

Mirrors `uform_gpu_device_t`.

- `vendor` `GpuVendor`: The vendor whose runtime drives the device.
- `vendorOrdinal` `number`: The device's ordinal in its vendor's runtime, as `nvidia-smi` or `rocm-smi` lists it.
- `gpuClass` `string | null`: The class whose kernels run on it, as `uform_gpu_class_name` spells it, like "nvidia-hopper".
- `computeUnits` `number`: Streaming multiprocessors or compute units.
- `memoryBytes` `number`: Device memory in bytes.
- `name` `string`: The vendor's own name for the device.

## ExecutorForRamConfig

Where the host threads of a RAM executor are placed, how many there are, and how many deferred calls it tracks.

```typescript
interface ExecutorForRamConfig
```

- `memoryDomains?` `number[]`: NUMA node IDs the threads are spread over; absent spreads them over every node.
- `cpuClass?` `string`: The CPU class to dispatch as, spelled like "intel-haswell"; absent derives the CPU's own.
- `threads?` `number`: Threads to spawn; absent takes 7/8 of the cores the domains carry.
- `deferredLimit?` `number`: Deferred calls tracked at once; absent takes a default.

## ExecutorOnPoolConfig

Where the borrowed pool of a RAM executor is placed, and how many deferred calls it tracks.

```typescript
interface ExecutorOnPoolConfig
```

- `memoryDomains?` `number[]`: NUMA node IDs of the models it will drive; absent derives every node.
- `cpuClass?` `string`: The CPU class to dispatch as, spelled like "intel-haswell"; absent derives the CPU's own.
- `deferredLimit?` `number`: Deferred calls tracked at once; absent takes a default.

## ExecutorForVramConfig

Which devices a VRAM executor drives, and on which streams.

```typescript
interface ExecutorForVramConfig
```

- `devices?` `number[]`: Device ordinals, positions in [`gpuDevices`](#gpuDevices), to drive; absent takes one free device.
- `gpuClass?` `string`: The GPU class every device dispatches as, spelled like "nvidia-hopper"; absent derives the devices' own.
- `memoryDomains?` `number[]`: NUMA node IDs whose cores the host decoders take; absent takes every node.
- `cpuClass?` `string`: The CPU class the host decoders dispatch as, spelled like "intel-haswell"; absent derives the CPU's own.
- `streams?` `bigint[]`: Borrowed CUDA stream addresses, one per device in `devices` order; absent creates its own.
- `scratchLimit?` `number`: Ceiling on scratch bytes per device; absent lets scratch grow as batches ask.
- `deferredLimit?` `number`: Deferred calls tracked at once; absent takes a default.

## Mesh

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

```typescript
class Mesh implements Disposable
```

## bootstrap

Joins this process to the run's mesh, once every process has joined.

```typescript
static bootstrap(
    executor: Executor,
    options: MeshOptions = {},
): Mesh
```

Calls `uform_mesh_bootstrap`.

- `executor`: A VRAM executor whose devices are this process's members of the mesh.
- `options`: Where this process sits and how the processes meet.

## describe

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

```typescript
describe(): MeshDescription
```

Calls `uform_mesh_describe`.

## free

Leaves the mesh; every trainer borrowing it must be freed first.

```typescript
free(): void
```

Calls `uform_mesh_free`.

## compiled

The backend handle a trainer borrows.

```typescript
readonly compiled: CompiledMesh
```

## Executor

The threads or streams a verb runs on, shared across models.

```typescript
class Executor implements Disposable
```

An executor owns host threads or device streams and the activation scratch; a model owns
weights. Keeping them apart is what lets one executor serve several models, and what makes the
cost of spawning visible at the line that spawns it. The WebAssembly backend computes on the
calling thread alone, so its executors only order calls. The collector frees it eventually;
[`free`](https://www.unum.cloud/docs/uform/javascript/training.md#free) joins the threads now.

## forRam

Spawns host threads for RAM-resident models.

```typescript
static forRam(config: ExecutorForRamConfig = {}): Executor
```

Calls `uform_executor_for_ram`.

## forVram

Opens one stream per device for VRAM-resident models.

```typescript
static forVram(config: ExecutorForVramConfig = {}): Executor
```

Calls `uform_executor_for_vram`.

## onPool

Borrows a ForkUnion `fu_pool_t` by address, which reports `unsupported` until borrowing is designed.

```typescript
static onPool(
    pool: bigint,
    config: ExecutorOnPoolConfig = {},
): Executor
```

Calls `uform_executor_on_pool`.

## drain

Waits for every call submitted to this executor so far, then idles in `mood`.

```typescript
drain(mood: Mood = Mood.Parked): void
```

Calls `uform_executor_drain`.

- `mood`: What the threads or streams do once they go idle.

## free

Runs every deferred call and joins the threads now; a second [`free`](https://www.unum.cloud/docs/uform/javascript/training.md#free) does nothing.

```typescript
free(): void
```

Calls `uform_executor_free`.

## compiled

The backend handle every verb is handed.

```typescript
readonly compiled: CompiledExecutor
```

## gpuDevices

Lists every GPU this process can drive, NVIDIA devices first, then AMD, so a device's position is the ordinal VRAM placements name it by; a machine without GPUs lists none.

```typescript
function gpuDevices(): GpuDevice[]
```

Calls `uform_gpu_devices`.

## capabilitiesCompiled

The hardware capabilities this binary was compiled with, comma-separated.

```typescript
function capabilitiesCompiled(): string
```

Calls `uform_capabilities_compiled`.

## capabilitiesAvailable

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

```typescript
function capabilitiesAvailable(): string
```

Calls `uform_capabilities_available`.

