-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
Fix RWKV backward on GPU #23774
Fix RWKV backward on GPU #23774
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,7 +159,7 @@ def forward(ctx, time_decay, time_first, key, value, state=None, return_state=Fa | |
|
||
@staticmethod | ||
# g stands for grad | ||
def backward(ctx, g_output): | ||
def backward(ctx, g_output, g_state=None): | ||
input_dtype = ctx.input_dtype | ||
|
||
time_decay, time_first, key, value, output = ctx.saved_tensors | ||
|
@@ -188,17 +188,14 @@ def backward(ctx, g_output): | |
g_key, | ||
g_value, | ||
) | ||
g_time_decay = torch.sum(g_time_decay, dim=0) | ||
g_time_first = torch.sum(g_time_first, dim=0) | ||
|
||
return ( | ||
None, | ||
None, | ||
None, | ||
Comment on lines
-191
to
-197
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also just out of curiosity why the number of outputs changed ? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because autograd wants one gradient per input of the forward :-) |
||
g_time_decay.to(input_dtype), | ||
g_time_first.to(input_dtype), | ||
g_key.to(input_dtype), | ||
g_value.to(input_dtype), | ||
None, | ||
None, | ||
) | ||
|
||
|
||
|
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.
why the variable is not used later on?
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.
Because we don't handle the gradient of the state. But autograd is not happy if this isn't here: it want one input gradient per output of the forward.
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.
Thanks so much for explaning!