Repository navigation
Conversation
|
cc @EugenHotaj |
|
@kwen2501 Weirdly I'm still getting the double backwards issue when running for more than 1 step. First step works fine but second fails: |
|
@kwen2501 - Single pass forward/backward is working nicely. Inference works as well. However, similar to what @EugenHotaj reported - I made a quick training loop and hit the same issue re: double backward error if you continue to the next iteration: |
| token_gather_buf: Optional[torch.Tensor] = None | ||
| input_splits: Optional[torch.Tensor] = None | ||
| output_splits: Optional[torch.Tensor] = None | ||
| token_send_buf: Optional[torch.Tensor] = [] |
There was a problem hiding this comment.
eh.. Optional[torch.Tensor] or List?
|
|
||
| # Assuming worst case, 2x tokens are routed to one EP rank | ||
| overflow = 2 | ||
| OnDeviceAllToAllV.max_output_len = ( |
There was a problem hiding this comment.
might be better if we don't modify the attributes of a global that could be shared by other usages? (I realize for now this is only used here..). I guess a cleaner pattern might be to create a class that encapsulates the states of the kernel- perhaps both its 'buffers' and its 'max-size' - then we can instantiate this object at the init of MoE and share it between layers?
There was a problem hiding this comment.
That's a good point. I don't know if a torch.autograd.Function is instantiatable (as an object). The reason it that its forward and backward methods are staticmethod, which have no access to self. So, is torch.autograd.Function by design shared?
| MoE.output_splits = symm_mem.empty( | ||
| self.ep_size, dtype=torch.int64, device=device | ||
| ) | ||
| for _ in range(self.microbatches): |
There was a problem hiding this comment.
we start to bleed more parallelism info into the model def.. seems logical to do it this way for now but conceptually what other options would we have?
normally the pipeline runtime manages the lifetime of the microbatch activations indirectly: it holds onto microbatch activations until running backward, then frees them via cuda caching allocator for reuse.
I guess for symm memm, we could rely on an allocator too? we could just ask the symm_memm allocator for new buffers on each forward pass. when would they be freed- would they naturally be kept alive as 'activations' and freed automatically (returned to the cache) once the pipelining runtime cleared their backward pass? I think that would be a lot cleaner than having to keep track of current microbatch inside model state.
There was a problem hiding this comment.
we start to bleed more parallelism info into the model def..
True and false. I sort of treat setup_symm_mem as a "side" helper to model definition in strict term. This is also the practice when people need to setup KV cache.
we could rely on an allocator too?
Yep, great idea.
| device=grad_output.device, | ||
| ) | ||
|
|
||
| # TODO: is there a way to tell autograd to feed grad_output directly to |
There was a problem hiding this comment.
what is preventing this today?
There was a problem hiding this comment.
cc @janeyx99 @soulitzer @albanD. This is what we talked about last week.
Is there a way to control where grad_output is fed (by a downstream op) so that we can avoid a copy (into comm-optimized memory) here?
There was a problem hiding this comment.
How much do we care about eager? It feels like compile is probably the right place for these types of optimizations.
General support in eager is tricky because the backward formula rather than the engine is what produces the grad_output, so you'd essentially either need to rewrite all backward formulas to use the out-variant or interpose on the out-of-place op via some kind of mode or subclass. Also, we don't even always have control over the backward formula, e.g. if it is a custom backward kernel.
If we care about a specific set of ops only, maybe one thing to try is just to have the all_to_all return a TorchFunctionSubclass that overrides a specific set of ops that you care about. You can also expose this as an extension point if users have their own ops they'd like to do this optimization for.
The torch function implementations of those ops would be custom autograd.Functions whose backward returns a grad_output in symmetric memory.
If we're looking for something fully general and automatic, maybe we need a lower level abstraction, e.g. some kind of context manager than can control how new tensor outputs are allocated. Even then its not very easy, because we'd need to somehow only apply this new context manager on the very last operation which produces the grad_output.
|
@EugenHotaj @lessw2020 Thank for trying and reporting the error! |
|
I have also bumped the steps to 2 in run.py to provide test coverage. |
lessw2020
left a comment
There was a problem hiding this comment.
working nicely!
1 - I ran with 10 iters and also implemented a memory check to ensure that max_reserved memory is not inflating during the iters. It is not and remains stable.
2 - Will has good points re: potential allocator class and maybe improving kernel config encapsulation but I'd prefer to approve and land this working code sooner, so I can start expanding a full training loop on real data instead of holding this out, but maybe talk with Will. (also, these other items should be made as issues for tracking? )
|
@kwen2501 thanks this worked for me as well! For DSV2-lite it seems to be slower (and maybe uses slightly more memory) than using non-symm memory. Do we expect to only see benefits once we scale to DSV3? Edit: for comparison here is the non-symm memory: vs the symm memory |
|
@EugenHotaj Thanks for the comparison! If we don't need the training, these two copies are both avoidable. To make them go away, we may need more changes from pytorch side, for example as @wconstab and @lessw2020 mentioned, having an allocator backed by symm mem, so that the last op's output are directly generated there, rather than the copy. |
A couple changes compared to previous try in pytorch#941: - Make `OnDeviceAllToAllV` in functional form (easier to form autograd graph). - Move symm-mem based splits buffer inside `OnDeviceAllToAllV` (this part does not need gradients). This would introduce a small copy from normal split tensor to this symm-mem backed split tensor, but since this copy is very small (of `ep_size`), so we should be able to tolerate it, and reduce outside dependency. - Create different symm mem space for data buffers of different micro-batches (from pipeline parallel), because autograd does not allow them to step on each other's toes. Test: `torchrun --standalone --nproc-per-node 8 run.py` Using traditional `torch_all_to_all`: ``` loss=tensor(681.1560, device='cuda:4', grad_fn=<MeanBackward0>) loss=tensor(681.1560, device='cuda:5', grad_fn=<MeanBackward0>) loss=tensor(688.9081, device='cuda:7', grad_fn=<MeanBackward0>) loss=tensor(688.9081, device='cuda:6', grad_fn=<MeanBackward0>) torch.linalg.norm(param.grad)=DTensor(local_tensor=104.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=136.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=104.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=136.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) ``` Using symm mem all_to_all_v: ``` loss=tensor(688.9081, device='cuda:7', grad_fn=<MeanBackward0>) loss=tensor(688.9081, device='cuda:6', grad_fn=<MeanBackward0>) loss=tensor(681.1560, device='cuda:5', grad_fn=<MeanBackward0>) loss=tensor(681.1560, device='cuda:4', grad_fn=<MeanBackward0>) torch.linalg.norm(param.grad)=DTensor(local_tensor=104.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=136.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=136.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=104.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) ```
A couple changes compared to previous try in pytorch#941: - Make `OnDeviceAllToAllV` in functional form (easier to form autograd graph). - Move symm-mem based splits buffer inside `OnDeviceAllToAllV` (this part does not need gradients). This would introduce a small copy from normal split tensor to this symm-mem backed split tensor, but since this copy is very small (of `ep_size`), so we should be able to tolerate it, and reduce outside dependency. - Create different symm mem space for data buffers of different micro-batches (from pipeline parallel), because autograd does not allow them to step on each other's toes. Test: `torchrun --standalone --nproc-per-node 8 run.py` Using traditional `torch_all_to_all`: ``` loss=tensor(681.1560, device='cuda:4', grad_fn=<MeanBackward0>) loss=tensor(681.1560, device='cuda:5', grad_fn=<MeanBackward0>) loss=tensor(688.9081, device='cuda:7', grad_fn=<MeanBackward0>) loss=tensor(688.9081, device='cuda:6', grad_fn=<MeanBackward0>) torch.linalg.norm(param.grad)=DTensor(local_tensor=104.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=136.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=104.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=136.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) ``` Using symm mem all_to_all_v: ``` loss=tensor(688.9081, device='cuda:7', grad_fn=<MeanBackward0>) loss=tensor(688.9081, device='cuda:6', grad_fn=<MeanBackward0>) loss=tensor(681.1560, device='cuda:5', grad_fn=<MeanBackward0>) loss=tensor(681.1560, device='cuda:4', grad_fn=<MeanBackward0>) torch.linalg.norm(param.grad)=DTensor(local_tensor=104.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=136.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=136.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) torch.linalg.norm(param.grad)=DTensor(local_tensor=104.0, device_mesh=DeviceMesh('cuda', [[0, 1], [2, 3]], mesh_dim_names=('ep', 'fsdp')), placements=(Replicate(), _NormPartial(reduce_op='sum', norm_type=2.0))) ```
A couple changes compared to previous try in #941:
OnDeviceAllToAllVin functional form (easier to form autograd graph).OnDeviceAllToAllV(this part does not need gradients). This would introduce a small copy from normal split tensor to this symm-mem backed split tensor, but since this copy is very small (ofep_size), so we should be able to tolerate it, and reduce outside dependency.Test:
torchrun --standalone --nproc-per-node 8 run.pyUsing traditional
torch_all_to_all:Using symm mem all_to_all_v: