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rwe1 =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   Z
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: )ZtrainingÚNotImplementedErrorÚnnÚLinearÚnniÚLinearLeakyReLUÚgetÚtypeÚutilsÚfusionZfuse_linear_bn_eval)Úis_qatÚlinearÚbnÚ
leaky_reluZmap_to_fused_module_evalÚfused_moduleZfused_linearÚfm© r(   úZ/var/www/auris/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†   ó   r1   c                 C   s   | \}}}|S r+   r(   )Úpatternr0   r/   r(   r(   r)   Ú_conv_add_root_node_getter_leftŠ   ó   
r4   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(   )r3   r0   Ú_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   r-   )r"   r.   Úbn_convr0   r$   r/   Ú
fused_convr(   r(   r)   Ú_fuse_conv_bn_add_leftž   ó
   rA   c                 C   s   | \}}}|\}}|S r+   r(   )Úadd_patternr0   r?   Ú_bnr/   r(   r(   r)   Ú"_conv_bn_add_root_node_getter_left§   ó   
rE   c                 C   r6   r7   r(   )rC   r0   Ú_bn_convr9   r(   r(   r)   Ú%_conv_bn_add_extra_inputs_getter_left­   r;   rH   Fc                 C   s   t  ||¡S r+   r,   )r"   r.   r0   r/   r(   r(   r)   Ú_fuse_conv_add_rightÛ   r2   rI   c                 C   s   | \}}}|S r+   r(   )r3   Ú_addr0   r/   r(   r(   r)   Ú _conv_add_root_node_getter_rightß   r5   rK   c                 C   ó   | \}}}|gS r7   r(   )r3   r0   r9   r8   r(   r(   r)   Ú#_conv_add_extra_inputs_getter_rightä   r;   rM   c                 C   s<   |\}}| rt d|||f› ƒ‚tjj ||¡}t ||¡S r<   r=   )r"   r.   r0   r?   r$   r/   r@   r(   r(   r)   Ú_fuse_conv_bn_add_rightó   rB   rN   c                 C   s   | \}}}|\}}|S r+   r(   )r3   rJ   r0   r?   rD   r/   r(   r(   r)   Ú#_conv_bn_add_root_node_getter_rightü   rF   rO   c                 C   rL   r7   r(   )r3   r0   r9   rG   r(   r(   r)   Ú&_conv_bn_add_extra_inputs_getter_right  r;   rP   c                 C   s   |\}}}t  |||¡S r+   ©r   ÚConvAddReLU2d)r"   ÚrelurC   r.   r/   r0   r(   r(   r)   Ú_fuse_conv_add_relu_left<  ó   
rT   c                 C   s   | \}}|\}}}|S r+   r(   )r3   Ú_relurC   r0   r/   r(   r(   r)   Ú$_conv_add_relu_root_node_getter_leftA  ó   
rW   c                 C   ó   | \}}|\}}}|gS r7   r(   )r3   rV   rC   r0   r8   r9   r(   r(   r)   Ú'_conv_add_relu_extra_inputs_getter_leftG  ó   
rZ   c           	      C   sJ   |\}}}|\}}| rt d||||f› ƒ‚tjj ||¡}t |||¡S r<   ©r   r   r    r!   r>   r   rR   )	r"   rS   rC   r.   r?   r0   r$   r/   r@   r(   r(   r)   Ú_fuse_conv_bn_add_relu_leftY  ó   
r]   c                 C   s   | \}}|\}}}|\}}|S r+   r(   ©r3   rV   rC   r0   r?   rD   r/   r(   r(   r)   Ú'_conv_bn_add_relu_root_node_getter_leftc  ó   
r`   c                 C   rY   r7   r(   )r3   rV   rC   r0   rG   r9   r(   r(   r)   Ú*_conv_bn_add_relu_extra_inputs_getter_leftj  r[   rb   c                 C   s   |\}}}t  |||¡S r+   rQ   )r"   rS   rC   r.   r0   r/   r(   r(   r)   Ú_fuse_conv_add_relu_right›  rU   rc   c                 C   s   | \}}|\}}}|S r+   r(   )r3   rV   rC   r0   Z_extra_inputr/   r(   r(   r)   Ú%_conv_add_relu_root_node_getter_right   rX   rd   c                 C   ó   | \}}|\}}}|gS r7   r(   )r3   rV   rC   r0   r9   r8   r(   r(   r)   Ú(_conv_add_relu_extra_inputs_getter_right¦  r[   rf   c           	      C   sJ   |\}}}|\}}| rt d||||f› ƒ‚tjj ||¡}t |||¡S r<   r\   )	r"   rS   rC   r.   r0   r?   r$   r/   r@   r(   r(   r)   Ú_fuse_conv_bn_add_relu_right¸  r^   rg   c                 C   s   | \}}|\}}}|\}}|S r+   r(   r_   r(   r(   r)   Ú(_conv_bn_add_relu_root_node_getter_rightÂ  ra   rh   c                 C   re   r7   r(   )r3   rV   rC   r0   r9   rG   r(   r(   r)   Ú+_conv_bn_add_relu_extra_inputs_getter_rightÉ  r[   ri   c
           
      C   s®   |   t||fƒ |¡ |¡ |¡¡ |   t||fƒ |¡ |¡ |¡¡ |   t|ƒ |¡ |¡ |¡ |	¡¡ |   t||fƒ |¡ |¡¡ |   t||fƒ |¡ |¡¡ d S r+   )Úappendr   Úset_dtype_configsÚset_fuser_methodÚset_fused_moduleÚset_observation_typeÚset_root_moduleÚset_reference_quantized_module)
ZconfigsZroot_moduleZroot_opZpost_moduleZpost_opZdtype_configsZfuser_methodr&   Úobservation_typeZref_quant_moduler(   r(   r)   Ú_add_eltwise_fusion_configs  s:   
ü
üû	
ý
ýrr   Ú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.
    Zonednn)r   Zset_backend_pattern_configsÚconv_configsÚlinear_configsr   Úbinary_op_dtype_configsZset_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ÚoperatorZtorchZtorch.ao.nn.intrinsicZaor   Z	intrinsicr   Ztorch.ao.nn.quantized.referenceZ	quantizedÚ	referenceZnnqrZtorch.nnZtorch.nn.functionalZ
functionalÚFZ+torch.ao.quantization.fuser_method_mappingsr   Ztorch.ao.quantization.utilsr   Z_common_operator_config_utilsr   r   r   r   r	   r
   r   r   r   r   r   Zbackend_configr   r   r   r   Zquint8Zqint8ÚfloatZ$onednn_weighted_op_int8_dtype_configZonednn_op_quint8_dtype_configZ onednn_dynamic_int8_dtype_configZ%onednn_weight_only_qint8_dtype_configZ,onednn_input_output_only_quint8_dtype_configr*   Z&OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUTrq   Zconv_dtype_configsrt   r1   r4   r:   rA   rE   rH   Úproductr.   Zconv_add_left_optioinsZwith_bnZadd_oprj   Z_set_pattern_complex_formatZBatchNorm2dZConv2drn   rk   rl   Z_set_root_node_getterZ_set_extra_inputs_getterrm   r-   rI   rK   rM   rN   rO   rP   Zconv_add_optioinsro   rp   rT   rW   rZ   r]   r`   rb   Zconv_add_relu_left_optioinsZReLUrR   rc   rd   rf   rg   rh   ri   Zlinear_dtype_configsru   rr   r   r#   Z	LeakyReLUr%   r   ZBatchNorm1dZTanhÚtanhZ
LinearTanhrv   rw   rx   ry   r{   r|   rz   r}   Ú__all__r(   r(   r(   r)   Ú<module>   sì  4üþûýü)	
þÿö
ÿö	
þÿö
ÿöû
	
þÿöÿö
	
þÿöÿöûþ2öüöÿ