U
    ©Ê¹j²  ã                   @   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   © ú`/var/www/html/bwave/python/venv/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sŒtd|› �ƒ‚t
|ƒdkr¤t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   ú
 þ( ÿ  þ