Skip to content

Commit 601c060

Browse files
tshu-wlantigajedyang97Borda
authored
Add convert_module to FSDP (#20323)
* Add `convert_module` to FSDP * Update ChangeLog * make plugin type check more flexible (#20186) Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> Co-authored-by: Luca Antiga <luca.antiga@gmail.com> * Make plugin type check more flexible (Fabric) (#20452) * make plugin type check more flexible * Change signature and make the equivalent changes to Fabric connector --------- Co-authored-by: Jianing Yang <jed970610@gmail.com> Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> * Pin setuptools for gpu builds * Fix link in doc --------- Co-authored-by: Luca Antiga <luca.antiga@gmail.com> Co-authored-by: Jianing Yang <jed970610@gmail.com> Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> Co-authored-by: Luca Antiga <luca@lightning.ai>
1 parent 30545d6 commit 601c060

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

src/lightning/fabric/plugins/precision/fsdp.py

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def __init__(self, precision: _PRECISION_INPUT, scaler: Optional["ShardedGradSca
7474
}
7575
self._desired_input_dtype = precision_to_type[self.precision]
7676

77+
@override
78+
def convert_module(self, module: Module) -> Module:
79+
if "true" in self.precision:
80+
return module.to(dtype=self._desired_input_dtype)
81+
return module
82+
7783
@property
7884
def mixed_precision_config(self) -> "TorchMixedPrecision":
7985
from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision as TorchMixedPrecision

src/lightning/pytorch/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
3535
- Fixed `_LoggerConnector`'s `_ResultMetric` to move all registered keys to the device of the logged value if needed ([#19814](https://github.com/Lightning-AI/pytorch-lightning/issues/19814))
3636
- Fixed `_optimizer_to_device` logic for special 'step' key in optimizer state causing performance regression ([#20019](https://github.com/Lightning-AI/lightning/pull/20019))
3737
- Fixed parameter counts in `ModelSummary` when model has distributed parameters (DTensor) ([#20163](https://github.com/Lightning-AI/pytorch-lightning/pull/20163))
38+
- Fixed PyTorch Lightning FSDP takes more memory than PyTorch FSDP ([#20323](https://github.com/Lightning-AI/pytorch-lightning/pull/20323))
3839

3940

4041
## [2.3.0] - 2024-06-13

src/lightning/pytorch/plugins/precision/fsdp.py

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import torch
1818
from lightning_utilities import apply_to_collection
1919
from torch import Tensor
20+
from torch.nn import Module
2021
from typing_extensions import get_args, override
2122

2223
import lightning.pytorch as pl
@@ -73,6 +74,12 @@ def __init__(self, precision: _PRECISION_INPUT, scaler: Optional["ShardedGradSca
7374
}
7475
self._desired_input_dtype = precision_to_type[self.precision]
7576

77+
@override
78+
def convert_module(self, module: Module) -> Module:
79+
if "true" in self.precision:
80+
return module.to(dtype=self._desired_input_dtype)
81+
return module
82+
7683
@override
7784
def clip_grad_by_norm(self, *_: Any, **__: Any) -> None:
7885
# see https://pytorch.org/docs/stable/fsdp.html#torch.distributed.fsdp.FullyShardedDataParallel.clip_grad_norm_

tests/tests_fabric/plugins/precision/test_fsdp.py

+18
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,21 @@ def test_invalid_precision_with_fsdp_precision():
127127

128128
with pytest.raises(ValueError, match="is not supported in FSDP. `precision` must be one of"):
129129
FSDPPrecision(precision="64-true")
130+
131+
132+
@pytest.mark.parametrize(
133+
("precision", "expected_dtype"),
134+
[
135+
("32-true", torch.float32),
136+
("bf16-mixed", torch.float32),
137+
("16-mixed", torch.float32),
138+
("bf16-true", torch.bfloat16),
139+
("16-true", torch.float16),
140+
],
141+
)
142+
def test_convert_module(precision, expected_dtype):
143+
precision = FSDPPrecision(precision=precision)
144+
module = torch.nn.Linear(2, 2)
145+
assert module.weight.dtype == module.bias.dtype == torch.float32
146+
module = precision.convert_module(module)
147+
assert module.weight.dtype == module.bias.dtype == expected_dtype

tests/tests_pytorch/plugins/precision/test_fsdp.py

+18
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ def test_fsdp_precision_config(precision, expected):
4040
assert config.reduce_dtype == expected[2]
4141

4242

43+
@pytest.mark.parametrize(
44+
("precision", "expected_dtype"),
45+
[
46+
("32-true", torch.float32),
47+
("bf16-mixed", torch.float32),
48+
("16-mixed", torch.float32),
49+
("bf16-true", torch.bfloat16),
50+
("16-true", torch.float16),
51+
],
52+
)
53+
def test_convert_module(precision, expected_dtype):
54+
precision = FSDPPrecision(precision=precision)
55+
module = torch.nn.Linear(2, 2)
56+
assert module.weight.dtype == module.bias.dtype == torch.float32
57+
module = precision.convert_module(module)
58+
assert module.weight.dtype == module.bias.dtype == expected_dtype
59+
60+
4361
def test_fsdp_precision_default_scaler():
4462
from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler
4563

0 commit comments

Comments
 (0)