o
    8wi1	                     @   s   d dl mZ 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
 d dlmZ d dlmZ d d	lmZmZ G d
d deZdS )    )AbstractContextManager)AnyLiteralN)apply_to_collection)Tensor)Module)override)	Precision)_convert_fp_tensor_DtypeContextManagerc                   @   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defddZedede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   d/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/lightning_fabric/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 r   r   r   r   r   r   module_init_context1      z!HalfPrecision.module_init_contextc                 C   r    r   r!   r   r   r   r   forward_context5   r#   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_input9   s   zHalfPrecision.convert_inputc                 C   s   t |ttt dS r&   )r   r
   r   r   get_default_dtyper)   r   r   r   convert_output=   s   zHalfPrecision.convert_output)r   )__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r   r   r   r"   r$   r   r*   r,   r   r   r   r   r      s    
 r   )
contextlibr   typingr   r   r   #lightning_utilities.core.apply_funcr   r   torch.nnr   typing_extensionsr   ,lightning_fabric.plugins.precision.precisionr	   (lightning_fabric.plugins.precision.utilsr
   r   r   r   r   r   r   <module>   s   