Ë
    Q/æj?  ã                  óè   — d dl mZ d dlZd dlmZmZmZ d dlZg d¢Z edd¬«      Z	 edd	¬«      Z
	 d	 	 	 	 	 	 	 dd
„Z	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zy)é    )ÚannotationsN)ÚOptionalÚTupleÚTypeVar)Úfuse_conv_bn_evalÚfuse_conv_bn_weightsÚfuse_linear_bn_evalÚfuse_linear_bn_weightsÚConvTztorch.nn.modules.conv._ConvNd)ÚboundÚLinearTztorch.nn.Linearc           
     ól  — | j                   s|j                   rJ d«       ‚t        j                  | «      }|j                  �|j                  €J ‚t        |j                  |j                  |j                  |j                  |j                  |j                  |j                  |«      \  |_        |_        |S )a+  Fuse a convolutional module and a BatchNorm module into a single, new convolutional module.

    Args:
        conv (torch.nn.modules.conv._ConvNd): A convolutional module.
        bn (torch.nn.modules.batchnorm._BatchNorm): A BatchNorm module.
        transpose (bool, optional): If True, transpose the convolutional weight. Defaults to False.

    Returns:
        torch.nn.modules.conv._ConvNd: The fused convolutional module.

    .. note::
        Both ``conv`` and ``bn`` must be in eval mode, and ``bn`` must have its running buffers computed.
    úFusion only for eval!)	ÚtrainingÚcopyÚdeepcopyÚrunning_meanÚrunning_varr   ÚweightÚbiasÚeps)ÚconvÚbnÚ	transposeÚ
fused_convs       úX/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/nn/utils/fusion.pyr   r      s–   € ð$ —’ §¢ÐFÐ/FÓFÐ-Ü—‘˜tÓ$€Jà�?‰?Ð&¨2¯>©>Ð+EÐEÐEÜ)=Ø×ÑØ�‰Ø
�‰Ø
�‰Ø
�‰Ø
�	‰	Ø
�‰Øó	*Ñ&€JÔ�z”ð Ðó    c                ó°  — | j                   }|�|j                   n|}	|€t        j                  |«      }|€t        j                  |«      }|€t        j                  |«      }t        j                  ||z   «      }
|r"ddgdgt        | j                  «      dz
  z  z   }n!ddgdgt        | j                  «      dz
  z  z   }| ||
z  j                  |«      z  j                  |¬«      }||z
  |
z  |z  |z   j                  |	¬«      }t        j                  j                  || j                  «      t        j                  j                  ||j                  «      fS )aÀ  Fuse convolutional module parameters and BatchNorm module parameters into new convolutional module parameters.

    Args:
        conv_w (torch.Tensor): Convolutional weight.
        conv_b (Optional[torch.Tensor]): Convolutional bias.
        bn_rm (torch.Tensor): BatchNorm running mean.
        bn_rv (torch.Tensor): BatchNorm running variance.
        bn_eps (float): BatchNorm epsilon.
        bn_w (Optional[torch.Tensor]): BatchNorm weight.
        bn_b (Optional[torch.Tensor]): BatchNorm bias.
        transpose (bool, optional): If True, transpose the conv weight. Defaults to False.

    Returns:
        Tuple[torch.nn.Parameter, torch.nn.Parameter]: Fused convolutional weight and bias.
    é   éÿÿÿÿé   ©Údtype)r#   ÚtorchÚ
zeros_likeÚ	ones_likeÚrsqrtÚlenÚshapeÚreshapeÚtoÚnnÚ	ParameterÚrequires_grad)Úconv_wÚconv_bÚbn_rmÚbn_rvÚbn_epsÚbn_wÚbn_br   Úconv_weight_dtypeÚconv_bias_dtypeÚbn_var_rsqrtr)   Úfused_conv_wÚfused_conv_bs                 r   r   r   8   sS  € ð2 Ÿ™ÐØ&,Ð&8�f—l’lÐ>O€OØ€~Ü×!Ñ! %Ó(ˆØ€|Ü�‰˜uÓ%ˆØ€|Ü×Ñ Ó&ˆÜ—;‘;˜u v™~Ó.€LáØ�B�˜1˜#¤ V§\¡\Ó!2°QÑ!6Ñ7Ñ7‰à�Q�˜1˜#¤ V§\¡\Ó!2°QÑ!6Ñ7Ñ7ˆà˜d \Ñ1×:Ñ:¸5ÓAÑA×EÑEØð Fó €Lð ˜e‘^ |Ñ3°dÑ:¸TÑA×EÑEØð Fó €Lô
 	�‰×Ñ˜<¨×)=Ñ)=Ó>Ü�‰×Ñ˜<¨×)=Ñ)=Ó>ðð r   c           	     óÊ  — | j                   s|j                   rJ d«       ‚t        j                  | «      }	 | j                  |j                  k(  s|j                  dk(  sJ d«       ‚|j
                  �|j                  €J ‚t        |j                  |j                  |j
                  |j                  |j                  |j                  |j                  «      \  |_        |_	        |S )a“  Fuse a linear module and a BatchNorm module into a single, new linear module.

    Args:
        linear (torch.nn.Linear): A Linear module.
        bn (torch.nn.modules.batchnorm._BatchNorm): A BatchNorm module.

    Returns:
        torch.nn.Linear: The fused linear module.

    .. note::
        Both ``linear`` and ``bn`` must be in eval mode, and ``bn`` must have its running buffers computed.
    r   r   zGTo fuse, linear.out_features == bn.num_features or bn.num_features == 1)r   r   r   Úout_featuresÚnum_featuresr   r   r
   r   r   r   )Úlinearr   Úfused_linears      r   r	   r	   m   sÎ   € ð  —’ 2§;¢;ÐHÐ1HÓHÐ/Ü—=‘= Ó(€Lð	ð 	×Ñ˜rŸ™Ò.°"·/±/ÀQÒ2FðQàPóQØFð �?‰?Ð&¨2¯>©>Ð+EÐEÐEÜ-CØ×ÑØ×ÑØ
�‰Ø
�‰Ø
�‰Ø
�	‰	Ø
�‰ó.Ñ*€LÔ˜Ô*ð Ðr   c                óÄ  — | j                   }|�|j                   n|}|€t        j                  |«      }|t        j                  ||z   «      z  }	| |	j	                  d«      j                  |¬«      z  }
||z
  |	z  |z   j                  |¬«      }t        j                  j                  |
| j                  «      t        j                  j                  ||j                  «      fS )a2  Fuse linear module parameters and BatchNorm module parameters into new linear module parameters.

    Args:
        linear_w (torch.Tensor): Linear weight.
        linear_b (Optional[torch.Tensor]): Linear bias.
        bn_rm (torch.Tensor): BatchNorm running mean.
        bn_rv (torch.Tensor): BatchNorm running variance.
        bn_eps (float): BatchNorm epsilon.
        bn_w (torch.Tensor): BatchNorm weight.
        bn_b (torch.Tensor): BatchNorm bias.

    Returns:
        Tuple[torch.nn.Parameter, torch.nn.Parameter]: Fused linear weight and bias.
    r    r"   )	r#   r$   r%   r'   Ú	unsqueezer+   r,   r-   r.   )Úlinear_wÚlinear_br1   r2   r3   r4   r5   Úlinear_weight_dtypeÚlinear_bias_dtypeÚbn_scaleÚfused_wÚfused_bs               r   r
   r
   œ   sÕ   € ð. #Ÿ.™.ÐØ*2Ð*>˜ŸšÐDWÐØÐÜ×#Ñ# EÓ*ˆØ”e—k‘k %¨&¡.Ó1Ñ1€Hà˜×+Ñ+¨BÓ/×2Ñ2Ð9LÐ2ÓMÑM€GØ˜5Ñ  HÑ,¨tÑ3×7Ñ7Ð>OÐ7ÓP€Gä�8‰8×Ñ˜g x×'=Ñ'=Ó>ÄÇÁ×@RÑ@RØ�×'Ñ'óAð ð r   )F)r   r   r   ú%torch.nn.modules.batchnorm._BatchNormr   ÚboolÚreturnr   )r/   útorch.Tensorr0   úOptional[torch.Tensor]r1   rL   r2   rL   r3   Úfloatr4   rM   r5   rM   r   rJ   rK   ú-Tuple[torch.nn.Parameter, torch.nn.Parameter])r>   r   r   rI   rK   r   )rB   rL   rC   rM   r1   rL   r2   rL   r3   rN   r4   rL   r5   rL   rK   rO   )Ú
__future__r   r   Útypingr   r   r   r$   Ú__all__r   r   r   r   r	   r
   © r   r   ú<module>rT      s8  ðÝ "ã ß +Ñ +ã ò€ñ 	�Ð>Ô?€Ù
�)Ð#4Ô
5€ð ð!Ø
ð!à-ð!ð ð!ð ó	!ðX ð2Øð2à"ð2ð ð2ð ð	2ð
 ð2ð !ð2ð !ð2ð ð2ð 3ó2ðj,Øð,à-ð,ð ó,ð^"Øð"à$ð"ð ð"ð ð	"ð
 ð"ð ð"ð ð"ð 3ô"r   