o
    Zh                  
   @   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    P/var/www/auris/lib/python3.10/site-packages/torch/ao/pruning/sparsifier/utils.py	<genexpr>   s    z2module_contains_param.<locals>.<genexpr>.<genexpr>N)any)r   keyZ
param_listr   r   r   r      s
    
z(module_contains_param.<locals>.<genexpr>F)r   r   Z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   Z
from_denseZ_forward_pre_hooksvaluesZregister_forward_pre_hookZ_forward_hooksZregister_forward_hookr   
parametersbufferslennextiterto)r   r   Z
sparse_modnew_modZpre_hook_fnZhook_fnZ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.
    r*   .N)Znamed_childrenr
   r   str)r+   r   r,   namechildZ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.
    r*   r-   N)splitgetattr)r+   r1   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
    r-   N)
module_fqnr   tensor_namer4   )r2   r%   r   )r+   r4   r7   r6   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__Zregister_buffer)selfr8   	__class__r   r   r:   ~   s   
zFakeSparsity.__init__c                 C   s   | j j|jks	J | j | S r   )r8   shape)r;   xr   r   r   forward   s   
zFakeSparsity.forwardc                 O   s   i S r   r   )r;   argskwargsr   r   r   
state_dict   s   zFakeSparsity.state_dict)__name__
__module____qualname____doc__r:   r@   rC   __classcell__r   r   r<   r   r   s   s
    
r   N)r*   )	itertoolsr   typingr   r   Ztorchr   Ztorch.nn.utils.parametrizer   r   __all__Moduletypeboolr   dictr	   r.   r
   r   r   r   r   r   r   r   <module>   s4   


(
" 