-
Notifications
You must be signed in to change notification settings - Fork 20
bring back torch.autograd.Function #316
base: gh/vkuzo/29/base
Are you sure you want to change the base?
Changes from 4 commits
c1487ef
8505776
a820346
47ef29d
ec8829e
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 |
---|---|---|
|
@@ -71,6 +71,54 @@ def _maybe_initialize_amaxes_scales_for_float8_cast( | |
scale.copy_(new_scale) | ||
|
||
|
||
# this code was resurrected from https://github.com/pytorch-labs/float8_experimental/pull/128/files | ||
@torch._dynamo.allow_in_graph | ||
class manual_float8_mm(torch.autograd.Function): | ||
""" | ||
Like torch.mm, but with X and W in float8 | ||
""" | ||
|
||
@staticmethod | ||
def forward( | ||
ctx, | ||
x_fp8, | ||
w_fp8_t, | ||
): | ||
ctx.save_for_backward(x_fp8, w_fp8_t) | ||
orig_shape = x_fp8.shape | ||
x_fp8_reshaped = x_fp8.reshape(-1, orig_shape[-1]) | ||
vkuzo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res_bits = torch.mm(x_fp8_reshaped, w_fp8_t) | ||
res_bits = res_bits.reshape(*orig_shape[:-1], res_bits.shape[-1]) | ||
return res_bits | ||
|
||
@staticmethod | ||
def backward(ctx, go_fp8): | ||
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. nit: align go_fp8 / other naming to the other PR 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. we can do that in separate PRs, since not user facing. Just keeping things small. 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. I dont know if that changes the size of the PR much but sure thats fine 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. probably just a style difference on how to sequence the renames, either is ok IMO |
||
x_fp8, w_fp8_t = ctx.saved_tensors | ||
|
||
go_fp8_orig_shape = go_fp8.shape | ||
go_fp8_reshaped = go_fp8.reshape(-1, go_fp8_orig_shape[-1]) | ||
|
||
# calculate dL/dX | ||
dL_dX = torch.mm( | ||
go_fp8_reshaped, | ||
w_fp8_t.t(), | ||
) | ||
dL_dX = dL_dX.reshape(*go_fp8_orig_shape[:-1], dL_dX.shape[-1]) | ||
|
||
x_fp8_orig_shape = x_fp8.shape | ||
x_fp8_reshaped = x_fp8.reshape(-1, x_fp8_orig_shape[-1]) | ||
|
||
# calculate dL/dW | ||
# Note: the variant below is slightly faster on LLaMa 3 8B pretraining | ||
# compared to than calculating `dL_dW_t = x_fp8_t @ go_fp8_reshaped` | ||
dL_dW = torch.mm( | ||
go_fp8_reshaped.t(), | ||
x_fp8_reshaped, | ||
) | ||
|
||
return dL_dX, dL_dW.t() | ||
|
||
|
||
@torch._dynamo.allow_in_graph | ||
class NoopFwToFloat8E5M2Bw(torch.autograd.Function): | ||
""" | ||
|
@@ -410,7 +458,7 @@ def forward(self, input: torch.Tensor) -> torch.Tensor: | |
x_fp8 = self.cast_x_to_float8(input, self.is_amax_initialized) | ||
w_fp8 = self.cast_w_to_float8(self.weight, self.is_amax_initialized) | ||
|
||
y = torch.matmul(x_fp8, w_fp8.t()) | ||
y = manual_float8_mm.apply(x_fp8, w_fp8.t()) | ||
|
||
# Cast gradY to float8_e5m2 during backward | ||
y = self.cast_y_to_float8_in_bw(y) | ||
|
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.
nit: Does the structure work out to put this in float8 ops?
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.
in how things look after this PR it would make sense, but might be good to see how the code looks after we add different granularities and the if/else branches on when to convert to lower precision. Maybe we can revisit then?