o
    wZŽhW5  ã                   @   sh  d dl mZ d dlmZ d dlZd dlm  mZ d dlmZ G dd„ deƒZ	G dd„ dej
ƒZd	ejd
ejdefdd„ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG d d!„ d!ejƒZG d"d#„ d#ejƒZG d$d%„ d%ejƒZG d&d'„ d'ejƒZdS )(é    )ÚAny)ÚBaseSparsifierN)Únnc                       sR   e Zd Zdeeef ddf‡ fdd„Zdejdedeeef ddfdd	„Z	‡  Z
S )
ÚImplementedSparsifierÚkwargsÚreturnNc                    s   t ƒ j|d d S )N)Údefaults)ÚsuperÚ__init__)Úselfr   ©Ú	__class__© úU/var/www/auris/lib/python3.10/site-packages/torch/testing/_internal/common_pruning.pyr
   
   s   zImplementedSparsifier.__init__ÚmoduleÚtensor_namec                 K   s4   d|j jd jd< | jd }| dd¡d |d< d S )Nr   zlinear1.weightZ
step_counté   )ZparametrizationsÚweightÚmaskÚstateÚget)r   r   r   r   Zlinear_stater   r   r   Úupdate_mask   s   
z!ImplementedSparsifier.update_mask)Ú__name__Ú
__module__Ú__qualname__ÚdictÚstrr   r
   r   ÚModuler   Ú__classcell__r   r   r   r   r   	   s    ,r   c                   @   s(   e Zd ZdZedejdd fdd„ƒZdS )ÚMockSparseLinearzË
    This class is a MockSparseLinear class to check convert functionality.
    It is the same as a normal Linear layer, except with a different type, as
    well as an additional from_dense method.
    Úmodr   c                 C   s   | |j |jƒ}|S )z	
        )Zin_featuresZout_features)Úclsr    Úlinearr   r   r   Ú
from_dense   s   ÿzMockSparseLinear.from_denseN)r   r   r   Ú__doc__Úclassmethodr   ÚLinearr#   r   r   r   r   r      s    r   Úsubset_tensorÚsuperset_tensorr   c                 C   sL   d}| D ]}|t |ƒk r t ||| ¡s|d7 }nn	|t |ƒk s dS qdS )zW
    Checks to see if all rows in subset tensor are present in the superset tensor
    r   r   FT)ÚlenÚtorchÚequal)r'   r(   ÚiÚrowr   r   r   Úrows_are_subset"   s   
üþr.   c                       ó8   e Zd ZdZd	‡ fdd„Zdejdejfdd„Z‡  ZS )
ÚSimpleLinearzžModel with only Linear layers without biases, some wrapped in a Sequential,
    some following the Sequential. Used to test basic pruned Linear-Linear fusion.r   Nc              	      sf   t ƒ  ¡  t tjddddtjddddtjdddd¡| _tjdddd| _tjdddd| _d S )Né   é   F©Úbiasé   é   é
   )r	   r
   r   Ú
Sequentialr&   ÚseqÚlinear1Úlinear2©r   r   r   r   r
   6   s   
ýzSimpleLinear.__init__Úxc                 C   ó"   |   |¡}|  |¡}|  |¡}|S ©N)r9   r:   r;   ©r   r=   r   r   r   Úforward@   ó   


zSimpleLinear.forward©r   N©	r   r   r   r$   r
   r*   ÚTensorrA   r   r   r   r   r   r0   2   ó    
r0   c                       r/   )
Ú
LinearBiaszŠModel with only Linear layers, alternating layers with biases,
    wrapped in a Sequential. Used to test pruned Linear-Bias-Linear fusion.r   Nc                    s^   t ƒ  ¡  t tjddddtjddddtjddddtjddddtjdddd¡| _d S )	Nr1   r2   Tr3   r5   Fé   r7   )r	   r
   r   r8   r&   r9   r<   r   r   r   r
   K   s   

ûzLinearBias.__init__r=   c                 C   s   |   |¡}|S r?   )r9   r@   r   r   r   rA   U   s   
zLinearBias.forwardrC   rD   r   r   r   r   rG   G   rF   rG   c                       r/   )
ÚLinearActivationzùModel with only Linear layers, some with bias, some in a Sequential and some following.
    Activation functions modules in between each Linear in the Sequential, and each outside layer.
    Used to test pruned Linear(Bias)-Activation-Linear fusion.r   Nc                    s†   t ƒ  ¡  t tjddddt ¡ tjddddt ¡ tjdddd¡| _tjdddd| _t ¡ | _	tjdd	dd| _
t ¡ | _d S )
Nr1   r2   Tr3   r5   Fr6   rH   r7   )r	   r
   r   r8   r&   ÚReLUÚTanhr9   r:   Úact1r;   Úact2r<   r   r   r   r
   _   s   
û
zLinearActivation.__init__r=   c                 C   ó6   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|S r?   )r9   r:   rL   r;   rM   r@   r   r   r   rA   m   ó   




zLinearActivation.forwardrC   rD   r   r   r   r   rI   Z   s    rI   c                       r/   )
ÚLinearActivationFunctionala,  Model with only Linear layers, some with bias, some in a Sequential and some following.
    Activation functions modules in between each Linear in the Sequential, and functional
    activationals are called in between each outside layer.
    Used to test pruned Linear(Bias)-Activation-Linear fusion.r   Nc                    sŽ   t ƒ  ¡  t tjddddt ¡ tjddddt ¡ tjdddd¡| _tjdddd| _tjdd	dd| _tjd	d
dd| _	t ¡ | _
d S )Nr1   r2   Tr3   r5   Fr6   rH   é   r7   )r	   r
   r   r8   r&   rJ   r9   r:   r;   Úlinear3rL   r<   r   r   r   r
   |   s   
ûz#LinearActivationFunctional.__init__r=   c                 C   sJ   |   |¡}|  |¡}t |¡}|  |¡}t |¡}|  |¡}t |¡}|S r?   )r9   r:   ÚFÚrelur;   rR   r@   r   r   r   rA   Š   s   






z"LinearActivationFunctional.forwardrC   rD   r   r   r   r   rP   v   s    rP   c                       r/   )
ÚSimpleConv2dz‡Model with only Conv2d layers, all without bias, some in a Sequential and some following.
    Used to test pruned Conv2d-Conv2d fusion.r   Nc              
      sh   t ƒ  ¡  t tjddddddtjdddddd¡| _tjdddddd| _tjdddddd| _d S )	Nr   é    rH   Fr3   é@   é0   é4   ©r	   r
   r   r8   ÚConv2dr9   Úconv2d1Úconv2d2r<   r   r   r   r
   ™   s   
þzSimpleConv2d.__init__r=   c                 C   r>   r?   ©r9   r\   r]   r@   r   r   r   rA   ¢   rB   zSimpleConv2d.forwardrC   rD   r   r   r   r   rU   •   s    	rU   c                       r/   )
Ú
Conv2dBiaszˆModel with only Conv2d layers, some with bias, some in a Sequential and some outside.
    Used to test pruned Conv2d-Bias-Conv2d fusion.r   Nc                    sz   t ƒ  ¡  t tjddddddtjddddddtjdddddd¡| _tjdddddd| _tjdd	dddd| _d S ©
Nr   rV   rH   Tr3   rW   FrX   rY   rZ   r<   r   r   r   r
   ­   s   
ýzConv2dBias.__init__r=   c                 C   r>   r?   r^   r@   r   r   r   rA   ·   rB   zConv2dBias.forwardrC   rD   r   r   r   r   r_   ©   rF   r_   c                       r/   )
ÚConv2dActivationa  Model with only Conv2d layers, some with bias, some in a Sequential and some following.
    Activation function modules in between each Sequential layer, functional activations called
    in-between each outside layer.
    Used to test pruned Conv2d-Bias-Activation-Conv2d fusion.r   Nc                    sŒ   t ƒ  ¡  t tjddddddt ¡ tjddddddt ¡ tjddddddt ¡ ¡| _tjdddddd| _tjdd	dddd| _	d S r`   )
r	   r
   r   r8   r[   rJ   rK   r9   r\   r]   r<   r   r   r   r
   Ä   s   
úzConv2dActivation.__init__r=   c                 C   s6   |   |¡}|  |¡}t |¡}|  |¡}t |¡}|S r?   )r9   r\   rS   rT   r]   Zhardtanhr@   r   r   r   rA   Ñ   rO   zConv2dActivation.forwardrC   rD   r   r   r   r   ra   ¾   s    ra   c                       r/   )
ÚConv2dPadBiasaQ  Model with only Conv2d layers, all with bias and some with padding > 0,
    some in a Sequential and some following. Activation function modules in between each layer.
    Used to test that bias is propagated correctly in the special case of
    pruned Conv2d-Bias-(Activation)Conv2d fusion, when the second Conv2d layer has padding > 0.r   Nc                    sÚ   t ƒ  ¡  t tjdddddddt ¡ tjddddddt ¡ tjdddddddt ¡ tjdddddddt ¡ tjddddddt ¡ ¡
| _tjdd	ddddd| _t ¡ | _	tjd	d
ddddd| _
t ¡ | _d S )Nr   rV   rH   T)Úpaddingr4   Fr3   rW   rX   rY   )r	   r
   r   r8   r[   rJ   rK   r9   r\   rL   r]   rM   r<   r   r   r   r
   à   s"   
ö
zConv2dPadBias.__init__r=   c                 C   rN   r?   )r9   r\   rL   r]   rM   r@   r   r   r   rA   ó   rO   zConv2dPadBias.forwardrC   rD   r   r   r   r   rb   Ú   s    rb   c                       r/   )
Ú
Conv2dPoolzèModel with only Conv2d layers, all with bias, some in a Sequential and some following.
    Activation function modules in between each layer, Pool2d modules in between each layer.
    Used to test pruned Conv2d-Pool2d-Conv2d fusion.r   Nc                    sÂ   t ƒ  ¡  t tjddddddtjddddt ¡ tjddddddt ¡ tjdddd¡| _	tjdd	dddd| _
tjdddd| _t ¡ | _tjd	d
dddd| _tjd
d
dddd| _d S )Nr   rV   rH   T©Úkernel_sizerc   r4   é   ©rf   Zstriderc   rW   rX   rY   )r	   r
   r   r8   r[   Ú	MaxPool2drJ   rK   Ú	AvgPool2dr9   r\   ÚmaxpoolÚaf1r]   Úconv2d3r<   r   r   r   r
     s   
ú
zConv2dPool.__init__r=   c                 C   s\   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}tj|dddd}t |¡}|  |¡}|S ©Nrg   r   rh   )	r9   r\   rk   rl   r]   rS   Z
avg_pool2drT   rm   r@   r   r   r   rA     s   






zConv2dPool.forwardrC   rD   r   r   r   r   rd   ü   s    rd   c                       r/   )
ÚConv2dPoolFlattenFunctionala  Model with Conv2d layers, all with bias, some in a Sequential and some following, and then a Pool2d
    and a functional Flatten followed by a Linear layer.
    Activation functions and Pool2ds in between each layer also.
    Used to test pruned Conv2d-Pool2d-Flatten-Linear fusion.r   Nc                    s¸   t ƒ  ¡  t tjddddddtjddddt ¡ tjddddddt ¡ tjdddd¡| _	tjdddddd| _
t ¡ | _tjdd	dddd| _t d
¡| _tjd	ddd| _d S )Nr   rH   Tre   rg   rh   r2   r1   é   )r   r   é   r3   )r	   r
   r   r8   r[   ri   rJ   rK   rj   r9   r\   rl   r]   ÚAdaptiveAvgPool2dÚavg_poolr&   Úfcr<   r   r   r   r
   #  s   
ú
z$Conv2dPoolFlattenFunctional.__init__r=   c                 C   s^   |   |¡}|  |¡}tj|dddd}|  |¡}|  |¡}|  |¡}t |d¡}|  	|¡}|S rn   )
r9   r\   rS   Ú
max_pool2drl   r]   rs   r*   Úflattenrt   r@   r   r   r   rA   3  s   





z#Conv2dPoolFlattenFunctional.forwardrC   rD   r   r   r   r   ro     s    ro   c                       r/   )
ÚConv2dPoolFlattena  Model with Conv2d layers, all with bias, some in a Sequential and some following, and then a Pool2d
    and a Flatten module followed by a Linear layer.
    Activation functions and Pool2ds in between each layer also.
    Used to test pruned Conv2d-Pool2d-Flatten-Linear fusion.r   Nc                    sÂ   t ƒ  ¡  t tjddddddtjddddt ¡ tjddddddt ¡ tjdddd¡| _	tjdddddd| _
t ¡ | _tjdd	dddd| _t d
¡| _t ¡ | _tjdddd| _d S )Nr   rH   Tre   rg   rh   r2   r1   rp   )rg   rg   é,   rq   r3   )r	   r
   r   r8   r[   ri   rJ   rK   rj   r9   r\   rl   r]   rr   rs   ZFlattenrv   r&   rt   r<   r   r   r   r
   E  s   
ú

zConv2dPoolFlatten.__init__r=   c                 C   s\   |   |¡}|  |¡}tj|dddd}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|S rn   )	r9   r\   rS   ru   rl   r]   rs   rv   rt   r@   r   r   r   rA   V  s   






zConv2dPoolFlatten.forwardrC   rD   r   r   r   r   rw   ?  s    rw   c                
       óV   e Zd ZdZdededededdf
‡ fdd	„Zd
ejdeejejf fdd„Z	‡  Z
S )ÚLSTMLinearModelzCContainer module with an encoder, a recurrent module, and a linear.Ú	input_dimÚ
hidden_dimÚ
output_dimÚ
num_layersr   Nc                    s,   t ƒ  ¡  t |||¡| _t ||¡| _d S r?   )r	   r
   r   ÚLSTMÚlstmr&   r"   ©r   r{   r|   r}   r~   r   r   r   r
   e  s   
zLSTMLinearModel.__init__Úinputc                 C   s    |   |¡\}}|  |¡}||fS r?   )r€   r"   )r   r‚   ÚoutputZ_hiddenÚdecodedr   r   r   rA   l  s   
zLSTMLinearModel.forward©r   r   r   r$   Úintr
   r*   rE   ÚtuplerA   r   r   r   r   r   rz   b  s    ÿÿÿÿþ(rz   c                
       ry   )ÚLSTMLayerNormLinearModelz9Container module with an LSTM, a LayerNorm, and a linear.r{   r|   r}   r~   r   Nc                    s8   t ƒ  ¡  t |||¡| _t |¡| _t ||¡| _d S r?   )	r	   r
   r   r   r€   Z	LayerNormÚnormr&   r"   r   r   r   r   r
   u  s   
z!LSTMLayerNormLinearModel.__init__r=   c                 C   s*   |   |¡\}}|  |¡}|  |¡}||fS r?   )r€   r‰   r"   )r   r=   r   r   r   r   rA   }  s   

z LSTMLayerNormLinearModel.forwardr…   r   r   r   r   rˆ   r  s    ÿÿÿÿþ(rˆ   )Útypingr   Ztorch.ao.pruningr   r*   Ztorch.nn.functionalr   Z
functionalrS   r   r&   r   rE   Úboolr.   r   r0   rG   rI   rP   rU   r_   ra   rb   rd   ro   rw   rz   rˆ   r   r   r   r   Ú<module>   s*   
"!"#