o
    oi                     @   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 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 )    )contextmanager)AnyContextManager	Generator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   ^/home/ubuntu/.local/lib/python3.10/site-packages/lightning/pytorch/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_context/   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dZ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:`~lightning.pytorch.core.hooks.ModelHooks.configure_model` instead.

    Args:
        pl_module: the model to wrap

    	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(...)`.!_ddp_params_and_buffers_to_ignorec                 S   s   g | ]}d | qS )zmodule.r   ).0pr   r   r   
<listcomp>T   s    z;LightningDoublePrecisionModule.__init__.<locals>.<listcomp>)super__init__r   typer'   r   getattrr1   )r   r/   r1   	__class__r   r   r6   J   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_doubleV   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   r@   Z   
   z,LightningDoublePrecisionModule.training_stepc                 O   r?   r   )r   validation_stepr.   r<   rA   r   r   r   rC   `   rB   z.LightningDoublePrecisionModule.validation_stepc                 O   r?   r   )r   	test_stepr.   r<   rA   r   r   r   rD   f   rB   z(LightningDoublePrecisionModule.test_stepc                 O   r?   r   )r   predict_stepr.   r<   rA   r   r   r   rE   l   rB   z+LightningDoublePrecisionModule.predict_stepc                 O   s   | j t|i t|S r   )r   r.   r<   rA   r   r   r   forwardr   s
   z&LightningDoublePrecisionModule.forward)r/   r0   r   N)r'   r(   r)   r*   r6   staticmethodr   r<   r@   rC   rD   rE   r	   rF   __classcell__r   r   r9   r   r.   ?   s    
 r.   )
contextlibr   typingr   r   r   r   r   torch.nnr,   #lightning_utilities.core.apply_funcr   r   typing_extensionsr	   lightning.pytorchpytorchpl(lightning.fabric.plugins.precision.utilsr
   r   -lightning.fabric.utilities.device_dtype_mixinr   -lightning.pytorch.plugins.precision.precisionr   %lightning.pytorch.utilities.rank_zeror   r   r-   r.   r   r   r   r   <module>   s   !