o
    Ih                  
   @   s
  d dl mZ d dl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	d
Zdejdeeej eej f dejfddZ	ddejdejdedee fddZdeej dedeej fddZdejdedeeef fddZG dd dejZdS )    )chain)AnyOptional)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                    s(   t | rt fdd| j D S dS )Nc                 3   s*    | ]\}}t  fd d|D V  qdS )c                 3   s    | ]}t | V  qd S N)
isinstance).0paramr    U/var/www/vscode/kcb/lib/python3.10/site-packages/torch/ao/pruning/sparsifier/utils.py	<genexpr>   s    z2module_contains_param.<locals>.<genexpr>.<genexpr>N)any)r   key
param_listr   r   r   r      s
    
z(module_contains_param.<locals>.<genexpr>F)r   r   parametrizationsitems)r   r   r   r   r   r      s
   r   modmappingc                 C   s   t | |v r]|t |  }|| }| j D ]}|| q| j D ]}|| q#dd t|  | 	 D }t
|dksFJ d| t
|dkrRt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                 S   s   h | ]}|j qS r   )device)r   pr   r   r   	<setcomp>6   s    zswap_module.<locals>.<setcomp>   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    r   r   r   r	      s    


r	    modelprefixc                 C   sH   || u rdS |   D ]\}}t||d}t|tr!|| |   S q
dS )zS
    Returns the fqn for a module or None if module not a descendent of model.
    r5   .N)named_childrenr
   r   str)r6   r   r7   namechildfqnr   r   r   r
   D   s   
r
   pathc                 C   s(   |dkr| dD ]}t| |d} q	| 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.
    r5   r8   N)splitgetattr)r6   r>   r;   r   r   r   r   S   s   r   
tensor_fqnc                 C   s@   | dd }|dt| d|v   }t| |}||||dS )zX
    Uses tensor_fqn to obtain a dict containing module_fqn, module and tensor_name
    r8   N)
module_fqnr   tensor_namerA   )r?   r,   r   )r6   rA   rD   rC   r   r   r   r   r   ^   s   
r   c                       s0   e Zd ZdZ fddZdd Zdd Z  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                    s   t    | d| d S )Nmask)super__init__register_buffer)selfrE   	__class__r   r   rG   ~   s   
zFakeSparsity.__init__c                 C   s   | j j|jks	J | j | S r   )rE   shape)rI   xr   r   r   forward   s   
zFakeSparsity.forwardc                 O   s   i S r   r   )rI   argskwargsr   r   r   
state_dict   s   zFakeSparsity.state_dict)__name__
__module____qualname____doc__rG   rN   rQ   __classcell__r   r   rJ   r   r   s   s
    
r   N)r5   )	itertoolsr   typingr   r   torchr   torch.nn.utils.parametrizer   r   __all__Moduletypeboolr   dictr	   r:   r
   r   r   r   r   r   r   r   <module>   s4   


(
" 