
    sg              
          d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
mZ g dZdej                  deej                     defd	Zd
ej                  deeej                     eej                     f   dej                  fdZ	 ddej                  dej                  dedee   fdZdeej                     dedeej                     fdZdej                  dedeeef   fdZ G d dej                        Zy)    )chain)AnyDictOptionalType)nn)is_parametrizedtype_before_parametrizations)module_contains_paramswap_modulemodule_to_fqnfqn_to_moduleget_arg_info_from_tensor_fqnFakeSparsitymoduleparametrizationreturnc                 t    t        |       r,t        fd| j                  j                         D              S y)Nc              3   L   K   | ]  \  }}t        fd |D                yw)c              3   6   K   | ]  }t        |        y wN)
isinstance).0paramr   s     T/var/www/html/venv/lib/python3.12/site-packages/torch/ao/pruning/sparsifier/utils.py	<genexpr>z2module_contains_param.<locals>.<genexpr>.<genexpr>   s     Ku
5/2Ks   N)any)r   key
param_listr   s      r   r   z(module_contains_param.<locals>.<genexpr>   s(      
Z K
KK
s   !$F)r	   r   parametrizationsitems)r   r   s    `r   r   r      s9    v 
#)#:#:#@#@#B
 
 	
     modmappingc                 J   t        |       |v r|t        |          }|j                  |       }| j                  j                         D ]  }|j	                  |        | j
                  j                         D ]  }|j                  |        t        | j                         | j                               D ch c]  }|j                   }}t        |      dk  s
J d|        t        |      dkD  rt        t        |            nd}|r|j                  |       |S | S c c}w )a%  Swaps the module using from_dense according to the mapping passed in.
    Args:
        mod: input module
        mapping: a dictionary that maps from nn module to sparse nn module
    Return:
        The corresponding sparse module of `mod` according to mapping, created using from_dense
       zOswap_module only works with cpu or single-device CUDA modules, but got devices r   N)r
   
from_dense_forward_pre_hooksvaluesregister_forward_pre_hook_forward_hooksregister_forward_hookr   
parametersbuffersdevicelennextiterto)	r#   r$   
sparse_modnew_modpre_hook_fnhook_fnpdevicesr/   s	            r   r   r      s    $C(G39#>?
 '', 1188: 	;K--k:	; ))002 	3G))'2	3 &+3>>+;S[[]%KL188LLLA	g\]d\ef	g(+Gq(8d7m$dJJv 
 Ms   8D modelprefixc                     || u ry| j                         D ],  \  }}t        ||d      }t        |t              s$||z   |z   c S  y)zS
    Returns the fqn for a module or None if module not a descendent of model.
     .N)named_childrenr   r   str)r:   r   r;   namechildfqns         r   r   r   D   sV     ++- 'eE63/c3D=3&&' r"   pathc                 V    |dk7  r#|j                  d      D ]  }t        | |d      }  | S )z
    Given an fqn, returns the corresponding module or tensor or None if the fqn given by `path`
    doesn't correspond to anything. Similar to model.get_submodule(path) but works for tensors.
    r=   r>   N)splitgetattr)r:   rD   rA   s      r   r   r   S   s5    
 rzJJsO 	/DE4.E	/Lr"   
tensor_fqnc                 x    |j                  d      d   }|dt        |       d|v z
   }t        | |      }||||dS )zX
    Uses tensor_fqn to obtain a dict containing module_fqn, module and tensor_name
    r>   N)
module_fqnr   tensor_namerH   )rF   r0   r   )r:   rH   rL   rK   r   s        r   r   r   ^   s\     ""3'+KEs;//3*3DEFJ5*-F !" 	 r"   c                   .     e Zd ZdZ fdZd Zd Z xZS )r   a;  Parametrization for the weights. Should be attached to the 'weight' or
    any other parameter that requires a mask applied to it.

    Note::

        Once the mask is passed, the variable should not change the id. The
        contents of the mask can change, but the mask reference itself should
        not.
    c                 F    t         |           | j                  d|       y )Nmask)super__init__register_buffer)selfrO   	__class__s     r   rQ   zFakeSparsity.__init__~   s    VT*r"   c                 j    | j                   j                  |j                  k(  sJ | j                   |z  S r   )rO   shape)rS   xs     r   forwardzFakeSparsity.forward   s*    yy!'')))yy1}r"   c                     i S r    )rS   argskwargss      r   
state_dictzFakeSparsity.state_dict   s	     	r"   )__name__
__module____qualname____doc__rQ   rX   r]   __classcell__)rT   s   @r   r   r   s   s    +r"   r   N)r=   )	itertoolsr   typingr   r   r   r   torchr   torch.nn.utils.parametrizer	   r
   __all__Moduleboolr   r   r@   r   r   r   r   rZ   r"   r   <module>rj      s    , ,  T")) d299o RV $	$!$ryy/4		?"BC$YY$P 8:99 ii14c]")), C HRYY<O 		 s tCQTH~ *299 r"   