Skip to main content

FloatCastAdapter

Struct FloatCastAdapter 

Source
pub struct FloatCastAdapter { /* private fields */ }
Expand description

Adapter that casts every float tensor to one target dtype.

Unlike HalfPrecisionAdapter, which infers the direction from the source dtype (F32 <-> F16 only) and converts a fixed list of module types, this adapter is target-driven: every float tensor (F64, F32, Flex32, F16, BF16) is converted to the given dtype regardless of which module it belongs to. Non-float tensors (ints, bools, quantized) and tensors already in the target dtype pass through unchanged.

This is the right tool for loading a stock checkpoint into a module whose backend runs at a different float precision, e.g. BF16 safetensors into an F16 model. HalfPrecisionAdapter passes BF16 tensors through unconverted and skips custom module types entirely, so the loaded params silently keep the source dtype instead of the module’s.

§Examples

Cast everything to the backend’s float element on load:

ⓘ
let adapter = FloatCastAdapter::to(<B::FloatElem as Element>::dtype());
// store.with_from_adapter(adapter);

Chain after a framework adapter:

let adapter = PyTorchToBurnAdapter.chain(FloatCastAdapter::to(DType::F16));

Implementations§

Source§

impl FloatCastAdapter

Source

pub fn to(target: DType) -> Self

Create an adapter that casts every float tensor to target.

§Panics

Panics if target is not a float dtype.

Trait Implementations§

Source§

impl Clone for FloatCastAdapter

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
Source§

impl Debug for FloatCastAdapter

Source§

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

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

impl ModuleAdapter for FloatCastAdapter

Source§

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

Adapt a tensor given where in the module hierarchy it was found. Read more
Source§

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

Clone the adapter into a boxed trait object
Source§

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

Get alternative parameter name to try during matching Read more
Source§

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

Chain adapters together, applying self first and then next. 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> MaybeSendSync for T
where T: Send + Sync,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.