a
    d9                  
   @   s  d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z d dl	mZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ  d dlm!Z" d dlm#Z$ d dlm%Z& d dlm'Z( d dlm)Z) d dl*m+Z, d dl-m.Z/ d dl-m0Z1 d dl-m2Z3 d dl-m4Z5 d dl-m6Z7 d dl-m8Z9 d dl:mZ; d dl:m<Z= d dl:m>Z? d dl:m@ZA d dl:mZB d dl:m!ZC d dl:m#ZD d dl:m%ZE d dl:m'ZF d dl:mGZH d dlImJZJmKZK ee5eef ZLeeLeMf ZNG d d! d!eeZOeeeMe,f  eeeeP eMePf  eeeeP eMePf  eeeeP eMePf  eeeeeP eMePf  eeeMe,f  f d"d#d$ZQeFe(d%d&d'ZRee5 ed(d)d*ZSd+S ),    )ABC)ListOptionalTupleUnion)Fabric)_PLUGIN_INPUT)_PRECISION_INPUT)CheckpointIOClusterEnvironment)DeepSpeedPrecision)DoublePrecision)MixedPrecision)	Precision)TPUBf16Precision)TPUPrecision)DataParallelStrategy)DDPStrategy)DeepSpeedStrategy)SingleDeviceStrategy)SingleTPUStrategy)Strategy)XLAStrategy)Accelerator)DeepSpeedPrecisionPlugin)DoublePrecisionPlugin)MixedPrecisionPlugin)PrecisionPlugin)TPUBf16PrecisionPlugin)TPUPrecisionPlugin)DDPShardedStrategy)DDPSpawnShardedStrategy)DDPSpawnStrategy)TPUSpawnStrategyrank_zero_deprecationrank_zero_warnc                       s   e Zd ZdZdeeeef  eeeef  eee	e
 ee
f  e
eeeee	e f  eee	e
 ee
f  eee	e
 ee
f  dd	 fddZ  ZS )	LightningLiteaV  Lite accelerates your PyTorch training or inference code with minimal changes required.

    .. deprecated:: v1.9.0
        The `pytorch_lightning.lite.LightningLite` class was deprecated in v1.9.0 and will be renamed to
        `lightning.fabric.Fabric` in v2.0.0. It is no longer part of the pure `pytorch_lightning` package, and now
        lives in the main `lightning` package.

    - Automatic placement of models and data onto the device.
    - Automatic support for mixed and double precision (smaller memory footprint).
    - Seamless switching between hardware (CPU, GPU, TPU) and distributed training strategies
      (data-parallel training, sharded training, etc.).
    - Automated spawning of processes, no launch utilities required.
    - Multi-node support.

    Args:
        accelerator: The hardware to run on. Possible choices are:
            ``"cpu"``, ``"cuda"``, ``"mps"``, ``"gpu"``, ``"tpu"``, ``"auto"``.
        strategy: Strategy for how to run across multiple devices. Possible choices are:
            ``"dp"``, ``"ddp"``, ``"ddp_spawn"``, ``"deepspeed"``, ``"fsdp"``.
        devices: Number of devices to train on (``int``), which GPUs to train on (``list`` or ``str``), or ``"auto"``.
            The value applies per node.
        num_nodes: Number of GPU nodes for distributed training.
        precision: Double precision (``64``), full precision (``32``), half precision (``16``),
            or bfloat16 precision (``"bf16"``).
        plugins: One or several custom plugins
        gpus: Provides the same function as the ``devices`` argument but implies ``accelerator="gpu"``.

            .. deprecated:: v1.8.0
                ``gpus`` has been deprecated in v1.8.0 and will be removed in v2.0.0.
                Please use ``accelerator='gpu'`` and ``devices=x`` instead.

        tpu_cores: Provides the same function as the ``devices`` argument but implies ``accelerator="tpu"``.

            .. deprecated:: v1.8.0
                ``tpu_cores`` has been deprecated in v1.8.0 and will be removed in v2.0.0.
                Please use ``accelerator='tpu'`` and ``devices=x`` instead.
    N       )	acceleratorstrategydevices	num_nodes	precisionpluginsgpus	tpu_coresreturnc	                    s   t d |d us|d ur,t||||d\}}t|tr@t|}	nt|trZdd |D }	n|}	t|ttfv sv|dv rd}
t|tu s|dkrd}
t	d	|
 d
t
 j|t|trt|n|||||	d d S )NzThe `pytorch_lightning.lite.LightningLite` class was deprecated in v1.9.0 and will be renamed to `lightning_fabric.Fabric` in v2.0.0.)r*   r,   r0   r1   c                 S   s"   g | ]}t |trt|n|qS  )
isinstancePLPrecisionPlugin_to_lite_precision).0pluginr3   r3   d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/lite/lite.py
<listcomp>~   s   z*LightningLite.__init__.<locals>.<listcomp>)Zddp_shardedddp_sharded_spawn r;   z, start_method='spawn'zLightningLite's sharded implementation using FairScale has been removed in favor of PyTorch's FSDP. You can try `Fabric(strategy=FSDPStrategy(sharding_strategy=ShardingStrategy.SHARD_GRAD_OPuj   ))` which implements optimizer-only sharding à la ZeRO-2. Or full sharding with `Fabric(strategy='fsdp')`)r*   r+   r,   r-   r.   r/   )r%    _convert_deprecated_device_flagsr4   r5   r6   listtypePLDDPShardedStrategyPLDDPSpawnShardedStrategyRuntimeErrorsuper__init__
PLStrategy_to_lite_strategy)selfr*   r+   r,   r-   r.   r/   r0   r1   Zlite_pluginsZspawn_message	__class__r3   r9   rD   a   sD    



zLightningLite.__init__)NNNr(   r)   NNN)__name__
__module____qualname____doc__r   r   strPLAcceleratorrE   r   intr	   _PL_PLUGIN_INPUTrD   __classcell__r3   r3   rH   r9   r'   :   s(   (        r'   )r*   r,   r0   r1   r2   c                 C   s   |durt d|d|d |dur<t d|d|d |pB|}|r|g ddfvr|rptd	| d
| d |dur|durtd|d|d | du r|rd} |rd} || fS )zEmit deprecation warnings for gpus and tpu_cores and translate them into the new accelerator and devices.

    Similar implementation as in ``pytorch_lightning.trainer.connectors.accelerator_connector``.
    NzSetting `Lite(gpus=zf)` is deprecated in v1.8.0 and will be removed in v2.0.0. Please use `Lite(accelerator='gpu', devices=z)` instead.zSetting `Lite(tpu_cores=zf)` is deprecated in v1.8.0 and will be removed in v2.0.0. Please use `Lite(accelerator='tpu', devices=r   0zThe option `devices=z0` will be ignored and the device specific numberz will be used instead.zBoth `Lite(gpus=z, tpu_cores=z5)` were specified. Please choose only one of the two.ZtpuZcudar$   )r*   r,   r0   r1   Z deprecated_devices_specific_flagr3   r3   r9   r=      s<    
r=   )r+   r2   c                 C   sv  t | }|tu rBtf | j| j| j| jt| j| j	| j
d| jS |tu rtf | j| j| j| jt| j| j	| j
| jd| jS |tu rt| j| j| jt| jdS |tu rt| j| j| jt| j| j	| j| j| j| j| j| j| j| jdS |tu rt| j| j| jt| jdS |tu r8t| j| j| jt| jdS |tu r`t | jj!| j| jt| jdS t"d|j# ddS )	zARe-instantiates a PL-Strategy as the corresponding Lite-Strategy.)r*   parallel_devicescluster_environmentcheckpoint_ior.   process_group_backendtimeout)r*   rT   rU   rV   r.   rW   rX   Zstart_method)r*   rT   rV   r.   )r*   rT   rU   r.   rW   configremote_deviceload_full_weights
loss_scaleinitial_scale_powerloss_scale_window
hysteresismin_loss_scale)devicer*   rV   r.   zUnsupported strategy: ``N)$r?   PLDDPStrategyLiteDDPStrategyr*   rT   rU   rV   r6   Zprecision_pluginrW   _timeoutZ_ddp_kwargsPLDDPSpawnStrategyZ_start_methodPLTPUSpawnStrategyr   PLDeepSpeedStrategyLiteDeepSpeedStrategyrY   rZ   r[   r\   r]   r^   r_   r`   PLDataParallelStrategyLiteDataParallelStrategyPLSingleDeviceStrategyLiteSingleDeviceStrategyZroot_devicePLSingleTPUStrategyLiteSingleTPUStrategyindexNotImplementedErrorrJ   )r+   Zstrategy_clsr3   r3   r9   rF      s    	


rF   )r8   r2   c                 C   s   t | tu rt S t | tu r2t| j| j| jdS t | tu rDt	 S t | t
u r\t| jdS t | tu rnt S t | tu rt S tddS )zPRe-instantiates a PL-PrecisionPlugin as the corresponding Lite-Precision plugin.)r.   ra   scaler)r.   a  You passed an unsupported plugin as input to Lite(plugins=...) or to a strategy. If you built a custom plugin, please change it to subclass the `lightning_lite.plugins.precision.Precision` class. Otherwise, please open an issue on the Lightning GitHub repository with your use case.N)r?   r5   LitePrecisionPLMixedPrecisionPluginLiteMixedPrecisionr.   ra   rr   PLDoublePrecisionPluginLiteDoublePrecisionPLDeepSpeedPrecisionPluginLiteDeepSpeedPrecisionPLTPUPrecisionPluginLiteTPUPrecisionPLTPUBf16PrecisionPluginLiteTPUBf16Precision	TypeError)r8   r3   r3   r9   r6     s&    r6   N)Tabcr   typingr   r   r   r   Zlightning_fabricr   Zlightning_fabric.connectorr   Z_LITE_PLUGIN_INPUTr	   Zlightning_fabric.pluginsr
   r   r   ry   r   rw   r   ru   r   rs   r   r}   r   r{   Zlightning_fabric.strategiesr   rk   r   rd   r   ri   r   rm   r   ro   r   ZLiteStrategyr   Zpytorch_lightning.acceleratorsr   rO   Zpytorch_lightning.pluginsr   rx   r   rv   r   rt   r   r5   r   r|   r   rz   Zpytorch_lightning.strategiesrj   r    r@   r!   rA   r"   rf   rc   rh   rl   rn   rE   r#   rg   Z%pytorch_lightning.utilities.rank_zeror%   r&   Z
_PL_PLUGINrN   rQ   r'   rP   r=   rF   r6   r3   r3   r3   r9   <module>   s^   c(+O