Ë
    ÄÓ^jÇ*  ã                   ó&  — U d dl Z d dlmZ d dlmZ d dlmc mc mZ	 d dl
mZ d dlmZmZmZ g d¢Zd„ Zd„ Zd„ Zd	„ Zd
„ Zi ej*                  ej,                  fe“ej*                  ej,                  ej.                  fe“ej0                  ej2                  fe“ej0                  ej2                  ej.                  fe“ej4                  ej6                  fe“ej4                  ej6                  ej.                  fe“ej*                  ej.                  f ee	j8                  «      “ej0                  ej.                  f ee	j:                  «      “ej4                  ej.                  f ee	j<                  «      “ej>                  ej,                  fe“ej>                  ej.                  f ee	j@                  «      “ej2                  ej.                  f ee	jB                  «      “ej6                  ej.                  f ee	jD                  «      “ejF                  ej,                  fe“ejH                  ej2                  fe“ejJ                  ej6                  fe“Z&e'e(ejR                  ez  f   e*d<   dd„Z+d„ Z,d„ Z-d„ Z.dede'eejR                  ez  f   fd„Z/y)é    N)ÚCallable)ÚAny)Úget_combined_dictÚMatchAllNodeÚPattern)Úfuse_conv_bnÚfuse_conv_bn_reluÚfuse_linear_bnÚfuse_convtranspose_bnÚget_fuser_methodÚget_fuser_method_newc                 óT  — |j                   |j                   k7  rt        d«      ‚t        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  i}| r‡|j                  |j                  k7  rt        d«      ‚|j                  st        d«      ‚|j                  st        d«      ‚|j                  t        |«      «      }|�	 |||«      S t!        d||f› �«      ‚t        j"                  j%                  ||«      S )a  Return the fused the conv and bn modules.
    Given the conv and bn modules, fuses them and returns the fused module

    Args:
        is_qat: a flag for whether we are using quantization aware training fusion
        or post training quantization fusion
        conv: Module instance of type conv2d/conv3d
        bn: Spatial BN instance that needs to be fused with the conv

    Examples::

        >>> m1 = nn.Conv2d(10, 20, 3)
        >>> b1 = nn.BatchNorm2d(20)
        >>> # xdoctest: +SKIP
        >>> m2 = fuse_conv_bn(m1, b1)
    ú:Conv and BN both must be in the same mode (train or eval).z@Output channel of Conv2d must match num_features of BatchNorm2d.ú7Only support fusing BatchNorm2d with affine set to TrueúGOnly support fusing BatchNorm2d with tracking_running_stats set to TrueúCannot fuse train modules: )ÚtrainingÚAssertionErrorÚnnÚConv1dÚnniÚConvBn1dÚConv2dÚConvBn2dÚConv3dÚConvBn3dÚnum_featuresÚout_channelsÚaffineÚtrack_running_statsÚgetÚtypeÚNotImplementedErrorÚutilsÚfuse_conv_bn_eval)Úis_qatÚconvÚbnÚfused_module_class_mapÚfused_module_classs        úv/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/ao/quantization/fuser_method_mappings.pyr   r      s  € ð" ‡}�}˜Ÿ™Ò#ÜØHó
ð 	
ô
 	�	‰	”3—<‘<Ü
�	‰	”3—<‘<Ü
�	‰	”3—<‘<ðÐñ Ø�?‰?˜d×/Ñ/Ò/Ü ØRóð ð �yŠyÜ ØIóð ð ×%Ò%Ü ØYóð ð 4×7Ñ7¼¸T»
ÓCÐØÐ)Ù% d¨BÓ/Ð/ä%Ð(CÀTÈ2ÀJÀ<Ð&PÓQÐQä�x‰x×)Ñ)¨$°Ó3Ð3ó    c                 óÌ  — |j                   |j                   cxk(  r|j                   k(  st        d«      ‚ t        d«      ‚d}| råt        j                  t        j
                  t        j                  t        j                  t        j                  t        j                  i}|j                  |j                  k7  rt        d«      ‚|j                  st        d«      ‚|j                  st        d«      ‚|j                  t        |«      «      }|�
 ||||«      S t!        d|||f› �«      ‚t        j                  t        j"                  t        j                  t        j$                  t        j                  t        j&                  i}|j                  t        |«      «      }|�3t        j(                  j*                  j-                  ||«      } |||«      S t!        d|||f› �«      ‚)aJ  Return the fused conv and bv modules.

    Given the conv and bn modules, fuses them and returns the fused module

    Args:
        is_qat: a flag for whether we are using quantization aware training fusion
        or post training quantization fusion
        conv: Module instance of type conv2d/conv3d
        bn: Spatial BN instance that needs to be fused with the conv

    Examples::

        >>> m1 = nn.Conv2d(10, 20, 3)
        >>> b1 = nn.BatchNorm2d(20)
        >>> r1 = nn.ReLU(inplace=False)
        >>> # xdoctest: +SKIP
        >>> m2 = fuse_conv_bn_relu(m1, b1, r1)
    r   Nz?Output channel of Conv2d must match num_features of BatchNorm2dr   r   r   zCannot fuse eval modules: )r   r   r   r   r   ÚConvBnReLU1dr   ÚConvBnReLU2dr   ÚConvBnReLU3dr   r   r   r    r!   r"   r#   Ú
ConvReLU1dÚ
ConvReLU2dÚ
ConvReLU3dr$   Úfusionr%   )r&   r'   r(   ÚreluÚfused_moduleÚmap_to_fused_module_trainÚmap_to_fused_module_evalÚ
fused_convs           r+   r	   r	   G   s®  € ð& �M‰M˜RŸ[™[Ô9¨D¯M©MÒ9ÜØHó
ð 	
ð :ÜØHó
ð 	
ð 04€LÙä�I‰I”s×'Ñ'Ü�I‰I”s×'Ñ'Ü�I‰I”s×'Ñ'ð%
Ð!ð
 �?‰?˜d×/Ñ/Ò/Ü ØQóð ð �yŠyÜ ØIóð ð ×%Ò%Ü ØYóð ð 1×4Ñ4´T¸$³ZÓ@ˆØÐ#Ù  b¨$Ó/Ð/ä%Ð(CÀTÈ2ÈtÐDTÐCUÐ&VÓWÐWô �I‰I”s—~‘~Ü�I‰I”s—~‘~Ü�I‰I”s—~‘~ð$
Ð ð
 0×3Ñ3´D¸³JÓ?ˆØÐ#ÜŸ™Ÿ™×:Ñ:¸4ÀÓDˆJÙ 
¨DÓ1Ð1ä%Ð(BÀDÈ"ÈdÐCSÐBTÐ&UÓVÐVr,   c                 ór  — |j                   |j                   k7  rt        d«      ‚| rh|j                  |j                  k7  rt        d«      ‚|j                  st        d«      ‚|j
                  st        d«      ‚t        j                  ||«      S t        j                  j                  j                  ||«      S )a  Return the fused linear and bn modules.
    Given the linear and bn modules, fuses them and returns the fused module

    Args:
        is_qat: a flag for whether we are using quantization aware training fusion
        or post training quantization fusion
        linear: Module instance of type Linear
        bn: BatchNorm1d instance that needs to be fused with the linear layer

    Examples::

        >>> m1 = nn.Linear(20, 10)
        >>> b1 = nn.BatchNorm1d(10)
        >>> # xdoctest: +SKIP
        >>> m2 = fuse_linear_bn(m1, b1)
    z<Linear and BN both must be in the same mode (train or eval).z@Output features of Linear must match num_features of BatchNorm1dz7Only support fusing BatchNorm1d with affine set to TruezGOnly support fusing BatchNorm1d with tracking_running_stats set to True)r   r   r   Úout_featuresr   r    r   Ú
LinearBn1dr   r$   r4   Úfuse_linear_bn_eval)r&   Úlinearr(   s      r+   r
   r
   „   s­   € ð" ‡�˜"Ÿ+™+Ò%ÜØJó
ð 	
ñ Ø�?‰?˜f×1Ñ1Ò1Ü ØRóð ð �yŠyÜ ØIóð ð ×%Ò%Ü ØYóð ô �~‰~˜f bÓ)Ð)ä�x‰x�‰×2Ñ2°6¸2Ó>Ð>r,   c                 ó¼   — |j                   |j                   k7  rt        d«      ‚| rt        d«      ‚t        j                  j
                  j                  ||d¬«      S )aÿ  Return the fused ConvTranspose and bn modules.
    Given ConvTranspose and bn modules, fuses them and returns the fused module

    Args:
        convt: Module instance of type ConvTransposeNd
        bn: BatchNormNd instance that needs to be fused with the linear layer.
            batch norm N should match the ConvTranspose N

    Examples::

        >>> m1 = nn.ConvTranspose2d(10, 20, 3)
        >>> b1 = nn.BatchNorm2d(20)
        >>> # xdoctest: +SKIP
        >>> m2 = fuse_convtranspose_bn(m1, b1)
    zCConvTranspose and BN both must be in the same mode (train or eval).z8Fusing ConvTranspose+BatchNorm not yet supported in QAT.T)Ú	transpose)r   r   Ú	Exceptionr   r$   r4   r%   )r&   Úconvtr(   s      r+   r   r   ¬   s[   € ð  ‡~�~˜Ÿ™Ò$ÜØQó
ð 	
ñ ÜØFó
ð 	
ô �x‰x�‰×0Ñ0°¸ÀdÐ0ÓKÐKr,   c                 ó   ‡ — ˆ fd„}|S )a!  Return a sequential wrapped that for is_qat and two modules.
    Given a sequential class for two modules, return a function that takes
    is_qat, and then two modules as argument, that ignores the is_qat flag
    and always returns the sequential that combines the two input modules
    c                 ó   •—  ‰||«      S ©N© )r&   Úm1Úm2Ú
sequentials      €r+   Úfuser_methodz*_sequential_wrapper2.<locals>.fuser_methodÐ   s   ø€ Ù˜"˜bÓ!Ð!r,   rF   )rI   rJ   s   ` r+   Ú_sequential_wrapper2rK   É   s   ø€ ô"ð Ðr,   Ú _DEFAULT_OP_LIST_TO_FUSER_METHODc                 ót   — |€i }t        t        |«      }|j                  | d«      }|€t        d| › d�«      ‚|S )z¢Get fuser method for the given list of module types.

    Get fuser method for the given list of module types,
    return None if fuser method does not exist
    Núdid not find fuser method for: ú )r   rL   r!   r   )Úop_listÚadditional_fuser_method_mappingÚall_mappingsrJ   s       r+   r   r   ê   sU   € ð 'Ð.Ø*,Ð'Ü$Ü(Ð*Ió€Lð  ×#Ñ# G¨TÓ2€LØÐÜÐ>¸w¸iÀqÐIÓJÐJØÐr,   c                 ó   ‡ — ˆ fd„}|S )Nc                 ó   •—  ‰| ||«      S rE   rF   )r&   ÚxÚyÚfs      €r+   Úreversedz_reverse2.<locals>.reversedü   s   ø€ Ù�˜˜A‹Ðr,   rF   ©rW   rX   s   ` r+   Ú	_reverse2rZ   û   s   ø€ ôð €Or,   c                 ó   ‡ — ˆ fd„}|S )Nc                 ó$   •— |\  }} ‰| |||«      S rE   rF   )r&   rU   ÚwrV   ÚzrW   s        €r+   rX   z_reverse3.<locals>.reversed  s   ø€ Ø‰ˆˆ1Ù�˜˜A˜qÓ!Ð!r,   rF   rY   s   ` r+   Ú	_reverse3r_     s   ø€ ô"ð €Or,   c                 ó¶   — t        | t        t        f«      r5| D �cg c]  }t        |«      ‘Œ }}t        t	        j
                  |Ž «      }|S | t        g}|S c c}w )aQ  Return a list of valid patterns generated from the op_pattern.

    Returns a list of valid patterns generated from the op_pattern,
    since MatchAllNode can match all types of nodes,
    e.g. pattern (torch.nn.Conv2d, torch.add) should also be able to match keys like
    (MatchAllNode, torch.add) and (torch.nn.Conv2d, MatchAllNode)

    Example Input:
    (torch.add, (torch.nn.ReLU, torch.nn.Conv2d))

    Example Output:
    [(torch.add, (torch.nn.ReLU, torch.nn.Conv2d)),
     (torch.add, (torch.nn.ReLU, MatchAllNode)),
     (torch.add, (MatchAllNode, torch.nn.Conv2d)),
     (torch.add, (MatchAllNode, MatchAllNode)),
     (MatchAllNode, (torch.nn.ReLU, torch.nn.Conv2d)),
     (MatchAllNode, (torch.nn.ReLU, MatchAllNode)),
     (MatchAllNode, (MatchAllNode, torch.nn.Conv2d)),
     (MatchAllNode, (MatchAllNode, MatchAllNode)),
    ]
    )Ú
isinstanceÚtupleÚlistÚ_get_valid_patternsÚ	itertoolsÚproductr   )Ú
op_patternÚsub_patternÚ	sub_combsÚresults       r+   rd   rd   
  s^   € ô. �*œu¤d˜mÔ,ØISÖT¸+Ô(¨Õ5ÐTˆ	ÐTÜ”i×'Ñ'¨Ð3Ó4ˆð €Mð œlÐ+ˆØ€Mùò	 Us   ›Arg   Úfuser_method_mappingc                 óz   — t        | «      }d}|D ]  } |j                  | «      }|€Œ n |€t        d| › d�«      ‚|S )z«Get fuser method.

    This will be made default after we deprecate the get_fuser_method
    Would like to implement this first and have a separate PR for deprecation
    NrN   rO   )rd   r!   r   )rg   rk   Úop_patternsrJ   s       r+   r   r   )  s^   € ô & jÓ1€KØ€LØ!ò ˆ
Ø+×/Ñ/°
Ó;ˆØÑ#Ùðð ÐÜÐ>¸z¸lÈ!ÐLÓMÐMØÐr,   rE   )0re   Úcollections.abcr   Útypingr   Útorch.ao.nn.intrinsicÚaor   Ú	intrinsicr   Útorch.nnÚtorch.ao.quantization.utilsr   r   r   Ú__all__r   r	   r
   r   rK   r   ÚBatchNorm1dÚReLUr   ÚBatchNorm2dr   ÚBatchNorm3dr1   r2   r3   ÚLinearÚ
LinearReLUÚBNReLU2dÚBNReLU3dÚConvTranspose1dÚConvTranspose2dÚConvTranspose3drL   Údictrb   Ú
SequentialÚ__annotations__r   rZ   r_   rd   r   rF   r,   r+   ú<module>r„      s—  ðä Ý $Ý ç #Ó #Ý ß PÑ Pò€ò/4òd:Wòz%?òPLò:
ðKØ‡Y�Y�—‘Ð ðKà‡Y�Y�—‘ §¡Ð(Ð*;ðKð ‡Y�Y�—‘Ð ðKð ‡Y�Y�—‘ §¡Ð(Ð*;ð	Kð
 ‡Y�Y�—‘Ð ðKð ‡Y�Y�—‘ §¡Ð(Ð*;ðKð ‡Y�Y�—‘ÐÑ.¨s¯~©~Ó>ðKð ‡Y�Y�—‘ÐÑ.¨s¯~©~Ó>ðKð ‡Y�Y�—‘ÐÑ.¨s¯~©~Ó>ðKð ‡Y�Y�—‘Ð ðKð ‡Y�Y�—‘ÐÑ.¨s¯~©~Ó>ðKð ‡^�^�R—W‘WÐÑ3°C·L±LÓAðKð ‡^�^�R—W‘WÐÑ3°C·L±LÓAðKð ×Ñ˜Ÿ™Ð(Ð*?ðKð ×Ñ˜Ÿ™Ð(Ð*?ðKð  ×Ñ˜Ÿ™Ð(Ð*?ð!KÐ   $ u¨b¯m©m¸hÑ.FÐ'FÑ"Gó ó(ò"òòð>Øðà˜w¨¯©¸Ñ(@Ð@ÑAôr,   