U
    d 	                     @   s   d dl Z d dlmZ d dlmZmZm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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 :/tmp/pip-unpacked-wheel-ua33x9lu/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   sV   t | tr| S t|t| kr4tdt| d dd t| |t|  d  D S )Nz%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>)r   intlen
ValueErrorformatzip)r$   r%   r   r   r   _list_with_default   s    
r/   )
state_dictprefixr&   c                 C   s   t |  }|D ],}||r|t|d }| || |< qd| kr| d }t| D ]0}t|dkrlqZ|t|d }||||< 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   )sortedkeys
startswithr+   poplist)r0   r1   r4   keyZnewkeymetadatar   r   r   'consume_prefix_in_state_dict_if_present+   s    
r:   )r   )r	   	itertoolsr   typingr   r   r   r   r   r   r   r   r#   r*   r/   strr:   r   r   r   r   <module>   s   





	
 