o
    h                  
   @   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 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 )    )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    W/var/www/html/ai/venv/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>4   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.
    r7   .N)named_childrenr   r   str)r8   r   r9   namechildfqnr   r   r   r   B   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.
    r7   r:   N)splitgetattr)r8   r@   r=   r   r   r   r   Q   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_namerC   )rA   r.   r   )r8   rC   rF   rE   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)selfrG   	__class__r   r   rI   |   s   
zFakeSparsity.__init__c                 C   s   | j j|jks	J | j | S r   )rG   shape)rK   xr   r   r   forward   s   
zFakeSparsity.forwardc                 O   s   i S r   r   )rK   argskwargsr   r   r   
state_dict   s   zFakeSparsity.state_dict)__name__
__module____qualname____doc__rI   rP   rS   __classcell__r   r   rL   r   r   q   s
    
r   N)r7   )typingr   r   r   r   torch.nn.utils.parametrizer   r   	itertoolsr   torchr	   __all__Moduleboolr
   r   r<   r   r   r   r   r   r   r   r   <module>   s4    


(
" 