
    קg              
          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dS )    )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          |           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     ]/var/www/html/ai-engine/env/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 r4|t          |                    }|                    |           }| j                                        D ]}|                    |           | j                                        D ]}|                    |           d t          |                                 | 	                                          D             }t          |          dk    sJ 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>6   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nextiterto)r#   r$   
sparse_modnew_modpre_hook_fnhook_fndevicesr(   s           r   r   r      sX    $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LLAf]dff (+Gq(8(8d7mm$$$d 	JJv 
r    modelprefixc                     || 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   D   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=   rA   N)splitgetattr)r>   rG   rD   s      r   r   r   S   s>    
 rzzJJsOO 	/ 	/DE4..EELr   
tensor_fqnc                     |                     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
    rA   N)
module_fqnr   tensor_namerK   )rI   r4   r   )r>   rK   rO   rN   r   s        r   r   r   ^   sm     ""3''+K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                 D    | j         j        |j        k    sJ | j         |z  S r   )rR   shape)rV   xs     r   forwardzFakeSparsity.forward   s%    y!'))))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   N)r=   )	itertoolsr   typingr   r   r   r   torchr   torch.nn.utils.parametrizer	   r
   __all__Moduleboolr   r   rC   r   r   r   r   r'   r   r   <module>rl      s         , , , , , , , , , , , ,       T T T T T T T T  ") d29o RV    $	$!$ry/4	?"BC$Y$ $ $ $P 8: 9 i14c]   "), C HRY<O    	 s tCQTH~    *    29     r   