Skip to main content

ModuleAdapter

Trait ModuleAdapter 

Source
pub trait ModuleAdapter: Send + Sync {
    // Required methods
    fn adapt(&self, tensor: PackTensor, ctx: ModuleContext<'_>) -> PackTensor;
    fn clone_box(&self) -> Box<dyn ModuleAdapter>;

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

Trait for adapting tensors between different module formats

Required Methods§

Source

fn adapt(&self, tensor: PackTensor, ctx: ModuleContext<'_>) -> PackTensor

Adapt a tensor given where in the module hierarchy it was found.

Takes the tensor by value so a pass-through costs nothing, and returns one whose data is still unmaterialized: a transform is composed onto the byte source rather than run here.

An adapter that changes dtype or shape must declare the resulting byte length, which means going through bridge::map_data rather than assigning those fields: the writer reserves space from byte_len before any provider runs.

tensor.name is the parameter’s path in the module being traversed, not the key it was stored under. An adapter that renames sets it, and the next adapter in a chain sees the renamed value.

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, _module_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
  • module_type - The user-defined module the parameter sits in (e.g. “Struct: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(tensor, ctx), ctx)
  • 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

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§