U
    ,-e                     @   s  d dl mZmZmZmZ d dlmZmZ d dlm	Z	 d dl
mZ ddddd	d
gZejeej edddZejeeej eej f ejdddZdejejeee dddZeej eeej dddZejeeeef ddd	ZG dd
 d
ejZdS )    )AnyDictOptionalType)type_before_parametrizationsis_parametrized)chain)nn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    b/var/www/html/Darija-Ai-Train/env/lib/python3.8/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
    )modmappingr   c                 C   s   t | |kr|t |  }|| }| j D ]}|| q,| j D ]}|| qFdd t|  | 	 D }t
|dkstd| t
|dkrtt|nd}|r|| |S | S d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>4   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AssertionErrornextiterto)r   r   Z
sparse_modnew_modZpre_hook_fnZhook_fnZdevicesr    r   r   r   r      s     


 )modelr   prefixr   c                 C   sH   || k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   B   s    
)r.   pathr   c                 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-   r0   N)splitgetattr)r.   r4   r2   r   r   r   r   Q   s    )r.   
tensor_fqnr   c                 C   s@   | dd }|dt| d|k  }t| |}||||dS )zX
    Uses tensor_fqn to obtain a dict containing module_fqn, module and tensor_name
    r0   N)
module_fqnr   tensor_namer7   )r5   r'   r   )r.   r7   r:   r9   r   r   r   r   r   \   s    
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)selfr;   	__class__r   r   r=   |   s    
zFakeSparsity.__init__c                 C   s   | j j|jkst| j | S r   )r;   shaper(   )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=   rC   rF   __classcell__r   r   r?   r   r   q   s   
N)r-   )typingr   r   r   r   Ztorch.nn.utils.parametrizer   r   	itertoolsr   Ztorchr	   __all__Moduleboolr
   r   r1   r   r   r   r   r   r   r   r   <module>   s2   
 (   