o
    zi	                     @   s   d dl mZ d dlmZmZmZmZ d dl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mZ d d	lmZ G d
d deZdS )    )contextmanager)AnyContextManager	GeneratorLiteralN)apply_to_collection)Tensor)Module)override)_convert_fp_tensor_DtypeContextManager)	Precisionc                   @   s   e Zd ZU dZdZed ed< dded ddfddZed	e	de	fd
dZ
edefddZedefddZeeded fddZededefddZdS )HalfPrecisionzPlugin for training with half precision.

    Args:
        precision: Whether to use ``torch.float16`` (``'16-true'``) or ``torch.bfloat16`` (``'bf16-true'``).

    16-true)	bf16-truer   	precisionreturnNc                 C   s&   || _ |dkrtj| _d S tj| _d S )Nr   )r   torchbfloat16float16_desired_input_dtype)selfr    r   \/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/plugins/precision/half.py__init__%   s    zHalfPrecision.__init__modulec                 C   s   |j | jdS )N)dtype)tor   )r   r   r   r   r   convert_module)   s   zHalfPrecision.convert_modulec                 C   s
   t | jS N)r   r   r   r   r   r   tensor_init_context-   s   
z!HalfPrecision.tensor_init_contextc                 C   s   |   S r   )r!   r    r   r   r   module_init_context1   s   z!HalfPrecision.module_init_context)NNNc              	   c   s:    t  }t | j zdV  W t | dS t | w )zA context manager to change the default tensor type when tensors get created during the module's forward.

        See: :meth:`torch.set_default_tensor_type`

        N)r   get_default_dtypeset_default_dtyper   )r   default_dtyper   r   r   forward_context5   s   zHalfPrecision.forward_contextdatac                 C   s   t |tt| jdS )N)functionr   dst_type)r   r   r   r   )r   r'   r   r   r   convert_inputD   s   zHalfPrecision.convert_input)r   )__name__
__module____qualname____doc__r   r   __annotations__r   r
   r	   r   r   r!   r"   r   r   r&   r   r*   r   r   r   r   r      s   
 r   )
contextlibr   typingr   r   r   r   r   lightning_utilitiesr   r   torch.nnr	   typing_extensionsr
   (lightning_fabric.plugins.precision.utilsr   r   -pytorch_lightning.plugins.precision.precisionr   r   r   r   r   r   <module>   s   