Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Added eager fp8 all-gather for dynamic scaling (subclass) - #247

Closed
awgu wants to merge 14 commits into
gh/awgu/10/basefrom
gh/awgu/10/head
Closed

awgu wants to merge 14 commits into
gh/awgu/10/basefrom
gh/awgu/10/head

Conversation

@awgu

@awgu awgu commented Mar 28, 2024 •

Copy link
Copy Markdown

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.

  • 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.

Differential Revision: D56192107

awgu pushed a commit that referenced this pull request Mar 28, 2024
ghstack-source-id: 62538b9
Pull Request resolved: #247
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 28, 2024
awgu pushed a commit that referenced this pull request Mar 28, 2024
ghstack-source-id: 8a1b2b7
Pull Request resolved: #247
awgu pushed a commit that referenced this pull request Apr 2, 2024
ghstack-source-id: a698c3f
Pull Request resolved: #247
awgu pushed a commit that referenced this pull request Apr 10, 2024
ghstack-source-id: ac2f493
Pull Request resolved: #247
awgu pushed a commit that referenced this pull request Apr 10, 2024
ghstack-source-id: 0bc3740
Pull Request resolved: #247
@awgu

awgu commented Apr 10, 2024

Copy link
Copy Markdown
Author

cc: @drisspg @vkuzo an initial pass over this PR would be much appreciated!

We should have some review on the PyTorch-side PR soon too.

@awgu
awgu requested review from drisspg and vkuzo April 10, 2024 19:25
}


class Float8DynamicLinearWeightTensor(torch.Tensor):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) 🤔 .

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: put in utils.py?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is code motion right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, what's the intended usage of this? Is there a risk for the cache to become invalid?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is pre-compute called by users or is it handled by other parts of the system?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do I understand correctly how this currently works?

  1. single GPU: Float8DynamicLinear is initialized with weight of type nn.Parameter
  2. when distributed code is initialized, Float8DynamicLinear instances get their weight wrapped with DTensor
  3. 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there anything stopping us from just wrapping the weight with Float8DynamicLinearWeightTensor Float8DynamicLinearWeightTensor regardless of whether distributed is used or not?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.optim optimizers and clip_grad_norm_ no longer default to foreach=True (which lead to silent performance degradation)
  • Ops now go through __torch_dispatch__, which currently runs up to two tree_map_only calls, 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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe these kind of utils warrant their own file?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we had a distributed utils file that we can add back

Comment thread test/test_fsdp2/test_fsdp2_eager.py Resolved
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 pushed a commit that referenced this pull request Apr 12, 2024
ghstack-source-id: faf1dc7
Pull Request resolved: #247
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 pushed a commit that referenced this pull request Apr 12, 2024
ghstack-source-id: 9e9f50b
Pull Request resolved: #247
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 pushed a commit that referenced this pull request Apr 12, 2024
ghstack-source-id: fccaa8c
Pull Request resolved: #247
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]
Andrew Gu added 2 commits April 12, 2024 11:50
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
awgu marked this pull request as ready for review April 12, 2024 19:47
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
awgu requested review from drisspg and vkuzo April 12, 2024 19:51
@classmethod
def __torch_dispatch__(cls, func, types, args, kwargs=None):
if func == torch.ops.aten.detach.default:
return WeightWithDynamicFloat8CastTensor(

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to this instead of return args[0] 😄 cc: @bdhirsh

@drisspg drisspg left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should document this new config somewhere and how to use?

@awgu

awgu commented Apr 12, 2024

Copy link
Copy Markdown
Author

Do you think we should document this new config somewhere and how to use?

Good point. Shall I add something to the readme? Or should we wait until we have added some torch.compile unit testing and have done some more validation on torchtrain?

For example, we will need to disable torch.compile on FSDP2's _pre_forward method to avoid erroring during tracing, so it looks like we will need to set a Dynamo config for that (pytorch/pytorch#123021).

@drisspg

drisspg commented Apr 12, 2024

Copy link
Copy Markdown
Contributor

@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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 vkuzo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had some nit comments but high level looks great!

Andrew Gu added 2 commits April 15, 2024 10:51
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

awgu commented Apr 16, 2024

Copy link
Copy Markdown
Author

@awgu has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@awgu

awgu commented Apr 16, 2024

Copy link
Copy Markdown
Author

I ran ./test/test_everything.sh. There is an existing failure on SAM (reproducible on trunk), but everything else passes (commenting out the SAM test).

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@awgu merged this pull request in ac065d0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants