-
Notifications
You must be signed in to change notification settings - Fork 28
EXPERIMENTAL tensor parallel REKLS #175
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
Draft
skyw
wants to merge
18
commits into
main
Choose a base branch
from
skyw/tp_rekls_exp
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4288397
add grad and kronecker factors gather function
skyw 66ff8d8
update tests
skyw cfd2cc5
add tp rekls
skyw 72a632e
add test
skyw 172a41e
add fallback and test
skyw 14c7e84
update doc
skyw 72349ef
fix couple tiny issues
skyw 258545e
disable closure
skyw fc173bc
move destroy process group to module tear down
skyw e90c7e4
narrow step overload
skyw 8316597
generalize partition_dim to be group setting
skyw 6eee42f
add dimension guard
skyw 3918459
improve naming consistency
skyw 619cc15
test svd for tp rekls
skyw c8418a5
remove svd exp
skyw 677f578
update branch
skyw b435cb5
Merge branch 'main' into skyw/tp_rekls_exp
skyw 208720c
EXP: use laprop inside REKLS
skyw 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,62 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import torch | ||
| from torch import distributed as dist | ||
|
|
||
| from emerging_optimizers.utils import get_pg_size | ||
|
|
||
|
|
||
| @torch.no_grad() # type: ignore[misc] | ||
| def all_gather_grad_and_kronecker_factors_tp( | ||
| kronecker_factor_list: list[torch.Tensor], | ||
| grad: torch.Tensor, | ||
| partition_dim: int, | ||
| tp_group: dist.ProcessGroup, | ||
| ) -> tuple[torch.Tensor, list[torch.Tensor]]: | ||
| """All-gathers a sharded gradient and its kronecker factors across the tensor parallel group. | ||
|
|
||
| This is a simple implementation to support tensor parallel. It assumes grad is sharded among tensor parallel domain | ||
| with partition_dim indicating the dimension it was sharded. To save memory, kronecker factors are also sharded | ||
| but always along dimension 0 to make gather operation easy. | ||
|
|
||
| Gradient and kronecker factors are both all-gathered to all tensor parallel ranks and returned so the caller | ||
| can pass them to ``update_kronecker_factors`` (and downstream eigenbasis computations) without further | ||
| communication. | ||
|
|
||
| Args: | ||
| kronecker_factor_list: List of preconditioner matrices (L and R), each sharded along dimension 0 across | ||
| the tensor parallel group. | ||
| grad: Local shard of the gradient tensor, sharded along ``partition_dim`` across the tensor parallel group. | ||
| partition_dim: Dimension along which ``grad`` is sharded across the tensor parallel group. | ||
| tp_group: Tensor parallel process group used to all-gather ``grad`` and ``kronecker_factor_list``. | ||
|
|
||
| Returns: | ||
| full_grad: Full (un-sharded) gradient tensor on every rank. | ||
| full_kronecker_factor_list: List of full (un-sharded) kronecker factor matrices ``[L, R]`` on every rank. | ||
| """ | ||
| tp_size = get_pg_size(tp_group) | ||
|
|
||
| grad_shards = [torch.empty_like(grad) for _ in range(tp_size)] | ||
| dist.all_gather(grad_shards, grad, group=tp_group) | ||
| full_grad = torch.cat(grad_shards, dim=partition_dim) | ||
|
|
||
| full_kronecker_factor_list: list[torch.Tensor] = [] | ||
| for kronecker_factor in kronecker_factor_list: | ||
| factor_shards = [torch.empty_like(kronecker_factor) for _ in range(tp_size)] | ||
| dist.all_gather(factor_shards, kronecker_factor, group=tp_group) | ||
| full_kronecker_factor_list.append(torch.cat(factor_shards, dim=0)) | ||
|
|
||
| return full_grad, full_kronecker_factor_list |
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.