forked from huggingface/optimum-habana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for Context Parallelism using Deepseed's DistributedAt…
…tention (huggingface#1501) Co-authored-by: regisss <[email protected]>
- Loading branch information
1 parent
9f9b41e
commit 0fbc457
Showing
10 changed files
with
625 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,30 @@ | ||
import torch | ||
|
||
from ..parallel_state import ( | ||
get_sequence_parallel_group, | ||
get_sequence_parallel_rank, | ||
get_sequence_parallel_world_size, | ||
) | ||
|
||
|
||
# Gather losses across context parallel group | ||
class _ContextParallelLoss(torch.autograd.Function): | ||
@staticmethod | ||
def forward(ctx, loss): | ||
ctx.seqlen = loss.size(0) * get_sequence_parallel_world_size() | ||
|
||
loss_all = torch.empty(ctx.seqlen, dtype=loss.dtype, device=loss.device) | ||
torch.distributed.all_gather_into_tensor(loss_all, loss, group=get_sequence_parallel_group()) | ||
return loss_all | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
step_seqlen = ctx.seqlen // get_sequence_parallel_world_size() | ||
sp_rank = get_sequence_parallel_rank() | ||
grad_output_part = grad_output[step_seqlen * sp_rank : step_seqlen * (sp_rank + 1)] | ||
|
||
return grad_output_part, None | ||
|
||
|
||
def _get_loss_from_context_parallel(vocab_parallel_loss): | ||
return _ContextParallelLoss.apply(vocab_parallel_loss) |
Oops, something went wrong.