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 patternsFields§
§tensors: Vec<Tensor>The collected tensors, already adapted.
Implementations§
Source§impl Collector
impl Collector
Sourcepub fn new(
filter: Option<PathFilter>,
adapter: Option<Box<dyn ModuleAdapter>>,
skip_enum_variants: bool,
) -> Self
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 optionalPathFilterto determine which tensors to collect. WhenNone, 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);Sourcepub fn into_tensors(self) -> Vec<PackTensor>
pub fn into_tensors(self) -> Vec<PackTensor>
Return the collected tensors.
Trait Implementations§
Source§impl ModuleVisitor for Collector
impl ModuleVisitor for Collector
Source§fn enter_module(&mut self, name: &str, container_type: &str)
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)
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>>)
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>>)
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>>)
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>,
)
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>,
)
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>,
)
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>)
fn visit_flag(&mut self, flag: &Param<Flag>)
Visit a
Param<Flag> in the module. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for Collector
impl !UnwindSafe for Collector
impl Freeze for Collector
impl Send for Collector
impl Sync for Collector
impl Unpin for Collector
impl UnsafeUnpin for Collector
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