Skip to content

Commit b0de6fc

Browse files
authored
[Fix] Avoid fp16-overflow NaNs in TTT fused-chunk eta gradient (#1597)
1 parent 23077dc commit b0de6fc

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

‎fla/ops/ttt/fused_chunk.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ def fused_chunk_ttt_linear_bwd_kernel_dh(
425425
mask = (o_i == i_last)
426426
b_dk -= b_e_last * tl.dot(b_v2, tl.trans(b_dh).to(b_v2.dtype))
427427
b_dk -= tl.dot(tl.trans(b_ds), tl.trans(b_q) * b_e[:, None])
428-
b_de = mask * tl.sum(- b_dh * tl.trans(tl.dot(tl.trans(b_v2), b_k))).to(b_k.dtype)
429-
b_de -= mask * tl.sum(b_dhb * tl.sum(b_v2, axis=0)).to(b_k.dtype)
428+
b_de = tl.where(mask, tl.sum(- b_dh * tl.trans(tl.dot(tl.trans(b_v2), b_k))), 0.0)
429+
b_de -= tl.where(mask, tl.sum(b_dhb * tl.sum(b_v2, axis=0)), 0.0)
430430
b_de -= tl.sum(tl.dot(b_ds, b_k) * tl.trans(b_q).to(b_k.dtype), axis=1)
431431
b_de -= tl.sum(b_ds, axis=1)
432432
b_dh += tl.dot(b_q, b_do.to(b_q.dtype)) + tl.dot(tl.trans(b_k).to(b_dkh.dtype), b_dkh)

‎tests/ops/test_ttt.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_chunk(
4444
pytest.skip(reason="Current CI do not support this config")
4545
if T > 1000:
4646
pytest.skip(reason="Current CI do not support this config")
47+
torch.manual_seed(42)
4748
eta_base = 5e-3
4849
q = torch.randn(B, T, H, D, dtype=dtype)
4950
k = F.normalize(torch.randn(B, T, H, D, dtype=torch.float32), p=2, dim=-1).to(dtype)
@@ -136,6 +137,7 @@ def test_fused_chunk(
136137
pytest.skip(reason="Current CI do not support this config")
137138
if T > 1000:
138139
pytest.skip(reason="Current CI do not support this config")
140+
torch.manual_seed(42)
139141
eta_base = 5e-3
140142
q = torch.randn(B, T, H, D, dtype=dtype)
141143
k = F.normalize(torch.randn(B, T, H, D, dtype=torch.float32), p=2, dim=-1).to(dtype)

0 commit comments

Comments
 (0)