Skip to main content

WeightMap

Struct WeightMap 

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

Map of tensor name → (f32 data, shape).

Implementations§

Source§

impl WeightMap

Source

pub fn from_weight_loader(loader: &mut dyn WeightLoader) -> Result<Self>

Drain every tensor from any WeightLoader (safetensors or GGUF).

Source

pub fn from_weight_loader_dequant_all( loader: &mut dyn WeightLoader, ) -> Result<Self>

Force-dequantize every tensor (including K-quants) into F32 and drop it in the map. Use when a family runner doesn’t have a packed-matmul lowering yet but still wants to load GGUFs whose trunk weights are K-quant. Trades memory (4× larger than the packed bytes) for correctness — every tensor goes through WeightLoader::take(...) which dequantizes on the fly.

Source

pub fn drain_loader( loader: &mut dyn WeightLoader, policy: WeightDrainPolicy, ) -> Result<(Self, Vec<NamedPackedWeight>)>

Drain with policy; returns packed K-quants separately when the loader supports take_packed.

Source

pub fn from_resolved_path(path: &Path) -> Result<Self>

Resolve a file or weights directory, then load (safetensors or GGUF).

Source

pub fn from_resolved_safetensors_only(path: &Path, runner: &str) -> Result<Self>

Resolve path; reject .gguf with a hint naming the right runner.

Source

pub fn from_file(path: &str) -> Result<Self>

Load weights from a safetensors file. Auto-converts bf16/f16 to f32.

Source

pub fn from_file_excluding( path: &str, exclude: &HashSet<String>, ) -> Result<Self>

Load weights, skipping tensor names present in exclude (saves RAM when bf16/NVFP4 linears are loaded separately for GPU upload).

Source

pub fn take(&mut self, key: &str) -> Result<(Vec<f32>, Vec<usize>)>

Take a tensor by name (removes from map). Returns (data, shape).

Source

pub fn take_transposed(&mut self, key: &str) -> Result<(Vec<f32>, Vec<usize>)>

Take and transpose a 2D weight: [out, in] → [in, out] for row-major matmul.

Source

pub fn has(&self, key: &str) -> bool

Check if a key exists.

Source

pub fn keys(&self) -> impl Iterator<Item = &str>

List all keys.

Source

pub fn len(&self) -> usize

Number of tensors remaining.

Source

pub fn is_empty(&self) -> bool

Source

pub fn from_tensors(tensors: HashMap<String, (Vec<f32>, Vec<usize>)>) -> Self

Create from pre-built HashMap (for testing without safetensors files).

Source

pub fn snapshot_from_path(path: &str) -> Result<F32WeightSnapshot>

Drain all tensors into a snapshot map (for runners that rebuild graphs per shape).

Source

pub fn from_safetensors_dir_selected( dir: &Path, want: &HashSet<String>, ) -> Result<Self>

Load only tensors whose names appear in want (HF sharded checkpoints).

Source

pub fn from_safetensors_dir(dir: &Path) -> Result<Self>

Load and merge every *.safetensors file in dir (e.g. HF text_encoder/).

Source

pub fn remap_keys<F>(&mut self, f: F)
where F: FnMut(String) -> String,

Rename keys in-place (e.g. strip model. HuggingFace prefix).

Source

pub fn get(&self, key: &str) -> Option<(&[f32], &[usize])>

Borrow tensor data + shape without removing from the map.

Source

pub fn merge_add_weight(&mut self, key: &str, delta: &[f32]) -> Result<()>

Element-wise add delta into an existing rank-2 weight (PyTorch [out, in] layout).

Trait Implementations§

Source§

impl WeightLoader for WeightMap

Source§

fn format_id(&self) -> &'static str

Format id (safetensors, gguf, or a custom registration).
Source§

fn len(&self) -> usize

Number of distinct weights in the file.
Source§

fn take(&mut self, key: &str) -> Result<(Vec<f32>, Vec<usize>)>

Take the named tensor as (f32_data, shape). Removes from the loader so callers can detect “weights I never used.”
Source§

fn take_transposed(&mut self, key: &str) -> Result<(Vec<f32>, Vec<usize>)>

Same as take but transposed (last two dims swapped). Most safetensors weights are stored row-major-of-PyTorch convention, which RLX’s IR consumes column-major; this helper is the convention-bridge.
Source§

fn remaining_keys(&self) -> Vec<String>

Names that haven’t been taken yet — useful for “did the model use every weight?” hygiene checks.
Source§

fn is_empty(&self) -> bool

Source§

fn take_packed(&mut self, key: &str) -> Result<Option<PackedWeightTensor>>

Take packed K-quant bytes when supported; default returns None.
Source§

fn tensor_bytes_borrowed(&self, key: &str) -> Option<&[u8]>

Borrow packed bytes without marking taken (GGUF mmap path).
Source§

fn arch_hint(&self) -> Option<&str>

Architecture name from the underlying file (general.architecture for GGUF, None for safetensors). Drain-style consumers use this to pick an arch-specific reverse name mapping when the canonical HF name depends on the model family (e.g. Gemma 2’s 4 norms per layer don’t share the Llama 2-norm reverse alias).

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.