
    Wj              
          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dS )    )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          |           r2t          fd| j                                        D                       S dS )Nc              3   T   K   | ]"\  }}t          fd |D                       V  #dS )c              3   8   K   | ]}t          |          V  d S N)
isinstance).0paramr   s     f/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/ao/pruning/sparsifier/utils.py	<genexpr>z2module_contains_param.<locals>.<genexpr>.<genexpr>   s-      KKu
5/22KKKKKK    N)any)r   key
param_listr   s      r   r   z(module_contains_param.<locals>.<genexpr>   sX       
 
Z KKKK
KKKKK
 
 
 
 
 
r   F)r   r   parametrizationsitems)r   r   s    `r   r   r      sa    v 
 
 
 
 
#)#:#@#@#B#B
 
 
 
 
 	
 5r   modmappingc                    t          |           |v r9|t          |                    }|                    |           }| j                                        D ]}|                    |           | j                                        D ]}|                    |           d t          |                                 | 	                                          D             }t          |          dk    rt          d|           t          |          dk    rt          t          |                    nd}|r|                    |           |S | S )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
    c                     h | ]	}|j         
S  )device)r   ps     r   	<setcomp>zswap_module.<locals>.<setcomp>7   s    LLL18LLLr      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lenAssertionErrornextiterto)r    r!   
sparse_modnew_modpre_hook_fnhook_fndevicesr%   s           r   r	   r	      s^    $C((G339#>>?
 '',, 188:: 	; 	;K--k:::: )0022 	3 	3G))'2222 MLU3>>+;+;S[[]]%K%KLLLw<<! kbikk   ),Gq(8(8d7mm$$$d 	JJv 
r    modelprefixNc                     || u rdS |                                  D ]5\  }}t          ||d          }t          |t                    r
||z   |z   c S 6dS )zS
    Returns the fqn for a module or None if module not a descendent of model.
    r;   .N)named_childrenr
   r   str)r<   r   r=   namechildfqns         r   r
   r
   F   ss     r++-- ' 'eE63//c3 	'D=3&&&&	'4r   pathc                 d    |dk    r)|                     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<   rE   rB   s      r   r   r   S   s>    
 rzzJJsOO 	/ 	/DE4..EELr   
tensor_fqnc                     |                     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_namerI   )rsplitr1   r   )r<   rI   rN   rM   r   s        r   r   r   ^   sr     ##C!#44R8KEs;///3*3DE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                 t    t                                                       |                     d|           d S )Nmask)super__init__register_buffer)selfrR   	__class__s     r   rT   zFakeSparsity.__init__~   s5    VT*****r   c                     | j         j        |j        k    r%t          d| j         j         d|j         d          | j         |z  S )Nzmask shape (z) must match x shape ())rR   shaper2   )rV   xs     r   forwardzFakeSparsity.forward   sO    9?ag%% PtyPPagPPP   y1}r   c                     i S r   r$   )rV   argskwargss      r   
state_dictzFakeSparsity.state_dict   s	     	r   )__name__
__module____qualname____doc__rT   r\   r`   __classcell__)rW   s   @r   r   r   s   s`         + + + + +        r   r   )r;   )	itertoolsr   typingr   torchr   torch.nn.utils.parametrizer   r   __all__Moduletypeboolr   dictr	   rA   r
   r   r   r   r$   r   r   <module>ro      s                     T T T T T T T T  ") d29o RV    &	&!$ry/4	?"BC&Y& & & &R
 
 
BI 
s 
CRVJ 
 
 
 
T)  T9I    	 s tCQTH~    *    29     r   