# UForm Templates

> One byte-level encoder for text, images, video and audio, trained in CUDA without a tokenizer, codec or PyTorch, from C++ Templates

`FetchContent_Declare(uform GIT_REPOSITORY https://github.com/unum-cloud/UForm.git GIT_SHALLOW TRUE)` bd07c64c

`include/uform/` holds the header-only templates and model implementations the C ABI is built from: the encoder towers and their kits, host media decoding, the GPU loaders, and the training engine with its mesh and collectives.

They serve advanced users composing their own towers, loaders or training loops rather than calling the library. These headers carry no stability promise: they change with the code that instantiates them, in any release, while the C ABI of `c/uform/*.h` and the C++ SDK of `cpp/uform/*.hpp` are versioned. Pin a commit, not a version, when building on them. `src/` keeps the verbs over the C ABI, the dispatch and the units compiled per class.

## Installation

bd07c64c

#### Getting Them

CMake names the headers `uform::header`, which links `uform::abi`, and an install copies them beside the C headers, so `find_package(uform)` names it too. The headers include NumKong, ForkUnion, StringZilla and SmashTable, which `uform::header` leaves to the consumer. A program vendoring UForm through `FetchContent` gets those siblings at the commits UForm pins, and links their targets beside it:

```cmake
FetchContent_Declare(uform GIT_REPOSITORY https://github.com/unum-cloud/UForm.git GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(uform)
target_link_libraries(your_target PRIVATE uform::header numkong::types forkunion::header stringzilla::types
                                          smashtable::header)
```

A program using the installed package finds each sibling’s own package instead.

## Executors

bd07c64c

#### Per-GPU Tables

`stack_array` keeps up to a compile-time count of elements inline, never relocating one once constructed, which is how every composition root owns one object per GPU:

```cpp
#include <cstddef> // `std::size_t`

#include "uform/utilities/array.hpp" // `stack_array`

namespace util = unum::uform::utilities;

/** One record per device, constructed in place and never moved while the table lives. */
struct device_entry_t {
    int ordinal = -1;
    std::size_t free_bytes = 0;
};

int main() {
    util::stack_array<device_entry_t, 8> entries;
    for (int ordinal = 0; ordinal != 3; ++ordinal)
        if (entries.push_back({ordinal, 0}).failed()) return 1; // past 8 elements, `out_of_memory_k`
    return entries.size() == 3 && entries.capacity() == 8 ? 0 : 1;
}
```

## Design

bd07c64c

#### Folder Map

- `utilities/`, in `unum::uform::utilities`:

  - `types.hpp`: statuses, `expected`, the scalar and tensor aliases, byte spans and tapes, and the keyed hasher.
  - `ram_device.hpp`: `for_n` over ForkUnion’s C handle `fu_pool_t`, running on the calling thread when the handle is empty; `spawn_pool`, the one way host threads are spawned, is a template there whose ForkUnion calls instantiate only in units that include ForkUnion’s SDK.
  - `array.hpp`: fixed-capacity arrays with inline storage, for tables of one entry per GPU.
  - `safetensors_format.hpp`: reading and writing safetensors checkpoints through NumKong views.
  - `vram_nvidia_device.hpp`, `vram_nvidia_memory.hpp` and `vram_nvidia_schedule.cuh`: the CUDA driver’s entry points and owning streams, the single-GPU allocators, and graphs captured once and replayed.
  - `vram_amd_device.hpp` and `vram_apple_device.hpp`: the HIP runtime’s entry points, and the Metal executor with its contexts, pipelines, shared buffers and launches, each opened at first use.
  - `mesh.hpp`, `multimem_collectives.cuh` and `infiniband_collectives.cuh`: the multi-level GPU mesh, and the collectives inside one multimem domain and across them.

- `encoder/`, in `unum::uform::encoder`:

  - `geometry.hpp`: the shape and numerics of one tower, read back from a checkpoint.
  - `tower.hpp`: what every tower shares, the packed weights, the activations a call reserves and the forward recipe.
  - `ram_tower.hpp` and `vram_tower.hpp`: the tower in host memory over a thread pool, and in one GPU’s memory over a stream.
  - `ram_kits.hpp` and `vram_kits.hpp`: the kits a tower runs on, NumKong’s products, attention and element-wise kernels per capability, and a ladder per family picking the highest one a mask runs.
  - `entries.hpp` and `tower_kernels.cuh`: the steps NumKong has no kernel for, the packed batch layout, the rotary grids and the E4M3 scaling.
  - `multimodal_batch.hpp` and `multimodal_batch.cuh`: training batches one per GPU, and the mixer shuffling them across GPUs.

- `media/`, in `unum::uform::media`:

  - `planning.hpp`: how many patches an image or a clip earns, how they factor, and what gets cropped.
  - `image.hpp`, `video.hpp` and `audio.hpp`: host decoding of images, clips and recordings into a batch’s layout.

- `loaders/`, in `unum::uform::loaders`:

  - `direct_storage.hpp` and `dataset_shards.hpp`: one process-wide storage setup, and the GPU-native shard readers over it.
  - `tokens_loader.cuh`, `image_loader.cuh`, `video_loader.cuh`, `audio_loader.cuh`, `paired_loader.cuh` and `choices_loader.cuh`: the Parquet loaders of each modality and pairing.

- `train/`, in `unum::uform::train`:

  - `encoder_tower.hpp` and `transformer_block.cuh`: the trainable tower and its pre-norm blocks, whose E4M3 twin runs forward only.
  - `contrastive_segments.cuh`, `contrastive_tokens.cuh` and `contrastive_relations.cuh`: the pooled and late-interaction contrastive losses, and the relation graph they relax.
  - `multimodal_masking.hpp`: the masked-modeling objective and its reconstruction heads.
  - `optimizer.hpp`: Adafactor with factored second moments.
  - `base.cuh`, `datasets.hpp`, `introspect.cuh` and `trainer.hpp`: one run’s configuration and replicas, its data mixtures, its telemetry, and the trainer stepping it.

#### Include Order

Families include one way, `utilities` ← `encoder` ← `media` ← `loaders` ← `train`, so a header reaches only the families to its left. `utilities/` includes nothing from the other four, and the hook refuses a header there that does. The mesh and its collectives include only system headers, the host types, the device allocators, the schedule and each other, so they lift out of UForm whole. A `ram_` header includes no `vram_` one, a vendor’s header includes only its own vendor’s and the shared ones, and a header every vendor compiles names none. Nothing here names a class, holds a capability mask other than a kit’s own `capability_k`, owns a ForkUnion pool, or sleeps on its own schedule: a template takes the kits, atomic references and pool views that `src/` picks for it.

#### Namespaces and Aliases

Every folder opens `unum::uform::<dir>`, and its headers are spelled `"uform/<dir>/..."` everywhere, `src/` included. A file reaching another family names it through one alias, declared in the namespace or translation unit that uses it:

- `util` for `unum::uform::utilities`, declared once in `unum::uform` by `"uform/utilities/types.hpp"`.
- `ue` for `unum::uform::encoder`.
- `um` for `unum::uform::media`.
- `ul` for `unum::uform::loaders`.
- `ut` for `unum::uform::train`.

Sibling projects follow the same pattern through `fu`, `nk`, `sz` and `st`. The full `unum::uform::` spelling appears only on alias and `using` lines, on the lines opening and closing a namespace, and in specializations of foreign templates such as `std::formatter`. A `.cpp` or `.cu` unit never opens a namespace block of its own, reaching the one it defines through `using namespace` or an alias.
