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;rìe1 =eƒ  >ejXe<e#ej?ej@fff¡ Ae/¡ Be0¡ Ce]¡ De^¡ Ee_¡ FejY¡¡ nFe1 =eƒ  >ejXe<e#ej@ff¡ Ae/¡ Be0¡ CeZ¡ De[¡ Ee\¡ FejY¡¡ qe1 =eejYƒ Ae/¡ Be0¡ Oej@¡ Pej@¡¡ e(e*gZaeeaƒZbd@dA„ Zcecebejde	jeejfe	jgeae ejhƒejhe/ejdƒ
 eb =eejdejiejffƒ Bea¡ Ce-¡ Fejh¡¡ ecebejde	jeejje jkeae ejlƒejle/ejdƒ
 e)gZme)gZne)gZoe)gZpe*gZqe+gZre,gZsedBœdCdD„ZtdDgZudS )Eé    Né   )Ú_get_conv_configsÚ_get_linear_configsÚ_get_binary_op_configsÚ_get_bn_configsÚ_get_cat_configÚ_get_default_op_configsÚ_get_embedding_op_configsÚ_get_fixed_qparams_op_configsÚ_get_ln_configsÚ_get_rnn_op_configsÚ_get_share_qparams_op_configs)ÚBackendPatternConfigÚBackendConfigÚDTypeConfigÚObservationTypeé   )Ú_sequential_wrapper2)ÚMatchAllNode)Ú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 ks tdƒ‚| r:td|||f› ƒ‚nVtjtji}| t|ƒd¡}|dk	r|tj	j
 ||¡}|||ƒ}|S td|||f› ƒ‚dS )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ÚAssertionErrorÚ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*   ú]/var/www/html/venv/lib/python3.8/site-packages/torch/ao/quantization/backend_config/onednn.pyÚ_fuse_linear_bn_leaky_reluJ   s    ÿ ÿ
r,   c                 C   s   t  ||¡S ©N©r   Ú	ConvAdd2d)r$   ÚaddÚconvÚ_r*   r*   r+   Ú_fuse_conv_add_left}   s    r3   c                 C   s   | \}}}|S r-   r*   )Úpatternr2   r1   r*   r*   r+   Ú_conv_add_root_node_getter_left€   s    
r5   c                 C   s   | \}}}|gS ©z‚ get inputs pattern for extra inputs, inputs for root node
    are assumed to be copied over from root node to the fused node
    r*   )r4   r2   r1   Úextra_inputr*   r*   r+   Ú"_conv_add_extra_inputs_getter_left„   s    
r8   c                 C   sB   |\}}| r"t d|||f› ƒ‚ntjj ||¡}t ||¡S d S ©Nr   ©r   r   r"   r#   Úfuse_conv_bn_evalr   r/   )r$   r0   Úbn_convr2   r&   r1   Ú
fused_convr*   r*   r+   Ú_fuse_conv_bn_add_left‘   s
    r>   c                 C   s   | \}}}|\}}|S r-   r*   )Úadd_patternr2   r<   r&   r1   r*   r*   r+   Ú"_conv_bn_add_root_node_getter_left™   s    
r@   c                 C   s   | \}}}|\}}|gS r6   r*   )r?   r2   r<   r7   r&   r1   r*   r*   r+   Ú%_conv_bn_add_extra_inputs_getter_leftž   s    
rA   Fc                 C   s   t  ||¡S r-   r.   )r$   r0   r2   r1   r*   r*   r+   Ú_fuse_conv_add_rightÅ   s    rB   c                 C   s   | \}}}|S r-   r*   )r4   r0   r2   r1   r*   r*   r+   Ú _conv_add_root_node_getter_rightÈ   s    
rC   c                 C   s   | \}}}|gS r6   r*   )r4   r2   r7   r1   r*   r*   r+   Ú#_conv_add_extra_inputs_getter_rightÌ   s    
rD   c                 C   sB   |\}}| r"t d|||f› ƒ‚ntjj ||¡}t ||¡S d S r9   r:   )r$   r0   r2   r<   r&   r1   r=   r*   r*   r+   Ú_fuse_conv_bn_add_rightÙ   s
    rE   c                 C   s   | \}}}|\}}|S r-   r*   )r4   r0   r2   r<   r&   r1   r*   r*   r+   Ú#_conv_bn_add_root_node_getter_rightá   s    
rF   c                 C   s   | \}}}|\}}|gS r6   r*   )r4   r2   r7   r<   r&   r1   r*   r*   r+   Ú&_conv_bn_add_extra_inputs_getter_rightæ   s    
rG   c                 C   s   |\}}}t  |||¡S r-   ©r   ÚConvAddReLU2d)r$   Úrelur?   r0   r1   r2   r*   r*   r+   Ú_fuse_conv_add_relu_left  s    
rK   c                 C   s   | \}}|\}}}|S r-   r*   ©r4   rJ   r?   r2   r1   r*   r*   r+   Ú$_conv_add_relu_root_node_getter_left  s    
rM   c                 C   s   | \}}|\}}}|gS r6   r*   )r4   rJ   r?   r2   r1   r7   r*   r*   r+   Ú'_conv_add_relu_extra_inputs_getter_left!  s    
rN   c           	      C   sP   |\}}}|\}}| r.t d||||f› ƒ‚ntjj ||¡}t |||¡S d S r9   ©r   r   r"   r#   r;   r   rI   )	r$   rJ   r?   r0   r<   r2   r&   r1   r=   r*   r*   r+   Ú_fuse_conv_bn_add_relu_left1  s    
rP   c                 C   s   | \}}|\}}}|\}}|S r-   r*   ©r4   rJ   r?   r2   r<   r&   r1   r*   r*   r+   Ú'_conv_bn_add_relu_root_node_getter_left:  s    
rR   c                 C   s    | \}}|\}}}|\}}|gS r6   r*   )r4   rJ   r?   r2   r<   r7   r&   r1   r*   r*   r+   Ú*_conv_bn_add_relu_extra_inputs_getter_left@  s    
rS   c                 C   s   |\}}}t  |||¡S r-   rH   )r$   rJ   r?   r0   r2   r1   r*   r*   r+   Ú_fuse_conv_add_relu_rightj  s    
rT   c                 C   s   | \}}|\}}}|S r-   r*   rL   r*   r*   r+   Ú%_conv_add_relu_root_node_getter_rightn  s    
rU   c                 C   s   | \}}|\}}}|gS r6   r*   )r4   rJ   r?   r2   r7   r1   r*   r*   r+   Ú(_conv_add_relu_extra_inputs_getter_rights  s    
rV   c           	      C   sP   |\}}}|\}}| r.t d||||f› ƒ‚ntjj ||¡}t |||¡S d S r9   rO   )	r$   rJ   r?   r0   r2   r<   r&   r1   r=   r*   r*   r+   Ú_fuse_conv_bn_add_relu_rightƒ  s    
rW   c                 C   s   | \}}|\}}}|\}}|S r-   r*   rQ   r*   r*   r+   Ú(_conv_bn_add_relu_root_node_getter_rightŒ  s    
rX   c                 C   s    | \}}|\}}}|\}}|gS r6   r*   )r4   rJ   r?   r2   r7   r<   r&   r1   r*   r*   r+   Ú+_conv_bn_add_relu_extra_inputs_getter_right’  s    
rY   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Ç  sV    ÿþýÿÿþýÿÿþýüÿÿþÿÿþÿrb   )Ú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_config  s.    ÿþýüûúùø	÷
öõrm   )vZtorchZtorch.nnr   Ztorch.ao.nn.intrinsicZaoZ	intrinsicr   Ztorch.nn.functionalZ
functionalÚFZtorch.ao.nn.quantized.referenceZ	quantizedÚ	referenceZnnqrZ_common_operator_config_utilsr   r   r   r   r   r   r	   r
   r   r   r   Zbackend_configr   r   r   r   Zfuser_method_mappingsr   ÚoperatorZtorch.ao.quantization.utilsr   Ú	itertoolsZquint8Zqint8Ú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_INPUTra   Zconv_dtype_configsrd   r3   r5   r8   r>   r@   rA   Úproductr0   Zconv_add_left_optioinsZwith_bnZadd_oprZ   Z_set_pattern_complex_formatZBatchNorm2dZConv2dr^   r[   r\   Z_set_root_node_getterZ_set_extra_inputs_getterr]   r/   rB   rC   rD   rE   rF   rG   Zconv_add_optioinsr_   r`   rK   rM   rN   rP   rR   rS   Zconv_add_relu_left_optioinsZReLUrI   rT   rU   rV   rW   rX   rY   Zconv_add_relu_optioinsZlinear_dtype_configsre   rb   r   r%   Z	LeakyReLUr'   r   ZBatchNorm1dZTanhÚtanhZ
LinearTanhrf   rg   rh   ri   rk   rl   rj   rm   Ú__all__r*   r*   r*   r+   Ú<module>   sD  4üþûýü#
þÿþýüûúùÿ

ÿþýüûúùÿ
þÿþýüûúùÿ

ÿþýüûúùÿ

ÿþýüÿ		
þÿþýüûúùÿ
ÿþýüûúùÿ		
þÿþýüûúùÿ
ÿþýüûúùÿ

ÿþýüÿþ#    ýÿþýÿ    ý	ÿ