KeyRemapper

Struct KeyRemapper 

Source
pub struct KeyRemapper {
    pub patterns: Vec<(Regex, String)>,
}
Expand description

Key remapper for transforming tensor names.

This allows mapping tensor names from one naming convention to another, which is useful for loading models from different frameworks or versions.

§Examples

// Create a key remapper
let remapper = KeyRemapper::new()
    .add_pattern(r"^pytorch\.(.*)", "burn.$1").expect("valid regex")  // pytorch.layer -> burn.layer
    .add_pattern(r"\.gamma$", ".weight").expect("valid regex");       // layer.gamma -> layer.weight

// Use remapper with stores
// store.remap(remapper)

Fields§

§patterns: Vec<(Regex, String)>

Pattern-based remapping rules (regex pattern, replacement string)

Implementations§

Source§

impl KeyRemapper

Source

pub fn new() -> Self

Create a new empty key remapper

Source

pub fn add_pattern<S1, S2>(self, from: S1, to: S2) -> Result<Self, Error>
where S1: AsRef<str>, S2: Into<String>,

Add a remapping pattern (compiles regex)

§Arguments
  • from - Source pattern (regex string)
  • to - Replacement string (can include capture groups like $1)
§Returns
  • Ok(Self) - Updated remapping configuration
  • Err(regex::Error) - If regex compilation fails
Source

pub fn from_compiled_patterns(patterns: Vec<(Regex, String)>) -> Self

Create from a list of compiled regex patterns

Source

pub fn from_patterns<S1, S2>(patterns: Vec<(S1, S2)>) -> Result<Self, Error>
where S1: AsRef<str>, S2: Into<String>,

Create from string patterns (will compile to regex)

§Arguments
  • patterns - Vector of (pattern, replacement) tuples
§Returns
  • Ok(Self) - New remapping configuration
  • Err(regex::Error) - If any regex compilation fails
Source

pub fn from_pattern_iter<I, S1, S2>(iter: I) -> Result<Self, Error>
where I: IntoIterator<Item = (S1, S2)>, S1: AsRef<str>, S2: Into<String>,

Create from an iterator of patterns

§Arguments
  • iter - Iterator yielding (pattern, replacement) tuples
§Returns
  • Ok(Self) - New remapping configuration
  • Err(regex::Error) - If any regex compilation fails
Source

pub fn is_empty(&self) -> bool

Check if the remapping is empty

Source

pub fn to_regex_pairs(&self) -> Vec<(Regex, String)>

Convert to the format expected by remap_tensor_paths_with_patterns

Source

pub fn remap( &self, tensors: Vec<TensorSnapshot>, ) -> (Vec<TensorSnapshot>, Vec<(String, String)>)

Remap tensor paths using the configured patterns.

§Arguments
  • tensors - Vec of TensorSnapshots to remap
§Returns

A tuple containing:

  • The remapped Vec of TensorSnapshots with updated paths
  • A vector of (new_path, original_path) showing the transformations

Trait Implementations§

Source§

impl Clone for KeyRemapper

Source§

fn clone(&self) -> KeyRemapper

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyRemapper

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for KeyRemapper

Source§

fn default() -> KeyRemapper

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V