
    `iW5                        d dl mZ d dlmZ d dlZd dlmc mZ d dlmZ  G d de          Z	 G d dej
                  Zd	ej        d
ej        defdZ G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d d ej                  Z G d! d"ej                  Z G d# d$ej                  Z G d% d&ej                  ZdS )'    )Any)BaseSparsifierN)nnc                   j     e Zd Zdeeef         ddf fdZdej        dedeeef         ddfdZ	 xZ
S )ImplementedSparsifierkwargsreturnNc                 L    t                                          |           d S )N)defaults)super__init__)selfr   	__class__s     z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/testing/_internal/common_pruning.pyr   zImplementedSparsifier.__init__
   s$    &)))))    moduletensor_namec                     d|j         j        d         j        d<   | j        d         }|                    dd          dz   |d<   d S )Nr   zlinear1.weight
step_count   )parametrizationsweightmaskstateget)r   r   r   r   linear_states        r   update_maskz!ImplementedSparsifier.update_mask   sL    45&q).q1z"23%1%5%5lA%F%F%J\"""r   )__name__
__module____qualname__dictstrr   r   r   Moduler   __classcell__r   s   @r   r   r   	   s        *c3h *D * * * * * *K") K# KcSVh K\` K K K K K K K Kr   r   c                   <    e Zd ZdZedej        dd f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                 2     | |j         |j                  }|S )z	
        )in_featuresout_features)clsr(   linears      r   
from_densezMockSparseLinear.from_dense   s$     S_%' 'r   N)r   r   r    __doc__classmethodr   Linearr.    r   r   r'   r'      sO         
 RY +=    [  r   r'   subset_tensorsuperset_tensorr	   c                     d}| D ]M}|t          |          k     r5t          j        |||                   s|dz  }nn|t          |          k     5 dS NdS )zW
    Checks to see if all rows in subset tensor are present in the superset tensor
    r   r   FT)lentorchequal)r3   r4   irows       r   rows_are_subsetr;   "   s|     	
A  #o&&&&;sOA$677 Q	 #o&&&& 55  4r   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )SimpleLinearzModel 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           	      l   t                                                       t          j        t          j        ddd          t          j        ddd          t          j        ddd                    | _        t          j        ddd          | _        t          j        ddd          | _        d S )N      Fbias      
   )r   r   r   
Sequentialr1   seqlinear1linear2r   r   s    r   r   zSimpleLinear.__init__6   s    =Ia'''Ia'''Ia'''
 

 yAE222yBU333r   xc                     |                      |          }|                     |          }|                     |          }|S N)rG   rH   rI   r   rK   s     r   forwardzSimpleLinear.forward@   4    HHQKKLLOOLLOOr   r	   N	r   r   r    r/   r   r7   TensorrO   r$   r%   s   @r   r=   r=   2   so        V V4 4 4 4 4 4 %,        r   r=   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )
LinearBiaszModel with only Linear layers, alternating layers with biases,
    wrapped in a Sequential. Used to test pruned Linear-Bias-Linear fusion.r	   Nc                 T   t                                                       t          j        t          j        ddd          t          j        ddd          t          j        ddd          t          j        ddd          t          j        ddd                    | _        d S )	Nr?   r@   TrA   rC   F   rE   )r   r   r   rF   r1   rG   rJ   s    r   r   zLinearBias.__init__K   s    =Ia&&&Ia'''Ia&&&Ia&&&Ia%(((
 
r   rK   c                 0    |                      |          }|S rM   )rG   rN   s     r   rO   zLinearBias.forwardU   s    HHQKKr   rQ   rR   r%   s   @r   rU   rU   G   so        O O
 
 
 
 
 
 %,        r   rU   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )LinearActivationzModel 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                    t                                                       t          j        t          j        ddd          t          j                    t          j        ddd          t          j                    t          j        ddd                    | _        t          j        ddd          | _        t          j                    | _	        t          j        dd	d          | _
        t          j                    | _        d S )
Nr?   r@   TrA   rC   FrD   rW   rE   )r   r   r   rF   r1   ReLUTanhrG   rH   act1rI   act2rJ   s    r   r   zLinearActivation.__init___   s    =Ia&&&GIIIa'''GIIIa&&&
 
 yAD111GII	yBU333GII			r   rK   c                     |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }|S rM   )rG   rH   r^   rI   r_   rN   s     r   rO   zLinearActivation.forwardm   R    HHQKKLLOOIIaLLLLOOIIaLLr   rQ   rR   r%   s   @r   rZ   rZ   Z   so        B B      %,        r   rZ   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )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                    t                                                       t          j        t          j        ddd          t          j                    t          j        ddd          t          j                    t          j        ddd                    | _        t          j        ddd          | _        t          j        dd	d          | _        t          j        d	d
d          | _	        t          j                    | _
        d S )Nr?   r@   TrA   rC   FrD   rW      rE   )r   r   r   rF   r1   r\   rG   rH   rI   linear3r^   rJ   s    r   r   z#LinearActivationFunctional.__init__|   s    =Ia&&&GIIIa'''GIIIa&&&
 
 yAD111yAE222yBU333GII			r   rK   c                 &   |                      |          }|                     |          }t          j        |          }|                     |          }t          j        |          }|                     |          }t          j        |          }|S rM   )rG   rH   FrelurI   rf   rN   s     r   rO   z"LinearActivationFunctional.forward   sj    HHQKKLLOOF1IILLOOF1IILLOOF1IIr   rQ   rR   r%   s   @r   rc   rc   v   so        B B
      %,        r   rc   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )SimpleConv2dzModel with only Conv2d layers, all without bias, some in a Sequential and some following.
    Used to test pruned Conv2d-Conv2d fusion.r	   Nc           
      P   t                                                       t          j        t          j        ddddd          t          j        ddddd                    | _        t          j        ddddd          | _        t          j        ddddd          | _        d S )	Nr       rW   FrA   @   0   4   r   r   r   rF   Conv2drG   conv2d1conv2d2rJ   s    r   r   zSimpleConv2d.__init__   s    =IaQ...Ib"a///
 
 yRAE:::yRAE:::r   rK   c                     |                      |          }|                     |          }|                     |          }|S rM   rG   rs   rt   rN   s     r   rO   zSimpleConv2d.forward   rP   r   rQ   rR   r%   s   @r   rk   rk      sm        1 1; ; ; ; ; ; %,        r   rk   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )
Conv2dBiaszModel with only Conv2d layers, some with bias, some in a Sequential and some outside.
    Used to test pruned Conv2d-Bias-Conv2d fusion.r	   Nc                    t                                                       t          j        t          j        ddddd          t          j        ddddd          t          j        ddddd                    | _        t          j        ddddd          | _        t          j        dd	ddd          | _        d S 
Nr   rm   rW   TrA   rn   Fro   rp   rq   rJ   s    r   r   zConv2dBias.__init__   s    =IaQ---Ib"a...Ib"a///
 

 yRAD999yRAE:::r   rK   c                     |                      |          }|                     |          }|                     |          }|S rM   rv   rN   s     r   rO   zConv2dBias.forward   rP   r   rQ   rR   r%   s   @r   rx   rx      sm        6 6; ; ; ; ; ; %,        r   rx   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )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                    t                                                       t          j        t          j        ddddd          t          j                    t          j        ddddd          t          j                    t          j        ddddd          t          j                              | _        t          j        ddddd          | _        t          j        dd	ddd          | _	        d S rz   )
r   r   r   rF   rr   r\   r]   rG   rs   rt   rJ   s    r   r   zConv2dActivation.__init__   s    =IaQ---GIIIb"a...GIIIb"a///GII
 
 yRAE:::yRAD999r   rK   c                     |                      |          }|                     |          }t          j        |          }|                     |          }t          j        |          }|S rM   )rG   rs   rh   ri   rt   hardtanhrN   s     r   rO   zConv2dActivation.forward   sN    HHQKKLLOOF1IILLOOJqMMr   rQ   rR   r%   s   @r   r}   r}      so        A A
: : : : : : %,        r   r}   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )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                    t                                                       t          j        t          j        dddddd          t          j                    t          j        ddddd          t          j                    t          j        dddddd          t          j                    t          j        dddddd          t          j                    t          j        ddddd          t          j                    
  
        | _        t          j        dd	dddd          | _        t          j                    | _	        t          j        d	d
dddd          | _
        t          j                    | _        d S )Nr   rm   rW   T)paddingrB   FrA   rn   ro   rp   )r   r   r   rF   rr   r\   r]   rG   rs   r^   rt   r_   rJ   s    r   r   zConv2dPadBias.__init__   s3   =IaQ14888GIIIb"a///GIIIb"aAD999GIIIb"aAD999GIIIb"a...GII
 
 yRAqtDDDGII	yRAqtDDDGII			r   rK   c                     |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }|S rM   )rG   rs   r^   rt   r_   rN   s     r   rO   zConv2dPadBias.forward   ra   r   rQ   rR   r%   s   @r   r   r      so        c c
     & %,        r   r   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )
Conv2dPoolzModel 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                    t                                                       t          j        t          j        ddddd          t          j        ddd          t          j                    t          j        ddddd          t          j                    t          j        ddd                    | _	        t          j        dd	ddd          | _
        t          j        ddd          | _        t          j                    | _        t          j        d	d
ddd          | _        t          j        d
d
ddd          | _        d S )Nr   rm   rW   Tkernel_sizer   rB      r   strider   rn   ro   rp   )r   r   r   rF   rr   	MaxPool2dr\   r]   	AvgPool2drG   rs   maxpoolaf1rt   conv2d3rJ   s    r   r   zConv2dPool.__init__  s   =IaADAAALQq!<<<GIIIb"!QTBBBGIILQq!<<<
 
 yRQMMM|!QGGG799yRQMMMyRQMMMr   rK   c                 Z   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }t          j        |ddd          }t          j        |          }|                     |          }|S Nr   r   r   )	rG   rs   r   r   rt   rh   
avg_pool2dri   r   rN   s     r   rO   zConv2dPool.forward  s    HHQKKLLOOLLOOHHQKKLLOOL!Q???F1IILLOOr   rQ   rR   r%   s   @r   r   r      ss        8 8N N N N N N 	 	%, 	 	 	 	 	 	 	 	r   r   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )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                    t                                                       t          j        t          j        ddddd          t          j        ddd          t          j                    t          j        ddddd          t          j                    t          j        ddd                    | _	        t          j        ddddd          | _
        t          j                    | _        t          j        dd	ddd          | _        t          j        d
          | _        t          j        d	dd          | _        d S )Nr   rW   Tr   r   r   r@   r?      )r   r      rA   )r   r   r   rF   rr   r   r\   r]   r   rG   rs   r   rt   AdaptiveAvgPool2davg_poolr1   fcrJ   s    r   r   z$Conv2dPoolFlattenFunctional.__init__#  s   =Ia14@@@LQq!<<<GIIIa14@@@GIILQq!<<<
 
 yA1adKKK799yBAqtLLL,V44)B...r   rK   c                 \   |                      |          }|                     |          }t          j        |ddd          }|                     |          }|                     |          }|                     |          }t          j        |d          }| 	                    |          }|S r   )
rG   rs   rh   
max_pool2dr   rt   r   r7   flattenr   rN   s     r   rO   z#Conv2dPoolFlattenFunctional.forward3  s    HHQKKLLOOL!Q???HHQKKLLOOMM!M!QGGAJJr   rQ   rR   r%   s   @r   r   r     so        @ @
/ / / / / / 	 	%, 	 	 	 	 	 	 	 	r   r   c                   H     e Zd ZdZd fdZdej        dej        fdZ xZS )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                    t                                                       t          j        t          j        ddddd          t          j        ddd          t          j                    t          j        ddddd          t          j                    t          j        ddd                    | _	        t          j        ddddd          | _
        t          j                    | _        t          j        dd	ddd          | _        t          j        d
          | _        t          j                    | _        t          j        ddd          | _        d S )Nr   rW   Tr   r   r   r@   r?   r   )r   r   ,   r   rA   )r   r   r   rF   rr   r   r\   r]   r   rG   rs   r   rt   r   r   Flattenr   r1   r   rJ   s    r   r   zConv2dPoolFlatten.__init__E  s   =Ia14@@@LQq!<<<GIIIa14@@@GIILQq!<<<
 
 yA1adKKK799yBAqtLLL,V44z||)B...r   rK   c                 \   |                      |          }|                     |          }t          j        |ddd          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|S r   )	rG   rs   rh   r   r   rt   r   r   r   rN   s     r   rO   zConv2dPoolFlatten.forwardV  s    HHQKKLLOOL!Q???HHQKKLLOOMM!LLOOGGAJJr   rQ   rR   r%   s   @r   r   r   ?  so        @ @
/ / / / / /"	 	%, 	 	 	 	 	 	 	 	r   r   c            
       v     e Zd ZdZdededededdf
 fdZd	ej        deej        ej        f         fd
Z	 xZ
S )LSTMLinearModelzCContainer module with an encoder, a recurrent module, and a linear.	input_dim
hidden_dim
output_dim
num_layersr	   Nc                     t                                                       t          j        |||          | _        t          j        ||          | _        d S rM   )r   r   r   LSTMlstmr1   r-   r   r   r   r   r   r   s        r   r   zLSTMLinearModel.__init__e  sG     	GIz:>>	i
J77r   inputc                 d    |                      |          \  }}|                     |          }||fS rM   )r   r-   )r   r   output_hiddendecodeds        r   rO   zLSTMLinearModel.forwardl  s2    ))E**++f%%r   r   r   r    r/   intr   r7   rS   tuplerO   r$   r%   s   @r   r   r   b  s        MM88*-8;>8LO8	8 8 8 8 8 8U\ eEL%,4N.O        r   r   c            
       v     e Zd ZdZdededededdf
 fdZd	ej        deej        ej        f         fd
Z	 xZ
S )LSTMLayerNormLinearModelz9Container module with an LSTM, a LayerNorm, and a linear.r   r   r   r   r	   Nc                     t                                                       t          j        |||          | _        t          j        |          | _        t          j        ||          | _        d S rM   )	r   r   r   r   r   	LayerNormnormr1   r-   r   s        r   r   z!LSTMLayerNormLinearModel.__init__u  sX     	GIz:>>	L,,	i
J77r   rK   c                     |                      |          \  }}|                     |          }|                     |          }||fS rM   )r   r   r-   )r   rK   r   s      r   rO   z LSTMLayerNormLinearModel.forward}  s<    99Q<<5IIaLLKKNN%xr   r   r%   s   @r   r   r   r  s        CC88*-8;>8LO8	8 8 8 8 8 8 %el0J*K        r   r   )typingr   torch.ao.pruningr   r7   torch.nn.functionalr   
functionalrh   r   r1   r'   rS   boolr;   r#   r=   rU   rZ   rc   rk   rx   r}   r   r   r   r   r   r   r2   r   r   <module>r      s7         + + + + + +                K K K K KN K K K    ry   5< %, SW         29   *       &    ry   8       >    29   (       *    ry   8    BI   D       B    ")   D         	      F    bi        ry     r   