Building up to Vision Mamba
Vision Mamba and Mamba present a very important alternative to Transformers when dealing with block-based sequence models. Although not as popular or present in top commercial models they offer something almost invaluable in the field, something other than attention that can perform the same task, which I personally have been dying to see get any form of coverage whatsoever. This post will cover State Space Models, Structured State Space Sequence Models, the general Mamba architecture, and the extension of it to computer vision in Vision Mamba.
State Space Models:
The architecture of Mamba is built from the ground up based on the concept of State Space Models, often shortened to SSMs. They simply represent a common way in Probability and Control Theory to represent a dynamic system with a set of continuous state variables. The general form for these models is described below with two distinct sections in a recurrent system. The hidden state (the internal state of the system) and the observation (the output of the system).
represents an optional input or control signal and and are the system and observation noise at time respectively. Due to the generalities of the representation this can be decomposed into any number of different models. One of the simplest forms comes in with the Linear-Gaussian SSM that uses the representation to create a model with parameters .
Structured State Space Sequence Models:
These models are then extended into their most popular form by Structured State Space Sequence Models, often shortened S4 Models. These are such an important version of the model in the AI space that when trying to search up anything about the underlying SSM you will still find information about S4 models, and even the Mamba paper itself calls S4 models SSMs for brevity. They are still represented by a form of continuous system described in variables, but the main improvements of the model come in the computations performed with them.
Discretization:
One of the most important steps to an S4 model is the discretization step. The model transforms the continuous variables of the model into discrete variables . This improves the stability and computational efficiency but more importantly moves the model towards real world scenarios in which the data used is often discrete. This is generalized to any fixed formula pair called the discretization rule that transform the continuous variables into discrete variables .
One of the most important properties of these systems is that both the continuous and discrete variables are all time invariant, as in fixed for all time-step. This allows for parallel computation, precomputation, and even leads to better theoretical convergence. The most common discretization rule and the one that will be most important for this use case will be the zero-order hold, often shortened to ZOH. It assumes that the input is constant between time steps and is very computationally efficient.
Computation:
These discrete variables are then used in the main computational step aptly called computation. There are two main computation options proposed in the model each with their own benefits and use cases. Linear Recurrence (shown in the first column) is better used for autoregressive tasks where the input is taken in parts, most often during inference. Global Convolution (shown in the second column) is used in situations where the entire sequence can be seen of time, most often during training.
Mamba:
In order to make this architecture scalable and to make it more flexible to the input data, Mamba introduces a repeatable block structure that is built off of S4 models. The architecture for each block is very simple to understand but I do highly encourage looking at the diagrams within the paper on page 8 for a visual backing to the explanation (that diagram is also the only one so far that will provide any meaningful help with understanding the topic across all the models explained here). Each block is composed of the following sequential parts where both the input and output receive linear projections to match dimensions. As well the nonlinearity section of the block is given a skip connection from the input after its processed with a separate linear projection.
- Convolutional Layer
- Activation (chosen as Swish or SiLU in the original paper)
- Selective State Space Model
- Nonlinearity (activation or multiplication)
Selective State Space Models:
Mamba’s main contribution comes in how it extends the S4 models covered previously. The model makes some of the state variables dependent on the input to the model. This creates a system that is more malleable and can provide zero-shot capabilities. Specifically each of are defined with functions of the inputs instead of being the weights by themselves. In the original model each function takes the form of the following with generalized functions .
The rest of the model stays the same as the above defined linear recurrence S4 model. Each of the functions are then defined with the following. The function broadcasts the input across a -dimensional feature space.
Vision Mamba:
The original Mamba architecture works well for autoregressive tasks where information can be accumulated over the sequence, but struggles when given other modalities. Vision Mamba extends the architecture to general Computer Vision tasks in a similar way that Vision Transformers did for Transformers. The architecture of the model can be broken down into 3 parts.
- Image Preprocessing
- Vision Mamba Encoder
- MLP Prediction Head
Image Pre-Processing:
Following the lead of the Vision Transformer architecture, the input image is turned into a set of equal size patches. Specifically each image is turned into a set of 2D patches where is the image size, is the number of channels, and is the patch size. Each patch is then linearly projected and given positional embeddings . This is described below for the -th patch of and a weight matrix to generate the input (which also includes a class token which is used later for the final output).
Vim Block:
The extension of the general Mamba Block to the specialized Vision Mamba (shortened to Vim) Block is used to reformat the dimensionality of the Mamba Blocks (the original 1D sequence based Mamba Block is not well suited for images) and introduce a bidirectional mechanism (transferring information both forwards and backwards). First, the previous sequence is normalized and linearly projected producing and shown below (where is the expanded state dimension and is the hidden state dimension).
After this processing is finished, the bidirectional mechanism starts. The following all repeat twice, once for each of which change the direction in which the information is processed creating a bidirectional exchange of information. As a first step linear projections of are produced where denotes the SSM dimension. A time step calculation is also calculated using the same information.
This time step calculation is used in the discretization step to compute the state transition matrices. Specifically this computes both .
Once discretized, an extension of the linear recurrence model from the original S4 model is used and shown below. This iterates over and each of and are intialized to have values.
Once both outputs for each direction are computed they are combined and produced as an output .
Prediction Head:
Once each Vim Block is used to process the information, the class token is used to generalize the output of the model. The token is normalized before being passed to the final MLP layer to produce the prediction output .