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>
impl<'a, R: Runtime> VarBuilder<'a, R>
Sourcepub fn push_prefix(&mut self, segment: &str) -> VarBuilder<'_, R>
pub fn push_prefix(&mut self, segment: &str) -> VarBuilder<'_, R>
Create a sub-builder with an additional prefix component.
Sourcepub fn pp(&mut self, segment: &str) -> VarBuilder<'_, R>
pub fn pp(&mut self, segment: &str) -> VarBuilder<'_, R>
Alias for push_prefix.
Sourcepub fn get_tensor(&self, name: &str) -> Result<&Tensor<R>>
pub fn get_tensor(&self, name: &str) -> Result<&Tensor<R>>
Get a standard tensor by name (relative to prefix).
Sourcepub fn get_quant_tensor(&self, name: &str) -> Result<&QuantTensor<R>>
pub fn get_quant_tensor(&self, name: &str) -> Result<&QuantTensor<R>>
Get a quantized tensor by name.
Sourcepub fn take_tensor(&mut self, name: &str) -> Result<Tensor<R>>
pub fn take_tensor(&mut self, name: &str) -> Result<Tensor<R>>
Take a standard tensor by name, removing it from the map (zero-copy).
Sourcepub fn take_tensor_optional(&mut self, name: &str) -> Result<Option<Tensor<R>>>
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).
Sourcepub fn take_weight_optional(&mut self, name: &str) -> Result<Option<Weight<R>>>
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.
Sourcepub fn take_maybe_quant_linear_optional(
&mut self,
name: &str,
bias_name: Option<&str>,
) -> Result<Option<MaybeQuantLinear<R>>>
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.
Sourcepub fn take_quant_tensor(&mut self, name: &str) -> Result<QuantTensor<R>>
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).
Sourcepub fn take_weight(&mut self, name: &str) -> Result<Weight<R>>
pub fn take_weight(&mut self, name: &str) -> Result<Weight<R>>
Take a weight (standard or quantized) by name, removing it from the map.
Sourcepub fn take_maybe_quant_linear(
&mut self,
name: &str,
bias_name: Option<&str>,
) -> Result<MaybeQuantLinear<R>>
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.
Sourcepub fn get_with_shape(
&self,
name: &str,
expected_shape: &[usize],
) -> Result<&Tensor<R>>
pub fn get_with_shape( &self, name: &str, expected_shape: &[usize], ) -> Result<&Tensor<R>>
Get a standard tensor and validate its shape.
Sourcepub fn take_tensor_dequant(
&mut self,
name: &str,
target_dtype: DType,
) -> Result<Tensor<R>>
pub fn take_tensor_dequant( &mut self, name: &str, target_dtype: DType, ) -> Result<Tensor<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.
Sourcepub fn take_tensor_shard(
&mut self,
name: &str,
dim: usize,
rank: usize,
world_size: usize,
) -> Result<Tensor<R>>
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>
impl<R: Runtime> VarBuilder<'static, R>
Sourcepub fn from_var_map(varmap: Box<VarMap<R>>, device: &'static R::Device) -> Self
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> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
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>
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>
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 moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.