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: LinearInput channel: d_model. Output channel: 2 * d_inner.
conv1d: Conv1dInput channel: d_inner. Output channel: d_inner.
x_proj: LinearInput channel: d_inner. Output channel: dt_rank + 2 * state_rank.
dt_proj: LinearInput 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: LinearInput channel: d_inner. Output channel: d_model.
Implementations§
Source§impl Mamba1
impl Mamba1
Sourcepub fn step(
&self,
x: Tensor<2>,
cache: Option<Mamba1Cache>,
) -> (Tensor<2>, Mamba1Cache)
pub fn step( &self, x: Tensor<2>, cache: Option<Mamba1Cache>, ) -> (Tensor<2>, Mamba1Cache)
§Shapes
- Input
[batch, d_model] - Output
[batch, d_model]
Sourcepub fn ssm_step(
&self,
u: Tensor<2>,
cache: Mamba1Cache,
) -> (Tensor<2>, Mamba1Cache)
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]
Sourcepub 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)
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
impl Mamba1
Sourcepub fn forward(
&self,
x: Tensor<3>,
cache: Option<Mamba1Cache>,
) -> (Tensor<3>, Mamba1Cache)
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]
Sourcepub fn ssm(&self, u: Tensor<3>, init_ssm: Tensor<3>) -> (Tensor<3>, Tensor<3>)
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]
Sourcepub 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>)
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
impl Mamba1
fn cache_config(&self, batch: usize) -> Mamba1CacheConfig
fn make_zero( &self, batch: usize, n_virtual: usize, device: &Device, ) -> Mamba1Caches
Trait Implementations§
Source§impl MambaBlock for Mamba1
impl MambaBlock for Mamba1
Source§type Cache = Mamba1Cache
type Cache = Mamba1Cache
Source§type Caches = Mamba1Caches
type Caches = Mamba1Caches
Source§fn block_forward(
&self,
x: Tensor<3>,
cache: Option<Mamba1Cache>,
_ssd_path: (),
) -> (Tensor<3>, Mamba1Cache)
fn block_forward( &self, x: Tensor<3>, cache: Option<Mamba1Cache>, _ssd_path: (), ) -> (Tensor<3>, Mamba1Cache)
Source§fn block_step(
&self,
x: Tensor<2>,
cache: Option<Mamba1Cache>,
) -> (Tensor<2>, Mamba1Cache)
fn block_step( &self, x: Tensor<2>, cache: Option<Mamba1Cache>, ) -> (Tensor<2>, Mamba1Cache)
Source§fn zero_caches_3d(&self, x: &Tensor<3>, n_virtual: usize) -> Mamba1Caches
fn zero_caches_3d(&self, x: &Tensor<3>, n_virtual: usize) -> Mamba1Caches
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
fn zero_caches_2d(&self, x: &Tensor<2>, n_virtual: usize) -> Mamba1Caches
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)
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)
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>
fn block_step_infinite(&self, x: Tensor<2>) -> Tensor<2>
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)
fn block_step_n_approx( &self, x: Tensor<2>, n: usize, cache: Option<Self::Cache>, ) -> (Tensor<2>, Self::Cache)
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
impl Module for Mamba1
Source§fn num_params(&self) -> usize
fn num_params(&self) -> usize
Source§fn visit<Visitor: ModuleVisitor>(&self, visitor: &mut Visitor)
fn visit<Visitor: ModuleVisitor>(&self, visitor: &mut Visitor)
Source§fn map<Mapper: ModuleMapper>(self, mapper: &mut Mapper) -> Self
fn map<Mapper: ModuleMapper>(self, mapper: &mut Mapper) -> Self
Source§fn collect_devices(&self, devices: Devices) -> Devices
fn collect_devices(&self, devices: Devices) -> Devices
Source§fn to_device(self, device: &Device) -> Self
fn to_device(self, device: &Device) -> Self
Source§fn fork(self, device: &Device) -> Self
fn fork(self, device: &Device) -> Self
§fn devices(&self) -> Vec<Device>
fn devices(&self) -> Vec<Device>
§fn train(self) -> Selfwhere
Self: AutodiffModule,
fn train(self) -> Selfwhere
Self: AutodiffModule,
§fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
§fn into_record(self) -> ModuleRecordwhere
Self: Sized,
fn into_record(self) -> ModuleRecordwhere
Self: Sized,
ModuleRecord. Read more§fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>where
Self: Sized,
fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>where
Self: Sized,
ModuleRecord to this module, returning the loaded
module. Read more§fn load_record(self, record: ModuleRecord) -> Selfwhere
Self: Sized,
fn load_record(self, record: ModuleRecord) -> Selfwhere
Self: Sized,
ModuleRecord to this module, consuming and returning
it. Read more