Skip to main content

ModuleAdapter

Trait ModuleAdapter 

Source
pub trait ModuleAdapter: Send + Sync {
    // Required methods
    fn adapt(&self, snapshot: &TensorSnapshot) -> TensorSnapshot;
    fn clone_box(&self) -> Box<dyn ModuleAdapter>;

    // Provided methods
    fn get_alternative_param_name(
        &self,
        _param_name: &str,
        _container_type: &str,
    ) -> Option<String> { ... }
    fn chain<A>(self, next: A) -> ChainAdapter
       where Self: Sized + 'static,
             A: ModuleAdapter + 'static { ... }
}
Expand description

Trait for adapting tensor snapshots between different module formats

Required Methods§

Source

fn adapt(&self, snapshot: &TensorSnapshot) -> TensorSnapshot

Adapt a tensor snapshot based on its container type and parameter name

Source

fn clone_box(&self) -> Box<dyn ModuleAdapter>

Clone the adapter into a boxed trait object

Provided Methods§

Source

fn get_alternative_param_name( &self, _param_name: &str, _container_type: &str, ) -> Option<String>

Get alternative parameter name to try during matching

When looking for a parameter in a module, this method provides an alternative name to try if the direct name doesn’t match. This enables matching parameters with different naming conventions (e.g., PyTorch’s “weight” vs Burn’s “gamma”).

§Arguments
  • param_name - The parameter name we’re looking for
  • container_type - The type of container module (e.g., “BatchNorm”)
§Returns

Alternative parameter name to try, or None if no alternative exists

Source

fn chain<A>(self, next: A) -> ChainAdapter
where Self: Sized + 'static, A: ModuleAdapter + 'static,

Chain adapters together, applying self first and then next.

This is useful when multiple transformations are required when importing model weights (e.g. PyTorch -> Burn layout conversion, then dtype casting, then custom remapping).

The semantics follow a simple pipeline:

  • adapt: next.adapt(&self.adapt(snapshot))
  • get_alternative_param_name: try self first; if it returns an alternative name, try next with that name, otherwise return the first alternative name.

Trait Implementations§

Source§

impl Clone for Box<dyn ModuleAdapter>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Implementors§