Skip to content

Question on MixedPrecisionPolicy in FSDP2 #600

Closed

Description

@karan-dalal

Hi - thank you for this incredible resource. I had a question regarding the module level mixed precision in the MixedPrecisionPolicy.

From https://github.com/pytorch/pytorch/blob/main/torch/distributed/_composable/fsdp/_fsdp_api.py#L9:

class MixedPrecisionPolicy:
    """
    This configures FSDP's mixed precision. Unlike autocast, this applies mixed
    precision at the module level, not op level, which means low-precision
    activations are saved for backward and high-to-low-precision casts are
    incurred only at module boundaries.

    FSDP works well with module-level mixed precision since it keeps the
    high-precision sharded parameters in memory anyway. In other words, FSDP
    does not require any extra memory to keep a high-precision copy of the
    parameters for the optimizer step.

I assume that FSDP2 will choose to cast a layer in a Transformer, for example, to BF16. How would this affect specific operations in the layer, such as the SoftMax, which typically run in FP32? Will any explicit casting (such as .to(float32)) be affected by this MP Policy?

Activity

  1. awgu commented on Oct 6, 2024

    @awgu
    Contributor

    With FSDP2's mixed precision, the forward (and backward) run with bf16 tensors. More specifically, FSDP2 can cast the module forward inputs for the user to bf16, and FSDP2 will all-gather the parameters in bf16. That is all -- there is no further implicit behavior or anything.

    In other words, it is like running a forward and backward on a single GPU but when model.to(torch.bfloat16) and passing input in torch.bfloat16 (if floating-point). Note however that for FSDP, the sharded parameters used for optimizer can still be fp32.

    Specific kernel implementations can still accumulate internally in fp32, but since the operator will output in bf16, there will be that rounding that happens at the end of the operator.

  2. karan-dalal commented on Oct 6, 2024

    @karan-dalal
    Author

    Makes sense. Thank you!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions