Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions torchtitan/distributed/deepep/deepep.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _dispatch_backward(
if grad_recv_x is None:
return None, None, None, None, None, None, None

handle = _handle_cache.get(ctx.cache_id_int)
handle = ctx.saved_handle
assert handle is not None, f"Handle not found for cache_id={ctx.cache_id_int}"

previous_event = _create_event_if_async(True)
Expand All @@ -161,7 +161,6 @@ def _dispatch_backward(
)

_sync_stream_if_async(True, after_event)
_handle_cache.pop(ctx.cache_id_int, None)

grad_x = grad_x.to(ctx.input_dtype)
grad_topk_weights = (
Expand All @@ -176,6 +175,7 @@ def _dispatch_setup_context(ctx, inputs, output):
recv_x, recv_indices, recv_scores, num_recv, cache_id = output
ctx.cache_id_int = cache_id.item()
ctx.input_dtype = x.dtype
ctx.saved_handle = _handle_cache.get(ctx.cache_id_int)


def _combine_backward(ctx, grad_combined):
Expand Down Expand Up @@ -207,7 +207,8 @@ def _combine_backward(ctx, grad_combined):
def _combine_setup_context(ctx, inputs, output):
x, cache_id = inputs
ctx.cache_id_int = cache_id.item()
ctx.saved_handle = _handle_cache.get(ctx.cache_id_int)
# Pop from cache - safe because dispatch_setup_context already saved handle for dispatch_backward
ctx.saved_handle = _handle_cache.pop(ctx.cache_id_int, None)


torch.library.register_autograd(
Expand Down