a
    dW	                     @   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 _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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>       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    
r1   )
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)r2   r3   r6   keyZnewkeymetadatar   r   r   r   -   s    
)r   )r
   	itertoolsr   typingr   r   r   __all__r   r   r   r   r   r%   r,   r1   strr   r   r   r   r   <module>   s   





	