
    VhW5                        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y)'    )Any)BaseSparsifierN)nnc                   h     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                 &    t         |   |       y )N)defaults)super__init__)selfr   	__class__s     V/home/dcms/DCMS/lib/python3.12/site-packages/torch/testing/_internal/common_pruning.pyr   zImplementedSparsifier.__init__
   s    &)    moduletensor_namec                     d|j                   j                  d   j                  d<   | j                  d   }|j	                  dd      dz   |d<   y )Nr   zlinear1.weight
step_count   )parametrizationsweightmaskstateget)r   r   r   r   linear_states        r   update_maskz!ImplementedSparsifier.update_mask   sN    45&&q)..q1zz"23%1%5%5lA%F%J\"r   )__name__
__module____qualname__dictstrr   r   r   Moduler   __classcell__r   s   @r   r   r   	   sR    *c3h *D *K")) K# KcSVh K\` Kr   r   c                   >    e Zd ZdZedej                  dd fd       Zy)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                 @     | |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'      s.    
 RYY +=  r   r'   subset_tensorsuperset_tensorr	   c                     d}| D ]@  }|t        |      k  r/t        j                  |||         s|dz  }n1|t        |      k  r/ y y)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Q	 #o&&  r   c                   \     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	   c           	      R   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      | _        y )N      Fbias      
   )r   r   r   
Sequentialr1   seqlinear1linear2r   r   s    r   r   zSimpleLinear.__init__6   sx    ==IIa'IIa'IIa'

 yyAE2yyBU3r   xc                 l    | j                  |      }| j                  |      }| j                  |      }|S N)rG   rH   rI   r   rK   s     r   forwardzSimpleLinear.forward@   .    HHQKLLOLLOr   r	   N	r   r   r    r/   r   r7   TensorrO   r$   r%   s   @r   r=   r=   2   s)    V4 %,, r   r=   c                   \     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	   c                 :   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            | _        y )	Nr?   r@   TrA   rC   F   rE   )r   r   r   rF   r1   rG   rJ   s    r   r   zLinearBias.__init__K   sn    ==IIa&IIa'IIa&IIa&IIa%(
r   rK   c                 (    | j                  |      }|S rM   )rG   rN   s     r   rO   zLinearBias.forwardU   s    HHQKr   rQ   rR   r%   s   @r   rU   rU   G   s)    O
 %,, r   rU   c                   \     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	   c                    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                         | _        y )
Nr?   r@   TrA   rC   FrD   rW   rE   )r   r   r   rF   r1   ReLUTanhrG   rH   act1rI   act2rJ   s    r   r   zLinearActivation.__init___   s    ==IIa&GGIIIa'GGIIIa&
 yyAD1GGI	yyBU3GGI	r   rK   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S rM   )rG   rH   r^   rI   r_   rN   s     r   rO   zLinearActivation.forwardm   H    HHQKLLOIIaLLLOIIaLr   rQ   rR   r%   s   @r   rZ   rZ   Z   s)    B %,, r   rZ   c                   \     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	   c                 
   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
                         | _
        y )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    ==IIa&GGIIIa'GGIIIa&
 yyAD1yyAE2yyBU3GGI	r   rK   c                    | j                  |      }| j                  |      }t        j                  |      }| j	                  |      }t        j                  |      }| j                  |      }t        j                  |      }|S rM   )rG   rH   FrelurI   rf   rN   s     r   rO   z"LinearActivationFunctional.forward   sb    HHQKLLOFF1ILLOFF1ILLOFF1Ir   rQ   rR   r%   s   @r   rc   rc   v   s)    B
 %,, r   rc   c                   \     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	   c           
      4   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      | _        y )	Nr       rW   FrA   @   0   4   r   r   r   rF   Conv2drG   conv2d1conv2d2rJ   s    r   r   zSimpleConv2d.__init__   sx    ==IIaQ.IIb"a/
 yyRAE:yyRAE:r   rK   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rM   rG   rs   rt   rN   s     r   rO   zSimpleConv2d.forward   rP   r   rQ   rR   r%   s   @r   rk   rk      s(    1; %,, r   rk   c                   \     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	   c                 f   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      | _        y 
Nr   rm   rW   TrA   rn   Fro   rp   rq   rJ   s    r   r   zConv2dBias.__init__   s    ==IIaQ-IIb"a.IIb"a/

 yyRAD9yyRAE:r   rK   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rM   rv   rN   s     r   rO   zConv2dBias.forward   rP   r   rQ   rR   r%   s   @r   rx   rx      s(    6; %,, r   rx   c                   \     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	   c                    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      | _	        y rz   )
r   r   r   rF   rr   r\   r]   rG   rs   rt   rJ   s    r   r   zConv2dActivation.__init__   s    ==IIaQ-GGIIIb"a.GGIIIb"a/GGI
 yyRAE:yyRAD9r   rK   c                     | j                  |      }| j                  |      }t        j                  |      }| j	                  |      }t        j
                  |      }|S rM   )rG   rs   rh   ri   rt   hardtanhrN   s     r   rO   zConv2dActivation.forward   sH    HHQKLLOFF1ILLOJJqMr   rQ   rR   r%   s   @r   r}   r}      s)    A
: %,, r   r}   c                   \     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	   c                    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                         | _        y )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__   s   ==IIaQ148GGIIIb"a/GGIIIb"aAD9GGIIIb"aAD9GGIIIb"a.GGI
 yyRAqtDGGI	yyRAqtDGGI	r   rK   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S rM   )rG   rs   r^   rt   r_   rN   s     r   rO   zConv2dPadBias.forward   ra   r   rQ   rR   r%   s   @r   r   r      s)    c
& %,, r   r   c                   \     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	   c                    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      | _        y )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    ==IIaADALLQq!<GGIIIb"!QTBGGILLQq!<
 yyRQM||!QG779yyRQMyyRQMr   rK   c                 .   | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }t        j                  |ddd      }t        j                  |      }| j                  |      }|S Nr   r   r   )	rG   rs   r   r   rt   rh   
avg_pool2dri   r   rN   s     r   rO   zConv2dPool.forward  sv    HHQKLLOLLOHHQKLLOLL!Q?FF1ILLOr   rQ   rR   r%   s   @r   r   r      s)    8N 	 	%,, 	r   r   c                   \     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	   c                 |   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      | _        y )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    ==IIa14@LLQq!<GGIIIa14@GGILLQq!<
 yyA1adK779yyBAqtL,,V4))B.r   rK   c                 0   | j                  |      }| j                  |      }t        j                  |ddd      }| j	                  |      }| j                  |      }| j                  |      }t        j                  |d      }| j                  |      }|S r   )
rG   rs   rh   
max_pool2dr   rt   r   r7   flattenr   rN   s     r   rO   z#Conv2dPoolFlattenFunctional.forward3  sz    HHQKLLOLL!Q?HHQKLLOMM!MM!QGGAJr   rQ   rR   r%   s   @r   r   r     s)    @
/ 	 	%,, 	r   r   c                   \     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	   c                    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      | _        y )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    ==IIa14@LLQq!<GGIIIa14@GGILLQq!<
 yyA1adK779yyBAqtL,,V4zz|))B.r   rK   c                 &   | j                  |      }| j                  |      }t        j                  |ddd      }| j	                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }|S r   )	rG   rs   rh   r   r   rt   r   r   r   rN   s     r   rO   zConv2dPoolFlatten.forwardV  sw    HHQKLLOLL!Q?HHQKLLOMM!LLOGGAJr   rQ   rR   r%   s   @r   r   r   ?  s)    @
/"	 	%,, 	r   r   c            
            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
                  ||      | _        y rM   )r   r   r   LSTMlstmr1   r-   r   r   r   r   r   r   s        r   r   zLSTMLinearModel.__init__e  s6     	GGIz:>	ii
J7r   inputc                 T    | j                  |      \  }}| j                  |      }||fS rM   )r   r-   )r   r   output_hiddendecodeds        r   rO   zLSTMLinearModel.forwardl  s,    ))E*++f%r   r   r   r    r/   intr   r7   rS   tuplerO   r$   r%   s   @r   r   r   b  s\    M88*-8;>8LO8	8U\\ eELL%,,4N.O r   r   c            
            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                  ||      | _        y rM   )	r   r   r   r   r   	LayerNormnormr1   r-   r   s        r   r   z!LSTMLayerNormLinearModel.__init__u  sF     	GGIz:>	LL,	ii
J7r   rK   c                 v    | j                  |      \  }}| j                  |      }| j                  |      }||fS rM   )r   r   r-   )r   rK   r   s      r   rO   z LSTMLayerNormLinearModel.forward}  s6    99Q<5IIaLKKN%xr   r   r%   s   @r   r   r   r  s\    C88*-8;>8LO8	8 %ell0J*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      s.    +    KN Kryy 5<< %,, SW  299 * &ryy 8 >299 ( *ryy 8BII D B")) D 		  Fbii  ryy r   