Skip to main content

Collector

Struct Collector 

Source
pub struct Collector {
    pub tensors: Vec<Tensor>,
    /* private fields */
}
Expand description

Collects a module’s tensors without copying data.

This collector traverses a module hierarchy and produces burn_pack::Tensors whose data is read back from the device only on demand, so a module can be walked for its structure and metadata alone.

§Examples

§Collect all tensors

let collector = Collector::new(None, None, false);
// Use with module.visit(&mut collector);
let all_tensors = collector.tensors;

§Filter with single pattern

let filter = PathFilter::new().with_regex(r"^encoder\..*");
let collector = Collector::new(Some(filter), None, false);
// Use with module.visit(&mut collector);
// Only collects tensors starting with "encoder."

§Filter with multiple patterns (OR union)

let filter = PathFilter::new()
    .with_regex(r"^encoder\..*")  // Match all encoder tensors
    .with_regex(r".*\.bias$");    // OR match any bias tensors
let collector = Collector::new(Some(filter), None, false);
// Use with module.visit(&mut collector);
// Collects tensors matching ANY of the patterns

Fields§

§tensors: Vec<Tensor>

The collected tensors, already adapted.

Implementations§

Source§

impl Collector

Source

pub fn new( filter: Option<PathFilter>, adapter: Option<Box<dyn ModuleAdapter>>, skip_enum_variants: bool, ) -> Self

Create a new collector with an optional filter and adapter.

§Arguments
  • filter - An optional PathFilter to determine which tensors to collect. When None, all tensors are collected.
  • adapter - Optional adapter to transform tensors based on container types. Applied to each tensor as it is collected.
  • skip_enum_variants - Skip enum variant names when building paths. When true, paths will not include enum variant names (e.g., “feature.weight” instead of “feature.BaseConv.weight”). Useful when exporting to formats like PyTorch that don’t use enum variants.
§Examples
// Collect all tensors without adapter
let collector = Collector::new(None, None, false);

// Use PathFilter builder
let filter = PathFilter::new()
    .with_regex(r"^encoder\..*")
    .with_full_path("decoder.weight");
let collector = Collector::new(Some(filter), None, false);

// Skip enum variants for PyTorch export
let collector = Collector::new(None, None, true);
Source

pub fn into_tensors(self) -> Vec<PackTensor>

Return the collected tensors.

Trait Implementations§

Source§

impl Default for Collector

Source§

fn default() -> Self

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

impl ModuleVisitor for Collector

Source§

fn enter_module(&mut self, name: &str, container_type: &str)

Called when entering a submodule. Read more
Source§

fn exit_module(&mut self, _name: &str, container_type: &str)

Called when exiting a submodule. Read more
Source§

fn visit_float<const D: usize>(&mut self, param: &Param<Tensor<D>>)

Visit a float parameter in the module. Read more
Source§

fn visit_int<const D: usize>(&mut self, param: &Param<Tensor<D, Int>>)

Visit an int parameter in the module. Read more
Source§

fn visit_bool<const D: usize>(&mut self, param: &Param<Tensor<D, Bool>>)

Visit a bool parameter in the module. Read more
Source§

fn visit_float_with_path<const D: usize>( &mut self, path: &[String], id: ParamId, tensor: &Tensor<D>, )

Visit a float tensor with its full module path. Read more
Source§

fn visit_int_with_path<const D: usize>( &mut self, path: &[String], id: ParamId, tensor: &Tensor<D, Int>, )

Visit an int tensor with its full module path. Read more
Source§

fn visit_bool_with_path<const D: usize>( &mut self, path: &[String], id: ParamId, tensor: &Tensor<D, Bool>, )

Visit a bool tensor with its full module path. Read more
Source§

fn visit_flag(&mut self, flag: &Param<Flag>)

Visit a Param<Flag> in the module. 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> MaybeSendSync for T
where T: Send + Sync,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.