Skip to main content

WeightsContext

Struct WeightsContext 

Source
pub struct WeightsContext { /* private fields */ }
Expand description

Memory-mapped safetensors files and an index from tensor name to file.

Implementations§

Source§

impl WeightsContext

Source

pub fn new() -> Self

Creates an empty context with no files loaded.

Examples found in repository?
examples/read_tensor.rs (line 4)
3fn main() {
4    let _ctx = WeightsContext::new();
5    // Assuming a file exists or just demonstrating the API
6    println!("WeightsContext created.");
7
8    // Note: In a real scenario, we'd call ctx.load_file("path/to/model.safetensors")
9    println!("API Usage: ctx.load_file(path), then ctx.read_bytes(name, callback)");
10}
More examples
Hide additional examples
examples/load_and_run.rs (line 13)
5fn main() {
6    env_logger::init();
7    pollster::block_on(async {
8        // 1. Setup Backend
9        let ctx = Arc::new(WgpuContext::new().await);
10        let engine = ComputeEngine::new(&ctx);
11
12        // 2. Setup Context (Mocking usage since we don't have a real file in this test)
13        let _weights_ctx = WeightsContext::new();
14
15        println!("Demonstrating full pipeline (conceptual)...");
16
17        // 3. Create GPU Tensors
18        let shape = vec![4];
19        let gpu_int8 = UnifiedTensor::new(&ctx, &[1.0, 2.0, 3.0, 4.0], &shape);
20        let gpu_fp32 = UnifiedTensor::empty(&ctx, &shape);
21
22        // 4. Dequantize
23        engine.dequantize(&ctx, &gpu_int8, &gpu_fp32);
24
25        // 5. Sequential Ops instead of Fused
26        let gpu_c = UnifiedTensor::new(&ctx, &[2.0, 2.0, 2.0, 2.0], &shape);
27        let gpu_out = UnifiedTensor::empty(&ctx, &shape);
28        let temp = UnifiedTensor::empty(&ctx, &shape);
29
30        engine.add(&ctx, &gpu_fp32, &gpu_c, &temp);
31        engine.mul(&ctx, &temp, &gpu_c, &gpu_out);
32
33        let result = gpu_out.to_vec(&ctx).await;
34        println!("Result: {:?}", result);
35        println!("SUCCESS: Pipeline logic verified!");
36    });
37}
Source

pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), LoaderError>

Maps one safetensors file and indexes its tensor names.

Source

pub fn load_model_dir<P: AsRef<Path>>( &mut self, model_dir: P, ) -> Result<(), LoaderError>

Loads a model directory: sharded files via the index, else model.safetensors, else consolidated.safetensors.

Source

pub fn get_tensor_info(&self, name: &str) -> Option<(Vec<usize>, String)>

Returns the shape and dtype name of a tensor, or None if it is missing.

Source

pub fn get_tensor_metadata( &self, name: &str, ) -> Result<TensorMetadata, LoaderError>

Returns the shape and dtype of a tensor, or an error if it is missing.

Source

pub fn read_bytes<F>(&self, name: &str, callback: F) -> Result<(), LoaderError>
where F: FnOnce(&[u8], &[usize]),

Calls callback with the raw bytes and shape of a tensor.

Source

pub fn read_f32_tensor( &self, name: &str, ) -> Result<(Vec<f32>, Vec<usize>), LoaderError>

Reads an F32, F16 or BF16 tensor as f32 values plus its shape.

Source

pub fn upload_quantized( &self, name: &str, ctx: &Arc<WgpuContext>, engine: &ComputeEngine, ) -> Result<UnifiedTensor, LoaderError>

Reads <name>.weight, .scales and .biases and uploads them as one quantized GPU tensor.

Trait Implementations§

Source§

impl Default for WeightsContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,