pub struct WeightsContext { /* private fields */ }Expand description
Memory-mapped safetensors files and an index from tensor name to file.
Implementations§
Source§impl WeightsContext
impl WeightsContext
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty context with no files loaded.
Examples found in repository?
More 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}Sourcepub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), LoaderError>
pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), LoaderError>
Maps one safetensors file and indexes its tensor names.
Sourcepub fn load_model_dir<P: AsRef<Path>>(
&mut self,
model_dir: P,
) -> Result<(), LoaderError>
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.
Sourcepub fn get_tensor_info(&self, name: &str) -> Option<(Vec<usize>, String)>
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.
Sourcepub fn get_tensor_metadata(
&self,
name: &str,
) -> Result<TensorMetadata, LoaderError>
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.
Sourcepub fn read_bytes<F>(&self, name: &str, callback: F) -> Result<(), LoaderError>
pub fn read_bytes<F>(&self, name: &str, callback: F) -> Result<(), LoaderError>
Calls callback with the raw bytes and shape of a tensor.
Sourcepub fn read_f32_tensor(
&self,
name: &str,
) -> Result<(Vec<f32>, Vec<usize>), LoaderError>
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.
Sourcepub fn upload_quantized(
&self,
name: &str,
ctx: &Arc<WgpuContext>,
engine: &ComputeEngine,
) -> Result<UnifiedTensor, LoaderError>
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§
Auto Trait Implementations§
impl Freeze for WeightsContext
impl RefUnwindSafe for WeightsContext
impl Send for WeightsContext
impl Sync for WeightsContext
impl Unpin for WeightsContext
impl UnsafeUnpin for WeightsContext
impl UnwindSafe for WeightsContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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