Skip to content

Commit a275808

Browse files
authored
Merge pull request #462 from Modalities/fix/rotary-fp32-frequencies
Fix rotary frequency precision
2 parents eadfd44 + 1fbd6d5 commit a275808

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎src/modalities/models/gpt2/gpt2_model.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def _update_cos_sin_tables(self, x):
393393
):
394394
self._seq_len_cached = seq_len
395395
t = torch.arange(x.shape[self.seq_length_dim], device=x.device, dtype=torch.float32)
396-
freqs = torch.einsum("i,j->ij", t, self.inv_freq.to(x.dtype))
396+
freqs = torch.einsum("i,j->ij", t, self.inv_freq.float())
397397
emb = torch.cat((freqs, freqs), dim=-1).to(
398398
x.device
399399
) # here, we combine the two matrices (not zipping them).

‎tests/test_rotary_qkv_transform.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ def test_rotary_transform():
4444
assert torch.equal(comp_rot_expected, comp_rot)
4545

4646

47+
def test_rotary_transform_computes_frequencies_in_fp32_for_bf16_inputs(monkeypatch):
48+
operand_and_result_dtypes = []
49+
original_einsum = torch.einsum
50+
51+
def recording_einsum(equation, *operands):
52+
result = original_einsum(equation, *operands)
53+
operand_and_result_dtypes.append((*[operand.dtype for operand in operands], result.dtype))
54+
return result
55+
56+
monkeypatch.setattr(torch, "einsum", recording_einsum)
57+
58+
rotary_transform = RotaryTransform(n_embd=128, n_head=2)
59+
q = torch.randn(1, 2, 16, 64, dtype=torch.bfloat16)
60+
k = torch.randn_like(q)
61+
v = torch.randn_like(q)
62+
63+
q_rot, k_rot, _ = rotary_transform(q=q, k=k, v=v)
64+
65+
assert operand_and_result_dtypes == [(torch.float32, torch.float32, torch.float32)]
66+
assert q_rot.dtype == torch.bfloat16
67+
assert k_rot.dtype == torch.bfloat16
68+
69+
4770
def _apply_rotary(x: torch.Tensor, cos_cached: torch.Tensor, sin_cached: torch.Tensor) -> torch.Tensor:
4871
cos_local = cos_cached[:, :, : x.shape[-2], :]
4972
sin_local = sin_cached[:, :, : x.shape[-2], :]
@@ -61,7 +84,7 @@ def _assert_yarn_outputs_match_reference(
6184
seq_length: int,
6285
) -> None:
6386
t = torch.arange(seq_length, device=q.device, dtype=torch.float32)
64-
freqs = torch.einsum("i,j->ij", t, rotary_transform.inv_freq.to(q.dtype))
87+
freqs = torch.einsum("i,j->ij", t, rotary_transform.inv_freq.float())
6588
emb = torch.cat((freqs, freqs), dim=-1)
6689
cos = (emb.cos() * rotary_transform.attention_scaling)[None, None, :, :].to(q.dtype)
6790
sin = (emb.sin() * rotary_transform.attention_scaling)[None, None, :, :].to(q.dtype)

0 commit comments

Comments
 (0)