Skip to content

[Fix] Prevent NaN failure in MesaNet - #1152

Open
Undermyth wants to merge 8 commits into
fla-org:mainfrom
Undermyth:main
Open

[Fix] Prevent NaN failure in MesaNet#1152
Undermyth wants to merge 8 commits into
fla-org:mainfrom
Undermyth:main

Conversation

@Undermyth

@Undermyth Undermyth commented Aug 19, 2026

Copy link
Copy Markdown

Summary

Fix for issue #1187 .

The current implementation casts q and k after l2norm to float16 forcely, and the dtype of states (h_kk and h_kv) are assigned as the same as k, which is float16.

Such precision is not enough since v is not normalized and accumulated to h_kv, which will soon cause NaN in fwd and bwd during training. Since other ops like DeltaNet assign states in bloat16 when training under bf16 mixed precision, assigning bfloat16 states here should be suitable.

Also the precision is not enough for h_kk, since fp16 can cause a numerically non-positive definite matrix in CG solver and thus the divergence. Lifting precision to fp32 solves the problem. We've tested the op on H100 to ensure that fp32 implementation can fit in the SRAM with dim=128.

Test plan

Use the original test as MesaNet. The error bound of chunk output is slightly relaxed (from 0.006 to 0.007), seems it not likely to be achieved under h_kv with bfloat16

Benchmark / NCU (kernel changes only)

None

Breaking changes

None

Checklist

  • I have read CONTRIBUTING.md and follow its conventions (code style, docstrings, commit prefixes).
  • I have read AGENTS.md and, where my change matches its scope, the relevant skill under .agents/skills.
  • This is not a minor/cosmetic-only PR (typo, formatting, style-only tweaks).
  • Dependent tests pass locally or in CI; new behavior is covered by tests where applicable.
  • Kernel changes include same-hardware before/after benchmark numbers (dense + varlen where applicable).

@Undermyth Undermyth changed the title Change precision of state in MesaNet from fp16 to bf16 for stability [Fix] Change precision of state in MesaNet from fp16 to bf16 for stability Aug 19, 2026
@zhiyuan1i zhiyuan1i added the bug Something isn't working label Aug 20, 2026
@Undermyth Undermyth mentioned this pull request Aug 28, 2026
2 tasks
@Undermyth

Copy link
Copy Markdown
Author

Sorry for the late and repeated changes since we took some time to verify the fix on models.
We've validated the fix on a 0.4B scale model with more than 5B training tokens and NaN no longer appears, so I think it's ready for merge.
Still, I'm not sure whether the fix is consistent with the original paper, because the gradient still seems not stable

@Undermyth Undermyth changed the title [Fix] Change precision of state in MesaNet from fp16 to bf16 for stability [Fix] Prevent NaN failure in MesaNet Aug 28, 2026
@Undermyth
Undermyth marked this pull request as ready for review August 28, 2026 04:28
@Undermyth

Copy link
Copy Markdown
Author

@zhiyuan1i could you please take a look on this? I think it's ready for review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants