You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 26, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 26, 2026. It is now read-only.
scale.sqrt() in the recurrent_forward function of the multiscale_retention module #47
wangmengzhi The normalization doesn't affect the final results, because GN is scale-invariant. But it helps to rescale the numerical values. Both should work fine in practice.
changed the title [-]There is a bug in the recurrent_forward function of the multiscale_retention module[/-][+]scale.sqrt() in the recurrent_forward function of the multiscale_retention module[/+]on Aug 3, 2023
The output is 22011.2265625. If layernorm_eps set to 1e-10, output is 254.89244079589844
But if I use kv = prev_kv * (prev_scale.sqrt() * decay / scale.sqrt()).view(self.num_heads, 1, 1) + kv / scale.sqrt().view(self.num_heads, 1, 1)
The output is 0.021843837574124336. The difference is much smaller. I don't think GN is enough.
Activity
sunyt32 commentedon Aug 3, 2023
The division here is to control numerical stability and invariant to LayerNorm. I believe your modification also works.
donglixp commentedon Aug 3, 2023
wangmengzhi The normalization doesn't affect the final results, because GN is scale-invariant. But it helps to rescale the numerical values. Both should work fine in practice.
[-]There is a bug in the recurrent_forward function of the multiscale_retention module[/-][+]scale.sqrt() in the recurrent_forward function of the multiscale_retention module[/+]wangmengzhi commentedon Aug 3, 2023
I wrote a piece of code to check consistency:
The output is 22011.2265625. If layernorm_eps set to 1e-10, output is 254.89244079589844
But if I use
kv = prev_kv * (prev_scale.sqrt() * decay / scale.sqrt()).view(self.num_heads, 1, 1) + kv / scale.sqrt().view(self.num_heads, 1, 1)The output is 0.021843837574124336. The difference is much smaller. I don't think GN is enough.
sunyt32 commentedon Aug 3, 2023
You are right!
.sqrt()here can avoid numerical underflow thus improving consistency and performance. We are looking forward to your pull request.Adding sqrt in the recurrent_forward of retnet to make it consistent …
Adding sqrt in the recurrent_forward of retnet to make it consistent …
donglixp commentedon Aug 4, 2023
wangmengzhi Awesome! Thanks for your insightful suggestion. The improvement looks terrific. We will also do more local tests to compare them.
wangmengzhi commentedon Aug 4, 2023
I'm glad to be of help, and thank you all for proposing such a great Retnet model.