U
    Mh-
                     @   s   d dl Z d dlmZ d dlmZmZmZ dgZdddZedd	Z	ed
dZ
eddZeddZdd Zee ee ee dddZeeef eddddZdS )    N)repeat)ListDictAny'consume_prefix_in_state_dict_if_presentparsec                    s    fdd}||_ |S )Nc                    s$   t | tjjrt| S tt|  S N)
isinstancecollectionsabcIterabletupler   )xn H/var/www/html/venv/lib/python3.8/site-packages/torch/nn/modules/utils.pyr   
   s    z_ntuple.<locals>.parse)__name__)r   namer   r   r   r   _ntuple	   s    r      _single   _pair   _triple   
_quadruplec                    s   t  fddt| D S )zReverse the order of `t` and repeat each element for `n` times.

    This can be used to translate padding arg used by Conv and Pooling modules
    to the ones used by `F.pad`.
    c                 3   s    | ]}t  D ]
}|V  qqd S r   )range).0r   _r   r   r   	<genexpr>   s     
  z(_reverse_repeat_tuple.<locals>.<genexpr>)r   reversed)tr   r   r   r   _reverse_repeat_tuple   s    r$   )out_sizedefaultsreturnc                 C   sd   dd l }t| t|jfr| S t|t| krBtdt| d  dd t| |t|  d  D S )Nr   z#Input dimension should be at least r   c                 S   s    g | ]\}}|d k	r|n|qS r   r   )r   vdr   r   r   
<listcomp>*   s    z&_list_with_default.<locals>.<listcomp>)torchr	   intZSymIntlen
ValueErrorzip)r%   r&   r+   r   r   r   _list_with_default"   s    r0   )
state_dictprefixr'   c                 C   s   t |  }|D ],}||r|t|d }| || |< qt| drt | j }|D ]N}t|dkrlqZ||ddks||rZ|t|d }| j|| j|< qZdS )a  Strip the prefix in state_dict in place, if any.

    ..note::
        Given a `state_dict` from a DP/DDP model, a local model can load it by applying
        `consume_prefix_in_state_dict_if_present(state_dict, "module.")` before calling
        :meth:`torch.nn.Module.load_state_dict`.

    Args:
        state_dict (OrderedDict): a state-dict to be loaded to the model.
        prefix (str): prefix.
    N	_metadatar   . )listkeys
startswithr-   pophasattrr3   replace)r1   r2   r7   keyZnewkeyr   r   r   r   /   s    

)r   )r
   	itertoolsr   typingr   r   r   __all__r   r   r   r   r   r$   r,   r0   strr   r   r   r   r   <module>   s   





	
 