Repository navigation
Conversation
[ghstack-poisoned]
This PR requires pytorch/pytorch#122908. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. [ghstack-poisoned]
| } | ||
|
|
||
|
|
||
| class Float8DynamicLinearWeightTensor(torch.Tensor): |
There was a problem hiding this comment.
maybe something like TensorWithFloat8CastDefined could be a more descriptive name?
Float8Tensor currently means "tensor with raw data in float8 precision, mimicing a high precision tensor
this object seems to be pretty different, would be good if the name is also different.
There was a problem hiding this comment.
I think the name may want to mention that the cast is using dynamic scaling (since delayed scaling would use a different cast and require a slightly different implementation of the pre-all-gather transformation).
Maybe TensorWithDynamicFloat8Cast? I also noticed that all of the existing subclasses are <...>Tensor (i.e. have a Tensor as suffix) 🤔 .
There was a problem hiding this comment.
Float8AwareTensor? DynamicallyCastableToFloat8Tensor? I'm open, I think I just care that the name emphasizes that this is not a tensor stored in float8 precision, this is a high precision tensor that knows how to cast itself to float8.
There was a problem hiding this comment.
I renamed it to WeightWithDynamicFloat8CastTensor for now since we may (eventually) want a subclass for delayed scaling too, in which case we could perhaps do WeightWithDelayedFloat8CastTensor.
(The reason why a separate subclass might be easier than trying to reuse the same one is that the cast function is somewhat different with different args, so unifying it might be more clunky than beneficial.)
| return fp8_tensor, None | ||
|
|
||
|
|
||
| def cast_to_float8_e4m3fn( |
There was a problem hiding this comment.
this is code motion right?
There was a problem hiding this comment.
This is moving from a method on the Float8DynamicLinear module to a standalone function so that it can be called from fsdp_pre_all_gather().
There was a problem hiding this comment.
Since this is mainly code motion and is the cast specific to dynamic scaling, I think it might be better to leave it in this file.
There was a problem hiding this comment.
ok, maybe move below Float8DynamicLinear to keep the main class first?
| self._tensor = tensor | ||
| self._emulate = emulate | ||
| # Optional cache for pre-computed fp8 data/scale | ||
| self._fp8_data: Optional[torch.Tensor] = None |
There was a problem hiding this comment.
just curious, what's the intended usage of this? Is there a risk for the cache to become invalid?
There was a problem hiding this comment.
This is so that we can pre-compute all of the Float8DynamicLinear modules' fp8 casts together to only issue one all-reduce and to fuse some computation.
The cache can become invalid if the user does not call the pre-compute function after every optimizer step.
There was a problem hiding this comment.
is pre-compute called by users or is it handled by other parts of the system?
There was a problem hiding this comment.
I moved this to the next PR (which is WIP), but the pre-compute would be called by the user. It would be similar to the sync amax function for delayed scaling. Users would need to call it after optimizer step and before the next forward.
| for m in module.modules() | ||
| if isinstance(m, Float8DynamicLinear) | ||
| and isinstance(m.weight, DTensor) | ||
| and isinstance(m.weight._local_tensor, Float8DynamicLinearWeightTensor) |
There was a problem hiding this comment.
do I understand correctly how this currently works?
- single GPU: Float8DynamicLinear is initialized with weight of type nn.Parameter
- when distributed code is initialized, Float8DynamicLinear instances get their weight wrapped with DTensor
- the wrapped DTensor weights have a _local_tensor attribute of type Float8DynamicLinearWeightTensor
?
if I understood that correctly, one confusion I have is that it seems that we are defining Float8DynamicLinear and Float8DynamicLinearWeightTensor together, but they need a separate piece of code (which lives outside of this repo) to know how to work together. Would be great to see if we can define the interaction more clearly here.
There was a problem hiding this comment.
is there anything stopping us from just wrapping the weight with Float8DynamicLinearWeightTensor Float8DynamicLinearWeightTensor regardless of whether distributed is used or not?
There was a problem hiding this comment.
is there anything stopping us from just wrapping the weight with Float8DynamicLinearWeightTensor Float8DynamicLinearWeightTensor regardless of whether distributed is used or not?
I think in an ideal world where tensor subclasses had parity with torch.Tensor, then we should unify the code paths and always use the subclass.
Some things with the subclass:
torch.optimoptimizers andclip_grad_norm_no longer default toforeach=True(which lead to silent performance degradation)- Ops now go through
__torch_dispatch__, which currently runs up to twotree_map_onlycalls, incurring extra CPU overheads
For FSDP2, we are accepting these concerns since we do not have much choice. For single-GPU, if users do not care about the FSDP all-gather, then they may not want to opt into these concerns.
do I understand correctly how this currently works?
Your understanding looks right to me!
if I understood that correctly, one confusion I have is that it seems that we are defining Float8DynamicLinear and Float8DynamicLinearWeightTensor together, but they need a separate piece of code (which lives outside of this repo) to know how to work together. Would be great to see if we can define the interaction more clearly here.
Is that separate piece of code referring to DTensor specifically? Would you prefer a comment to document this in the file or something else?
There was a problem hiding this comment.
Also the DTensor in question is not a result of TP/SP but rather the ones used in the implementation of FSDP2 right? Do you think other subclasses looking to interop correctly with fsdp2 will also need similiar logic?
There was a problem hiding this comment.
Also the DTensor in question is not a result of TP/SP but rather the ones used in the implementation of FSDP2 right?
Yep!
Do you think other subclasses looking to interop correctly with fsdp2 will also need similiar logic?
I think the particular issue is related to wrapper subclasses (including DTensor). Wrapper subclasses do not respect the isinstance() checks that would otherwise work on their inner tensor(s).
I am not sure what a good solution here would be since some wrapper subclasses like DTensoralways have 1 inner tensor, in which case forwarding a type/isinstance check to the inner tensor could make sense, while other wrapper subclasses have several inner tensors and forwarding such checks to inner tensors may not make sense.
There was a problem hiding this comment.
I think the rest of the code looks good, curious if @vkuzo has more thoughts on this aspect
| child.amax_and_scale_synced = True | ||
|
|
||
|
|
||
| def precompute_float8_weights(module: nn.Module) -> None: |
There was a problem hiding this comment.
maybe these kind of utils warrant their own file?
There was a problem hiding this comment.
yeah, we had a distributed utils file that we can add back
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
| @classmethod | ||
| def __torch_dispatch__(cls, func, types, args, kwargs=None): | ||
| if func == torch.ops.aten.detach.default: | ||
| return WeightWithDynamicFloat8CastTensor( |
There was a problem hiding this comment.
changed to this instead of return args[0] 😄 cc: @bdhirsh
Good point. Shall I add something to the readme? Or should we wait until we have added some For example, we will need to disable |
|
@awgu sounds like we have some more work to do before broadcasting this support, im cool with punting on updating readme till its been a more validated |
| return Float8Tensor.to_float8( | ||
| inpt_tensor, scale, torch.float8_e4m3fn, mm_config=self.forward_config | ||
| ) | ||
| def cast_to_float8_e4m3fn( |
There was a problem hiding this comment.
it's unusual to have a regular function as well as a class method with the same name but different args, can we just have one?
There was a problem hiding this comment.
For consistency, I will also move cast_to_float8_e5m2_bw to be outside the Float8DynamicLinear. Otherwise, we have one cast function as standalone and the other as on the module.
| new_mod.bias = mod.bias | ||
| new_mod.forward_config = ScaledMMConfig(emulate, True if not emulate else False) | ||
| new_mod.backward_config = ScaledMMConfig(emulate, False) | ||
| if config.enable_fsdp_fp8_all_gather: |
There was a problem hiding this comment.
how does the UX look like for applying FSDP2? How do we expect the user to sequence applying float8 and applying FSDP2?
if FSDP2 is applied after float8, should this weight wrapping live somewhere else?
There was a problem hiding this comment.
looking at one of the test cases,
ref_module = self.swap_linear_with_dynamic(ref_module).cuda()
with set_enable_fsdp_fp8_all_gather(enable_fsdp_fp8_all_gather):
module = self.swap_linear_with_dynamic(module)
for submodule in module:
fully_shard(submodule)
fully_shard(module)
seems like clean places to put the wrapping would be either (1) in from_float but not dependent on FSDP, or (2) inside or after the fully_shard function.
is having it in the config temporary or the long term solution? Can we move it to (1) after the parameter wrapper works without FSDP?
There was a problem hiding this comment.
I hope that the current global flag is a temporary solution. The risk for it to not be a temporary solution would be if the performance of fp8 all-gather cannot beat without the fp8 all-gather for all realistic cases. Notably, we probably require the extra call to precompute_float8_casts after the optimizer step in order to make the fp8 all-gather performant enough (for dynamic scaling), which means that enabling the fp8 all-gather would require an intrusive change in UX.
For now, I think the flag will help with testing/experimentation. Originally, it was a bool arg to from_float, but @bdhirsh brought up the fact that fp8 all-gather makes more sense to be to totally enabled on all float8 linears or totally disabled, suggesting that a global flag may make more sense than an arg to from_float.
| mm_config: Optional[ScaledMMConfig] = None | ||
|
|
||
| def unwrap(t): | ||
| nonlocal mm_config |
There was a problem hiding this comment.
trying to wrap my brain around this...if mm_config is not coming from t, then where is it coming from? do we ever need to merge it other than for scaled_mm?
There was a problem hiding this comment.
I think this was more for future proofing. As is, since we only have a select few ops preserve the subclass and none of those ops take in >1 tensor input, then we should never have to merge. However, in the future, we may want to have more ops preserve subclass.
I can remove the merge logic and error with NotImplementedError if you prefer. Feel free to let me know if this does not make sense.
if mm_config is not coming from t, then where is it coming from?
I think mm_config is always coming from the ts in the inputs.
| # FSDP pads its local tensor on dim-0. The subclass should be preserved such | ||
| # that the padded local tensor (and any transformations like copying to GPU) | ||
| # is of the subclass as well. | ||
| _ops_to_preserve_subclass = { |
There was a problem hiding this comment.
is there anything we need to support other than these ops and creating a float8tensor? If not, can we just support what we need and not the generic case?
There was a problem hiding this comment.
I think the user could do arbitrary ops on the parameters (e.g. gradient norm clipping, metrics calculations, EMA, etc.). For those, we do not need to preserve the subclass, but we should still consider the ops valid for the tensor.
I was thinking that it might be difficult to nail down all of the ops that we want to support. It seems okay to me to know which ops should preserve the subclass since those would be the ones needed for sharding.
vkuzo
left a comment
There was a problem hiding this comment.
had some nit comments but high level looks great!
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
This PR requires pytorch/pytorch#122908. This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first. - We add a config option `enable_fsdp_fp8_all_gather: bool = False` in `float8_experimental.config` that can be set to `True` to enable the new behavior. - We only support `Float8DynamicLinear` for now. - For the fp8 all-gather, we make `Float8DynamicLinear.weight` to be a tensor subclass `Float8DynamicLinearWeightTensor`. This can be thought of as a `torch.Tensor` weight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclass `Float8DynamicLinearWeightTensor`. - The subclass is a `__torch_dispatch__` wrapper subclass since that is the kind of subclass most compatible with `torch.compile`. - The subclass defines `fsdp_pre_all_gather()` and `fsdp_post_all_gather()` to implement the fp8 all-gather. - We include unit testing that covers eager-mode correctness and memory usage. [ghstack-poisoned]
|
@awgu has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
I ran |
Stack from ghstack (oldest at bottom):
This PR requires pytorch/pytorch#122908.
This PR adds an option for FSDP fp8 all-gather with FSDP2, focusing on correctness first.
enable_fsdp_fp8_all_gather: bool = Falseinfloat8_experimental.configthat can be set toTrueto enable the new behavior.Float8DynamicLinearfor now.Float8DynamicLinear.weightto be a tensor subclassFloat8DynamicLinearWeightTensor. This can be thought of as atorch.Tensorweight that knows how to cast itself to fp8. Since the cast logic is specific to dynamic scaling (i.e. it would not be the exact same for delayed scaling), I preferred to simply name the subclassFloat8DynamicLinearWeightTensor.__torch_dispatch__wrapper subclass since that is the kind of subclass most compatible withtorch.compile.fsdp_pre_all_gather()andfsdp_post_all_gather()to implement the fp8 all-gather.Differential Revision: D56192107