Rust · Models

Models

use uform::Model;use uform::ModelDescribe;use uform::ModelDescription;use uform::ModelLoadRamBufferConfig;use uform::ModelLoadRamConfig;use uform::ModelLoadVramBufferConfig;use uform::ModelLoadVramConfig;use uform::ModelVerbs;use uform::Residency; 4.0.0-rc.1 · 3ee5e534

Model::load_ram(path) and Model::load_vram(path) copy a safetensors checkpoint into host memory or onto GPUs, and load_ram_buffer and load_vram_buffer take the checkpoint’s bytes instead. A bare path or buffer loads on the calling thread with Scalar::BF16 blocks, and its config reaches the executor, the placement and its class pin, Scalar::E4M3 quantization and a progress handler:

let model = Model::load_vram(ModelLoadVramConfig {
    executor: Some(&executor),
    devices: &[0],
    quantization: Scalar::E4M3,
    ..ModelLoadVramConfig::from("model.safetensors")
})?;
let description = model.describe()?;                          // geometry, placement, classes and heads

A description reports the cpu_class or gpu_class the model dispatched as, and the heads the checkpoint carries: a nonzero token_width for token vectors, and a nonzero choice_temperature_groups for choices. Model carries no type parameter: quantization is the runtime Scalar passed at load, as in C.

API

StructModelA loaded encoder: its weights, their packed copies, and on a GPU the session its attention runs on, whose verbs come from ModelVerbs and EncodeVerbs.8
model.rs:334 ↗
pub struct Model
use uform::*;

let model = Model::load_ram("model.safetensors")?;
let executor = Executor::for_ram(&[], None, 0, 0)?;
let mut arena = Arena::new_in(&executor)?;
let encoded = model.on(&executor).text_encode(&["a caption"], Outputs::EMBEDDINGS, Scalar::F32, &mut arena)?;
if let Some(embeddings) = encoded.embeddings {
    println!("{} rows of {}", embeddings.count(), embeddings.columns());
}

Mirrors uform_model_t; dropping it calls uform_model_free.

Implements

Debug, Send, Sync, Drop, ModelVerbs<'model>
onThe receiver whose verbs block on executor, after the calls submitted there before them.
model.rs:357 ↗
pub fn on<'model>(&'model self, executor: &'model Executor) -> On<'model, Model>

Rust-only.

deferredThe receiver whose verbs submit their calls to executor, answering a Deferred from the same verbs given owned arguments.
model.rs:398 ↗
pub fn deferred<'model>(&'model self, executor: &'model Executor) -> On<'model, Model, Deferring>

The library reads a deferred call's inputs after the verb returns, so lending borrowed texts does not compile:

use uform::*;

let model = Model::load_ram("model.safetensors")?;
let executor = Executor::for_ram(&[], None, 0, 0)?;
let mut arena = Arena::new_in(&executor)?;
let texts = vec![String::from("a caption")];
let call = model.deferred(&executor).text_encode(&texts, Outputs::EMBEDDINGS, Scalar::F32, &mut arena)?;

Owned texts move into the call, and the arena is borrowed as a blocking call borrows it, and so is the waited result:

use uform::*;

let model = Model::load_ram("model.safetensors")?;
let executor = Executor::for_ram(&[], None, 0, 0)?;
let mut arena = Arena::new_in(&executor)?;
let texts = vec![String::from("a caption")];
let call = model.deferred(&executor).text_encode(texts, Outputs::EMBEDDINGS, Scalar::F32, &mut arena)?;
let encoded = call.wait()?;

Rust-only.

load_ramCopies a checkpoint file into host memory and packs its products for the CPU; a bare path loads on the calling thread in BFloat16.
model.rs:410 ↗
pub fn load_ram<'config>(config: impl Into<ModelLoadRamConfig<'config>>) -> Result<Self>

Calls uform_model_load_ram.

load_ram_bufferCopies a checkpoint held in memory into host memory and packs its products for the CPU.
model.rs:435 ↗
pub fn load_ram_buffer<'config>(
    config: impl Into<ModelLoadRamBufferConfig<'config>>,
) -> Result<Self>

Calls uform_model_load_ram_buffer.

load_vramCopies a checkpoint file onto the named GPUs and packs its products there.
model.rs:460 ↗
pub fn load_vram<'config>(config: impl Into<ModelLoadVramConfig<'config>>) -> Result<Self>

Calls uform_model_load_vram.

load_vram_bufferCopies a checkpoint held in memory onto the named GPUs and packs its products there.
model.rs:485 ↗
pub fn load_vram_buffer<'config>(
    config: impl Into<ModelLoadVramBufferConfig<'config>>,
) -> Result<Self>

Calls uform_model_load_vram_buffer.

*× 2The model this receiver reaches and the executor its calls run on.
model.rs:542 ↗

Inherited from ModelVerbs

fn handles(&self) -> Handles
*× 2Reports the model's geometry and placement in one call.
model.rs:531 ↗

Inherited from ModelVerbs

fn describe(
    self,
) -> <<Self as ModelVerbs<'model>>::Completion as Completion>::Answer<'model, ModelDescribe>

Calls uform_model_describe.

StructModelDescribeA report of the model's geometry; see ModelVerbs::describe.
model.rs:568 ↗
pub struct ModelDescribe

Calls uform_model_describe.

Implements

Send
StructModelDescriptionWhat a loaded model reports about itself.
model.rs:139 ↗
pub struct ModelDescription

Mirrors uform_model_description_t.

Fields

dimensionsusizeScalars per embedding row; Matryoshka prefixes of it are embeddings too.
contextusizeTokens one item may span before it is truncated: bytes of text, patches of an image, tubes of a clip, frames of a recording.
quantizationScalarThe scalar the transformer blocks run in.
memory_usageusizeBytes the model holds; activations are the executor's.
cpu_classOption<CPUClass>The class a RAM model's tower was packed for; None for a VRAM model.
gpu_classOption<GPUClass>The class every replica of a VRAM model runs as; None for a RAM model.
token_widthusizeScalars per token vector; zero when the checkpoint carries no token head.
choice_temperature_groupsusizeQuestion types the choice temperatures were calibrated over: zero without the byte decoder, one when uncalibrated.

Implements

Clone, Copy, StructuralPartialEq, PartialEq, Eq, Debug, Hash
StructModelLoadRamBufferConfigA checkpoint held in memory and how it is copied into a new model in host memory; see Model::load_ram_buffer.
model.rs:236 ↗
pub struct ModelLoadRamBufferConfig<'config>

Mirrors uform_model_load_ram_buffer_t.

Fields

buffer&'config [u8]The safetensors checkpoint itself, copied before the load completes.
executorOption<&'config Executor>The executor the load runs on; None loads on the calling thread.
memory_domains&'config [i32]NUMA node IDs to place the weights in; empty derives every node the process may allocate on.
cpu_classOption<CPUClass>The class to pack and dispatch as, as for ModelLoadRamConfig::cpu_class.
quantizationScalarThe scalar the transformer blocks run in, as for ModelLoadRamConfig::quantization.
progressOption<&'config mut ProgressCallback<'config>>Reports the tensors placed and stops wherever it says to; a stopped load creates no model.
progress_stepsusizeReports the call makes at most, the last at completion; zero derives to ten.

Implements

From<&'config [u8]>, From<&'config Bytes>
StructModelLoadRamConfigA checkpoint path and how it is copied into a new model in host memory; see Model::load_ram.
model.rs:163 ↗
pub struct ModelLoadRamConfig<'config>

Mirrors uform_model_load_ram_t.

Fields

path&'config PathA safetensors checkpoint, read in full and closed before the load completes.
executorOption<&'config Executor>The executor the load runs on; None loads on the calling thread.
memory_domains&'config [i32]NUMA node IDs to place the weights in; empty derives every node the process may allocate on.
cpu_classOption<CPUClass>The class to pack and dispatch as, admitting every rung whose features it holds; None derives the CPU's own.
quantizationScalarThe scalar the transformer blocks run in: Scalar::BF16 as the checkpoint stores it, or Scalar::E4M3 quantized at load.
progressOption<&'config mut ProgressCallback<'config>>Reports the tensors placed and stops wherever it says to; a stopped load creates no model.
progress_stepsusizeReports the call makes at most, the last at completion; zero derives to ten.

Implements

From<&'config Location>
StructModelLoadVramBufferConfigA checkpoint held in memory and how it is copied onto devices; see Model::load_vram_buffer.
model.rs:274 ↗
pub struct ModelLoadVramBufferConfig<'config>

Mirrors uform_model_load_vram_buffer_t.

Fields

buffer&'config [u8]The safetensors checkpoint itself, in host or device memory, copied before the load completes.
executorOption<&'config Executor>The executor the upload runs on, one stream per device; None uploads on the calling thread.
devices&'config [i32]Positions in what gpu_devices lists, each holding a replica; empty derives one free device.
gpu_classOption<GPUClass>The class every device dispatches as, as for ModelLoadVramConfig::gpu_class.
quantizationScalarThe scalar the transformer blocks run in, as for ModelLoadRamConfig::quantization.
progressOption<&'config mut ProgressCallback<'config>>Reports the tensors placed and stops wherever it says to; a stopped load creates no model.
progress_stepsusizeReports the call makes at most, the last at completion; zero derives to ten.

Implements

From<&'config [u8]>, From<&'config Bytes>
StructModelLoadVramConfigA checkpoint path and how it is copied onto devices; see Model::load_vram.
model.rs:199 ↗
pub struct ModelLoadVramConfig<'config>

Mirrors uform_model_load_vram_t.

Fields

path&'config PathA safetensors checkpoint, read in full and closed before the load completes.
executorOption<&'config Executor>The executor the upload runs on, one stream per device; None uploads on the calling thread.
devices&'config [i32]Positions in what gpu_devices lists, each holding a replica; empty derives one free device.
gpu_classOption<GPUClass>The class every device dispatches as; None derives the devices' own, which must agree.
quantizationScalarThe scalar the transformer blocks run in, as for ModelLoadRamConfig::quantization.
progressOption<&'config mut ProgressCallback<'config>>Reports the tensors placed and stops wherever it says to; a stopped load creates no model.
progress_stepsusizeReports the call makes at most, the last at completion; zero derives to ten.

Implements

From<&'config Location>
TraitModelVerbsEvery verb of the model header, written once for every receiver: &Model blocks on the calling thread, and the On that Model::on or Model::deferred makes runs on an executor.1
model.rs:521 ↗
pub trait ModelVerbs<'model>: Copy

A blocking receiver answers what the call produced; a deferring one answers a Deferred that borrows the model. EncodeVerbs extends it with the forward passes.

Rust-only.

Implementors

CompletionBlocking or Deferring.
model.rs:523 ↗
type Completion: Completion;
EnumResidencyWhere an executor computes, and with it where the arenas it makes keep their memory.
types.rs:1361 ↗
pub enum Residency

Mirrors uform_residency_t.

Values

Ram1Host pages, which the host reads directly.
Vram2One GPU's memory, which only device code reads.

Implements

Clone, Copy, StructuralPartialEq, PartialEq, Eq, Debug, Hash