a
    d                     @   sl   d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	 erXd dl
mZ d dlmZ G dd deZdS )	    )OptionalTYPE_CHECKINGN)LiteralMixedPrecision)_TORCH_GREATER_EQUAL_1_12ShardedGradScalerc                       sH   e Zd ZdZded eed dd fddZedd	d
dZ	  Z
S )FSDPPrecisionz-AMP for Fully Sharded Data Parallel training.N)16   bf16r	   )	precisiondevicescalerreturnc                    sH   t stdddlm} t j|||d u r<t|dkr<| nd d d S )Nz:`FSDPPrecision` is supported from PyTorch v1.12.0 onwards.r   r   r   )r   r   r   )r   NotImplementedError*torch.distributed.fsdp.sharded_grad_scalerr	   super__init__str)selfr   r   r   r	   	__class__ p/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/plugins/precision/fsdp.pyr      s    zFSDPPrecision.__init__TorchMixedPrecision)r   c                 C   sP   ddl m} | jdkrtj}n$| jdkr0tj}ntd| jd||||dS )Nr   r   r   r   z-Was unable to infer precision type, received .)Zparam_dtypeZreduce_dtypeZbuffer_dtype)2torch.distributed.fsdp.fully_sharded_data_parallelr   r   torchfloat16Zbfloat16
ValueError)r   r   Zdtyper   r   r   mixed_precision_config,   s    

z$FSDPPrecision.mixed_precision_config)N)__name__
__module____qualname____doc__r   r   r   r   propertyr"   __classcell__r   r   r   r   r
      s    r
   )typingr   r   r   Ztyping_extensionsr   Z-lightning_fabric.plugins.precision.native_ampr   Z"lightning_fabric.utilities.importsr   r   r   r   r	   r
   r   r   r   r   <module>   s   