mamba icon indicating copy to clipboard operation
mamba copied to clipboard

A question about matrix A

Open WaitDumplings opened this issue 2 years ago • 7 comments

Mamba is a great job!

After reading your paper, I noticed a slight discrepancy regarding the discrete matrix A.

In your paper: The discrete matrix A is represented as: exp(A * delta)

However, in your code: The matrix A (referred to as log A in reality) is initialized as follows:

A = repeat(
    torch.arange(1, self.d_state + 1, dtype=torch.float32, device=device),
    "n -> d n",
    d=self.d_inner,
).contiguous()
A_log = torch.log(A)  # Keeping A_log in float32
self.A_log = nn.Parameter(A_log)

Then, during inference:

A = -torch.exp(self.A_log.float())  # (d_inner, d_state)

is used. It seems like we use log(-A) for initialization but the paper says it is matrix A. (so I am confused about this)

Could you please explain why log(-A) is initialized instead of A for further computations? Is this choice made for computational efficiency or some other reason?

WaitDumplings avatar May 17 '24 15:05 WaitDumplings

No the code you quoted is all about initialization and parameterization. In the CUDA code we still do A_bar = exp(A * delta). We want A to be always negative, and delta always positive. To make A negative, we parameterize it by self.A_log (which is unconstained), then set A = -exp(self.A_log). To make delta always positive, we have delta = softplus(dt + self.dt_bias).

tridao avatar May 17 '24 17:05 tridao

Thanks for your reply, and it really makes sense. So, A is like a trainable Hippo Matrix in Mamaba, and that's why each element in A should be negative. Additionally, elements in delta should be positive, since this matrix is related to time (it's not possible for values to be negative in a time matrix). Does that align with your understanding?

WaitDumplings avatar May 18 '24 08:05 WaitDumplings

Yes

tridao avatar May 18 '24 08:05 tridao

But I'm still confused. Why should A(hippo) matrix be set to negative value. thanks.

Fiona-cn avatar May 22 '24 14:05 Fiona-cn

@Liyuansongdsb , since A is a diagonal matrix, having all negative values means that its eigenvalues will all be negative. With negative eigenvalues then the system is considered stable, i.e. bounded input bounded output stable. This goes back to stuff in digital signal processing, but essentially we don't want our hidden state exploding, which would lead to the vanishing gradient problem and thus unstable training.

This is just my guess, so I could be wrong.

Hprairie avatar May 26 '24 15:05 Hprairie

@Hprairie Ok, I get it now, thanks!

Fiona-cn avatar May 30 '24 09:05 Fiona-cn

So, if I'm not going to continue updating the parameter A, can I directly save the value after A_log exp

anon-figures-2026 avatar Apr 18 '25 09:04 anon-figures-2026