§
    Ë²Å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dS )(é    )Ú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.LinearFÚconvÚbnú%torch.nn.modules.batchnorm._BatchNormÚ	transposeÚboolÚreturnc           
     ó
  — | 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)r   r   r   Ú
fused_convs       úFC:\Projects\Historical\venv\Lib\site-packages\torch/nn/utils/fusion.pyr   r      sŽ   € ð$ ”ÐF ¤ÐFÐFÐ/FÑFÔFÐ-Ý”˜tÑ$Ô$€JàŒ?Ð&¨2¬>Ð+EÐ+EÐEÝ)=ØÔØŒØ
ŒØ
ŒØ
ŒØ
Œ	Ø
ŒØñ	*ô 	*Ñ&€JÔ�z”ð Ðó    Úconv_wútorch.TensorÚconv_búOptional[torch.Tensor]Úbn_rmÚbn_rvÚbn_epsÚfloatÚbn_wÚbn_bú-Tuple[torch.nn.Parameter, torch.nn.Parameter]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                       |¦  «        z                       |¬¦  «        }||z
  |
z  |z  |z                        |	¬¦  «        }t          j	         
                    || j        ¦  «        t          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.
    Né   éÿÿÿÿé   ©Údtype)r1   ÚtorchÚ
zeros_likeÚ	ones_likeÚrsqrtÚlenÚshapeÚreshapeÚtoÚnnÚ	ParameterÚrequires_grad)r!   r#   r%   r&   r'   r)   r*   r   Úconv_weight_dtypeÚconv_bias_dtypeÚbn_var_rsqrtr7   Úfused_conv_wÚfused_conv_bs                 r   r   r   8   se  € ð2 œÐØ&,Ð&8�f”l�lÐ>O€OØ€~ÝÔ! %Ñ(Ô(ˆØ€|ÝŒ˜uÑ%Ô%ˆØ€|ÝÔ Ñ&Ô&ˆÝ”;˜u v™~Ñ.Ô.€Làð 8Ø�B�˜1˜#¥ V¤\Ñ!2Ô!2°QÑ!6Ñ7Ñ7ˆˆà�Q�˜1˜#¥ V¤\Ñ!2Ô!2°QÑ!6Ñ7Ñ7ˆà˜d \Ñ1×:Ò:¸5ÑAÔAÑA×EÒEØð Fñ ô €Lð ˜e‘^ |Ñ3°dÑ:¸TÑA×EÒEØð Fñ ô €Lõ
 	Œ×Ò˜<¨Ô)=Ñ>Ô>ÝŒ×Ò˜<¨Ô)=Ñ>Ô>ðð r    Úlinearc           	     óT  — | 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   )rB   r   Úfused_linears      r   r	   r	   m   sÄ   € ð  ”ÐH 2¤;ÐHÐHÐ1HÑHÔHÐ/Ý”= Ñ(Ô(€Lð	ð 	Ô˜rœÒ.Ð.°"´/ÀQÒ2FÐ2FÐ2FØPñ 3GÔ2FÐFð Œ?Ð&¨2¬>Ð+EÐ+EÐEÝ-CØÔØÔØ
ŒØ
ŒØ
ŒØ
Œ	Ø
Œñ.ô .Ñ*€LÔ˜Ô*ð Ðr    Úlinear_wÚlinear_bc                ó°  — | j         }|�|j         n|}|€t          j        |¦  «        }|t          j        ||z   ¦  «        z  }	| |	                     d¦  «                             |¬¦  «        z  }
||z
  |	z  |z                        |¬¦  «        }t          j                             |
| j        ¦  «        t          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.
    Nr.   r0   )	r1   r2   r3   r5   Ú	unsqueezer9   r:   r;   r<   )rG   rH   r%   r&   r'   r)   r*   Úlinear_weight_dtypeÚlinear_bias_dtypeÚbn_scaleÚfused_wÚfused_bs               r   r
   r
   œ   sÜ   € ð. #œ.ÐØ*2Ð*>˜œ˜ÐDWÐØÐÝÔ# EÑ*Ô*ˆØ•e”k %¨&¡.Ñ1Ô1Ñ1€Hà˜×+Ò+¨BÑ/Ô/×2Ò2Ð9LÐ2ÑMÔMÑM€GØ˜5Ñ  HÑ,¨tÑ3×7Ò7Ð>OÐ7ÑPÔP€GåŒ8×Ò˜g xÔ'=Ñ>Ô>ÅÄ×@RÒ@RØ�Ô'ñAô Að ð r    )F)r   r   r   r   r   r   r   r   )r!   r"   r#   r$   r%   r"   r&   r"   r'   r(   r)   r$   r*   r$   r   r   r   r+   )rB   r   r   r   r   r   )rG   r"   rH   r$   r%   r"   r&   r"   r'   r(   r)   r"   r*   r"   r   r+   )Ú
__future__r   r   Útypingr   r   r   r2   Ú__all__r   r   r   r   r	   r
   © r    r   ú<module>rT      sõ   ðØ "Ð "Ð "Ð "Ð "Ð "à €€€Ø +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +à €€€ðð ð €ð 	ˆ�Ð>Ð?Ñ?Ô?€Ø
ˆ'�)Ð#4Ð
5Ñ
5Ô
5€ð ð!ð !ð !ð !ð !ðX ð2ð 2ð 2ð 2ð 2ðj,ð ,ð ,ð ,ð^"ð "ð "ð "ð "ð "r    