-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Multi-GPU Context Parallel Mamba2 #664
Open
josiahbjorgaard
wants to merge
3
commits into
state-spaces:main
Choose a base branch
from
josiahbjorgaard:main
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,83 @@ | ||
from typing import Optional | ||
|
||
import torch | ||
from torch import nn, Tensor | ||
import torch.distributed as dist | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
|
||
def send_and_receive_(x, receive_buffer, send_to_rank, receive_from_rank, group): | ||
assert send_to_rank is not None or receive_from_rank is not None | ||
ops = [] | ||
if send_to_rank is not None: | ||
ops.append(dist.P2POp(dist.isend, x, send_to_rank, group)) | ||
if receive_from_rank is not None: | ||
ops.append(dist.P2POp(dist.irecv, receive_buffer, receive_from_rank, group)) | ||
|
||
reqs = dist.batch_isend_irecv(ops) | ||
for req in reqs: | ||
req.wait() | ||
dist.barrier() | ||
|
||
class ContextParallelMixerFn(torch.autograd.Function): | ||
@staticmethod | ||
def forward(ctx, x, padding=0, process_group=torch.distributed.group.WORLD): | ||
#Prepends the last n_padding tokens from layer_n to layer_{n+1} | ||
#These are mixed into subsequent tokens of layer n+1 by convolution, but their index is then discarded | ||
# the convolution is causal, so the mixing only goes in one direction | ||
rank, world_size = dist.get_rank(process_group), dist.get_world_size(process_group) | ||
if world_size == 1: | ||
return x | ||
|
||
send_to_rank = rank + 1 if rank < world_size - 1 else None | ||
receive_from_rank = rank - 1 if rank > 0 else None | ||
#print('dist', rank, 'send',send_to_rank, 'recieve',receive_from_rank) | ||
#_, pre_tokens = x.split(x.shape[1]-self.padding, dim=1) | ||
pre_tokens = x[:,-padding:].contiguous() | ||
#print('dist',rank,'padding',padding) | ||
assert pre_tokens.shape[1] == padding | ||
receive_buffer = torch.zeros_like(pre_tokens, requires_grad=True).contiguous() #TODO this isn't used by rank=0 | ||
send_and_receive_(pre_tokens, receive_buffer, send_to_rank, receive_from_rank, process_group) | ||
if rank > 0: | ||
x = F.pad(x, (0, 0, padding, 0), 'constant', 0) | ||
x[:, :padding] = receive_buffer | ||
#print('x', rank, x.shape) | ||
ctx.padding=padding | ||
ctx.process_group = process_group | ||
return x | ||
|
||
@staticmethod | ||
def backward(ctx, grad_x): | ||
""" | ||
grad x is input with the padding tokens from the next layer | ||
the input of forward is not padded, this gradient needs to be popped and transfered | ||
to the previous layer... | ||
""" | ||
process_group = ctx.process_group | ||
rank, world_size = dist.get_rank(process_group), dist.get_world_size(process_group) | ||
padding = ctx.padding | ||
#print('grad_x', rank, grad_x.shape) | ||
if world_size == 1: | ||
return grad_x, None | ||
send_to_rank = rank -1 if rank > 0 else None | ||
receive_from_rank = rank + 1 if rank < world_size - 1 else None | ||
pre_tokens_grad = grad_x[:, :padding].contiguous() | ||
if rank > 0: | ||
grad_x_out = grad_x[:, padding:].contiguous() | ||
else: | ||
grad_x_out = grad_x.clone() | ||
assert pre_tokens_grad.shape[1] == ctx.padding | ||
receive_buffer = torch.zeros_like(pre_tokens_grad).contiguous() #TODO this isn't used by rank=0 | ||
send_and_receive_(pre_tokens_grad, receive_buffer, send_to_rank, receive_from_rank, process_group) | ||
if rank < world_size -1: | ||
grad_x_out[:, -padding:] += receive_buffer | ||
return grad_x_out, None, None | ||
|
||
class ContextParallelMixerLayer(nn.Module): | ||
def __init__(self, padding=0, process_group=torch.distributed.group.WORLD): | ||
super(ContextParallelMixerLayer, self).__init__() | ||
self.padding = padding | ||
self.process_group = process_group | ||
|
||
def forward(self, x): | ||
return ContextParallelMixerFn.apply(x, self.padding, self.process_group) |
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
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.
We are entering the boolean trap here with this API design...