Skip to main content

VarBuilder

Struct VarBuilder 

Source
pub struct VarBuilder<'a, R: Runtime> { /* private fields */ }
Expand description

Scoped access to weights in a VarMap.

VarBuilder holds a reference to a VarMap and a prefix string for navigating hierarchical weight names (e.g., “model.layers.0.self_attn”).

Implementations§

Source§

impl<'a, R: Runtime> VarBuilder<'a, R>

Source

pub fn new(varmap: &'a mut VarMap<R>, device: &'a R::Device) -> Self

Create a root VarBuilder.

Source

pub fn push_prefix(&mut self, segment: &str) -> VarBuilder<'_, R>

Create a sub-builder with an additional prefix component.

Source

pub fn pp(&mut self, segment: &str) -> VarBuilder<'_, R>

Alias for push_prefix.

Source

pub fn get(&self, name: &str) -> Result<&Weight<R>>

Get a weight by name (relative to prefix).

Source

pub fn get_tensor(&self, name: &str) -> Result<&Tensor<R>>

Get a standard tensor by name (relative to prefix).

Source

pub fn get_quant_tensor(&self, name: &str) -> Result<&QuantTensor<R>>

Get a quantized tensor by name.

Source

pub fn take_tensor(&mut self, name: &str) -> Result<Tensor<R>>

Take a standard tensor by name, removing it from the map (zero-copy).

Source

pub fn take_tensor_optional(&mut self, name: &str) -> Result<Option<Tensor<R>>>

Take a standard tensor by name if it exists, returning None if absent.

Useful for tensors that only exist in some architectures (e.g., attention biases, Q/K layer norms for Command-R).

Source

pub fn take_weight_optional(&mut self, name: &str) -> Result<Option<Weight<R>>>

Take a weight by name if it exists, returning None if absent.

Source

pub fn take_maybe_quant_linear_optional( &mut self, name: &str, bias_name: Option<&str>, ) -> Result<Option<MaybeQuantLinear<R>>>

Take a MaybeQuantLinear if the weight exists, returning None if absent.

Source

pub fn take_quant_tensor(&mut self, name: &str) -> Result<QuantTensor<R>>

Take a quantized tensor by name, removing it from the map (zero-copy).

Source

pub fn take_weight(&mut self, name: &str) -> Result<Weight<R>>

Take a weight (standard or quantized) by name, removing it from the map.

Source

pub fn take_maybe_quant_linear( &mut self, name: &str, bias_name: Option<&str>, ) -> Result<MaybeQuantLinear<R>>

Take a weight and construct a MaybeQuantLinear from it.

If bias_name is provided, attempts to take a standard tensor for bias.

Source

pub fn get_with_shape( &self, name: &str, expected_shape: &[usize], ) -> Result<&Tensor<R>>

Get a standard tensor and validate its shape.

Source

pub fn device(&self) -> &R::Device

Device for this builder.

Source

pub fn contains(&self, name: &str) -> bool

Check if a name exists (relative to prefix).

Source

pub fn prefix(&self) -> &str

Current prefix.

Source

pub fn take_tensor_dequant( &mut self, name: &str, target_dtype: DType, ) -> Result<Tensor<R>>
where R: Runtime<DType = DType>, R::Client: DequantOps<R>,

Take a tensor by name, dequantizing if it’s quantized.

Useful for weights like embeddings that must be standard tensors but may be stored quantized in GGUF files.

Source

pub fn take_tensor_shard( &mut self, name: &str, dim: usize, rank: usize, world_size: usize, ) -> Result<Tensor<R>>

Take a tensor and narrow it along dim for the given TP rank.

Takes the full tensor from the VarMap, narrows to the rank’s shard along dim, returns contiguous shard. The full tensor is removed from the VarMap (zero-copy take, then narrow).

Column-parallel uses dim=0, row-parallel uses dim=1.

Source§

impl<R: Runtime> VarBuilder<'static, R>

Source

pub fn from_var_map(varmap: Box<VarMap<R>>, device: &'static R::Device) -> Self

Create a VarBuilder from a boxed VarMap.

Takes ownership of the VarMap by boxing and leaking it to obtain a 'static reference, which is required for VarBuilder<'static, R>. This is appropriate when the VarMap must outlive any particular scope.

Auto Trait Implementations§

§

impl<'a, R> Freeze for VarBuilder<'a, R>

§

impl<'a, R> RefUnwindSafe for VarBuilder<'a, R>

§

impl<'a, R> Send for VarBuilder<'a, R>

§

impl<'a, R> Sync for VarBuilder<'a, R>

§

impl<'a, R> Unpin for VarBuilder<'a, R>

§

impl<'a, R> UnsafeUnpin for VarBuilder<'a, R>

§

impl<'a, R> !UnwindSafe for VarBuilder<'a, R>

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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
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> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
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.