
    shW5                        S SK Jr  S SKJr  S SKrS SKJs  Jr  S SKJr   " S S\5      r	 " S S\R                  5      rS	\R                  S
\R                  S\4S jr " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S \R                  5      r " S! S"\R                  5      r " S# S$\R                  5      r " S% S&\R                  5      rg)'    )Any)BaseSparsifierN)nnc                   v   ^  \ rS rSrS\\\4   SS4U 4S jjrS\R                  S\S\\\4   SS4S jr
S	rU =r$ )
ImplementedSparsifier	   kwargsreturnNc                     > [         TU ]  US9  g )N)defaults)super__init__)selfr	   	__class__s     z/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/testing/_internal/common_pruning.pyr   ImplementedSparsifier.__init__
   s    &)    moduletensor_namec                     SUR                   R                  S   R                  S'   U R                  S   nUR	                  SS5      S-   US'   g )Nr   zlinear1.weight
step_count   )parametrizationsweightmaskstateget)r   r   r   r	   linear_states        r   update_mask!ImplementedSparsifier.update_mask   sN    45&&q)..q1zz"23%1%5%5lA%F%J\"r    )__name__
__module____qualname____firstlineno__dictstrr   r   r   Moduler   __static_attributes____classcell__r   s   @r   r   r   	   sX    *c3h *D *K")) K# KcSVh K\` K Kr   r   c                   H    \ rS rSrSr\S\R                  SS 4S j5       rSr	g)MockSparseLinear   z
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                 @    U " UR                   UR                  5      nU$ )z	
        )in_featuresout_features)clsr/   linears      r   
from_denseMockSparseLinear.from_dense   s"     S__%%'r   r!   N)
r"   r#   r$   r%   __doc__classmethodr   Linearr5   r)   r!   r   r   r-   r-      s.    
 RYY +=  r   r-   subset_tensorsuperset_tensorr
   c                     SnU  HI  nU[        U5      :  a7  [        R                  " X1U   5      (       d  US-  nOM8  U[        U5      :  a  M7    g   g)zO
Checks to see if all rows in subset tensor are present in the superset tensor
r   r   FT)lentorchequal)r:   r;   irows       r   rows_are_subsetrB   "   sY     	
A#o&&;;sA$677Q	 #o&&   r   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	SimpleLinear2   zModel 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           
      @  > [         TU ]  5         [        R                  " [        R                  " SSSS9[        R                  " SSSS9[        R                  " SSSS95      U l        [        R                  " SSSS9U l        [        R                  " SSSS9U l        g )N      Fbias      
   )r   r   r   
Sequentialr9   seqlinear1linear2r   r   s    r   r   SimpleLinear.__init__6   sx    ==IIa'IIa'IIa'

 yyAE2yyBU3r   xc                 l    U R                  U5      nU R                  U5      nU R                  U5      nU$ N)rO   rP   rQ   r   rT   s     r   forwardSimpleLinear.forward@   .    HHQKLLOLLOr   )rP   rQ   rO   r
   Nr"   r#   r$   r%   r7   r   r>   TensorrX   r)   r*   r+   s   @r   rD   rD   2   s.    V4 %,,  r   rD   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	
LinearBiasG   zModel with only Linear layers, alternating layers with biases,
wrapped in a Sequential. Used to test pruned Linear-Bias-Linear fusion.r
   c                 (  > [         TU ]  5         [        R                  " [        R                  " SSSS9[        R                  " SSSS9[        R                  " SSSS9[        R                  " SSSS9[        R                  " SSSS95      U l        g )	NrG   rH   TrI   rK   F   rM   )r   r   r   rN   r9   rO   rR   s    r   r   LinearBias.__init__K   sn    ==IIa&IIa'IIa&IIa&IIa%(
r   rT   c                 (    U R                  U5      nU$ rV   rO   rW   s     r   rX   LinearBias.forwardU   s    HHQKr   re   r[   r\   r+   s   @r   r_   r_   G   s.    O
 %,,  r   r_   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	LinearActivationZ   zModel 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U ]  5         [        R                  " [        R                  " SSSS9[        R
                  " 5       [        R                  " SSSS9[        R                  " 5       [        R                  " SSSS95      U l        [        R                  " SSSS9U l        [        R
                  " 5       U l	        [        R                  " SS	SS9U l
        [        R                  " 5       U l        g )
NrG   rH   TrI   rK   FrL   rb   rM   )r   r   r   rN   r9   ReLUTanhrO   rP   act1rQ   act2rR   s    r   r   LinearActivation.__init___   s    ==IIa&GGIIIa'GGIIIa&
 yyAD1GGI	yyBU3GGI	r   rT   c                     U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      nU$ rV   )rO   rP   rm   rQ   rn   rW   s     r   rX   LinearActivation.forwardm   H    HHQKLLOIIaLLLOIIaLr   )rm   rn   rP   rQ   rO   r[   r\   r+   s   @r   rh   rh   Z   s.    B %,,  r   rh   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	LinearActivationFunctionalv   a   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U ]  5         [        R                  " [        R                  " SSSS9[        R
                  " 5       [        R                  " SSSS9[        R
                  " 5       [        R                  " SSSS95      U l        [        R                  " SSSS9U l        [        R                  " SS	SS9U l        [        R                  " S	S
SS9U l	        [        R
                  " 5       U l
        g )NrG   rH   TrI   rK   FrL   rb      rM   )r   r   r   rN   r9   rk   rO   rP   rQ   linear3rm   rR   s    r   r   #LinearActivationFunctional.__init__|   s    ==IIa&GGIIIa'GGIIIa&
 yyAD1yyAE2yyBU3GGI	r   rT   c                    U R                  U5      nU R                  U5      n[        R                  " U5      nU R	                  U5      n[        R                  " U5      nU R                  U5      n[        R                  " U5      nU$ rV   )rO   rP   FrelurQ   rx   rW   s     r   rX   "LinearActivationFunctional.forward   sb    HHQKLLOFF1ILLOFF1ILLOFF1Ir   )rm   rP   rQ   rx   rO   r[   r\   r+   s   @r   rt   rt   v   s.    B
 %,,  r   rt   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	SimpleConv2d   zModel with only Conv2d layers, all without bias, some in a Sequential and some following.
Used to test pruned Conv2d-Conv2d fusion.r
   c                 &  > [         TU ]  5         [        R                  " [        R                  " SSSSSS9[        R                  " SSSSSS95      U l        [        R                  " SSSSSS9U l        [        R                  " SSSSSS9U l        g )	Nr       rb   FrI   @   0   4   r   r   r   rN   Conv2drO   conv2d1conv2d2rR   s    r   r   SimpleConv2d.__init__   sx    ==IIaQ.IIb"a/
 yyRAE:yyRAE:r   rT   c                 l    U R                  U5      nU R                  U5      nU R                  U5      nU$ rV   rO   r   r   rW   s     r   rX   SimpleConv2d.forward   rZ   r   r   r   rO   r[   r\   r+   s   @r   r   r      s-    1; %,,  r   r   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	
Conv2dBias   zModel with only Conv2d layers, some with bias, some in a Sequential and some outside.
Used to test pruned Conv2d-Bias-Conv2d fusion.r
   c                 T  > [         TU ]  5         [        R                  " [        R                  " SSSSSS9[        R                  " SSSSSS9[        R                  " SSSSSS95      U l        [        R                  " SSSSSS9U l        [        R                  " SS	SSSS9U l        g 
Nr   r   rb   TrI   r   Fr   r   r   rR   s    r   r   Conv2dBias.__init__   s    ==IIaQ-IIb"a.IIb"a/

 yyRAD9yyRAE:r   rT   c                 l    U R                  U5      nU R                  U5      nU R                  U5      nU$ rV   r   rW   s     r   rX   Conv2dBias.forward   rZ   r   r   r[   r\   r+   s   @r   r   r      s-    6; %,,  r   r   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	Conv2dActivation   a  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U ]  5         [        R                  " [        R                  " SSSSSS9[        R
                  " 5       [        R                  " SSSSSS9[        R                  " 5       [        R                  " SSSSSS9[        R
                  " 5       5      U l        [        R                  " SSSSSS9U l        [        R                  " SS	SSSS9U l	        g r   )
r   r   r   rN   r   rk   rl   rO   r   r   rR   s    r   r   Conv2dActivation.__init__   s    ==IIaQ-GGIIIb"a.GGIIIb"a/GGI
 yyRAE:yyRAD9r   rT   c                     U R                  U5      nU R                  U5      n[        R                  " U5      nU R	                  U5      n[        R
                  " U5      nU$ rV   )rO   r   r{   r|   r   hardtanhrW   s     r   rX   Conv2dActivation.forward   sH    HHQKLLOFF1ILLOJJqMr   r   r[   r\   r+   s   @r   r   r      s.    A
: %,,  r   r   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	Conv2dPadBias   aE  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U ]  5         [        R                  " [        R                  " SSSSSSS9[        R
                  " 5       [        R                  " SSSSSS9[        R
                  " 5       [        R                  " SSSSSSS9[        R
                  " 5       [        R                  " SSSSSSS9[        R
                  " 5       [        R                  " SSSSSS9[        R                  " 5       5
      U l        [        R                  " SS	SSSSS9U l        [        R
                  " 5       U l	        [        R                  " S	S
SSSSS9U l
        [        R                  " 5       U l        g )Nr   r   rb   T)paddingrJ   FrI   r   r   r   )r   r   r   rN   r   rk   rl   rO   r   rm   r   rn   rR   s    r   r   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   rT   c                     U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      nU$ rV   )rO   r   rm   r   rn   rW   s     r   rX   Conv2dPadBias.forward   rr   r   )rm   rn   r   r   rO   r[   r\   r+   s   @r   r   r      s.    c
& %,,  r   r   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	
Conv2dPool   zModel 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                 n  > [         TU ]  5         [        R                  " [        R                  " SSSSSS9[        R
                  " SSSS9[        R                  " 5       [        R                  " SSSSSS9[        R                  " 5       [        R                  " SSSS95      U l	        [        R                  " SS	SSSS9U l
        [        R
                  " SSSS9U l        [        R                  " 5       U l        [        R                  " S	S
SSSS9U l        [        R                  " S
S
SSSS9U l        g )Nr   r   rb   Tkernel_sizer   rJ      r   strider   r   r   r   )r   r   r   rN   r   	MaxPool2drk   rl   	AvgPool2drO   r   maxpoolaf1r   conv2d3rR   s    r   r   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   rT   c                 ,   U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      n[
        R                  " USSSS9n[
        R                  " U5      nU R                  U5      nU$ Nr   r   r   )	rO   r   r   r   r   r{   
avg_pool2dr|   r   rW   s     r   rX   Conv2dPool.forward  sv    HHQKLLOLLOHHQKLLOLL!Q?FF1ILLOr   )r   r   r   r   r   rO   r[   r\   r+   s   @r   r   r      s.    8N 	 	%,, 	 	r   r   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	Conv2dPoolFlattenFunctionali  a  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                 j  > [         TU ]  5         [        R                  " [        R                  " SSSSSS9[        R
                  " SSSS9[        R                  " 5       [        R                  " SSSSSS9[        R                  " 5       [        R                  " SSSS95      U l	        [        R                  " SSSSSS9U l
        [        R                  " 5       U l        [        R                  " SS	SSSS9U l        [        R                  " S
5      U l        [        R                  " S	SSS9U l        g )Nr   rb   Tr   r   r   rH   rG      )r   r      rI   )r   r   r   rN   r   r   rk   rl   r   rO   r   r   r   AdaptiveAvgPool2davg_poolr9   fcrR   s    r   r   $Conv2dPoolFlattenFunctional.__init__#  s    ==IIa14@LLQq!<GGIIIa14@GGILLQq!<
 yyA1adK779yyBAqtL,,V4))B.r   rT   c                 .   U R                  U5      nU R                  U5      n[        R                  " USSSS9nU R	                  U5      nU R                  U5      nU R                  U5      n[        R                  " US5      nU R                  U5      nU$ r   )
rO   r   r{   
max_pool2dr   r   r   r>   flattenr   rW   s     r   rX   #Conv2dPoolFlattenFunctional.forward3  sz    HHQKLLOLL!Q?HHQKLLOMM!MM!QGGAJr   )r   r   r   r   r   rO   r[   r\   r+   s   @r   r   r     s.    @
/ 	 	%,, 	 	r   r   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	Conv2dPoolFlatteni?  a
  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U ]  5         [        R                  " [        R                  " SSSSSS9[        R
                  " SSSS9[        R                  " 5       [        R                  " SSSSSS9[        R                  " 5       [        R                  " SSSS95      U l	        [        R                  " SSSSSS9U l
        [        R                  " 5       U l        [        R                  " SS	SSSS9U l        [        R                  " S
5      U l        [        R                  " 5       U l        [        R"                  " SSSS9U l        g )Nr   rb   Tr   r   r   rH   rG   r   )r   r   ,   r   rI   )r   r   r   rN   r   r   rk   rl   r   rO   r   r   r   r   r   Flattenr   r9   r   rR   s    r   r   Conv2dPoolFlatten.__init__E  s    ==IIa14@LLQq!<GGIIIa14@GGILLQq!<
 yyA1adK779yyBAqtL,,V4zz|))B.r   rT   c                 "   U R                  U5      nU R                  U5      n[        R                  " USSSS9nU R	                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU$ r   )	rO   r   r{   r   r   r   r   r   r   rW   s     r   rX   Conv2dPoolFlatten.forwardV  sw    HHQKLLOLL!Q?HHQKLLOMM!LLOGGAJr   )r   r   r   r   r   r   rO   r[   r\   r+   s   @r   r   r   ?  s.    @
/"	 	%,, 	 	r   r   c            
          ^  \ rS rSrSrS\S\S\S\SS4
U 4S	 jjrS
\R                  S\	\R                  \R                  4   4S jr
SrU =r$ )LSTMLinearModelib  zCContainer module with an encoder, a recurrent module, and a linear.	input_dim
hidden_dim
output_dim
num_layersr
   Nc                    > [         TU ]  5         [        R                  " XU5      U l        [        R
                  " X#5      U l        g rV   )r   r   r   LSTMlstmr9   r4   r   r   r   r   r   r   s        r   r   LSTMLinearModel.__init__e  s2     	GGI:>	ii
7r   inputc                 P    U R                  U5      u  p#U R                  U5      nXB4$ rV   )r   r4   )r   r   output_hiddendecodeds        r   rX   LSTMLinearModel.forwardl  s(    ))E*++f%r   )r4   r   r"   r#   r$   r%   r7   intr   r>   r]   tuplerX   r)   r*   r+   s   @r   r   r   b  sa    M88*-8;>8LO8	8U\\ eELL%,,4N.O  r   r   c            
          ^  \ rS rSrSrS\S\S\S\SS4
U 4S	 jjrS
\R                  S\	\R                  \R                  4   4S jr
SrU =r$ )LSTMLayerNormLinearModelir  z9Container module with an LSTM, a LayerNorm, and a linear.r   r   r   r   r
   Nc                    > [         TU ]  5         [        R                  " XU5      U l        [        R
                  " U5      U l        [        R                  " X#5      U l        g rV   )	r   r   r   r   r   	LayerNormnormr9   r4   r   s        r   r   !LSTMLayerNormLinearModel.__init__u  sB     	GGI:>	LL,	ii
7r   rT   c                 r    U R                  U5      u  pU R                  U5      nU R                  U5      nX4$ rV   )r   r   r4   )r   rT   r   s      r   rX    LSTMLayerNormLinearModel.forward}  s2    99Q<IIaLKKNxr   )r4   r   r   r   r+   s   @r   r   r   r  sa    C88*-8;>8LO8	8 %ell0J*K  r   r   )typingr   torch.ao.pruningr   r>   torch.nn.functionalr   
functionalr{   r   r9   r-   r]   boolrB   r(   rD   r_   rh   rt   r   r   r   r   r   r   r   r   r   r!   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   