a
    d
                     @   s   d dl mZmZmZ d dlmZ g 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)nn)module_to_fqnfqn_to_moduleget_arg_info_from_tensor_fqnFakeSparsity )modelmoduleprefixreturnc                 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   
isinstancestr)r   r   r   namechildZfqn r   j/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/ao/pruning/sparsifier/utils.pyr      s    
r   )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   )r   
tensor_fqnr   c                 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_namer   )r   lenr   )r   r   r   r   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 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J | j | S N)r   shape)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   	r	   N)r
   )typingr   r   r   Ztorchr   __all__Moduler   r   r   r   r	   r   r   r   r   <module>   s   