
    sg@5                        d dl mZmZ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)'    )DictAnyTuple)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     Y/var/www/html/venv/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__r   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)r4   r5   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   
Sequentialr2   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)rH   rI   rJ   r   rL   s     r   forwardzSimpleLinear.forward@   .    HHQKLLOLLOr   r   N	r    r!   r"   r0   r   r8   TensorrP   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@   rA   TrB   rD   F   rF   )r   r   r   rG   r2   rH   rK   s    r   r   zLinearBias.__init__K   sn    ==IIa&IIa'IIa&IIa&IIa%(
r   rL   c                 (    | j                  |      }|S rN   )rH   rO   s     r   rP   zLinearBias.forwardU   s    HHQKr   rR   rS   r&   s   @r   rV   rV   G   s)    O
 %,, r   rV   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@   rA   TrB   rD   FrE   rX   rF   )r   r   r   rG   r2   ReLUTanhrH   rI   act1rJ   act2rK   s    r   r   zLinearActivation.__init___   s    ==IIa&GGIIIa'GGIIIa&
 yyAD1GGI	yyBU3GGI	r   rL   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S rN   )rH   rI   r_   rJ   r`   rO   s     r   rP   zLinearActivation.forwardm   H    HHQKLLOIIaLLLOIIaLr   rR   rS   r&   s   @r   r[   r[   Z   s)    B %,, 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 )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@   rA   TrB   rD   FrE   rX      rF   )r   r   r   rG   r2   r]   rH   rI   rJ   linear3r_   rK   s    r   r   z#LinearActivationFunctional.__init__|   s    ==IIa&GGIIIa'GGIIIa&
 yyAD1yyAE2yyBU3GGI	r   rL   c                    | j                  |      }| j                  |      }t        j                  |      }| j	                  |      }t        j                  |      }| j                  |      }t        j                  |      }|S rN   )rH   rI   FrelurJ   rg   rO   s     r   rP   z"LinearActivationFunctional.forward   sb    HHQKLLOFF1ILLOFF1ILLOFF1Ir   rR   rS   r&   s   @r   rd   rd   v   s)    B
 %,, r   rd   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       rX   FrB   @   0   4   r   r   r   rG   Conv2drH   conv2d1conv2d2rK   s    r   r   zSimpleConv2d.__init__   sx    ==IIaQ.IIb"a/
 yyRAE:yyRAE:r   rL   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rN   rH   rt   ru   rO   s     r   rP   zSimpleConv2d.forward   rQ   r   rR   rS   r&   s   @r   rl   rl      s(    1; %,, r   rl   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   rn   rX   TrB   ro   Frp   rq   rr   rK   s    r   r   zConv2dBias.__init__   s    ==IIaQ-IIb"a.IIb"a/

 yyRAD9yyRAE:r   rL   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rN   rw   rO   s     r   rP   zConv2dBias.forward   rQ   r   rR   rS   r&   s   @r   ry   ry      s(    6; %,, r   ry   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 r{   )
r   r   r   rG   rs   r]   r^   rH   rt   ru   rK   s    r   r   zConv2dActivation.__init__   s    ==IIaQ-GGIIIb"a.GGIIIb"a/GGI
 yyRAE:yyRAD9r   rL   c                     | j                  |      }| j                  |      }t        j                  |      }| j	                  |      }t        j
                  |      }|S rN   )rH   rt   ri   rj   ru   hardtanhrO   s     r   rP   zConv2dActivation.forward   sH    HHQKLLOFF1ILLOJJqMr   rR   rS   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   rn   rX   T)paddingrC   FrB   ro   rp   rq   )r   r   r   rG   rs   r]   r^   rH   rt   r_   ru   r`   rK   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   rL   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }|S rN   )rH   rt   r_   ru   r`   rO   s     r   rP   zConv2dPadBias.forward   rb   r   rR   rS   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   rn   rX   Tkernel_sizer   rC      r   strider   ro   rp   rq   )r   r   r   rG   rs   	MaxPool2dr]   r^   	AvgPool2drH   rt   maxpoolaf1ru   conv2d3rK   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   rL   c                 .   | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }t        j                  |ddd      }t        j                  |      }| j                  |      }|S Nr   r   r   )	rH   rt   r   r   ru   ri   
avg_pool2drj   r   rO   s     r   rP   zConv2dPool.forward  sv    HHQKLLOLLOHHQKLLOLL!Q?FF1ILLOr   rR   rS   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   rX   Tr   r   r   rA   r@      )r   r      rB   )r   r   r   rG   rs   r   r]   r^   r   rH   rt   r   ru   AdaptiveAvgPool2davg_poolr2   fcrK   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   rL   c                 0   | j                  |      }| j                  |      }t        j                  |ddd      }| j	                  |      }| j                  |      }| j                  |      }t        j                  |d      }| j                  |      }|S r   )
rH   rt   ri   
max_pool2dr   ru   r   r8   flattenr   rO   s     r   rP   z#Conv2dPoolFlattenFunctional.forward3  sz    HHQKLLOLL!Q?HHQKLLOMM!MM!QGGAJr   rR   rS   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   rX   Tr   r   r   rA   r@   r   )r   r   ,   r   rB   )r   r   r   rG   rs   r   r]   r^   r   rH   rt   r   ru   r   r   Flattenr   r2   r   rK   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   rL   c                 &   | j                  |      }| j                  |      }t        j                  |ddd      }| j	                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }|S r   )	rH   rt   ri   r   r   ru   r   r   r   rO   s     r   rP   zConv2dPoolFlatten.forwardV  sw    HHQKLLOLL!Q?HHQKLLOMM!LLOGGAJr   rR   rS   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 rN   )r   r   r   LSTMlstmr2   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 rN   )r   r.   )r   r   outputhiddendecodeds        r   rP   zLSTMLinearModel.forwardl  s,    5)++f%r   r    r!   r"   r0   intr   r8   rT   r   rP   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 rN   )	r   r   r   r   r   	LayerNormnormr2   r.   r   s        r   r   z!LSTMLayerNormLinearModel.__init__u  sF     	GGIz:>	LL,	ii
J7r   rL   c                 v    | j                  |      \  }}| j                  |      }| j                  |      }||fS rN   )r   r   r.   )r   rL   r   s      r   rP   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   r   r   torch.ao.pruningr   r8   torch.nn.functionalr   
functionalri   r	   r2   r(   rT   boolr<   r$   r>   rV   r[   rd   rl   ry   r~   r   r   r   r   r   r   r3   r   r   <module>r      s1   $ # +    KN Kryy 5<< %,, SW  299 * &ryy 8 >299 ( *ryy 8BII D B")) D 		  Fbii  ryy r   