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=e"ƒ  >ejXe<eej?ej@fff¡ Ae/¡ Be0¡ Ce]¡ De^¡ Ee_¡ FejY¡¡ qHe1 =e"ƒ  >ejXe<eej@ff¡ Ae/¡ Be0¡ CeZ¡ De[¡ Ee\¡ FejY¡¡ qHe1 =e"ejYƒ Ae/¡ Be0¡ Oej@¡ Pej@¡¡ e(e*gZ`ee`ƒZad?d@„ Zbebeaejcejdejeejfe`eejgƒejge/ejcƒ
 ea =e"ejcejhejefƒ Be`¡ Ce-¡ Fejg¡¡ ebeaejcejdejiejje`eejkƒejke/ejcƒ
 e)gZle)gZme)gZne)gZoe*gZpe+gZqe,gZrdAe!fdBdC„ZsdCgZtdS )Dé    N)Ú_sequential_wrapper2)ÚMatchAllNodeé   )Ú_get_binary_op_configsÚ_get_bn_configsÚ_get_cat_configÚ_get_conv_configsÚ_get_default_op_configsÚ_get_embedding_op_configsÚ_get_fixed_qparams_op_configsÚ_get_linear_configsÚ_get_ln_configsÚ_get_rnn_op_configsÚ_get_share_qparams_op_configs)ÚBackendConfigÚBackendPatternConfigÚDTypeConfigÚObservationType)Úinput_dtypeÚoutput_dtypeÚweight_dtypeÚ
bias_dtype)r   r   T)r   r   r   r   Ú
is_dynamic)r   r   r   c                 C   sŽ   |j |j kr|j |j ksJ dƒ‚| rtd|||f› ƒ‚tjtji}| t|ƒd¡}|dur=tjj	 
||¡}|||ƒ}|S td|||f› ƒ‚)a•  Given the linear, bn and leaky_relu 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
        leaky_relu: LeakyReLU instance that needs to be fused with the linear layer
    Examples::
        >>> # xdoctest: +SKIP(failing)
        >>> m1 = nn.Linear(20, 10)
        >>> b1 = nn.BatchNorm1d(10)
        >>> lr = nn.LeakyReLU(0.01)
        >>> m2 = _fuse_linear_bn_leaky_relu(m1, b1, lr)
    zFLinear, BN and LeakyReLU all must be in the same mode (train or eval).úCannot fuse train modules: NzCannot fuse eval modules: )ÚtrainingÚNotImplementedErrorÚnnÚLinearÚnniÚLinearLeakyReLUÚgetÚtypeÚutilsÚfusionÚfuse_linear_bn_eval)Úis_qatÚlinearÚbnÚ
leaky_reluÚmap_to_fused_module_evalÚfused_moduleÚfused_linearÚfm© r-   ú_/var/www/vscode/kcb/lib/python3.10/site-packages/torch/ao/quantization/backend_config/onednn.pyÚ_fuse_linear_bn_leaky_reluL   s"   ÿÿÿ
ÿr/   c                 C   s   t  ||¡S ©N©r   Ú	ConvAdd2d)r%   ÚaddÚconvÚ_r-   r-   r.   Ú_fuse_conv_add_left†   ó   r6   c                 C   s   | \}}}|S r0   r-   )Úpatternr5   r4   r-   r-   r.   Ú_conv_add_root_node_getter_leftŠ   ó   
r9   c                 C   ó   | \}}}|gS ©zget inputs pattern for extra inputs, inputs for root node
    are assumed to be copied over from root node to the fused node
    r-   )r8   r5   Ú_convÚextra_inputr-   r-   r.   Ú"_conv_add_extra_inputs_getter_left   ó   
r?   c                 C   s<   |\}}| rt d|||f› ƒ‚tjj ||¡}t ||¡S ©Nr   ©r   r   r"   r#   Úfuse_conv_bn_evalr   r2   )r%   r3   Úbn_convr5   r'   r4   Ú
fused_convr-   r-   r.   Ú_fuse_conv_bn_add_leftž   ó
   rF   c                 C   s   | \}}}|\}}|S r0   r-   )Úadd_patternr5   rD   Ú_bnr4   r-   r-   r.   Ú"_conv_bn_add_root_node_getter_left§   ó   
rJ   c                 C   r;   r<   r-   )rH   r5   Ú_bn_convr>   r-   r-   r.   Ú%_conv_bn_add_extra_inputs_getter_left­   r@   rM   Fc                 C   s   t  ||¡S r0   r1   )r%   r3   r5   r4   r-   r-   r.   Ú_fuse_conv_add_rightÛ   r7   rN   c                 C   s   | \}}}|S r0   r-   )r8   Ú_addr5   r4   r-   r-   r.   Ú _conv_add_root_node_getter_rightß   r:   rP   c                 C   ó   | \}}}|gS r<   r-   )r8   r5   r>   r=   r-   r-   r.   Ú#_conv_add_extra_inputs_getter_rightä   r@   rR   c                 C   s<   |\}}| rt d|||f› ƒ‚tjj ||¡}t ||¡S rA   rB   )r%   r3   r5   rD   r'   r4   rE   r-   r-   r.   Ú_fuse_conv_bn_add_rightó   rG   rS   c                 C   s   | \}}}|\}}|S r0   r-   )r8   rO   r5   rD   rI   r4   r-   r-   r.   Ú#_conv_bn_add_root_node_getter_rightü   rK   rT   c                 C   rQ   r<   r-   )r8   r5   r>   rL   r-   r-   r.   Ú&_conv_bn_add_extra_inputs_getter_right  r@   rU   c                 C   s   |\}}}t  |||¡S r0   ©r   ÚConvAddReLU2d)r%   ÚrelurH   r3   r4   r5   r-   r-   r.   Ú_fuse_conv_add_relu_left<  ó   
rY   c                 C   s   | \}}|\}}}|S r0   r-   )r8   Ú_relurH   r5   r4   r-   r-   r.   Ú$_conv_add_relu_root_node_getter_leftA  ó   
r\   c                 C   ó   | \}}|\}}}|gS r<   r-   )r8   r[   rH   r5   r=   r>   r-   r-   r.   Ú'_conv_add_relu_extra_inputs_getter_leftG  ó   
r_   c           	      C   sJ   |\}}}|\}}| rt d||||f› ƒ‚tjj ||¡}t |||¡S rA   ©r   r   r"   r#   rC   r   rW   )	r%   rX   rH   r3   rD   r5   r'   r4   rE   r-   r-   r.   Ú_fuse_conv_bn_add_relu_leftY  ó   
rb   c                 C   s   | \}}|\}}}|\}}|S r0   r-   ©r8   r[   rH   r5   rD   rI   r4   r-   r-   r.   Ú'_conv_bn_add_relu_root_node_getter_leftc  ó   
re   c                 C   r^   r<   r-   )r8   r[   rH   r5   rL   r>   r-   r-   r.   Ú*_conv_bn_add_relu_extra_inputs_getter_leftj  r`   rg   c                 C   s   |\}}}t  |||¡S r0   rV   )r%   rX   rH   r3   r5   r4   r-   r-   r.   Ú_fuse_conv_add_relu_right›  rZ   rh   c                 C   s   | \}}|\}}}|S r0   r-   )r8   r[   rH   r5   Ú_extra_inputr4   r-   r-   r.   Ú%_conv_add_relu_root_node_getter_right   r]   rj   c                 C   ó   | \}}|\}}}|gS r<   r-   )r8   r[   rH   r5   r>   r=   r-   r-   r.   Ú(_conv_add_relu_extra_inputs_getter_right¦  r`   rl   c           	      C   sJ   |\}}}|\}}| rt d||||f› ƒ‚tjj ||¡}t |||¡S rA   ra   )	r%   rX   rH   r3   r5   rD   r'   r4   rE   r-   r-   r.   Ú_fuse_conv_bn_add_relu_right¸  rc   rm   c                 C   s   | \}}|\}}}|\}}|S r0   r-   rd   r-   r-   r.   Ú(_conv_bn_add_relu_root_node_getter_rightÂ  rf   rn   c                 C   rk   r<   r-   )r8   r[   rH   r5   r>   rL   r-   r-   r.   Ú+_conv_bn_add_relu_extra_inputs_getter_rightÉ  r`   ro   c
           
      C   s®   |   t||fƒ |¡ |¡ |¡¡ |   t||fƒ |¡ |¡ |¡¡ |   t|ƒ |¡ |¡ |¡ |	¡¡ |   t||fƒ |¡ |¡¡ |   t||fƒ |¡ |¡¡ d S r0   )Úappendr   Úset_dtype_configsÚset_fuser_methodÚset_fused_moduleÚset_observation_typeÚset_root_moduleÚset_reference_quantized_module)
ÚconfigsÚroot_moduleÚroot_opÚpost_moduleÚpost_opÚdtype_configsÚfuser_methodr*   Úobservation_typeÚref_quant_moduler-   r-   r.   Ú_add_eltwise_fusion_configs  s:   
ü
üû	
ý
ýr€   Úreturnc                   C   sn   t dƒ t¡ t¡ ttƒ¡ ttƒ¡ t	tƒ¡ t
tƒ¡ ttƒ¡ ttƒ¡ ttƒ¡ ttƒ¡ ttƒ¡S )zI
    Return the `BackendConfig` for PyTorch's native ONEDNN backend.
    Úonednn)r   Úset_backend_pattern_configsÚconv_configsÚlinear_configsr   Úbinary_op_dtype_configsÚset_backend_pattern_configr   Údefault_op_dtype_configsr	   r   Úfixed_qparams_op_dtype_configsr   Úshare_qparams_op_dtype_configsr   r   Úlayer_norm_op_dtype_configsr   Úrnn_op_dtype_configsr
   Úembedding_op_dtype_configsr-   r-   r-   r.   Úget_onednn_backend_configl  s&   


ÿÿ


ÿðrŽ   )uÚ	itertoolsÚoperatorÚtorchÚtorch.ao.nn.intrinsicÚaor   Ú	intrinsicr   Útorch.ao.nn.quantized.referenceÚ	quantizedÚ	referenceÚnnqrÚtorch.nnÚtorch.nn.functionalÚ
functionalÚFÚ+torch.ao.quantization.fuser_method_mappingsr   Útorch.ao.quantization.utilsr   Ú_common_operator_config_utilsr   r   r   r   r	   r
   r   r   r   r   r   Úbackend_configr   r   r   r   Úquint8Úqint8ÚfloatÚ$onednn_weighted_op_int8_dtype_configÚonednn_op_quint8_dtype_configÚ onednn_dynamic_int8_dtype_configÚ%onednn_weight_only_qint8_dtype_configÚ,onednn_input_output_only_quint8_dtype_configr/   Ú&OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUTr~   Úconv_dtype_configsr„   r6   r9   r?   rF   rJ   rM   Úproductr3   Úconv_add_left_optioinsÚwith_bnÚadd_oprp   Ú_set_pattern_complex_formatÚBatchNorm2dÚConv2drt   rq   rr   Ú_set_root_node_getterÚ_set_extra_inputs_getterrs   r2   rN   rP   rR   rS   rT   rU   Úconv_add_optioinsru   rv   rY   r\   r_   rb   re   rg   Úconv_add_relu_left_optioinsÚReLUrW   rh   rj   rl   rm   rn   ro   Úlinear_dtype_configsr…   r€   r   r&   Ú	LeakyReLUr(   r   ÚBatchNorm1dÚTanhÚtanhÚ
LinearTanhr†   rˆ   r‰   rŠ   rŒ   r   r‹   rŽ   Ú__all__r-   r-   r-   r.   Ú<module>   sì  4üþûýü)	
þÿö
ÿö	
þÿö
ÿöû
	
þÿöÿö
	
þÿöÿöûþ2öüöÿ