U
    Kºc
  ã                   @   sš   d dl mZmZmZ d dlmZ ddddg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)ÚnnÚmodule_to_fqnÚfqn_to_moduleÚget_arg_info_from_tensor_fqnÚFakeSparsityÚ )ÚmodelÚmoduleÚprefixÚreturnc                 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   Ú
isinstanceÚstr)r   r   r   ÚnameÚchildZfqn© r   úF/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/sparsity/sparsifier/utils.pyr      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
   r   N)ÚsplitÚgetattr)r   r   r   r   r   r   r      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
    r   éÿÿÿÿN)Ú
module_fqnr   Útensor_namer   )r   Úlenr   )r   r   r   r   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 parmeter 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    D   s    
zFakeSparsity.__init__c                 C   s   | j j|jkst‚| j | S ©N)r   ÚshapeÚAssertionError)r!   Úxr   r   r   ÚforwardH   s    zFakeSparsity.forwardc                 O   s   i S r$   r   )r!   ÚargsÚkwargsr   r   r   Ú
state_dictL   s    zFakeSparsity.state_dict)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r(   r+   Ú__classcell__r   r   r"   r   r	   :   s   	N)r
   )Útypingr   r   r   Ztorchr   Ú__all__ÚModuler   r   r   r   r	   r   r   r   r   Ú<module>   s   ü