
    i              
          d dl mZ d dl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dz  fdZdej                  dz  dedej                  dz  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)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     [/var/www/html/engine/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                 R   t        |       |v r|t        |          }|j                  |       }| j                  j                         D ]  }|j	                  |        | j
                  j                         D ]  }|j                  |        t        | j                         | j                               D ch c]  }|j                   }}t        |      dkD  rt        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AssertionError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w<! abiajk  ),Gq(8d7m$dJJv 
 Ms   8D$modelprefixNc                     || 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)r8   r   r9   namechildfqns         r   r
   r
   F   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)r8   rB   r?   s      r   r   r   S   s5    
 rzJJsO 	/DE4.E	/Lr   
tensor_fqnc                 |    |j                  d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<   r#   )maxsplitN)
module_fqnr   tensor_namerF   )rsplitr-   r   )r8   rF   rK   rJ   r   s        r   r   r   ^   sa     ##C!#4R8K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                  k7  r0t        d| j                   j                   d|j                   d      | j                   |z  S )Nzmask shape (z) must match x shape ())rO   shaper.   )rS   xs     r   forwardzFakeSparsity.forward   sS    99??agg% tyy//EaggYaP  yy1}r   c                     i S r    )rS   argskwargss      r   
state_dictzFakeSparsity.state_dict   s	     	r   )__name__
__module____qualname____doc__rQ   rY   r^   __classcell__)rT   s   @r   r   r   s   s    +r   r   )r;   )	itertoolsr   typingr   torchr   torch.nn.utils.parametrizer   r   __all__Moduletypeboolr   dictr	   r>   r
   r   r   r   r[   r   r   <module>rm      s      T")) d299o RV &	&!$ryy/4		?"BC&YY&R
 
BII 
s 
CRVJ 
T)  T9I 		 s tCQTH~ *299 r   