|
| 1 | +import sys |
| 2 | +from typing import TYPE_CHECKING, Literal, Optional |
| 3 | + |
| 4 | +import lightning.pytorch as pl |
| 5 | +from lightning.fabric.utilities.rank_zero import rank_zero_deprecation |
| 6 | +from lightning.pytorch.plugins.precision import ( |
| 7 | + BitsandbytesPrecision, |
| 8 | + DeepSpeedPrecision, |
| 9 | + DoublePrecision, |
| 10 | + FSDPPrecision, |
| 11 | + HalfPrecision, |
| 12 | + MixedPrecision, |
| 13 | + Precision, |
| 14 | + TransformerEnginePrecision, |
| 15 | + XLAPrecision, |
| 16 | +) |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler |
| 20 | + |
| 21 | + |
| 22 | +def _patch_sys_modules() -> None: |
| 23 | + sys.modules["lightning.pytorch.plugins.precision.precision_plugin"] = sys.modules[ |
| 24 | + "lightning.pytorch.plugins.precision.precision" |
| 25 | + ] |
| 26 | + |
| 27 | + |
| 28 | +class FSDPMixedPrecisionPlugin(FSDPPrecision): |
| 29 | + """AMP for Fully Sharded Data Parallel (FSDP) Training. |
| 30 | +
|
| 31 | + .. deprecated:: Use :class:`FSDPPrecision` instead. |
| 32 | +
|
| 33 | + .. warning:: This is an :ref:`experimental <versioning:Experimental API>` feature. |
| 34 | +
|
| 35 | + """ |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, precision: Literal["16-mixed", "bf16-mixed"], device: str, scaler: Optional["ShardedGradScaler"] = None |
| 39 | + ) -> None: |
| 40 | + rank_zero_deprecation( |
| 41 | + f"The `{type(self).__name__}` is deprecated." |
| 42 | + " Use `lightning.pytorch.plugins.precision.FSDPPrecision` instead." |
| 43 | + ) |
| 44 | + super().__init__(precision=precision, scaler=scaler) |
| 45 | + |
| 46 | + |
| 47 | +def _patch_classes() -> None: |
| 48 | + classes_map = ( |
| 49 | + # module name, old name, new class |
| 50 | + ("bitsandbytes", "BitsandbytesPrecisionPlugin", BitsandbytesPrecision), |
| 51 | + ("deepspeed", "DeepSpeedPrecisionPlugin", DeepSpeedPrecision), |
| 52 | + ("double", "DoublePrecisionPlugin", DoublePrecision), |
| 53 | + ("fsdp", "FSDPPrecisionPlugin", FSDPPrecision), |
| 54 | + ("fsdp", "FSDPMixedPrecisionPlugin", FSDPPrecision), |
| 55 | + ("half", "HalfPrecisionPlugin", HalfPrecision), |
| 56 | + ("amp", "MixedPrecisionPlugin", MixedPrecision), |
| 57 | + ("precision", "PrecisionPlugin", Precision), |
| 58 | + ("transformer_engine", "TransformerEnginePrecisionPlugin", TransformerEnginePrecision), |
| 59 | + ("xla", "XLAPrecisionPlugin", XLAPrecision), |
| 60 | + ) |
| 61 | + |
| 62 | + for module_name, deprecated_name, new_class in classes_map: |
| 63 | + setattr(getattr(pl.plugins.precision, module_name), deprecated_name, new_class) |
| 64 | + setattr(pl.plugins.precision, deprecated_name, new_class) |
| 65 | + setattr(pl.plugins, deprecated_name, new_class) |
| 66 | + |
| 67 | + # special treatment for `FSDPMixedPrecisionPlugin` because it has a different signature |
| 68 | + setattr(pl.plugins.precision.fsdp, "FSDPMixedPrecisionPlugin", FSDPMixedPrecisionPlugin) |
| 69 | + setattr(pl.plugins.precision, "FSDPMixedPrecisionPlugin", FSDPMixedPrecisionPlugin) |
| 70 | + setattr(pl.plugins, "FSDPMixedPrecisionPlugin", FSDPMixedPrecisionPlugin) |
| 71 | + |
| 72 | + |
| 73 | +_patch_sys_modules() |
| 74 | +_patch_classes() |
0 commit comments