
    Χg$                       d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZmZ g dZ ej        ej        d	          Z ed
           ej        d          d0d                        Z ed          d1d0d            Z ed          d1d0d            Z ed           ej        d           ej        dd          d0d                                    Z ed           ej        ddddddddd	  	        d0d                        Z ed          d0d            Z ed           ej        dddddddd          	 	 	 	 	 d2d3d,                        Zd4d.Zd5d/ZdS )6a&  This file exports ONNX ops for opset 14.

Note [ONNX operators that are added/updated in opset 14]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
New operators:
    HardSwish, Trilu

Updated operators:
    Reshape
    Add, Sub, Mul, Div
    GRU, LSTM, RNN
    BatchNorm, Cumsum, Relu
    )annotationsN)
_constants_type_utilssymbolic_helper)GLOBALS)	jit_utilsregistration)	hardswishtriltriureshape
batch_normquantized_hardswishscaled_dot_product_attention   )opsetzaten::hardswishvgjit_utils.GraphContextc                .    |                      d|          S )N	HardSwishop)r   selfs     W/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/onnx/symbolic_opset14.pyr
   r
   *   s     44T"""    z
aten::trilc                4    |                      d||d          S )NTrilur   upper_ir   r   r   diagonalouts       r   r   r   0       44x4333r   z
aten::triuc                4    |                      d||d          S )Nr      r   r   r!   s       r   r   r   5   r$   r   zaten::reshapeTc                2    t          j        | ||d          S )Nr   )	allowzero)r   _reshape_helper)r   r   shapes      r   r   r   :   s     *1dEQGGGGr   zaten::batch_normifc
                   t          j                    rAt          j        |||||g          s(t          j        dk     rt          j        dddd|          S t          j        |d           t          j        | |||||          \  }}}}| 	                    d||||||d|z
  |sdnd|sdnd	
  
        }
|s|
S |
\  }}}|
                    |                                           |
                    |                                           |S )
N   BatchNormalizationr   zaAll input tensors must have the same `dtype`. Turn off Autocast or export using opset version 15.r   r&   r      )	epsilon_f
momentum_ftraining_mode_ioutputs)torchis_autocast_enabledr   args_have_same_dtyper   export_onnx_opset_version _onnx_opset_unsupported_detailedcheck_training_mode_batchnorm_helperr   setTypetype)r   inputweightbiasrunning_meanrunning_vartrainingmomentumepscudnn_enabledr#   resnew_running_meannew_running_vars                 r   r   r   C   s[    	!##
4FD,<
 


 -22? C
 
 	
 ',???.=.O	5&$k/ /+FD, $$x<!)0q!(q   C  
14.  !2!2!4!4555 0 0 2 2333
r   zquantized::hardswishc                    t          j        | |          \  }}}}t          | |          }t          j        | |||          S N)r   dequantize_helperr
   quantize_helper)r   xop_scaleop_zero_point_outputs         r   r   r   z   s@     21a88JAq!Qq!__F*1fhNNNr   z"aten::scaled_dot_product_attentionb        Fquerytorch._C.Valuekeyvalue	attn_masktorch._C.Value | None	dropout_pfloat	is_causalboolscale
enable_gqac	                J   |r |rt          j        |          s
J d            |r
J d            t          j        |          rt          | |          }|rt          | ||          }t          j        |          }	t          t          |	                    }
|
d         |
d         c|
d<   |
d<   |                     d||
          }|                     d||                     d|                    }|                     d||                     d|                    }|                     d	||          }t          j        |          r|}n\t          j	        
                    |          t          j	        j        k    r|                     d
t          j        dg                    }|                     d
t          j        t          d           g                    }|                     d|||          }|                     d||          }nt          j	        
                    |          t          j	        j        t          j	        j        t          j	        j        fv r|                     d||          }n/t%          dt          j	        
                    |                     |                     d|d          }|dk    rJ|                     d||                     d
t          j        |t          j                                      }|                     d	||          S )Nz6is_causal and attn_mask cannot be set at the same timezPconversion of scaled_dot_product_attention not implemented if enable_gqa is True	Transpose)perm_iMulSqrtMatMulConstantrT   value_tinfWhereAddz Unsupported type for attn_mask: Softmaxaxis_ir   Dropoutdtype)r   _is_none_attention_scale_causal_attention_mask_get_tensor_ranklistranger   r   JitScalarType
from_valueBOOLr5   tensorr\   FLOATHALFBFLOAT16
ValueError)r   rU   rW   rX   rY   r[   r]   r_   r`   key_shape_builtinkey_transposed_axeskey_transposedquery_scaledkey_transposed_scaledmul_qk
mul_qk_add
const_zeroconst_neg_infattn_weights                      r   r   r      s     @@%.y99@ @?@ @ @ mmmmmm&& + E** :*1eS99	
 (8==u%67788BB 504 TT+s3FTGGN 44uadd65&9&9::LDDVU8K8KLLTT(L*?@@F	** 


!,,Y77$)	* 	* TT*elC5.A.ATBB
Ze}o1N1NOODD)ZGG	TT%33

		"	-	-i	8	8!'!&!*= 
 

 TT%33

`{/H/S/ST]/^/^``
 
 	
 $$y*R$88KA~~ddDDU\)5;%O%O%ODPP
 
 44+u---r   returnc                $   |                      d|          }|                      d||                      dt          j        dgt          j                            |                      dt          j        t          j        gt          j                                      }|                      d|t          j                            |          	                                          }|                      dt          j        d	gt          j
                            }|                      d
||                      d|                    }|                      d|t          j                            |          	                                          }|S )zCalculate the scale factor for the attention result.

    Args:
        query: Tensor of shape [..., L, E]

    Returns:
        Scalar scale factor := 1 / math.sqrt(query.size(-1))
    ShapeSliceri   rc   rs   rj   Cast)to_i      ?Divrg   )r   r5   r~   int64r   	INT64_MAXr   r{   r|   	onnx_typer\   )r   rU   query_shapequery_shape_lastembedding_size	const_oner_   s          r   rv   rv      s[    $$w&&Ktt	Zrd%+!F!F!FGG	j.B-C5; W W W 	 	
 	
	  TT&11%88BBDD   N
 Zse5;)O)O)OPPIDD	144#?#?@@EDD&11%88BBDD   E
 Lr   c                   |                      d|          }|                      d|          }|                      dt          j        dgt          j                            }|                      dt          j        dgt          j                            }|                      d|||          }|                      d|||          }|                      d||d	
          }	|                      dt          j        dg                    }
|                      d|
|	          }|                      d|d	          }|                      dt          j        dg                    }|                      dt          j        t	          d           g                    }|                      d|                      d||          ||          }|S )a  Create a causal mask for the given query and key tensors.

    Equivalent to::
        mask = torch.ones(L, S, dtype=torch.bool).tril(diagonal=0)
        attn_mask = torch.zeros(L, S, dtype=torch.float)
        attn_mask = attn_mask.masked_fill(not mask, -float("inf"))

    Args:
        query: Tensor of shape [..., L, E]
        key: Tensor of shape [..., S, E]

    Returns:
        Tensor of shape [L, S]
    r   ri   rc   rs   rj   rb   r   Concatr   rp   r   Expandr   r   rT   rl   rm   Equal)r   r5   r~   r   r\   )r   rU   rW   r   	key_shapelast_idxsecond_last_idxtarget_lengthsource_lengthsizer   rY   r   r   s                 r   rw   rw      s   $ $$w&&KWc""IttJbT(M(M(MtNNHdd:u|RD/T/T/TdUUODD+IIMDD)_hGGM44-q4AADZse)<)<==IXy$//IWi33Ij%,u*=*=>>JDDU\E%LL=/-J-JDKKMgy*55}j I r   )r   r   rK   )NrT   FNF)r   r   rU   rV   rW   rV   rX   rV   rY   rZ   r[   r\   r]   r^   r_   rZ   r`   r^   )r   r   rU   rV   r   rV   )r   r   rU   rV   rW   rV   r   rV   )__doc__
__future__r   	functoolsr5   
torch.onnxr   r   r   torch.onnx._globalsr   torch.onnx._internalr   r	   __all__partialonnx_symbolic_onnx_symbolic
parse_argsr
   r   r   quantized_argsr   r   r   r   rv   rw    r   r   <module>r      s     # " " " " "      ? ? ? ? ? ? ? ? ? ? ' ' ' ' ' ' 8 8 8 8 8 8 8 8   #"<#=RHHH !""C  # # # !  #"# 4 4 4 4 4 4 4 4 4 4   %%C%%H H H &% &% ! H "##Cc3S#sCHH2 2 2 IH $#2j &''O O O ('O 455Cc3S#sCC (,#'F. F. F. F. DC 65F.R! ! ! !H% % % % % %r   