-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add optional torchembed RoPE backend to apply_rotary_pos_emb #8052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
py-ai-dev
wants to merge
1
commit into
deepspeedai:master
Choose a base branch
from
py-ai-dev:add-torchembed-rope-backend
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from deepspeed.sequence.layer import apply_rotary_pos_emb, _rotate_half | ||
|
|
||
|
|
||
| def _make_freqs(seq_len, rot_dim, theta=10000.0, device="cpu"): | ||
| inv_freq = 1.0 / (theta ** (torch.arange(0, rot_dim, 2, device=device).float() / rot_dim)) | ||
| t = torch.arange(seq_len, device=device).float() | ||
| freqs = torch.einsum("i,j->ij", t, inv_freq) | ||
| emb = torch.cat((freqs, freqs), dim=-1) | ||
| return emb.cos(), emb.sin() | ||
|
|
||
|
|
||
| def _ref_apply_rotary(t, freqs_cos, freqs_sin): | ||
| rot_dim = freqs_cos.shape[-1] | ||
| t, t_pass = t[..., :rot_dim], t[..., rot_dim:] | ||
| t = (t * freqs_cos) + (_rotate_half(t) * freqs_sin) | ||
| return t if t_pass.shape[-1] == 0 else torch.cat((t, t_pass), dim=-1) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("seq_len", [1, 17, 128]) | ||
| @pytest.mark.parametrize("dim", [32, 64, 128]) | ||
| @pytest.mark.parametrize("rotary_dim", [None, 16, 32, 64]) | ||
| def test_apply_rotary_pos_emb(seq_len, dim, rotary_dim): | ||
| rot_dim = rotary_dim if rotary_dim is not None else dim | ||
| if rot_dim > dim or rot_dim % 2 != 0: | ||
| pytest.skip("rotary_dim must be <= dim and even") | ||
|
|
||
| t = torch.randn(seq_len, 4, dim) | ||
| freqs_cos, freqs_sin = _make_freqs(seq_len, rot_dim) | ||
| freqs_cos = freqs_cos[:, :rot_dim] | ||
| freqs_sin = freqs_sin[:, :rot_dim] | ||
|
|
||
| result = apply_rotary_pos_emb(t, freqs_cos, freqs_sin) | ||
| expected = _ref_apply_rotary(t, freqs_cos, freqs_sin) | ||
|
|
||
| assert torch.allclose(result, expected, atol=1e-6), ( | ||
| f"seq_len={seq_len}, dim={dim}, rot_dim={rot_dim}: max diff={((result - expected).abs().max()).item()}" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dtype", [torch.float32, torch.float16]) | ||
| def test_apply_rotary_pos_emb_grad_flow(dtype): | ||
| seq_len, n_heads, dim = 8, 4, 64 | ||
| rot_dim = 64 | ||
| t = torch.randn(seq_len, n_heads, dim, dtype=dtype, requires_grad=True) | ||
| freqs_cos, freqs_sin = _make_freqs(seq_len, rot_dim) | ||
| freqs_cos = freqs_cos[:, :rot_dim] | ||
| freqs_sin = freqs_sin[:, :rot_dim] | ||
|
|
||
| out = apply_rotary_pos_emb(t, freqs_cos, freqs_sin) | ||
| loss = out.sum() | ||
| loss.backward() | ||
|
|
||
| assert t.grad is not None | ||
| assert not torch.isnan(t.grad).any(), "NaNs in gradient" | ||
| assert t.grad.shape == t.shape, f"grad shape {t.grad.shape} != {t.shape}" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
torchembedis installed and the tensor is CUDA, this reshapes every input as if the sequence length wereorig_shape[-2](the head dimension for[seq,b,heads,dim]or[b,seq,heads,dim]tensors). The existing function contract saystis[seq_length, ..., dim], and callers such asfpdt_layer.pyalso pass[b,l,nh,hd], so this sends the fused kernel a fake sequence length equal to the number of heads whilefreqs_cos/sinstill describe the real sequence length, producing wrong rotations or shape/indexing failures only in the optional fused path.Useful? React with 👍 / 👎.