-
Notifications
You must be signed in to change notification settings - Fork 979
kda in torchtitan (training) #4164
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
Merged
Merged
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
7c59ed5
Update
liangel-02 d14f950
Update
liangel-02 c123534
Update
liangel-02 c6270e6
Update
liangel-02 d374707
Update
liangel-02 7645b8a
Update
liangel-02 1497ce9
Update
liangel-02 4eda642
Update
liangel-02 1d9d57b
Update
liangel-02 acffa35
Update
liangel-02 3e71b97
Update
liangel-02 9ca0f1e
Update
liangel-02 1ed6bda
Update
liangel-02 926c1d1
Update
liangel-02 bf45145
Update
liangel-02 6981698
Update
liangel-02 49bc979
Update
liangel-02 7d366c7
Update
liangel-02 ec66697
Update
liangel-02 8152b7b
Update
liangel-02 92533a4
Update
liangel-02 843e99c
Update
liangel-02 8f3dd67
Update
liangel-02 52ebbfa
Update
liangel-02 7388061
Update
liangel-02 2af908b
Update
liangel-02 c88f2c3
Update
liangel-02 7f704a3
Update
liangel-02 41bfa2a
Update
liangel-02 c0bf671
Update
liangel-02 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,3 @@ av | |
| einops | ||
| pillow | ||
| torchvision | ||
| flash-linear-attention | ||
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 |
|---|---|---|
|
|
@@ -8,3 +8,5 @@ safetensors | |
| einops | ||
| pillow | ||
| spmd_types==0.2.3 | ||
| flash-linear-attention | ||
| attn-gym[linear]==0.0.5 | ||
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,133 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| """Unit tests for the KDA linear-attention layer.""" | ||
|
|
||
| import importlib.util | ||
| import unittest | ||
|
|
||
| import torch | ||
|
|
||
| from torchtitan.models.common import Conv1d, Linear | ||
| from torchtitan.models.common.attention import create_varlen_metadata_for_document | ||
| from torchtitan.models.kimi_k3.kda import InnerKDA, KDA, KDAKernel, KimiRMSNormGated | ||
|
|
||
| _HAS_BLACKWELL = ( | ||
| importlib.util.find_spec("attn_gym") is not None | ||
| and torch.cuda.is_available() | ||
| and torch.cuda.get_device_capability() in {(10, 0), (10, 3)} | ||
| ) | ||
|
|
||
|
|
||
| def _kda_config() -> KDA.Config: | ||
| def linear(in_features: int, out_features: int) -> Linear.Config: | ||
| return Linear.Config( | ||
| in_features=in_features, | ||
| out_features=out_features, | ||
| bias=False, | ||
| ) | ||
|
|
||
| projection_dim = 256 | ||
|
|
||
| def conv() -> Conv1d.Config: | ||
| return Conv1d.Config( | ||
| in_channels=projection_dim, | ||
| out_channels=projection_dim, | ||
| kernel_size=4, | ||
| groups=projection_dim, | ||
| bias=False, | ||
| ) | ||
|
|
||
| return KDA.Config( | ||
| num_heads=2, | ||
| head_dim=128, | ||
| conv_kernel_size=4, | ||
| q_proj=linear(32, projection_dim), | ||
| k_proj=linear(32, projection_dim), | ||
| v_proj=linear(32, projection_dim), | ||
| q_conv=conv(), | ||
| k_conv=conv(), | ||
| v_conv=conv(), | ||
| forget_a=linear(32, 128), | ||
| forget_b=linear(128, projection_dim), | ||
| beta=linear(32, 2), | ||
| output_gate=linear(32, projection_dim), | ||
| inner_kda=InnerKDA.Config( | ||
| head_dim=128, | ||
| kernel=KDAKernel.Config(), | ||
| ), | ||
| output_norm=KimiRMSNormGated.Config(dim=128), | ||
| output_proj=linear(projection_dim, 32), | ||
| ) | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| _HAS_BLACKWELL, "KDA requires Attention Gym on CUDA capability 10.0 or 10.3" | ||
| ) | ||
| class TestKDA(unittest.TestCase): | ||
| def _make_kda(self): | ||
| model = _kda_config().build() | ||
| model = model.to(device="cuda", dtype=torch.bfloat16) | ||
| torch.manual_seed(1) | ||
| with torch.no_grad(): | ||
| for param in model.parameters(): | ||
| param.normal_(mean=0.0, std=0.02) | ||
| model.A_log.uniform_(1.0, 16.0).log_() | ||
| model.dt_bias.zero_() | ||
| model.output_norm.weight.fill_(1.0) | ||
| return model | ||
|
|
||
| def _inputs(self, seed: int, tokens: int = 128) -> torch.Tensor: | ||
| torch.manual_seed(seed) | ||
| return torch.randn(tokens, 32, device="cuda", dtype=torch.bfloat16) | ||
|
|
||
| def test_varlen_matches_independent_documents(self): | ||
| lengths = (37, 64, 91) | ||
| x_TD = self._inputs(seed=2, tokens=sum(lengths)).requires_grad_() | ||
| positions_T = torch.tensor( | ||
| [index for length in lengths for index in range(length)], | ||
| device="cuda", | ||
| dtype=torch.int32, | ||
| ) | ||
| masks = create_varlen_metadata_for_document( | ||
| positions_T, | ||
| include_host_offsets=True, | ||
| ) | ||
| self.assertEqual(masks.cu_seq_q_host, (0, 37, 101, 192)) | ||
|
|
||
| model = self._make_kda() | ||
| packed_TD = model(x_TD, masks) | ||
| independent_TD = torch.cat( | ||
| [model(document_TD, None) for document_TD in x_TD.split(lengths)] | ||
| ) | ||
| torch.testing.assert_close( | ||
| packed_TD.float(), | ||
| independent_TD.float(), | ||
| rtol=2e-2, | ||
| atol=2e-2, | ||
| ) | ||
| output_grad_TD = torch.randn_like(packed_TD) | ||
| parameters = tuple(model.parameters()) | ||
| packed_grads = torch.autograd.grad( | ||
| packed_TD, | ||
| (x_TD, *parameters), | ||
| output_grad_TD, | ||
| ) | ||
| independent_grads = torch.autograd.grad( | ||
| independent_TD, | ||
| (x_TD, *parameters), | ||
| output_grad_TD, | ||
| ) | ||
| torch.testing.assert_close( | ||
| packed_grads, | ||
| independent_grads, | ||
| rtol=2e-2, | ||
| atol=2e-2, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.