a
    Ž¥»aË  ã                   @   s”   d dl Z d dlmZ d dlmZmZmZ dd„ ZedƒZedƒZ	edƒZ
e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ÚAnyc                    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© úP/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/nn/modules/utils.pyÚparse   s    z_ntuple.<locals>.parser   )r   r   r   r   r   Ú_ntuple   s    r   é   é   é   é   c                    s   t ‡ fdd„t| ƒD ƒƒS )zµReverse 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>   ó    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 u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| v 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)r+   r,   r/   ÚkeyZnewkeyÚmetadatar   r   r   Ú'consume_prefix_in_state_dict_if_present*   s    
r5   )r   Ú	itertoolsr   Útypingr   r   r   r   Z_singleZ_pairZ_tripleZ
_quadrupler   r%   r*   Ústrr5   r   r   r   r   Ú<module>   s   		þ