o
    8wi                     @   s   d dl mZmZ d dlmZ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Zd dlmZmZ d d	lmZ d d
lmZ d dlmZ G dd deZG dd deejZdS )    )	GeneratorIterable)AbstractContextManagercontextmanager)AnyLiteralN)apply_to_collection)Tensor)override)_convert_fp_tensor_DtypeContextManager)_DeviceDtypeModuleMixin)	Precision)rank_zero_deprecationc                   @   s   e Zd ZU dZdZed ed< edej	dej	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 )DoublePrecisionz>Plugin for training with double (``torch.float64``) precision.z64-true	precisionmodulereturnc                 C   s   |  S N)double)selfr    r   g/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pytorch_lightning/plugins/precision/double.pyconvert_module$      zDoublePrecision.convert_modulec                 C   s
   t tjS r   )r   torchfloat64r   r   r   r   tensor_init_context(   s   
z#DoublePrecision.tensor_init_contextc                 C   s   |   S r   r   r   r   r   r   module_init_context,   r   z#DoublePrecision.module_init_context)NNNc                 c   s6    |    dV  W d   dS 1 sw   Y  dS )zlA context manager to change the default tensor type.

        See: :func:`torch.set_default_dtype`

        Nr   r   r   r   r   forward_context0   s   
"zDoublePrecision.forward_contextdatac                 C   s   t |tttjdS )N)functiondtypedst_type)r   r   r	   r   r   )r   r"   r   r   r   convert_input;      zDoublePrecision.convert_inputN)__name__
__module____qualname____doc__r   r   __annotations__r
   nnModuler   r   r   r    r   r   r!   r   r&   r   r   r   r   r      s   
 	r   c                       s   e Zd ZU dZee ed< d fddZed	e	de	fd
dZ
de	de	de	fddZde	de	de	fddZde	de	de	fddZde	de	de	fddZede	de	de	fddZ  ZS )LightningDoublePrecisionModulea'  LightningModule wrapper which converts incoming floating point data in ``*_step`` and ``forward`` to double
    (``torch.float64``) precision.

    .. deprecated:: Use :meth:`~pytorch_lightning.core.hooks.ModelHooks.configure_model` instead.

    Args:
        pl_module: the model to wrap

    !_ddp_params_and_buffers_to_ignore	pl_modulepl.LightningModuler   Nc                    sF   t    tdt| j d || _t|dg }dd |D | _d S )NzThe `z` is deprecated and no longer needed. Convert the inputs to the `*_step` methods directly using `trainer.precision_plugin.convert_input(...)`.r0   c                 S   s   g | ]}d | qS )zmodule.r   ).0pr   r   r   
<listcomp>W   s    z;LightningDoublePrecisionModule.__init__.<locals>.<listcomp>)super__init__r   typer(   r   getattrr0   )r   r1   r0   	__class__r   r   r7   M   s   
z'LightningDoublePrecisionModule.__init__
collectionc                 C   s   t | tttjdS )N)r#   r%   )r   r	   r   r   r   )r<   r   r   r   _move_float_tensors_to_doubleY   r'   z<LightningDoublePrecisionModule._move_float_tensors_to_doubleargskwargsc                 O      | j jt|i t|S r   )r   training_stepr/   r=   r   r>   r?   r   r   r   rA   ]   
   z,LightningDoublePrecisionModule.training_stepc                 O   r@   r   )r   validation_stepr/   r=   rB   r   r   r   rD   c   rC   z.LightningDoublePrecisionModule.validation_stepc                 O   r@   r   )r   	test_stepr/   r=   rB   r   r   r   rE   i   rC   z(LightningDoublePrecisionModule.test_stepc                 O   r@   r   )r   predict_stepr/   r=   rB   r   r   r   rF   o   rC   z+LightningDoublePrecisionModule.predict_stepc                 O   s   | j t|i t|S r   )r   r/   r=   rB   r   r   r   forwardu   s
   z&LightningDoublePrecisionModule.forward)r1   r2   r   N)r(   r)   r*   r+   r   strr,   r7   staticmethodr   r=   rA   rD   rE   rF   r
   rG   __classcell__r   r   r:   r   r/   @   s   
 
 r/   )collections.abcr   r   
contextlibr   r   typingr   r   r   torch.nnr-   #lightning_utilities.core.apply_funcr   r	   typing_extensionsr
   pytorch_lightningpl(lightning_fabric.plugins.precision.utilsr   r   -lightning_fabric.utilities.device_dtype_mixinr   -pytorch_lightning.plugins.precision.precisionr   %pytorch_lightning.utilities.rank_zeror   r   r.   r/   r   r   r   r   <module>   s   !