Repository navigation
Generalize Optimizer validation to accommodate both FSDP 1.x and 2.0 #16732
Description
Description & Motivation
Generalize Optimizer validation to accommodate both FSDP 1.x and 2.x
In preparing the downstream package I maintain (finetuning-scheduler) to support PyTorch 2.0's version of FSDP, I noticed that Lightning has yet to be adapted to accommodate FSDP 2.x's useful use_orig_params params mode.
Currently, false-positive "The optimizer does not seem to reference any FSDP parameters" ValueErrors will be triggered when the use_orig_params FSDP configuration is used because such a configuration does not require FlatParameter instances be present in the Optimizer.
FSDP versions 1.13 and 2.0 both ensure FSDP flattened parameters have the _fsdp_flattened attribute set, allowing for a single check to work across both versions (this is the check used internally by PyTorch FSDP). For BC, we need to keep the existing FlatParameter-based check for versions below 1.13, but we should be able to remove that version guard once 1.12 is deprecated.
def _optimizer_has_flat_params(optimizer: Optimizer) -> bool:
if _TORCH_GREATER_EQUAL_1_13:
return any(getattr(param, _FSDP_FLATTENED, False) for param in optimizer.param_groups[0]["params"])
else:
from torch.distributed.fsdp import FlatParameter
return any(isinstance(param, FlatParameter) for param in optimizer.param_groups[0]["params"])I'm sure support for this (along with other FSDP 2.x features) will be arriving as the team's priorities dictate but I'm hoping this issue and the PR I'll be opening momentarily will allow PyTorch 2.x development/testing to continue for use_orig_params in a BC-compatible manner while also adding some useful 1.x and 2.x FSDP tests.
Pitch
The PR I'll be opening for this feature makes 4 minor changes:
- Generalizes
_optimizer_has_flat_paramsto accommodate FSDP 1.x and 2.0 usage.- To avoid directly depending upon PyTorch's protected
_is_fsdp_flattenedmethod and protected constant (which have both changed locations between versions), I've included the relevant constant and attribute check infabric/strategies/fsdp.pydirectly rather than relying on a protected attribute whose location is version dependent.
- To avoid directly depending upon PyTorch's protected
- Adds an FSDP 2.0 compatible
auto_wrap_policytest - Adds an FSDP 2.0
_FSDPPolicy-based test that usesuse_orig_paramsto validate the multi-versionOptimizervalidation while also testing the preferred_FSDPPolicy-based auto-wrapping approach moving forward - Guards the 1.x compatible
auto_wrap_policybehind the necessarymax_torchmark
Alternatives
One could use an FSDP instance-based check but that seems both more complicated and brittle. The _fsdp_flattened attribute is used extensively internally and was the best candidate attribute for this check IMHO.
Additional context
No response
Activity
stale commentedon Mar 19, 2023
This issue has been automatically marked as stale because it hasn't had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions - the Lightning Team!
speediedan commentedon Mar 23, 2023
#16733 is set to be reviewed soon to address this issue