o
    oi                  	   @   s0  d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZmZ d dlmZ eeZddede
e de
ee  d	efd
dZdeded	ejfddZG dd dZ ded	dfddZ!edZ"edZ#edddZ$G dd de	e"e#e$f Z%ere&Z'dS e%Z'dS )    N)TYPE_CHECKINGAnyCallableDictGenericOptionalTypeTypeVar)RequirementCache)nn)Concatenate	ParamSpecmethod_nameinstanceparentreturnc                 C   s~   |d u rdS |d u r3t |tjrtj}nt |tjrtj}n	t |tjr'tj}|d u r3t| tdddlm} || ||S )NFzExpected a parentr   )is_overridden)	
isinstanceplLightningModuleLightningDataModuleCallback_check_mixed_imports
ValueError"lightning_utilities.core.overridesr   )r   r   r   _is_overridden r   ]/home/ubuntu/.local/lib/python3.10/site-packages/lightning/pytorch/utilities/model_helpers.pyr      s   r   
model_namekwargsc                 K   sZ   ddl m} |stt|ddlm} td}|r#|j| fi |S t|| di |S )Nr   )_TORCHVISION_AVAILABLE)modelsztorchvision>=0.14.0r   )	#lightning.pytorch.utilities.importsr    ModuleNotFoundErrorstrtorchvisionr!   r
   	get_modelgetattr)r   r   r    r!   torchvision_greater_equal_0_14r   r   r   get_torchvision_model1   s   r)   c                   @   sB   e Zd ZdZdddZdejddfddZdejddfd	d
ZdS )_ModuleModezjCaptures the ``nn.Module.training`` (bool) mode of every submodule, and allows it to be restored later on.r   Nc                 C   s
   i | _ d S N)mode)selfr   r   r   __init__C      
z_ModuleMode.__init__modulec                 C   s,   | j   | D ]
\}}|j| j |< q	d S r+   )r,   clearnamed_modulestrainingr-   r0   namemodr   r   r   captureF   s   
z_ModuleMode.capturec                 C   s@   |  D ]\}}|| jvrtd| d q| j| |_qd S )Nz#Restoring training mode on module 'zI' not possible, it was never captured. Is your module structure changing?)r2   r,   _logdebugr3   r4   r   r   r   restoreK   s   

z_ModuleMode.restore)r   N)	__name__
__module____qualname____doc__r.   r   Moduler7   r:   r   r   r   r   r*   @   s
    
r*   c                 C   sr   d\}}t | }|j}||rt|rn||r&t|r&||}}nd S td| dt | j d| d)N)pytorch_lightningzlightning.pytorchzYou passed a `z
` object (z) to a `z2` Trainer. Please switch to a single import style.)typer<   
startswithr;   	TypeErrorr=   )r   oldnewklassr0   r   r   r   r   V   s   r   _T_P_R_coT)	covariantc                   @   s\   e Zd ZdZdeeee ef e	f ddfddZ
dee dee deee	f fd	d
ZdS )_restricted_classmethod_implzDrop-in replacement for @classmethod, but raises an exception when the decorated method is called on an instance
    instead of a class type.methodr   Nc                 C   s
   || _ d S r+   )rL   )r-   rL   r   r   r   r.   o   r/   z%_restricted_classmethod_impl.__init__r   clsc                    s.   t jdtdtdtf fdd}|S )Nargsr   r   c                     sV   t dd t D }d ur|std j djj dj g| R i |S )Nc                 s   s$    | ]}t jd d|jv V  qdS )torchjitN)ospathjoinfilename).0	frameinfor   r   r   	<genexpr>w   s   " zH_restricted_classmethod_impl.__get__.<locals>.wrapper.<locals>.<genexpr>zThe classmethod `.zk` cannot be called on an instance. Please call it on the class type and make sure the return value is used.)anyinspectstackrC   r;   rL   )rN   r   is_scriptingrM   r   r-   r   r   wrappert   s   z5_restricted_classmethod_impl.__get__.<locals>.wrapper)	functoolswrapsrL   r   rI   )r-   r   rM   r^   r   r]   r   __get__r   s   
 
z$_restricted_classmethod_impl.__get__)r;   r<   r=   r>   r   r   r   rG   rH   rI   r.   r   ra   r   r   r   r   rK   k   s    &*rK   )NN)(r_   rZ   loggingrQ   typingr   r   r   r   r   r   r   r	    lightning_utilities.core.importsr
   rO   r   typing_extensionsr   r   lightning.pytorchpytorchr   	getLoggerr;   r8   r$   objectboolr   r?   r)   r*   r   rG   rH   rI   rK   classmethod_restricted_classmethodr   r   r   r   <module>   s*   (
(