-
Notifications
You must be signed in to change notification settings - Fork 20
[FSDP2] precompute scale after optimizer.step for dynamic scaling #266
Changes from 30 commits
9d5595c
e7005c2
e41d589
e0bee10
c0ba5a2
8da238e
ffff5ed
aefa21b
d4a1db7
d36e79b
6f244a2
dc5eab0
546e979
229ede6
d5b3ff6
4f05e04
3de59af
ffcd197
6b18947
562424c
75e0e45
fe95f8b
1cbaa13
fe2e0a0
e4eaa2a
e4245e4
e12c973
9ef67fb
fa2f08a
ba085e5
ac0afb0
8e56dfc
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,61 @@ | ||||||||||||
import math | ||||||||||||
import warnings | ||||||||||||
from typing import List | ||||||||||||
|
||||||||||||
import torch | ||||||||||||
import torch.nn as nn | ||||||||||||
from float8_experimental.float8_dynamic_utils import WeightWithDynamicFloat8CastTensor | ||||||||||||
from float8_experimental.float8_linear import Float8Linear | ||||||||||||
from float8_experimental.float8_linear_utils import linear_requires_sync | ||||||||||||
from float8_experimental.float8_utils import EPS | ||||||||||||
|
||||||||||||
|
||||||||||||
def precompute_float8_scale_for_fsdp(module: nn.Module) -> None: | ||||||||||||
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. Should we add a 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. good catch. adding @torch.no_grad() |
||||||||||||
""" | ||||||||||||
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. improve docstring with example API usage 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. nice! can we add this to the README? 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. just added API usage to README 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: maybe we can make sure 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. renaming to |
||||||||||||
Calculate scale for all float8 parameters after optimizer step | ||||||||||||
It performs a single all-reduce instead of many all-reduces for each parameter | ||||||||||||
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. suggestion:
Suggested change
|
||||||||||||
Exmaple usage: | ||||||||||||
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 (typo):
Suggested change
@vkuzo I assume that there are no docs builds for Otherwise, we might need to check the formatting -- I recall the format for examples being a bit different. |
||||||||||||
model(input).sum().backward() | ||||||||||||
optim.step() | ||||||||||||
precompute_float8_scale_for_fsdp(model) | ||||||||||||
""" | ||||||||||||
from torch.distributed._tensor import DTensor | ||||||||||||
|
||||||||||||
if any( | ||||||||||||
weifengpy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
isinstance(m, Float8Linear) | ||||||||||||
and linear_requires_sync( | ||||||||||||
m.scaling_type_x, m.scaling_type_w, m.scaling_type_dL_dY | ||||||||||||
) | ||||||||||||
for m in module.modules() | ||||||||||||
): | ||||||||||||
raise NotImplementedError("Only supports delayed scaling") | ||||||||||||
float8_linears: List[Float8Linear] = [ | ||||||||||||
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. is this expensive for real models? if yes, maybe we can offer option to precompute this? 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. My intuition is that this should be pretty fast as the number of 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. |
||||||||||||
m | ||||||||||||
for m in module.modules() | ||||||||||||
if isinstance(m, Float8Linear) | ||||||||||||
and isinstance(m.weight, DTensor) | ||||||||||||
and isinstance(m.weight._local_tensor, WeightWithDynamicFloat8CastTensor) | ||||||||||||
] | ||||||||||||
weights: List[DTensor] = [float8_linear.weight for float8_linear in float8_linears] | ||||||||||||
|
||||||||||||
def compute_scales(weights: List[DTensor]): | ||||||||||||
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. optional nit: maybe move outside to prevent nested functions? 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. curious what is the downside of nested functions @weifengpy By the way, this was originally a nested function just so that we could try to 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 will remove nested functions to make the code easy to read. I profiled the unit test and |
||||||||||||
# inf-norm is equivalent to max(abs(w)) | ||||||||||||
max_weights = torch._foreach_norm(weights, ord=math.inf) # Partial | ||||||||||||
amax_tensor = torch.vstack(max_weights) # Partial | ||||||||||||
# clamp is dispatched through DTensor | ||||||||||||
# it will issue a single all-reduce | ||||||||||||
amax_tensor = torch.clamp(amax_tensor, EPS) # Replicate | ||||||||||||
scale_tensor = torch.finfo(torch.float8_e4m3fn).max / amax_tensor # Replicate | ||||||||||||
if amax_tensor.dtype is torch.float16: | ||||||||||||
scale_tensor = torch.clamp(scale_tensor, max=torch.finfo(torch.float16).max) | ||||||||||||
scales = torch.split(scale_tensor, 1) # Replicate | ||||||||||||
return scales | ||||||||||||
|
||||||||||||
if weights: | ||||||||||||
scales = compute_scales(weights) | ||||||||||||
for scale, float8_linear in zip(scales, float8_linears): | ||||||||||||
float8_linear.weight._local_tensor._precomputed_scale = scale._local_tensor | ||||||||||||
else: | ||||||||||||
warnings.warn( | ||||||||||||
"Calling precompute_float8_weights without any weights using FSDP fp8 all-gather!" | ||||||||||||
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. function name in the warning needs to be updated I am okay with not including this warning by the way. This was also to help debugging to make sure we actually found 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. got you. I am removing the warnings for simplicity |
||||||||||||
) |
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.
should we say that this is specific to FSDP2 with float8 all-gather turned on? Also, maybe we can show how to turn that on, since I don't think it's documented in the README yet? Can be a follow-up PR.
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.
will change it in this PR
good catch. will polish README again after landing changes in torchtitan to turn on/off fp8 all-gather