
    sg
                         d dl Z d dlmZ d dlmZmZmZ dgZddZ edd      Z	 edd	      Z
 ed
d      Z edd      Zd Zdee   dee   dee   fdZdeeef   deddfdZy)    N)repeat)AnyDictList'consume_prefix_in_state_dict_if_presentc                        fd}||_         |S )Nc                     t        | t        j                  j                        rt	        |       S t	        t        |             S N)
isinstancecollectionsabcIterabletupler   )xns    I/var/www/html/venv/lib/python3.12/site-packages/torch/nn/modules/utils.pyparsez_ntuple.<locals>.parse   s1    a1128OVAq\""    )__name__)r   namer   s   `  r   _ntupler   
   s    #
 ENLr      _single   _pair   _triple   
_quadruplec                 >    t        f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   B   K   | ]  }t              D ]  }|   y wr
   )range).0r   _r   s      r   	<genexpr>z(_reverse_repeat_tuple.<locals>.<genexpr>    s!     :qq:A::s   )r   reversed)tr   s    `r   _reverse_repeat_tupler(      s     :HQK:::r   out_sizedefaultsreturnc                    dd l }t        | t        |j                  f      r| S t	        |      t	        |       k  rt        dt	        |       dz          t        | |t	        |        d        D cg c]  \  }}||n| c}}S c c}}w )Nr   z#Input dimension should be at least r   )torchr   intSymIntlen
ValueErrorzip)r)   r*   r-   vds        r   _list_with_defaultr5   #   s    (S%,,/0
8}H%>s8}q?P>QRSS.1(Hc(m^EU<V.W&*aQ]!  s   0B
state_dictprefixc                    t        | j                               }|D ]6  }|j                  |      s|t        |      d }| j	                  |      | |<   8 t        | d      rt        | j                  j                               }|D ]n  }t        |      dk(  r||j                  dd      k(  s|j                  |      s9|t        |      d }| j                  j	                  |      | j                  |<   p yy)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
startswithr0   pophasattrr9   replace)r6   r7   r=   keynewkeys        r   r   r   /   s     
!"D 5>>&!V'F!+!4Jv5 z;'J((--/0 
	MC
 3x1}fnnS"--1GS[]+/9/C/C/G/G/L
$$V,
	M (r   )r   )r   	itertoolsr   typingr   r   r   __all__r   r   r   r   r   r(   r.   r5   strr    r   r   <module>rI      s      " " 5
5 !Y
7
!Y
Q%
;	c 	d3i 	DI 	"MS#X"M"M 
"Mr   