pub struct Collector {
pub tensors: Vec<TensorSnapshot>,
/* private fields */
}Expand description
Collects tensor views from modules without copying data.
This collector traverses a module hierarchy and creates lightweight views
of tensors that can be materialized to TensorData on demand.
§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<TensorSnapshot>Collection of tensor views
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 tensor view 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 all collected tensors before returning.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<TensorSnapshot>
pub fn into_tensors(self) -> Vec<TensorSnapshot>
Apply the adapter to collected tensors and return the result.
Trait Implementations§
Source§impl<B: Backend> ModuleVisitor<B> for Collector
impl<B: Backend> ModuleVisitor<B> 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<B, D>>)
fn visit_float<const D: usize>(&mut self, param: &Param<Tensor<B, D>>)
Visit a float parameter in the module. Read more
Source§fn visit_int<const D: usize>(&mut self, param: &Param<Tensor<B, D, Int>>)
fn visit_int<const D: usize>(&mut self, param: &Param<Tensor<B, D, Int>>)
Visit an int parameter in the module. Read more
Source§fn visit_bool<const D: usize>(&mut self, param: &Param<Tensor<B, D, Bool>>)
fn visit_bool<const D: usize>(&mut self, param: &Param<Tensor<B, 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<B, D>,
)
fn visit_float_with_path<const D: usize>( &mut self, path: &[String], id: ParamId, tensor: &Tensor<B, D>, )
Visit a float tensor with its full module path. Read more
Auto Trait Implementations§
impl Freeze for Collector
impl !RefUnwindSafe for Collector
impl !Send for Collector
impl !Sync for Collector
impl Unpin for Collector
impl !UnwindSafe 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