Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Don't save unused tensors in Linear backward #137

Merged
merged 1 commit into from
Sep 19, 2023
Merged
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
10 changes: 4 additions & 6 deletions trident/operation/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ def forward(ctx: Any, *args: Any, **kwargs: Any):
output = Linear.__forward(input, weight, bias, use_accelerator)
util.pop_trace()

ctx.save_for_backward(input, weight, bias, output)
ctx.save_for_backward(input, weight, bias)
ctx.use_accelerator = use_accelerator

return output

@staticmethod
def backward(ctx: Any, *grad_outputs: Any):
(grad_output,) = grad_outputs
input, weight, bias, output = ctx.saved_tensors
input, weight, bias = ctx.saved_tensors

util.push_trace("Linear.__backward")
grad_input, grad_weight, grad_bias = Linear.__backward(
grad_output, output, input, weight, bias, ctx.use_accelerator
)
grad_input, grad_weight, grad_bias = Linear.__backward(grad_output, input, weight, bias, ctx.use_accelerator)
util.pop_trace()

return grad_input, grad_weight, grad_bias, None, None
Expand Down Expand Up @@ -81,7 +79,7 @@ def grid(meta):
return output

@staticmethod
def __backward(grad_output, output, input, weight, bias, use_accelerator):
def __backward(grad_output, input, weight, bias, use_accelerator):
factory_kwargs = {"device": input.device, "dtype": input.dtype}
num_batches, m_size, k_size = input.shape
n_size, _ = weight.shape
Expand Down