Skip to main content

Mamba1

Struct Mamba1 

Source
pub struct Mamba1 {
    pub in_proj: Linear,
    pub conv1d: Conv1d,
    pub x_proj: Linear,
    pub dt_proj: Linear,
    pub a_log: Param<Tensor<2>>,
    pub d: Param<Tensor<1>>,
    pub out_proj: Linear,
}
Expand description

The Mamba-1 selective SSM block.

Fields§

§in_proj: Linear

Input channel: d_model. Output channel: 2 * d_inner.

§conv1d: Conv1d

Input channel: d_inner. Output channel: d_inner.

§x_proj: Linear

Input channel: d_inner. Output channel: dt_rank + 2 * state_rank.

§dt_proj: Linear

Input channel: dt_rank. Output channel: d_inner.

§a_log: Param<Tensor<2>>

Dims: [d_inner, state_rank].

§d: Param<Tensor<1>>

Dims: [d_inner].

§out_proj: Linear

Input channel: d_inner. Output channel: d_model.

Implementations§

Source§

impl Mamba1

Source

pub fn step( &self, x: Tensor<2>, cache: Option<Mamba1Cache>, ) -> (Tensor<2>, Mamba1Cache)

§Shapes
  • Input [batch, d_model]
  • Output [batch, d_model]
Source

pub fn ssm_step( &self, u: Tensor<2>, cache: Mamba1Cache, ) -> (Tensor<2>, Mamba1Cache)

Single-token counterpart of Mamba1::ssm: computes the selective-SSM parameters (Δ, A, B, C) for one token and advances the recurrence by one step via Self::selective_scan_step.

§Shapes
  • Input u [batch, d_inner]
  • Output [batch, d_inner]
Source

pub fn selective_scan_step( delta: Tensor<2>, a: Tensor<2>, b: Tensor<2>, c: Tensor<2>, d: Tensor<1>, u: Tensor<2>, cache: Mamba1Cache, ) -> (Tensor<2>, Mamba1Cache)

Selective Scan.

Does selective scan algorithm. See:

  • Section 2 State Space Models from the Mamba paper;
  • Algorithm 2 in Section 3.2 from the Mamba paper;
  • run_SSM(A, B, C, u) from The Annotated S4.
§Shapes
  • Input delta [batch, d_inner]
  • Input a [d_inner, state_rank]
  • Input b [batch, state_rank]
  • Input c [batch, state_rank]
  • Input d [d_inner]
  • Input u [batch, d_inner]
  • Output [batch, d_inner]
Source§

impl Mamba1

Source

pub fn forward( &self, x: Tensor<3>, cache: Option<Mamba1Cache>, ) -> (Tensor<3>, Mamba1Cache)

See also Self::step.

Mirrors crate::mamba2::mamba2::Mamba2::forward: an optional cache supplies the initial convolution window and SSM state (zero-initialised when None), and the updated cache is returned so a sequence can be processed in segments (prefill then decode, or chunked prefill).

§Shapes
  • Input [batch, sequence, d_model]
  • Output [batch, sequence, d_model]
Source

pub fn ssm(&self, u: Tensor<3>, init_ssm: Tensor<3>) -> (Tensor<3>, Tensor<3>)

Computes the selective-SSM parameters (Δ, A, B, C) from the conv output and runs the Self::selective_scan recurrence over the full sequence.

§Shapes
  • Input u [batch, sequence, d_inner]
  • Input init_ssm [batch, d_inner, state_rank]
  • Output [batch, sequence, d_inner]
  • Output (final state) [batch, d_inner, state_rank]
Source

pub fn selective_scan( delta: Tensor<3>, a: Tensor<2>, b: Tensor<3>, c: Tensor<3>, d: Tensor<1>, u: Tensor<3>, init_ssm: Tensor<3>, ) -> (Tensor<3>, Tensor<3>)

Selective Scan.

Does selective scan algorithm. See:

  • Section 2 State Space Models from the Mamba paper;
  • Algorithm 2 in Section 3.2 from the Mamba paper;
  • run_SSM(A, B, C, u) from The Annotated S4.
§Shapes
  • Input delta [sequence, batch, d_inner]
  • Input a [d_inner, state_rank]
  • Input b [batch, sequence, state_rank]
  • Input c [sequence, batch, state_rank]
  • Input d [d_inner]
  • Input u [batch, sequence, d_inner]
  • Input init_ssm [batch, d_inner, state_rank]
  • Output [batch, sequence, d_inner]
  • Output (final state) [batch, d_inner, state_rank]
Source§

impl Mamba1

Source

fn cache_config(&self, batch: usize) -> Mamba1CacheConfig

Source

fn make_zero( &self, batch: usize, n_virtual: usize, device: &Device, ) -> Mamba1Caches

Trait Implementations§

Source§

impl AutodiffModule for Mamba1

Source§

fn valid(&self) -> Self

Returns the same module, but on the inner backend without auto-differentiation.
Source§

fn from_inner(module: Self) -> Self

Wraps an inner module back into an auto-diff module.
Source§

impl Clone for Mamba1

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 Mamba1

Source§

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

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

impl Display for Mamba1

Source§

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

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

impl MambaBlock for Mamba1

Source§

type SsdPath = ()

Mamba-1 has no SSD chunking, so there is no path selector.

Source§

type Cache = Mamba1Cache

Per-block streaming cache (one layer’s worth of state).
Source§

type Caches = Mamba1Caches

The per-network cache collection for this family.
Source§

fn block_forward( &self, x: Tensor<3>, cache: Option<Mamba1Cache>, _ssd_path: (), ) -> (Tensor<3>, Mamba1Cache)

Full-sequence (chunked) pass — training / prefill.
Source§

fn block_step( &self, x: Tensor<2>, cache: Option<Mamba1Cache>, ) -> (Tensor<2>, Mamba1Cache)

Single-token recurrent step — decoding.
Source§

fn zero_caches_3d(&self, x: &Tensor<3>, n_virtual: usize) -> Mamba1Caches

Build n_virtual zero caches sized for a [batch, sequence, d_model] input.
Source§

fn zero_caches_2d(&self, x: &Tensor<2>, n_virtual: usize) -> Mamba1Caches

Build n_virtual zero caches sized for a [batch, d_model] input.
Source§

fn block_forward_with_state_moments( &self, x: Tensor<3>, cache: Option<Self::Cache>, ssd_path: Self::SsdPath, ) -> (Tensor<3>, Self::Cache, StateMoments)

Self::block_forward, additionally returning the exact pooled moments of the block’s per-token SSM states (StateMoments — the inputs of a state participation ratio), matching what a Self::block_step loop reading the cache would accumulate (for Mamba-3, the physical-frame states). The default implementation panics — Mamba-2 provides the closed form (Mamba2::forward_with_state_moments) and Mamba-3 the serial chunkwise de-rotated one (Mamba3::forward_with_state_moments).
Source§

fn block_forward_with_state_moments_grad( &self, x: Tensor<3>, cache: Option<Self::Cache>, ssd_path: Self::SsdPath, ) -> (Tensor<3>, Self::Cache, StateMoments)

Self::block_forward_with_state_moments with the moments left attached to the autodiff graph, for a differentiable loss term over them (e.g. a state-PR penalty). The default implementation panics — Mamba-2 and Mamba-3 provide it (see Mamba2::forward_with_state_moments_grad).
Source§

fn block_step_infinite(&self, x: Tensor<2>) -> Tensor<2>

Closed-form stationary fixed point: the limit of Self::block_step outputs when the same constant token is stepped forever. The limit forgets the starting state, so no cache is taken or returned. The default implementation panics — only Mamba-3 currently provides the closed form (see Mamba3::step_infinite).
Source§

fn block_step_n_approx( &self, x: Tensor<2>, n: usize, cache: Option<Self::Cache>, ) -> (Tensor<2>, Self::Cache)

Closed-form jump equivalent to n consecutive Self::block_step calls on the same constant token: the last step’s output and the final cache, in O(1). The default implementation panics — only Mamba-3 currently provides it (see Mamba3::step_n_approx).
Source§

impl Module for Mamba1

Source§

fn num_params(&self) -> usize

Get the number of parameters the module has, including all of its sub-modules.
Source§

fn visit<Visitor: ModuleVisitor>(&self, visitor: &mut Visitor)

Visit each tensor parameter in the module with a visitor.
Source§

fn map<Mapper: ModuleMapper>(self, mapper: &mut Mapper) -> Self

Map each tensor parameter in the module with a mapper.
Source§

fn collect_devices(&self, devices: Devices) -> Devices

Return all the devices found in the underneath module tree added to the given vector without duplicates.
Source§

fn to_device(self, device: &Device) -> Self

Move the module and all of its sub-modules to the given device. Read more
Source§

fn fork(self, device: &Device) -> Self

Fork the module and all of its sub-modules to the given device. Read more
§

fn devices(&self) -> Vec<Device>

Return all the devices found in the underneath module tree without duplicates.
§

fn no_grad(self) -> Self

Each tensor in the module tree will not require grad. Read more
§

fn train(self) -> Self
where Self: AutodiffModule,

Move the module and all of its sub-modules to the autodiff backend. Read more
§

fn quantize_weights(self, quantizer: &mut Quantizer) -> Self

Quantize the weights of the module.
§

fn into_record(self) -> ModuleRecord
where Self: Sized,

Collect this module’s parameters into a ModuleRecord. Read more
§

fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>
where Self: Sized,

Apply a ModuleRecord to this module, returning the loaded module. Read more
§

fn load_record(self, record: ModuleRecord) -> Self
where Self: Sized,

Apply a ModuleRecord to this module, consuming and returning it. Read more
Source§

impl ModuleDisplay for Mamba1

§

fn format(&self, passed_settings: DisplaySettings) -> String

Formats the module with provided display settings. Read more
§

fn custom_settings(&self) -> Option<DisplaySettings>

Custom display settings for the module. Read more
§

fn custom_content(&self, _content: Content) -> Option<Content>

Custom attributes for the module. Read more
Source§

impl ModuleDisplayDefault for Mamba1

Source§

fn content(&self, content: Content) -> Option<Content>

Attributes of the module used for display purposes. Read more
Source§

fn num_params(&self) -> usize

Gets the number of the parameters of the module.

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
§

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

§

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.

§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.