o
    )iF'                  
   @   s  d dl Z d dlZd dl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mZ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 g dZeG dd dZG dd deZde defddZ!deee f dee fddZ"dedee fddZ#edej$dZ%i Z&d(dee  deede%f gede%f f fddZ'			d)dee deee  e df d eee  e df dee  fd!d"Z(de dedej$f fd#d$Z)de d%edej$fd&d'Z*dS )*    N)	dataclass)Enum)partial)	signature)
ModuleType)AnyCallableDictIterableListMappingOptionalSetTypeTypeVarUnion)nn   )load_state_dict_from_url)WeightsEnumWeights	get_modelget_model_builderget_model_weights
get_weightlist_modelsc                   @   sD   e Zd ZU dZeed< eed< eeef ed< dede	fddZ
d	S )
r   a  
    This class is used to group important attributes associated with the pre-trained weights.

    Args:
        url (str): The location where we find the weights.
        transforms (Callable): A callable that constructs the preprocessing method (or validation preset transforms)
            needed to use the model. The reason we attach a constructor method rather than an already constructed
            object is because the specific object might have memory and thus we want to delay initialization until
            needed.
        meta (Dict[str, Any]): Stores meta-data related to the weights of the model and its configuration. These can be
            informative attributes (for example the number of parameters/flops, recipe link/methods used in training
            etc), configuration parameters (for example the `num_classes`) needed to construct the model or important
            meta-data (for example the `classes` of a classification model) needed to use the model.
    url
transformsmetaotherreturnc                 C   s   t |tstS | j|jkrdS | j|jkrdS t | jtr;t |jtr;| jj|jjko:| jj|jjko:| jj	|jj	kS | j|jkS )NF)

isinstancer   NotImplementedr   r   r   r   funcargskeywords)selfr    r'   f/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torchvision/models/_api.py__eq__)   s   
zWeights.__eq__N)__name__
__module____qualname____doc__str__annotations__r   r	   r   boolr)   r'   r'   r'   r(   r      s   
 r   c                   @   sv   e Zd ZdZededefddZdededeeef fdd	Z	defd
dZ
edd Zedd Zedd ZdS )r   aM  
    This class is the parent class of all model weights. Each model building method receives an optional `weights`
    parameter with its associated pre-trained weights. It inherits from `Enum` and its values should be of type
    `Weights`.

    Args:
        value (Weights): The data class entry with the weight information.
    objr    c                 C   sX   |d ur*t |tu r| || jd d }|S t|| s*td| j d|jj d|S )N. z(Invalid Weight class provided; expected z but received )typer.   replacer*   r!   	TypeError	__class__)clsr1   r'   r'   r(   verifyN   s   
zWeightsEnum.verifyr$   kwargsc                 O   s   t | jg|R i |S N)r   r   )r&   r$   r:   r'   r'   r(   get_state_dictY   s   zWeightsEnum.get_state_dictc                 C   s   | j j d| j S )Nr2   )r7   r*   _name_r&   r'   r'   r(   __repr__\   s   zWeightsEnum.__repr__c                 C      | j jS r;   )valuer   r>   r'   r'   r(   r   _      zWeightsEnum.urlc                 C   r@   r;   )rA   r   r>   r'   r'   r(   r   c   rB   zWeightsEnum.transformsc                 C   r@   r;   )rA   r   r>   r'   r'   r(   r   g   rB   zWeightsEnum.metaN)r*   r+   r,   r-   classmethodr   r9   r   r.   r<   r?   propertyr   r   r   r'   r'   r'   r(   r   D   s    	


r   namer    c           	      C   s   z	|  d\}}W n ty   td|  dw dtjt j ddd }t|}|gdd t	|tj
D  }d}|D ]}|j|d}|durWt|trW|} nqA|du rdtd| d	|| S )
z
    Gets the weights enum value by its full name. Example: "ResNet50_Weights.IMAGENET1K_V1"

    Args:
        name (str): The name of the weight enum entry.

    Returns:
        WeightsEnum: The requested weight enum.
    r2   zInvalid weight name provided: ''.Nc                 S   s$   g | ]}|d  j dr|d  qS )   z__init__.py)__file__endswith).0xr'   r'   r(   
<listcomp>}   s    zget_weight.<locals>.<listcomp>zThe weight enum 'z0' for the specific method couldn't be retrieved.)split
ValueErrorjoinsysmodulesr*   	importlibimport_moduleinspect
getmembersismodule__dict__get
issubclassr   )	rE   Z	enum_nameZ
value_nameZbase_module_namebase_moduleZmodel_modulesweights_enummZpotential_classr'   r'   r(   r   l   s(   
 

r   c                 C   s   t | tr	t| n| }t|S )a  
    Returns the weights enum class associated to the given model.

    Args:
        name (callable or str): The model builder function or the name under which it is registered.

    Returns:
        weights_enum (WeightsEnum): The weights enum class associated with the model.
    )r!   r.   r   _get_enum_from_fn)rE   modelr'   r'   r(   r      s   
r   fnc                 C   s   t | }d|jvrtdt | jd j}d}t|tr$t|tr$|}n|jD ]}t|tr7t|tr7|} nq'|du r@td|S )z
    Internal method that gets the weight enum of a specific model builder method.

    Args:
        fn (Callable): The builder method used to create the model.
    Returns:
        WeightsEnum: The requested weight enum.
    weightsz-The method is missing the 'weights' argument.NzjThe WeightsEnum class for the specific method couldn't be retrieved. Make sure the typing info is correct.)	r   
parametersrO   
annotationr!   r4   rZ   r   __args__)r`   sigannr\   tr'   r'   r(   r^      s"   	

r^   M)bound.c                    s*   dt dtf dt dtf f fdd}|S )Nr`   .r    c                    s6    d ur n| j }|tv rtd| d| t|< | S )Nz/An entry is already registered under the name 'rF   )r*   BUILTIN_MODELSrO   )r`   keyrE   r'   r(   wrapper   s
   zregister_model.<locals>.wrapper)r   rh   )rE   rm   r'   rl   r(   register_model   s   &rn   moduleincludeexcludec                    s    fddt  D }|r(t }t|tr|g}|D ]}|tt||B }qn|}|rCt|tr4|g}|D ]}|tt|| }q6t|S )a  
    Returns a list with the names of registered models.

    Args:
        module (ModuleType, optional): The module from which we want to extract the available models.
        include (str or Iterable[str], optional): Filter(s) for including the models from the set of all models.
            Filters are passed to `fnmatch <https://docs.python.org/3/library/fnmatch.html>`__ to match Unix shell-style
            wildcards. In case of many filters, the results is the union of individual filters.
        exclude (str or Iterable[str], optional): Filter(s) applied after include_filters to remove models.
            Filter are passed to `fnmatch <https://docs.python.org/3/library/fnmatch.html>`__ to match Unix shell-style
            wildcards. In case of many filters, the results is removal of all the models that match any individual filter.

    Returns:
        models (list): A list with the names of available models.
    c                    s4   h | ]\}} d u s|j ddd  jkr|qS )Nr2   rH   r   )r+   rsplitr*   )rK   kvro   r'   r(   	<setcomp>   s    .zlist_models.<locals>.<setcomp>)rj   itemssetr!   r.   fnmatchfiltersorted)ro   rp   rq   Z
all_modelsmodelsZinclude_filterZexclude_filterr'   ru   r(   r      s"   


r   c                 C   s4   |   } zt|  }W |S  ty   td|  w )z
    Gets the model name and returns the model builder method.

    Args:
        name (str): The name under which the model is registered.

    Returns:
        fn (Callable): The model builder method.
    zUnknown model )lowerrj   KeyErrorrO   )rE   r`   r'   r'   r(   r      s   

r   configc                 K   s   t | }|di |S )a&  
    Gets the model name and configuration and returns an instantiated model.

    Args:
        name (str): The name under which the model is registered.
        **config (Any): parameters passed to the model builder method.

    Returns:
        model (nn.Module): The initialized model.
    Nr'   )r   )rE   r   r`   r'   r'   r(   r   	  s   r   r;   )NNN)+ry   rS   rU   rQ   dataclassesr   enumr   	functoolsr   r   typesr   typingr   r   r	   r
   r   r   r   r   r   r   r   Ztorchr   Z_internally_replaced_utilsr   __all__r   r   r.   r   r   r^   Modulerh   rj   rn   r   r   r   r'   r'   r'   r(   <module>   sH    4/($!2
(