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§
Sourcefn adapt(&self, tensor: PackTensor, ctx: ModuleContext<'_>) -> PackTensor
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.
Sourcefn clone_box(&self) -> Box<dyn ModuleAdapter>
fn clone_box(&self) -> Box<dyn ModuleAdapter>
Clone the adapter into a boxed trait object
Provided Methods§
Sourcefn get_alternative_param_name(
&self,
_param_name: &str,
_module_type: &str,
) -> Option<String>
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 formodule_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
Sourcefn chain<A>(self, next: A) -> ChainAdapterwhere
Self: Sized + 'static,
A: ModuleAdapter + 'static,
fn chain<A>(self, next: A) -> ChainAdapterwhere
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: tryselffirst; if it returns an alternative name, trynextwith that name, otherwise return the first alternative name.
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".