mlx_rs::nn

Struct QuantizedLinear

Source
pub struct QuantizedLinear {
    pub group_size: i32,
    pub bits: i32,
    pub scales: Param<Array>,
    pub biases: Param<Array>,
    pub inner: Linear,
}
Expand description

Applies an affine transformation to the input using a quantized weight matrix.

It is the quantized equivalent of Linear. For now its parameters are frozen and will not be included in any gradient computation but this will probably change in the future.

QuantizedLinear also provides several useful static to convert linear layers to QuantizedLinear layers.

Fields§

§group_size: i32

Quantization group size. Default to QuantizedLinear::DEFAULT_GROUP_SIZE

§bits: i32

Bits per parameter. Default to QuantizedLinear::DEFAULT_BITS

§scales: Param<Array>

Scales

§biases: Param<Array>

Biases

§inner: Linear

Inner linear layer

Implementations§

Source§

impl QuantizedLinear

Source

pub fn new( input_dims: impl Into<i32>, output_dims: impl Into<i32>, ) -> Result<Self, <<Self as Buildable>::Builder as Builder<Self>>::Error>

Creates a new instance of QuantizedLinear.

Source§

impl QuantizedLinear

Source

pub const DEFAULT_GROUP_SIZE: i32 = 64i32

Default group size

Source

pub const DEFAULT_BITS: i32 = 4i32

Default bits

Source

pub fn try_from_linear( linear: Linear, group_size: impl Into<Option<i32>>, bits: impl Into<Option<i32>>, ) -> Result<Self, Exception>

Convert a linear layer to a quantized linear layer.

§Params

Trait Implementations§

Source§

impl Buildable for QuantizedLinear

Source§

type Builder = QuantizedLinearBuilder

The builder type for this buildable type
Source§

impl Builder<QuantizedLinear> for QuantizedLinearBuilder

Source§

type Error = Exception

Error with building
Source§

fn build(self) -> Result<QuantizedLinear, Self::Error>

Build the type
Source§

impl Clone for QuantizedLinear

Source§

fn clone(&self) -> QuantizedLinear

Returns a copy 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 QuantizedLinear

Source§

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

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

impl Module<&Array> for QuantizedLinear

Source§

type Error = Exception

Error type for the module.
Source§

type Output = Array

Output type of the module.
Source§

fn forward(&mut self, x: &Array) -> Result<Array, Self::Error>

Forward pass of the module.
Source§

fn training_mode(&mut self, mode: bool)

Set whether the module is in training mode. Read more
Source§

impl ModuleParameters for QuantizedLinear

Source§

fn freeze_parameters(&mut self, recursive: bool)

Freeze all parameters in the module.
Source§

fn unfreeze_parameters(&mut self, recursive: bool)

Unfreeze all parameters in the module.
Source§

fn parameters(&self) -> ModuleParamRef<'_>

Get references to the module parameters.
Source§

fn parameters_mut(&mut self) -> ModuleParamMut<'_>

Get mutable references to the module parameters.
Source§

fn trainable_parameters(&self) -> ModuleParamRef<'_>

Get references to the trainable parameters. A parameter is trainable if it is NOT frozen.
Source§

fn all_frozen(&self) -> Option<bool>

Check if all parameters in the module are frozen. Returns None if there are no parameters.
Source§

fn any_frozen(&self) -> Option<bool>

Check if any parameter in the module is frozen. Returns None if there are no parameters.
Source§

fn update(&mut self, parameters: ModuleParam)

Update the module parameters.
Source§

fn update_flattened(&mut self, flattened_parameters: FlattenedModuleParam)

Update the module parameters from a flattened representation.
Source§

impl TryFrom<Linear> for QuantizedLinear

Source§

type Error = Exception

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

fn try_from(linear: Linear) -> Result<Self, Self::Error>

Performs the conversion.

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, dst: *mut u8)

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

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoOption<T> for T

Source§

fn into_option(self) -> Option<T>

Convert into an Option.
Source§

impl<T> IntoStrideBy for T

Source§

fn stride_by(self, stride: i32) -> StrideBy<T>

Create a stride indexing operation.
Source§

impl<T> ModuleParametersExt for T

Source§

fn eval(&self) -> Result<(), Exception>

Evaluate the module parameters.
Source§

fn load_safetensors(&mut self, path: impl AsRef<Path>) -> Result<(), IoError>

Load module parameters from a safetensors file.
Source§

fn save_safetensors(&self, path: impl AsRef<Path>) -> Result<(), IoError>

Save module parameters to a file in safetensors format.
Source§

impl<T> Parameter for T

Source§

fn freeze(&mut self, recursive: bool)

Freeze the parameter.
Source§

fn unfreeze(&mut self, recursive: bool)

Unfreeze the parameter.
Source§

fn is_frozen(&self) -> Option<bool>

Check if the parameter is frozen. Returns None if the parameter is a module that has no parameters.
Source§

fn as_nested_value(&self) -> NestedValue<Rc<str>, &Array>

Get the parameter as a nested value.
Source§

fn as_nested_value_mut(&mut self) -> NestedValue<Rc<str>, &mut Array>

Get the parameter as a mutable nested value.
Source§

fn as_trainable_nested_value(&self) -> Option<NestedValue<Rc<str>, &Array>>

Get the parameter as a nested value if it is trainable.
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<T> Updatable for T

Source§

fn updatable_states(&self) -> impl IntoIterator<Item = &Array>

Returns a list of references to the updatable states. Read more
Source§

fn updatable_states_mut(&mut self) -> impl IntoIterator<Item = &mut Array>

Returns a list of mutable references to the updatable states. Read more
Source§

impl<M> Activation for M
where M: UnaryModule<Error = Exception> + Debug + DynClone,

Source§

impl<T> SequentialModuleItem for T
where T: UnaryModule + Debug,

Source§

impl<T> UnaryModule for T
where T: for<'a> Module<&'a Array, Output = Array>,