Skip to content

Latest commit

 

History

History

python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

UForm for Python

The bindings are pure CPython, with no PyBind11, no NanoBind, and no SWIG. They need CPython 3.12 or newer on Linux, macOS or Windows, free-threaded builds included, and the extension never requires NumPy. Every verb is a batch over buffer-protocol inputs, and every output whose length the call decides lands in an arena, a fresh one per call or one you pass to reuse.

  • uform.Model encodes text, images, video and audio into pooled embeddings, per-token states and token vectors, and scores calibrated choices over texts.
  • uform.Trainer trains one on GPUs, joined across hosts by a uform.Mesh.
  • uform.Executor owns the threads or streams every call runs on, apart from any model.

Installation

Install from PyPI, which ships wheels for Linux, macOS and Windows on x86 and Arm:

pip install uform

The wheels include the CPU library, with both block-product dtypes, bf16 and e4m3. A source build narrows them through UFORM_ENCODER_DTYPES in the environment or -C cmake.define.UFORM_ENCODER_DTYPES=bf16, and loading a model in a left-out one raises uform.UnsupportedError. uform.encoder_dtypes_compiled() answers the kept ones as a mask, bit n standing for the uform.Scalar valued n. A build from source compiles the C library and the extension through scikit-build-core, and the suites run against the stub library alone:

pip install . -C cmake.define.UFORM_BUILD_STUB=ON
uv run pytest test/python

Quickstart

Encode two captions and a photo into one space, then pick the caption closest to the photo:

import numpy as np
import uform

executor = uform.Executor.for_ram()  # the threads every receiver names
model = uform.Model.load_ram("model.safetensors")

captions = ["a cat on a mat", "a sunrise over the bay"]
texts = np.array(model.on(executor).text_encode(captions).embeddings)  # each call owns a fresh arena
with open("cat.png", "rb") as photo:
    image = np.array(model.on(executor).image_encode([photo.read()]).embeddings)[0]

cosines = texts @ image / (np.linalg.norm(texts, axis=1) * np.linalg.norm(image))
print(f"{captions[cosines.argmax()]!r} describes the photo best")

Receivers & Deferred Calls

A receiver holds what every C argument structure puts before its inputs: the executor and the completion. The model runs a verb on the calling thread, model.on(executor) answers a uform.On whose verbs block on that executor, and model.deferred(executor) answers a uform.DeferredOn whose verbs submit there and answer a uform.Deferred. The receiver's type alone chooses the completion, so all three offer the same verbs with the same arguments. In a coroutine, await a deferred call; anywhere else, wait() blocks the thread with its state detached:

call = model.deferred(executor).image_encode(files)
encoded = await call  # or: call.wait()

call.status polls without waiting, reading Status.DEFERRED until the call is final. await re-polls it on the next turn of the running loop through loop.call_soon, never sleeping and parking no thread, so calls on two executors run together and asyncio.gather awaits both:

async def fan_out(model, left, right, files, texts):
    images = model.deferred(left).image_encode(files)
    captions = model.deferred(right).text_encode(texts)
    return await asyncio.gather(images, captions)  # both submitted before either is awaited

Dropping a Deferred waits for its call first, since the library still reads the inputs it holds. A trainer runs step and save blocking on the executor it was created on, and trainer.on(...) and trainer.deferred(...) choose another completion.

Executors

uform.Executor.for_ram(threads=0, *, memory_domains=None, cpu_class_ceiling=None) creates an executor of host threads, and uform.Executor.for_vram(devices=None, *, scratch_limit=0, gpu_class_ceiling=None) opens one stream per device. A device is named by its position in uform.gpu_devices(), which lists every GPU the process can drive as a uform.GPUDevice, NVIDIA devices first, then AMD, then Apple. A uform.CPUClass or uform.GPUClass ceiling dispatches as the best class under it the hardware runs, and None sets no ceiling. An executor runs one call at a time, and a call finding another one holding it raises ContendedError once waited for. A deferred call on the executor's own threads finishes while Python only awaits it, and its progress callbacks run on those threads, holding the GIL while they do. executor.drain(uform.Mood.PARKED) waits until no call holds the executor and its devices are idle, then applies the mood. In a with block, it closes on exit, which never raises: later verbs naming it raise UninitializedError, and its threads or streams are freed once the last arena, result and receiver holding it is gone.

Arenas

Every output whose length only the call knows, the embeddings, the states and their offsets, lands in a uform.Arena, and the verb answers views over it without copying. A call given no arena makes a fresh one on its executor, which its result owns, so results of separate calls never collide. Passing one reuses it: uform.Arena(executor) places it where that executor computes, decided once, and uform.Arena() makes host pages for the calling thread. Each call naming an arena rewinds it, so a steady loop allocates once, and a result of an earlier call on it raises ValueError once read. A live memoryview, NumPy view, DLPack capsule, Arrow array or CUDA Array Interface consumer of a result keeps its arena from rewinding: a call naming it raises ContendedError, which is also a BufferError, as does a call naming an arena another call holds. A long-lived view, such as np.from_dlpack of a result kept across calls, belongs on the arena-less path, where its arena is its own. An arena from a VRAM executor holds device memory: the buffer protocol raises BufferError wherever the host may not read the pages, and CUDA device and managed memory also export __cuda_array_interface__ for CuPy or Numba. An Apple GPU arena's outputs cross as host memory, through kDLCPU, plain Arrow arrays and the buffer protocol, and Metal tensors or Arrow arrays offered as inputs raise UnsupportedError, as no Metal exchange is supported yet. Every arena and result holds the executor it was made on, so results stay readable after the executor's with block ends, and the executor is freed once the last of them is gone.

arena = uform.Arena(executor)
for batch in batches:
    encoded = model.on(executor).text_encode(batch, arena)  # rewinds: the last batch's results are stale
    index.add(np.array(encoded.embeddings))  # a copy, which outlives the next rewind and holds no view

Encoding

One verb per input form, each spelled as in C:

  • text_encode(texts) takes UTF-8 texts, a byte per token, since models are tokenizer-free.
  • image_encode(files) takes JPEG, PNG or WebP files.
  • image_encode_pixels(pixels) takes C-contiguous uint8 buffers of shape [height, width, 3].
  • video_encode(files) takes MP4 or WebM clips.
  • audio_encode(files) takes WAV, FLAC, MP3 or Opus recordings.
  • audio_encode_samples(samples, sample_rate) takes 1-D float32 buffers.

A batch is a sequence of separate objects, str or bytes-like for texts and bytes-like for files, gathered into one tape with one copy. A (data, offsets) tuple of buffers, the data and count + 1 offsets of the size_t type, is read in place as a tape without a copy, and both buffers must stay alive and unchanged until the call is final:

data = b"first documentthe second"
offsets = np.array([0, 14, 24], dtype=np.uint64)
encoded = model.text_encode((data, offsets))

Each member of the pair crosses through DLPack first, a versioned capsule before a legacy one, then a primitive Arrow array, a device export before a host one, then the CUDA Array Interface on a CUDA arena, then the buffer protocol; device memory crosses only onto the GPU the arena lives on. On a GPU arena, a DLPack producer on that vendor's device is handed the arena's first stream to order its writes before, and the stream a CUDA Array Interface names is waited on from that first stream, so the host never blocks. An Apache Arrow array or stream of strings, binaries or lists of float32 is read the same way, as Arrow lays them out with offsets too: 64-bit offsets in place, 32-bit ones widened while the bytes stay in place, and a stream of several chunks gathered with one copy. Arrow nulls are refused, and its offsets must be memory the host may read, as they drive host-side dispatch, so for an array carrying a sync_event the arena's first stream follows it and the host waits for that stream before reading:

texts = pyarrow.array(["first document", "the second"], pyarrow.large_string())
encoded = model.text_encode(texts)

Every output exports itself in place: __dlpack__ for NumPy, PyTorch or CuPy, __arrow_c_array__ and __arrow_c_device_array__ for PyArrow, rows as fixed-size lists of their columns, and a RowsTape also as __arrow_c_stream__, a large list of each item's rows. While any such export is alive, a call naming its arena raises ContendedError, a BufferError, so a consumer never reads rows a later call rewrote. A stream handed to __dlpack__ must sit on the output's device, and once the consumer drops the capsule, the arena's next call waits for the work queued on that stream before it rewinds.

outputs names the surfaces a call keeps, and decides which C output pointers the binding passes:

encoded = model.text_encode(texts, outputs=uform.Outputs.EMBEDDINGS | uform.Outputs.STATES)
embeddings, states, token_vectors = encoded  # an `ArenaBuffer` of (count, dimensions), a `RowsTape`, and None
offsets = np.asarray(states.offsets)  # item `i` spans rows `offsets[i]` up to `offsets[i + 1]`

Outputs.EMBEDDINGS is the default, and a surface left out answers None and costs nothing past the final norm. Outputs.TOKEN_VECTORS keeps the token head's unit vectors, a RowsTape of (tokens, token_width) on the states' offsets, for late-interaction reranking; a model whose describe().token_width is zero raises UnsupportedError. outputs_scalar picks the scalar every output lands as, Scalar.F32 by default; Scalar.BF16 outputs export as raw bytes, a (count, row_bytes) run of 'B'. stats=uform.TextEncodeStats() and its siblings are overwritten with the truncated items and the encoded tokens. progress=handler is called with (completed, total) items, and a handler returning False stops the call, which raises uform.CancelledError; one that raises is reported through sys.unraisablehook and stops the call, which raises CancelledError too.

Choices

text_choose(texts, choices) scores each text's legal single-byte choices through one forward, a softmax over them alone at the calibrated inverse temperature of the text's question type:

probabilities = model.text_choose(["keep this candidate? ", "expand which neighbour? "], ["yn", "ABCD"])
offsets = np.asarray(probabilities.offsets)  # [0, 2, 6]: one float32 per choice, in `choices` order

temperature_groups names each text's question type, below the model's choice_temperature_groups, and None puts every text in group zero. A model without the byte decoder, its choice_temperature_groups zero, raises UnsupportedError.

Models

uform.Model.load_ram(path) and uform.Model.load_vram(path, devices=[0]) copy a safetensors checkpoint into host memory or onto GPUs, and load_ram_buffer and load_vram_buffer take the checkpoint's bytes instead. Each takes quantization, Scalar.BF16 as stored or Scalar.E4M3 quantized at load, an optional executor the load runs on, and a progress handler. The RAM loads take a cpu_class_ceiling and the VRAM loads a gpu_class_ceiling, and devices sharing no class under the ceiling raise UnsupportedError. model.describe() answers a uform.ModelDescription of dimensions, context, quantization, memory_usage, cpu_class, gpu_class, token_width and choice_temperature_groups.

model = uform.Model.load_ram("model.safetensors", quantization=uform.Scalar.E4M3)  # Float8 blocks, half the bytes
described = model.describe()
print(described.dimensions, described.context, described.cpu_class, described.memory_usage)

Training

Training is GPU-only. The configuration arrives as keyword-built structures mirroring the C ones field by field, so a field left out is zero, which the library derives:

executor = uform.Executor.for_vram(devices=[0, 1])
mesh = uform.Mesh.bootstrap(
    executor,
    uform.MeshOptions(host_domain_index=rank, host_domain_count=nodes, directory="/shared/exchange", job="run-17"),
)
trainer = uform.Trainer.init_vram(
    executor,
    mesh=mesh,
    depth=12,
    hidden=512,
    objective=uform.Objective.JOINT,
    batch=uform.BatchShape(
        max_rows_per_device=64,
        max_segments_per_device=256,
        max_columns_per_row=1024,
        max_bags_per_device=128,
        image=uform.ImageShape(patch_side=16),
    ),
    schedule=uform.Schedule(steps=100_000),
    seed=42,
)
stats = uform.TrainerStepStats()
trainer.step(modalities, contents, bags=bags, relations=uform.Relations(attract=(offsets, ids)), stats=stats)
trainer.save("checkpoint.safetensors")

BatchShape nests an ImageShape, a VideoShape and an AudioShape, each also taken as a tuple of its fields in order. Masking, Contrastive, LateInteraction, Choices and Schedule configure the objectives, and Relations holds three Edges sets of buffers: size_t offsets, uint32 IDs and optional float32 weights. Contrastive and LateInteraction take a uform.PairLossForm, and trainer.step takes each item's uform.LateInteractionSide as sides. choices, taken as text_choose takes them, and choice_targets, a float32 tape shaped as the RowsTape it answers, train the calibrated choices within the batch's max_choices_per_item, so (probabilities, probabilities.offsets) from one model can train another. Paths, like the mesh's directory, cross as os.fsencode gives their bytes, while its job is a str name. trainer.step answers the pooled embeddings the loss scored when given an arena, and None otherwise. mesh.describe() answers a uform.MeshDescription, its multimem_tier a uform.MultimemTier and its gpu_class the uform.GPUClass every GPU of the run dispatches as.

Errors

Every failure raises a subclass of uform.Error, itself a RuntimeError, and each holds the status the C ABI reported as its status attribute, a uform.Status member:

try:
    model.on(executor).text_encode(texts, progress=lambda completed, total: completed < 100)
except uform.CancelledError:
    ...  # The handler stopped the call, which published nothing
except uform.Error as error:
    print(error.status)  # Status.SHAPE_MISMATCH, Status.DEVICE, ...

SUCCESS and DEFERRED raise nothing, and every other member has its own class, which also derives the built-in naming the same fault where one exists:

  • OUT_OF_MEMORY → OutOfMemoryError, a MemoryError
  • SHAPE_MISMATCH → ShapeMismatchError, a ValueError
  • UNSUPPORTED_LAYOUT → UnsupportedLayoutError, a ValueError
  • UNKNOWN_KIND → UnknownKindError, a ValueError
  • IO → InputOutputError, an OSError
  • DEVICE → DeviceError
  • MALFORMED → MalformedError, a ValueError
  • INTERNAL → InternalError
  • OVERFLOW → CapacityOverflowError, an OverflowError
  • UNINITIALIZED → UninitializedError
  • CONFIG_MISMATCH → ConfigMismatchError, a ValueError
  • CONTENDED → ContendedError, a BufferError
  • CANCELLED → CancelledError
  • UNSUPPORTED → UnsupportedError, a NotImplementedError
  • INVALID_ARGUMENT → InvalidArgumentError, a ValueError

A failure the binding catches before the boundary raises the class the library would have raised for the same fault. Arguments the binding refuses raise at the call, while the library's refusals of a deferred call raise at await or wait().

Design

  • A thin layer over the C ABI. One module per header, types, model, encode and train, one method per exported verb spelled as in C, and enums mirroring the C enums as enum.IntEnum classes built from the C name functions, never strings.
  • The receiver chooses, the verb stays. The model runs a verb on the calling thread, on(executor) blocks on an executor, and deferred(executor) submits it, all with the same arguments.
  • Deferred calls use the language's own idioms. status polls through uform_outcome_status, wait blocks through uform_outcome_wait, and await re-polls status once per turn of the running loop through loop.call_soon, so no thread waits.
  • No life of its own. The binding starts no threads and keeps no queues or caches; releasing a Deferred rides reference counting, which waits for the call first.
  • Arenas are fresh by default and reused by choice, never shared. Every output verb answers views into an arena, its own unless the caller passes one; a result is current until the next call on that arena, a live view keeps that call from rewinding it, and the library refuses an arena another call holds.
  • Nothing costs anything until it is used. Blocking calls take no deferred-table entry, a surface outputs leaves out is never computed, and an arena allocates only when a call writes into it.
  • Blocking calls stay as simple as the language allows. model.text_encode(texts) takes no executor, completion, receiver or arena argument and returns its result.
  • Suites run against the stub library, and against the real one with the tiny checkpoint UFORM_TEST_CHECKPOINT names.