
    Ph3                        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
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)$    )BaseSparsifierN)nnc                   $     e Zd Z fdZd Z xZS )ImplementedSparsifierc                 &    t         |   |       y )N)defaults)super__init__)selfkwargs	__class__s     qC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/testing/_internal/common_pruning.pyr
   zImplementedSparsifier.__init__	   s    &)    c                     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   moduler   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__r
   r   __classcell__r   s   @r   r   r      s    *Kr   r   c                        e Zd ZdZe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.
    c                 @     | |j                   |j                        }|S )z	
        )in_featuresout_features)clsmodlinears      r   
from_densezMockSparseLinear.from_dense   s"     S__%%'r   N)r   r   r   __doc__classmethodr(    r   r   r!   r!      s    
  r   r!   returnc                     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)subset_tensorsuperset_tensorirows       r   rows_are_subsetr5   !   sX     	
A#o&&;;sOA$67Q	 #o&&   r   c                   (     e Zd ZdZ fdZ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.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   
SequentialLinearseqlinear1linear2r   r   s    r   r
   zSimpleLinear.__init__5   sx    ==IIa'IIa'IIa'

 yyAE2yyBU3r   c                 l    | j                  |      }| j                  |      }| j                  |      }|S N)rB   rC   rD   r   xs     r   forwardzSimpleLinear.forward?   .    HHQKLLOLLOr   r   r   r   r)   r
   rJ   r   r   s   @r   r7   r7   1   s    V4r   r7   c                   (     e Zd ZdZ fdZ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.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 )	Nr9   r:   Tr;   r=   F   r?   )r	   r
   r   r@   rA   rB   rE   s    r   r
   zLinearBias.__init__J   sn    ==IIa&IIa'IIa&IIa&IIa%(
r   c                 (    | j                  |      }|S rG   )rB   rH   s     r   rJ   zLinearBias.forwardT   s    HHQKr   rL   r   s   @r   rN   rN   F   s    O
r   rN   c                   (     e Zd ZdZ fdZ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.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 )
Nr9   r:   Tr;   r=   Fr>   rP   r?   )r	   r
   r   r@   rA   ReLUTanhrB   rC   act1rD   act2rE   s    r   r
   zLinearActivation.__init__^   s    ==IIa&GGIIIa'GGIIIa&
 yyAD1GGI	yyBU3GGI	r   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S rG   )rB   rC   rW   rD   rX   rH   s     r   rJ   zLinearActivation.forwardl   H    HHQKLLOIIaLLLOIIaLr   rL   r   s   @r   rS   rS   Y   s    Br   rS   c                   (     e Zd ZdZ fdZ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.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 )Nr9   r:   Tr;   r=   Fr>   rP      r?   )r	   r
   r   r@   rA   rU   rB   rC   rD   linear3rW   rE   s    r   r
   z#LinearActivationFunctional.__init__{   s    ==IIa&GGIIIa'GGIIIa&
 yyAD1yyAE2yyBU3GGI	r   c                    | j                  |      }| j                  |      }t        j                  |      }| j	                  |      }t        j                  |      }| j                  |      }t        j                  |      }|S rG   )rB   rC   FrelurD   r_   rH   s     r   rJ   z"LinearActivationFunctional.forward   sb    HHQKLLOFF1ILLOFF1ILLOFF1Ir   rL   r   s   @r   r\   r\   u   s    B
r   r\   c                   (     e Zd ZdZ fdZ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.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       rP   Fr;   @   0   4   r	   r
   r   r@   Conv2drB   conv2d1conv2d2rE   s    r   r
   zSimpleConv2d.__init__   sx    ==IIaQ.IIb"a/
 yyRAE:yyRAE:r   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rG   rB   rl   rm   rH   s     r   rJ   zSimpleConv2d.forward   rK   r   rL   r   s   @r   rd   rd      s    1;r   rd   c                   (     e Zd ZdZ fdZ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.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   rf   rP   Tr;   rg   Frh   ri   rj   rE   s    r   r
   zConv2dBias.__init__   s    ==IIaQ-IIb"a.IIb"a/

 yyRAD9yyRAE:r   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rG   ro   rH   s     r   rJ   zConv2dBias.forward   rK   r   rL   r   s   @r   rq   rq      s    6;r   rq   c                   (     e Zd ZdZ fdZ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.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 rs   )
r	   r
   r   r@   rk   rU   rV   rB   rl   rm   rE   s    r   r
   zConv2dActivation.__init__   s    ==IIaQ-GGIIIb"a.GGIIIb"a/GGI
 yyRAE:yyRAD9r   c                     | j                  |      }| j                  |      }t        j                  |      }| j	                  |      }t        j
                  |      }|S rG   )rB   rl   ra   rb   rm   hardtanhrH   s     r   rJ   zConv2dActivation.forward   sH    HHQKLLOFF1ILLOJJqMr   rL   r   s   @r   rv   rv      s    A
:r   rv   c                   (     e Zd ZdZ fdZ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.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   rf   rP   T)paddingr<   Fr;   rg   rh   ri   )r	   r
   r   r@   rk   rU   rV   rB   rl   rW   rm   rX   rE   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   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S rG   )rB   rl   rW   rm   rX   rH   s     r   rJ   zConv2dPadBias.forward   rZ   r   rL   r   s   @r   r{   r{      s    c
&r   r{   c                   (     e Zd ZdZ fdZ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.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   rf   rP   Tkernel_sizer}   r<      r   strider}   rg   rh   ri   )r	   r
   r   r@   rk   	MaxPool2drU   rV   	AvgPool2drB   rl   maxpoolaf1rm   conv2d3rE   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   c                 .   | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }t        j                  |ddd      }t        j                  |      }| j                  |      }|S Nr   r   r   )	rB   rl   r   r   rm   ra   
avg_pool2drb   r   rH   s     r   rJ   zConv2dPool.forward  sv    HHQKLLOLLOHHQKLLOLL!Q?FF1ILLOr   rL   r   s   @r   r   r      s    8N 	r   r   c                   (     e Zd ZdZ fdZ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.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   rP   Tr   r   r   r:   r9      )r   r      r;   )r	   r
   r   r@   rk   r   rU   rV   r   rB   rl   r   rm   AdaptiveAvgPool2davg_poolrA   fcrE   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   c                 0   | j                  |      }| j                  |      }t        j                  |ddd      }| j	                  |      }| j                  |      }| j                  |      }t        j                  |d      }| j                  |      }|S r   )
rB   rl   ra   
max_pool2dr   rm   r   r/   flattenr   rH   s     r   rJ   z#Conv2dPoolFlattenFunctional.forward2  sz    HHQKLLOLL!Q?HHQKLLOMM!MM!QGGAJr   rL   r   s   @r   r   r     s    @
/ 	r   r   c                   (     e Zd ZdZ fdZ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.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   rP   Tr   r   r   r:   r9   r   )r   r   ,   r   r;   )r	   r
   r   r@   rk   r   rU   rV   r   rB   rl   r   rm   r   r   Flattenr   rA   r   rE   s    r   r
   zConv2dPoolFlatten.__init__D  s    ==IIa14@LLQq!<GGIIIa14@GGILLQq!<
 yyA1adK779yyBAqtL,,V4zz|))B.r   c                 &   | j                  |      }| j                  |      }t        j                  |ddd      }| j	                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }|S r   )	rB   rl   ra   r   r   rm   r   r   r   rH   s     r   rJ   zConv2dPoolFlatten.forwardU  sw    HHQKLLOLL!Q?HHQKLLOMM!LLOGGAJr   rL   r   s   @r   r   r   >  s    @
/"	r   r   c                   :     e Zd ZdZdedededef fdZd Z xZS )LSTMLinearModelzCContainer module with an encoder, a recurrent module, and a linear.	input_dim
hidden_dim
output_dim
num_layersc                     t         |           t        j                  |||      | _        t        j
                  ||      | _        y rG   )r	   r
   r   LSTMlstmrA   r'   r   r   r   r   r   r   s        r   r
   zLSTMLinearModel.__init__d  s6     	GGIz:>	ii
J7r   c                 T    | j                  |      \  }}| j                  |      }||fS rG   )r   r'   )r   inputoutputhiddendecodeds        r   rJ   zLSTMLinearModel.forwardk  s,    5)++f%r   r   r   r   r)   intr
   rJ   r   r   s   @r   r   r   a  s0    M88*-8;>8LO8r   r   c                   :     e Zd ZdZdedededef fdZd Z xZS )LSTMLayerNormLinearModelz9Container module with an LSTM, a LayerNorm, and a linear.r   r   r   r   c                     t         |           t        j                  |||      | _        t        j
                  |      | _        t        j                  ||      | _        y rG   )	r	   r
   r   r   r   	LayerNormnormrA   r'   r   s        r   r
   z!LSTMLayerNormLinearModel.__init__t  sF     	GGIz:>	LL,	ii
J7r   c                 v    | j                  |      \  }}| j                  |      }| j                  |      }||fS rG   )r   r   r'   )r   rI   r   s      r   rJ   z LSTMLayerNormLinearModel.forward|  s6    99Q<5IIaLKKN%xr   r   r   s   @r   r   r   q  s0    C88*-8;>8LO8r   r   )torch.ao.pruningr   r/   torch.nn.functionalr   
functionalra   r   rA   r!   boolr5   Moduler7   rN   rS   r\   rd   rq   rv   r{   r   r   r   r   r   r+   r   r   <module>r      s   ,    KN Kryy t  299 * &ryy 8 >299 ( *ryy 8BII D B")) D 		  Fbii  ryy r   