
    JThA                      % S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJrJ	r	J
r
JrJr  S SKJrJr  S SKrS SKJs  Jr  S SKJr  S SKJrJrJrJr  S SKJr  S SKJ r   \RB                  (       a  S S	K"J#r#  S S
K$J%r%  \" S5      r&\" S5      r'\" S5      r(\
S   r)  S     SS jjr*SS jr+SS jr,    SS jr-S r.S r/SS jr0SS jr1SS jr2SS jr3    SS jr4SSSS.         SS jjr5SS jr6S r7SS jr8SS  jr9SS! jr:SS" jr;SS# jr<SS$ jr=SS% jr>SS& jr?SS' jr@SS( jrASS) jrBSSS* jjrCSS+ jrDSS, jrESSS- jjrFSSS. jjrG S         SS/ jjrH S           SS0 jjrISS1 jrJSS2 jrKSS3 jrL    SS4 jrMSSS5 jjrN S SS6 jjrOSS7 jrPSS8 jrQSS9 jrRSSS: jjrS S SS; jjrTSS< jrUS= rVSS> jrWSS? jrX   S SS@ jjrYSSA jrZSSB jr[SSC jr\SSD jr]  SSE jr^          SSF jr_SG r`  SSH jraSSI jrbSSJ jrcSSK jrd  SSL jre S   SSM jjrfSSN jrgSSO jrhSSP jriSSSQ jjrj  SSR jrk      SSS jrlSST jrmSSU jrnSV roSW rpSSX jrq S       SSY jjrr S           SSZ jjrs S SS[ jjrtS\ ruSS] jrvSS^ jrwS_ rxSS` jrySa rz S     SSb jjr{SSSc jjr|SSSd jjr}SSe jr~\4" SfSgShSh5      SSi j5       r  SSj jr            SSk jr\GR                  GR                  \GR                  GR                  \GR                  GR
                  \GR                  GR                  \GR                  GR                  \GR                  GR                  \GR                  GR                  \GR                  GR                  \GR                  GR                  \GR                  GR                  \GR                  GR                  \GR                  GR                  \GR                  GR                  Sl.rSmSnSoSpSqSrSsStSuSvSwSxSySzS{S|.r\GR$                  \GR&                  \GR(                  \GR*                  \GR,                  \GR.                  \GR0                  \GR2                  \GR4                  \GR6                  \GR8                  \GR:                  \GR<                  \GR>                  \GR@                  \GRB                  /r\GR$                  \GR&                  \GR2                  \GR0                  \GR.                  \GR*                  \GR,                  \GR(                  \GR:                  \GR6                  \GR8                  \GR<                  \GR>                  \GR@                  \GRB                  S}.r\Sm   \Sn   \St   \Sr   \Ss   \Sq   \Sp   \So   \S~   \Sv   \Sw   \Su   \Sn   \Sm   \Sr   \S{   /r\" 5       rS\S'   g)    )annotationsN)AnyCallableLiteralNoReturnTypeVar)Concatenate	ParamSpec)_C)
_constants_type_utilserrorsutils)GLOBALS)	jit_utils)Sequence)Number_T_U_P)	viisffsbstnonec           	        US:X  a  U $ US:X  d  [        U 5      (       d  U $ U R                  5       nUR                  5       (       a  g UR                  5       S:X  a  [	        US5      nUS:X  a  [        U5      $ US:X  a  [        U5      $ US:X  a  [        U5      $ US:X  a  [        U5      $ US	:X  a  U$ US
:X  a  U Vs/ s H  n[        U5      PM     sn$ US:X  a  U Vs/ s H  n[        U5      PM     sn$ [        R                  " SU SU S3U 5      eUR                  5       S:X  a  US
:X  a  UR                  5        HE  nUR                  5       nUR                  5       S:w  d  M)  [        R                  " SU SU S3U 5      e   U R                  5       R                  5        Vs/ s H&  n[        [	        UR                  5       S5      5      PM(     sn$ [        R                  " SU S3U 5      eUb  Uc)  [        R                  " SUR                  5        S3U 5      e[        R                  " SU SU SUR                  5        S3U 5      es  snf s  snf s  snf )Nr   r   onnx::Constantvaluer   r   r   r   r   r   r   z5ONNX symbolic does not understand the Constant node 'z' specified with descriptor ''.prim::ListConstructzFailed to export a node 'z' (in list node z_) because it is not constant. Please try to make things (e.g. kernel sizes) static if possible.zbONNX symbolic does not know how to unpack the ListConstruct node that is not a list of integers: ''z*Expected node type 'onnx::Constant', got 'z2Expected node type 'onnx::Constant' for argument 'z' of node 'z', got ')	_is_valuenode
mustBeNonekind	_node_getintfloatboolstrr   SymbolicValueErrorinputs)r"   descarg_name	node_namer'   node_valr   element_nodes           R/var/www/auris/envauris/lib/python3.13/site-packages/torch/onnx/symbolic_helper.py
_parse_argr7   1   sy    v~s{)E**::<Dyy{&&T7+3;x= S[?"S[>!S[x= S[OT\$,-HqCFH--T\&./hE!Hh//++Gv N..2V27 
 
-	-4<[[] vvx$$&*:: 333L> B))- /\]   # @Ezz|?R?R?TU?T!C	!&&(G45?TUU++//3fA7  9,''8RH
 	

 
#
#	!
+i[R	Q G ./& Vs   =II(-Ic                    [        U [        R                  5      (       d   eU R                  U5      n[	        X5      " U5      $ )z@Gets attributes of a node which is polymorphic over return type.)
isinstancer   NodekindOfgetattr)r'   keysels      r6   r*   r*   v   s7    dBGG$$$$
++c
C4c""    c                D    U R                  5       R                  5       S:H  $ )z$Whether a Value is an ONNX constant.r!   r'   r)   r"   s    r6   _is_onnx_constantrC   }   s    ::<"222r?   c                z    [        U [        R                  5      (       a  [        U 5      (       a  [	        X5      $ U $ N)r9   r   ValuerC   r7   )r"   
descriptors     r6   _maybe_get_constrH      s0     %""'8'?'?%,,Lr?   c                    [        U S5      n[        U[        R                  5      (       a  UR                  S:X  a  U$ U $ )Nr    )rH   r9   torchTensorshape)r"   value_ts     r6   _maybe_get_scalarrO      s4    uc*G'5<<((W]]b-@Lr?   c                t    [        U 5      (       d  [        R                  " SU SU  S3U 5      e[        X5      $ )Nz0ONNX symbolic expected a constant value of the 'z' argument, got 'r%   )_is_constantr   r/   r7   )r"   r1   r2   s      r6   
_get_constrR      sI    ''>xj I7!
 	

 e""r?   c                    U R                  5       nUR                  5       S:w  a  [        R                  " SU S3U 5      e[	        UR                  5       5      $ )Nr$   z;ONNX symbolic expected node type prim::ListConstruct, got 'r#   )r'   r)   r   r/   listr0   )
list_value	list_nodes     r6   _unpack_listrW      sW    !I~~00''I)TVW
 	
 	  "##r?   c                    U R                  5       n[        U 5      (       d)  [        R                  " SUR	                  5        S3U 5      e[        UR                  5       5      $ )Nz>ONNX symbolic expected node type 'prim::TupleConstruct', got 'r#   )r'   _is_tuple_constructr   r/   r)   tupler0   )tuple_value
tuple_nodes     r6   _unpack_tupler]      s`    !!#J{++''OO%&b*
 	

 ""$%%r?   c                0   U R                  5       n[        U 5      (       d<  [        R                  " SU SUR	                  5        S[
        R                   3U 5      e[        UR                  5       5      n[        U5      S:X  d  [        U5      S:X  d   eU$ )zUnpacks a quantized tensor into a tuple of tensor and scale/zero_point.
Args:
    tuple_value: A tuple of tensor, scale, zero_point, and optionally axis.
Returns:
    A tuple of tensor, scale, zero_point, and optionally axis.
z&ONNX symbolic expected the output of `zQ` to be a quantized tensor. Is this likely due to missing support for quantized `z`. Please create an issue on       )
r'   rY   r   r/   r)   r   PYTORCH_GITHUB_ISSUES_URLrZ   r0   len)r[   r\   unpackeds      r6   _unpack_quantized_tensorrd      s     !!#J{++''4ZL A!""?
@d@d?eg 	
 	
 Z&&()Hx=AX!!333Or?   c                h    [        U 5      =(       a!    U R                  5       R                  5       S:H  $ )Nr$   r&   r'   r)   )rU   s    r6   _is_packed_listrg      s(    Z VZ__%6%;%;%=AV%VVr?   c                     ^      SU 4S jjnU$ )a  A decorator which converts args from torch._C.Value to built-in types.

For example:

```
@parse_args('v', 'i', 'fs')
foo(g, a, b, c):
    assert isinstance(a, torch._C.Value)
    assert isinstance(b, int)
    assert isinstance(c, list)
    assert isinstance(c[0], float)
```

Args:
    arg_descriptors: list of str, where each element is
        a string that specifies the type to convert to. Valid descriptors:
        "v": no conversion, keep torch._C.Value.
        "i": int
        "is": list of int
        "f": float
        "fs": list of float
        "b": bool
        "s": str
        "t": torch.Tensor
        "none": the variable is unused
c                \   >^  TT l         [        R                  " T 5      SUU 4S jj5       nU$ )Nc                  > Sn[        T
5      [        U5      :  d/   S[        U5       S[        T
5       STR                   SU 35       e [        R                  " T5      n[	        UR
                  R                  5       5      SS  nTR                  n[        UT
U5       VVV	s/ s H  u  pxn	[        XxX5      PM     nnnn	[        U5      S::  d   STR                   SU 35       e[        U5      S:X  a  S	U;   d   STR                   S
U 35       eT" U /UQ70 UD6$ ! [         a    S /[        U5      -  nS n Nf = fs  sn	nnf )NzIf you believe this is not due to custom symbolic implementation within your code or an external library, please file an issue at https://github.com/pytorch/pytorch/issues/new?template=bug-report.yml to report this bug.z,A mismatch between the number of arguments (z) and their descriptors (z") was found at symbolic function 'z'.    zSymbolic function z4's '**kwargs' can contain a single key/value entry. _outputsz='s '**kwargs' can only contain '_outputs' key at '**kwargs'. )
rb   __name__inspect	signaturerT   
parameterskeys	Exceptionzipr7   )gargskwargsFILE_BUG_MSGsig	arg_namesfn_nameargarg_descr2   arg_descriptorsfns             r6   wrapper.parse_args.<locals>.decorator.<locals>.wrapper   s   l 
 '3t94 >s4yk J&&)/&:%;;]^`^i^i]jjm."4''+ !4!4!67;	++ 034)/T/T+C8 3(</T  
 v;!# $R[[M 2$."# 6{a!V+ ( 65#n&+
 a)$)&))-   "FSY.		
s   AD  %E D>=D>)rt   r   ru   z_P.argsrv   z	_P.kwargsreturnr   )_arg_descriptors	functoolswraps)r~   r   r}   s   ` r6   	decoratorparse_args.<locals>.decorator   s0     .		'	* 
'	*R r?   )r~   "Callable[_Concatenate[_U, _P], _T]r   r   rJ   )r}   r   s   ` r6   
parse_argsr      s!    </./	+/b r?   T)scale
zero_pointquantize_outputc                    ^ ^^^ UUU U4S jnU$ )a  A decorator which extends support for quantized version of the base operator.

Quantization is detected by examining the arguments that are annotated by
`arg_q_descriptors`.

If quantization is detected, the base operator symbolic function will be wrapped with
argument de-quantization and output quantization.

Otherwise, only the base symbolic function will be invoked.

For example:

```
@quantized_args(True, False)
def foo(g, x, y):
    return x + y
```

is equivalent to

```
def q_foo(g, x, y):
    if is_quantized_tensor(x):
        x = dequantize(x)
        out = foo(g, x, y)
        return quantize(out)
    else:
        return foo(g, x, y)
```

Args:
    arg_q_descriptors: A sequence of bool, where each element represents if the
      argument is QTensor for quantized version of this operator. It defaults
      to False for unspecified (variable length) arguments.
    scale: Quantized output scale. If None, derive from
      the first quantized input scale.
    zero_point: Quantized output zero point. If None,
      derive from the first quantized input zero point.
    quantize_output: If True, quantize the output of the base operator. Default is True
c                P   >^  [         R                  " T 5      UU UUU4S j5       nU$ )Nc                T  >^^ Tb%  U R                  S[        R                  " T5      S9nOS nTb%  U R                  S[        R                  " T5      S9nOS nTS[        U5      [        T5      -
  -  -   n[	        [        XQ5      5      nS m/ nU Hh  u  mn[        U5      (       a:  UR                  UU4S jUR                  5       R                  5        5       5        MP  UR                  T" TU5      5        Mj     [        U5      (       d  T" U /UQ70 UD6$ / n	U H  u  mnT" TU5      (       a-  [        X5      u  ppU	R                  U
5        Uc  UnUc  UnM?  MA  [        U5      (       as  UR                  5       R                  5        H>  nT" TU5      (       d  M  [        X5      u  n
nnnUc  UnUc  UnUR                  U
5        M@     U	R                  U5        M  U	R                  U5        M     T" U /U	Q70 UD6nUc   S5       eUc   S5       eT(       a  [        XX45      $ U$ )NConstantrN   )Fc                N    U =(       a    [        U5      =(       a    [        U5      $ rE   )r&   rY   )rG   r{   s     r6   _is_arg_quantizedMquantized_args.<locals>.decorator.<locals>.wrapper.<locals>._is_arg_quantizedd  s    !QinQ9LS9QQr?   c              3  6   >#    U  H  nT" TU5      v   M     g 7frE   rJ   ).0	arg_inputr   rG   s     r6   	<genexpr>Equantized_args.<locals>.decorator.<locals>.wrapper.<locals>.<genexpr>l  s"      ()<I **i@@)<s   z-Bug: Scale must be set for quantized operatorz2Bug: Zero point must be set for quantized operator)oprK   tensorrb   rZ   rs   rg   extendr'   r0   appendanydequantize_helperreplaceAllUsesWithquantize_helper)rt   ru   rv   _scale_zero_pointarg_q_descriptors_extendeddescriptor_argsis_quantizedr{   non_quantized_argsdequantized_arg	arg_scalearg_zero_point_r   outputr   rG   arg_q_descriptorsr~   r   r   r   s                   @@r6   r   2quantized_args.<locals>.decorator.<locals>.wrapperQ  s^     j%,,u2EF%dd:u||J7OdP" *;XD	C 122> *& $C(B$IJOR (*L#2
C"3'' '' (),):):)<( 
 !''(9*c(JK $3 |$$!-d-f-- "$#2
C$Z55DUEAO '--o>~!*"*&4 + %S))%(XXZ%6%6%8	,ZCC !2! ? / ) . !  &~)2*2.<%88I &9 '--c2 '--c2A $3F 9.9&9F%V'VV%* D* &q&FFMr?   r   r   )r~   r   r   r   r   r   s   ` r6   r   !quantized_args.<locals>.decoratorP  s.    		R	 R	 
R	h r?   rJ   )r   r   r   r   r   s   ```` r6   quantized_argsr   !  s    ^V Vp r?   c                    [        U [        R                  5      (       a   U R                  S:X  a  U R	                  5       $ g)z,Convert a scalar tensor into a Python value.rJ   N)r9   rK   rL   rM   itemxs    r6   _scalarr     s+    !U\\""qww"}vvxr?   c                P   [        U [        R                  5      (       a  U $ [        R                  R                  U[        R                  R                  5      nU[        R                  R                  :w  a.  UR                  5       R                  5       n[        X5      " 5       $ U $ )z
Convert self into the same type of tensor, as necessary.
We only support implicit casting for scalars, so we never
actually need to insert an ONNX cast operator here; just
fix up the scalar.
)
r9   r   rF   r   JitScalarType
from_value	UNDEFINEDscalar_namelowerr<   )selfr   scalar_typetys       r6   _if_scalar_type_asr     s     $!!++66))33K k//999$$&,,.t ""Kr?   c                    U S L =(       d?    [        U [        R                  5      (       a  U R                  5       R	                  5       $ S$ NF)r9   r   rF   r'   r(   r   s    r6   _is_noner     s5    9U*Q2I2I,,.UuUr?   c                6    [        U [        R                  5      $ rE   )r9   r   rF   r   s    r6   r&   r&     s    a""r?   c                r    [        U 5      (       + =(       d!    U R                  5       R                  5       S;   $ )N>   r!   prim::Constantrf   rB   s    r6   rQ   rQ     s2     5::<#4#4#6 ; $ r?   c                x    U R                  5       R                  [        R                  R	                  5       5      $ rE   )typeisSubtypeOfr   
TensorTypegetr   s    r6   
_is_tensorr     s&    668 1 1 344r?   c                F    [        U [        R                  5      (       a  U $ g rE   )r9   r   ListType)jit_types    r6   _as_list_typer     s    (BKK((r?   c                8    [        U R                  5       5      S L$ rE   )r   r   r   s    r6   _is_listr     s    "$..r?   c                    [        U R                  5       5      nUc  g[        UR                  5       [        R
                  5      $ r   )r   r   r9   getElementTyper   r   r   x_types     r6   _is_tensor_listr     s4    1668$F~f++-r}}==r?   c                    [        U R                  5       5      nUc  g[        R                  R	                  U 5      nUR                  5       $ )zChecks if x is a scalar list, for example: List[float], List[int].

Besides checking the type is ListType, we also check if the data type is
a valid ONNX data type.
F)r   r   r   r   r   onnx_compatible)r   r   r   s      r6   _is_scalar_listr     sA     1668$F~++66q9K&&((r?   c                D    U R                  5       R                  5       S:H  $ )Nprim::TupleConstructrA   r   s    r6   rY   rY     s    668==?444r?   c                2   [        U 5      (       d   e[        R                  R                  U [        R                  R                  5      [        R                  R
                  [        R                  R                  [        R                  R                  1;   $ rE   )r&   r   r   r   r   	COMPLEX32	COMPLEX64
COMPLEX128r   s    r6   is_complex_valuer     ss    Q<<<$$//	;$$.. 	!!++!!++!!,,
 r?   c                    [        U 5      (       a  U R                  5       c  g U R                  5       n[        R                  " [        R
                  U5      nUR                  5       $ rE   )r   r   typingcastr   r   dimr   s     r6   _get_tensor_rankr     sD    a==AFFH,VVXF[[/F::<r?   c                    [        U 5      (       a  U R                  5       c  g U R                  5       n[        R                  " [        R
                  U5      nU(       a  UR                  5       $ UR                  5       $ rE   )r   r   r   r   r   r   varyingSizessizes)r   allow_nonstaticr   s      r6   _get_tensor_sizesr     sZ    a==AFFH,VVXF[[/F ""$$ <<>r?   c                2    [        U 5      nU(       a  X!   $ S $ rE   )r   )r   r   r   s      r6   _get_tensor_dim_sizer     s    a E5:(D(r?   c                    US:X  a  [        U 5      nUc   eX-   $ Uc3  [        U 5      nUc   e[        U5       H  u  pEUc  M
  US:X  d  M  Us  $    U$ )Nr_   )r   r   	enumerate)r   r   tensor_rankr   indexsizes         r6   _get_dim_for_crossr   !  sl    
by&q)&&&  
{!!$   $U+KEDAI , Jr?   c                    [         R                  [        R                  R                  :X  a  [        U  SU 3U5        g g )Nz, )r   operator_export_type_C_onnxOperatorExportTypesONNX_onnx_unsupported)r   msgr"   s      r6   _unimplementedr   0  s6    ##w'B'B'G'GGRD3%.%0 Hr?   c                    SU  S[         R                   3n[        U[        R                  5      (       a  [
        R                  " UU5      e[
        R                  " U5      e)Nz%Unsupported: ONNX export of operator zR. Please feel free to request support or submit a pull request on PyTorch GitHub: )r   ra   r9   r   rF   r   r/   OnnxExporterError)op_namer"   messages      r6   r   r   6  sd    
/y 9(BBC	E 
 %""''
 	
 
"
"7
++r?   c                    SU  SU SU S3n[        U[        R                  5      (       a  [        R                  " UU5      e[        R
                  " U5      e)NUnsupported: ONNX export of 
 in opset . Please try opset version .r9   r   rF   r   r/   r   )r   current_opsetsupported_opsetr"   r   s        r6   _onnx_opset_unsupportedr  D  sh     'wiz- I$$3#4A	7  %""''
 	
 
"
"7
++r?   c           	         SU  SU SU SU S3	n[        U[        R                  5      (       a  [        R                  " UU5      e[        R
                  " U5      e)Nr  r  z. r  r  r  )r   r  r  reasonr"   r   s         r6    _onnx_opset_unsupported_detailedr  V  sp     'wi 0r&)D_DUUV	X  %""''
 	
 
"
"7
++r?   c                   ^  U 4S jnU$ )Nc                 Z   > [         R                  " ST S[        R                   S35      e)NzONNX export failed on z%, which is not implemented for opset z*. Try exporting with other opset versions.)r   r   r   export_onnx_opset_version)ru   rv   names     r6   symbolic_fn)_block_list_in_opset.<locals>.symbolic_fnj  s6    &&$TF*O001 277
 	
r?   rJ   )r  r  s   ` r6   _block_list_in_opsetr  i  s    
 r?   c                     U  H]  n[         R                  R                  U[         R                  R                  5      nU[         R                  R                  :w  d  M[  Us  $    g rE   )r   r   r   r   )ru   r{   r   s      r6   _try_get_scalar_typer  t  sT    !//::**44
 +33===  r?   c                 ~   [         R                  R                  nU  Vs/ s H  n[        U5      PM     nn[	        U5      S:X  a  U$ [	        U5      S:X  a  US   $ US   R                  5       nU H'  n[        R                  " XER                  5       5      nM)     [         R                  R                  U5      $ s  snf )Nr   rk   )	r   r   r   r  rb   dtyperK   promote_types
from_dtype)ru   undefr{   	jit_types	new_dtyper   s         r6   _type_promote_from_valuesr  ~  s    %%//E6:;ds%c*dI;
9~
9~|!""$I''	779=	 $$//	:: <s   B:c                    [         R                  R                  U[         R                  R                  5      U:w  a  U R	                  SUUR                  5       S9$ U$ NCastto_i)r   r   r   r   r   	onnx_type)rt   r"   r   s      r6   _maybe_cast_to_typer#    s_     	!!,,UK4M4M4W4WX	 tt##%  
 	

 Lr?   c           
     l   [        U5      n[        U5      n[        U5      (       d&  U R                  S[        R
                  " U/5      S9nO?Ub<  U(       a5  US:X  a/  [        XU R                  S[        R
                  " S/5      S95      n[        R                  R                  U[        R                  R                  5      nU[        R                  R                  [        R                  R                  1;  a)  U R                  SU[        R                  R                  S9nU R                  SXUS9$ )	Nr   r   r   rk   r  r   Gatheraxis_i)rO   r   r&   r   rK   
LongTensor_reshape_helperr   r   r   r   INT64INTr   TensorProtoDataType)rt   r   r   r   apply_reshapeindex_const	index_dimindex_scalar_types           r6   _select_helperr1    s   #E*K 'I[!!Z)9)9;-)HI		=>#!$$z53C3CQC3H$IE $11<<{((22 !!''!!%%!  VU)D)D)J)JK44$c422r?   c                b    U R                   S::  a  SSKJn  U" XX#U5      $ SSKJn  U" XX#XE5      $ )N	   r   )_slice)opsettorch.onnx.symbolic_opset9r4  torch.onnx.symbolic_opset10)rt   inputaxesstartsendssteps_slice9_slice10s           r6   _slice_helperr?    s3     	ww!|@qt44B$<<r?   c                @   [         R                  R                  U [         R                  R                  5      [         R                  R                  [         R                  R
                  [         R                  R                  [         R                  R                  1;   $ rE   )r   r   r   r   FLOATDOUBLEHALFBFLOAT16rB   s    r6   _is_fprE    st    $$//{((22 	!!''!!((!!&&!!**	
 r?   c                    [         R                  R                  U [         R                  R                  5      [         R                  R                  1;   $ rE   )r   r   r   r   BOOLrB   s    r6   _is_boolrH    sC    $$//{((22

#
#
(
(	)* *r?   c                   [        U[        R                  5      (       a   e[        U[        5      (       a1  U R	                  S[        R
                  " U[        R                  S9S9$ U R	                  S[        R
                  " U5      S9$ )a  Creates a wrapped number based on https://github.com/pytorch/pytorch/issues/9515.

A Tensor is a considered a "wrapped number" if it is
auto-wrapped from a C++ or Python number type. Integer types are
wrapped as 0-dim int64 tensors and floating-point types are
wrapped as 0-dim double tensors.

The input to this function is constant value. If the data type
is a floating point type, it is converted to a 0-dim double
tensor, else it is converted to a 0-dim tensor of its original type
r   r  r   )r9   rK   rL   r,   r   r   double)rt   scalars     r6   _generate_wrapped_numberrM    sf     &%,,////&%  ttJV5<<(PtQQ44
ELL$8499r?   c                X   Ub  [        SS5        U R                  SU5      nU R                  SUU R                  S[        R                  " U/[        R                  S9S95      nU R
                  S::  a%  U(       d  [        SS	5        U R                  S
XUSS9$ U R                  S
XX#SS9$ )NSortOut parameter is not supportedShaper%  r   rJ  r   
   Ascending is not supportedTopK   r'  outputs)r'  	largest_irW  )r   r   rK   r   int64r5  )rt   r8  r   	decendingoutshape_	dim_size_s          r6   _sort_helperr^    s    
v?@TT'5!F	Zse5;;!GHI
 	ww"}6#?@ttFES!tDDttESq  
 	
r?   c           
     4   Ub  [        SS5        [        U5      (       d3  U R                  S[        R                  " U/[        R
                  S9S9nO[        XU R                  S[        R                  " S/5      S95      n[        U5      [        R                  R                  :w  a)  U R                  SU[        R                  R                  S9nU R                  S	::  a%  U(       d  [        SS
5        U R                  SXUSS9$ U R                  SXX4USS9$ )NrT  rP  r   rJ  r   rk   r  r   rR  rS  rU  rV  )r'  rX  sorted_irW  )r   r&   r   rK   r   rY  r)  r  r   r   r*  r   r,  r5  )rt   r8  kr   largestsortedr[  s          r6   _topk_helperrd    s     v?@Q<<DDU\\1#U[[%IDJA!$$z5<<;L$"MN"k&?&?&E&EEVQW%@%@%F%FGAww"}6#?@ttFES!t<<ttESfVW  
 	
r?   c                ^    U R                   S::  a  SSKJn  U" XU5      $ SSKJn  U" XU5      $ )N   r   )lt)r5  torch.onnx.symbolic_opset8rg  r6  )rt   r8  other_lt8_lt9s        r6   
_lt_helperrl    s-    ww!|9Ae$$9Ae$$r?   c                    [         R                  S:  a  SOSn[        R                  " SU-   S-   [	        [         R                  5      -   S-   5        g )NrR  zonnx:Resizezonnx:Upsamplez(You are trying to export the model with z for ONNX opset version a  . This operator might cause results to not match the expected results by PyTorch.
ONNX's Upsample/Resize operator did not match Pytorch's Interpolation until opset 11. Attributes to determine how to transform the input were added in onnx:Resize in opset 11 to support Pytorch's behavior (like coordinate_transformation_mode and nearest_mode).
We recommend using opset 11 and above for models using this operator.)r   r  warningswarnr.   )interpolate_modeonnx_ops     r6   _interpolate_warningrr    s]     ::b@o  MM2
		 223	47P	P
r?   c                   [        U5      S:X  a  U$ [        US   5      (       ac  U R                  S:  aC  U R                  S[        R
                  " U[        R                  S9S9nU R                  SX5      $ U R                  SXS9$ U R                  S:  a  [        R                  " SU5      eU R                  SXS   5      $ )	Nr      r   rJ  r   	Unsqueezeaxes_iz<Opset version must be >= 13 for Unsqueeze with dynamic axes.)	rb   rQ   r5  r   rK   r   longr   r/   )rt   r8  rw  r9  s       r6   _unsqueeze_helperry  +  s    
6{a	fQi	 	 77b=44
ELLuzz,R4SD44U11ttKt66ww|''JE
 	
 44U1I..r?   c                   [        US   5      (       ac  U R                  S:  aC  U R                  S[        R                  " U[        R
                  S9S9nU R                  SX5      $ U R                  SXS9$ U R                  S:  a  [        R                  " SU5      eUS   n[        U5      nUc   eUS	:  a  [        R                  " S
U5      eUS:X  a  [        XS/5      nU R                  SX5      $ U R                  SX5      $ )Nr   rt  r   rJ  r   Squeezerv  z:Opset version must be >= 13 for Squeeze with dynamic axes.rk   zCFor Squeeze axses as input, the axes rank must be one in ONNX spec.)
rQ   r5  r   rK   r   rx  r   r/   r   ry  )rt   r8  rw  r9  axes_t	axes_ranks         r6   _squeeze_helperr~  <  s    F1I77b=44
ELLuzz,R4SD44	5//ttIut44ww|''H%
 	
 AYF (I   1}''QSX
 	
 
a"1qc2ttIu--44	5))r?   c                6   [        US5      nU R                  S:  am  U(       aT  [        U5      (       d1  U R                  S[        R
                  " U[        R                  S9S9nU R                  SUUUUS9$ U R                  SUUUS9$ U R                  SXUS9$ )	Nr   rt  r   rJ  r   	ReduceSum)
keepdims_inoop_with_empty_axes_irw  r  )rH   r5  r&   r   rK   r   rx  )rt   r8  rw  r  r  s        r6   _reducesum_helperr  U  s     "*c2Jww"}V$$V5::(N   44%'=    tt!#9	  
 	
 ttK*tMMr?   c           	        [        US5      n[        U5      (       a  SnU R                  S[        R                  " U[        R
                  S9S9nU R                  SU[        R                  R                  S9n[        X R                  SU5      S	/[        R                  /U/S
9nU R                  SU[        R                  R                  S9nU R                  SXg5      nU R                  SXXS	S9n	U	$ [        S	U5       V
s/ s HJ  n
U
S:  a  SO>[        X#U
-
  *    5      [        UR                  5       R                  5       X:-
  *    5      -  PML     nn
U R                  S[        R                   " U[        R
                  S9S9n	U	$ s  sn
f )Nr   rU  r   rJ  r   r  r   rQ  r   r9  r;  r:  DivConcatr&        ?)rH   r&   r   rK   onesfloat32r   r,  rA  r?  sysmaxsizeranger,   r   r   r   )rt   r8  output_sizer   offsetoffsetsdividenddivisor
scale_dimsscalesr   scales_constants               r6   _interpolate_size_to_scalesr  t  sy   ";5K$$z5::fEMM+R$S44'2M2M2S2S4TttGU#1#S[[M6(
 $$vwW-H-H-N-N$OTT%3
hA> M 1c]

 # 1u {1W:./EJJL&&(374566 # 	 
 _EMM R  
 M
s   AFc           	     T   [        US   S5      S:g  =(       a    [        US   5      (       + nU(       d  g U R                  S[        R                  " S[        R
                  S9S9nU R                  S[        R                  " [        US   S5      5      S9nU R                  SX4SS	9nU$ )
Nr   r   r   r   rU  rJ  r   r  r&  )rH   r   r   rK   r  r  r   )rt   r  available_scalesr  scales_lists        r6   $_interpolate_get_scales_if_availabler    s    'q	48B> xq	H D dd:uzz!5=='IdJG$$ELL)9&)T)JK  K TT(GT;FMr?   c                N    US:X  a  S nUSS  nO
US   nUSS  n[        X5      nXC4$ )Nnearestr   rk   )r  )rt   moderu   align_cornersr  s        r6   _get_interpolate_attributesr    sA    yabQab1!<F  r?   c                   U R                  S[        R                  " S[        R                  S9S9n[	        U5      n[        UR                  5       [        R                  5      (       d	  Ub  US:  a  U R                  SX1SS9$ [        XS/5      nU R                  SU[        R                  R                  S	9n[        US-
  5       Vs/ s H  oQPM     nnU R                   " SU/UQ7S
S06nU$ s  snf )Nr   rU  rJ  r   r   r  r&  r  r   r'  )r   rK   r  r  r   r9   r   r   r   ry  r   r,  rA  r  )rt   scale_factorr   r  scale_factor_rankr   r  s          r6   _interpolate_get_scalesr    s    dd:uzz!5=='IdJG(6,##%r{{33%*;a*?ttHgAt>>(1#>ttLw'B'B'H'H  
 ).cAg71,744'=F=1=L 8s   
C0c                   [        US5      nSU;   a  SnSU;   a  Sn[        U5        [        US5      n[        U[        5      (       a  U(       a  [	        SS5      $ UR                  5       R                  5       (       d  [	        SS5      $ UR                  5       R                  5       n[        U5      (       d  [        XU5      nX44$ [        U5      (       d  [        U5      (       dd  [        US5      R                  5       S	:H  nU(       a@  [        XS	/5      n[        US
-
  5       Vs/ s H  oPM     nnU R                  " S/UQ7SS	06n[        XX&5      nX44$ [	        SS5      $ s  snf )Nr   linearcubicr   interpolatezalign_corners == Truemissing input shaper   r   rU  r  r'  z.Both size and scales are None in __interpolate)rH   rr  r9   r-   r   r   r   r   r  rg   ry  r  r   r  )	rt   r8  r   r  r  r  r   	is_scalarr   s	            r6    _interpolate_get_scales_and_moder    sY    D#&D4$$]C8M-&&=m-DEE::<m-BCC
**,


CL!!.qD  d^^t$$(s3779Q>I(1#6&+C!Gn5nn5ttH6t6A621TG
  K
 	
	 6s   )E'c           
       ^ ^ U U4S jn[        U5      (       a  [        T UT R                  S[        R                  " S/5      S95      nU" USSS9nU(       ai  T R                  SU5      nT R                  SU5      n	T R                  S	U	[        R                  " S
/[        R
                  S9S9n
T R                  SXz5      nU$ [        US5      nU" XUS9$ )Nc                l   > TR                   S:  a  TR                  TU UUSS9$ TR                  TXUS9$ )N   F)r'  r  select_last_index_ir'  r  )r5  r   )r8  r'  r  rt   r   s      r6   
op_wrapper)_argmin_argmax_helper.<locals>.op_wrapper  sJ    77b=44%$)    ttGUjtIIr?   r   r   r   r   Fr  rQ  ConstantOfShaperk   rJ  Reshaper   )r   r)  r   rK   r   rY  r7   )rt   r8  r   keepdimr   r  	flattenedr   input_shapeinput_shape_shape	new_shapes   `   `      r6   _argmin_argmax_helperr    s    	J }}#uadd:u||RD/AdB
	 IaEB$$w.K !Wk :!!aS<  I
 TT)V7F
S#
CeG<<r?   c                4   ^ [        SSS5      U4S j5       nU$ )NTFc                   > [        U TU5      u  pE[        U5      nTS:X  a  SO
U(       a  SOSnUc  U R                  SU5      n[        XS/S/S/S9nU R                  S	U[        R
                  R                  S
9nU R                  SXSS9nU R                  S:  a  [        U 5      n	[        U 5      n
ObU R                  S[        R                  " / [        R                  S9S9n	U R                  S[        R                  " / [        R                  S9S9n
U R                  SUU	U
UUSTSS9	$ U R                  S:  a  [        U 5      n	O1U R                  S[        R                  " / [        R                  S9S9n	U R                  SUU	UUSTSS9$ )Nr  
asymmetricr  
half_pixelrQ  r   rU  r  r  r   r  r&  rt  r   rJ  r   Resize      floor coordinate_transformation_mode_scubic_coeff_a_fmode_snearest_mode_s)r  rO   r   r?  r   r,  r*  r5  "_optional_input_placeholder_tensorrK   r   r  )rt   r8  r  ru   r  r  coordinate_transformation_mode
input_sizeinput_size_beg	empty_roiempty_scalesrp  s              r6   r  (_interpolate_helper.<locals>.symbolic_fn  s    ;A?OQU V)-8  9,   ! 	' >gu-J*QCqc1#N $$'*E*E*K*K  K $$xQ$OKww"}>qA	A!DDDRu}}(M ! 	  !ttRu}}(M  $   441O %'&  
 
 ww"}>qA	DDRu}}(M ! 	 441O %'&  	 	r?   )r   )r  r   rp  r  s     ` r6   _interpolate_helperr    s'    D%'< (<| r?   c                   [        US5      nSU;   a  SnSU;   a  Sn[        US5      n[        U[        5      (       d  SOUnUS:X  a  SO
U(       a  SOS	n[        U5      (       Gd  U R	                  S
U5      n[        XS/S/S/S9n [        U5      (       + =(       a    [        US5      R                  5       S:H  n	U	(       aZ  [        U5      n
U
c  [        SS5      $ [        XS/5      n[        U
S-
  5       Vs/ s H  oPM     nnU R                  " S/UQ7SS06nU R	                  SU[        R                   R"                  S9nU R	                  SXSS9nU R$                  S:  a  ['        U 5      n['        U 5      nObU R	                  S[(        R*                  " / [(        R,                  S9S9nU R	                  S[(        R*                  " / [(        R,                  S9S9nU R	                  SUUUUUSUSS9	$ [        U5      n
U
c  [        SS 5      $ U R$                  S:  a  ['        U 5      nO1U R	                  S[(        R*                  " / [(        R,                  S9S9n[/        XU
5      nU R	                  SUUUUSUSS9$ ! [         a1    [        U5      (       + n	U	(       d  [        R                  " S5         GNf = fs  snf )!Nr   r  r  r   Fr  r  r  r  rQ  r   rU  r  r   zkCannot verify if the output_size is a scalar while exporting interpolate. Assuming that it is not a scalar.z'interpolate (with a scalar output_size)z?missing input shape (try giving an array of output_size values)r  r'  r  r   r&  rt  r   rJ  r   r  r  r  r  zinterpolate (with scales)r  )rH   r9   r-   r   r   r?  rg   r   AttributeErrorrn  ro  r   r   ry  r  r   r,  r*  r5  r  rK   r   r  r  )rt   r8  r   r  r  r  recompute_scale_factorr  r  r  rankr   r  r  r  s                  r6   __interpolate_helperr  F  s    D#&D4$$]C8M!+M4!@!@EmM 9 	   # D>>TT'5)
"1s!aSQ


	+D11  s+//1Q6  #E*D|%=U  %Qqc2D"'q/2/QD/D2442422DttFDw'B'B'H'HtIttHjqt977b=:1=I=a@LZb1VWI44ELL5==$I   L tt-K!"  

 
	
  &<!"=?TUU77b=:1=IZb1VWI($?tt-K!"  	
 		
i  	+D11IU	  3s   4J  :J> 7J;:J;c                |    U R                   S:  a  SSKJn  OU R                   S::  a  SSKJn  OSSKJn  U" XX#5      $ )N   r   )unbindr  )r5  r6  r  torch.onnx.symbolic_opset11torch.onnx.symbolic_opset13)rt   r   r   rl   r  s        r6   _unbind_helperr    s.    ww|5	
B66!3))r?   c                P    U R                   S::  a  SSKJn  OSSKJn  U" XX#U5      $ )NrR  r   scatter)r5  r6  r  r  )rt   r   r   r   srcr  s         r6   _scatter_helperr    s$    ww"}6 	81C,,r?   c                    U R                   S::  a  U R                  SUS/U-  X2S9nO6SSKJn  U R                  S[        R
                  " S/U-  5      S9nU" XXcUS	9nUS:  a  U$ U/$ )
Nr  Splitrk   )split_ir'  rW  r   )splitr   r   )rl   )r5  r   r  r  rK   r   )rt   r   repsr   	split_outr  repeatss          r6   _repeat_interleave_split_helperr    sn    ww"}DD$d
3DU	5$$z5<<d
+C$D!7$?	q91yk1r?   c                &   SSK JnJn  [        U5      (       d$  U R	                  S[
        R                  " U5      S9n[        U5      n[        US5      n[        U5      S:X  a5  U R	                  SX R	                  S[
        R                  " S/5      S95      nU" XUS-   5      nU(       aD  [
        R                  " [        U5      [
        R                  S9n	XyUS-   '   U R	                  SU	S9n
OU R	                  S	U" XS-   S5      U R	                  S[
        R                  " [        U5      5      S9U R	                  S
U R	                  S[
        R                  " S/5      S9USS95      n	U" X	SS5      n
U R	                  SX5      nU" XX3S-   5      $ )Nr   )flatten	unsqueezer   r   r   r  rk   rJ  OneHotr  r&  Tile)r6  r  r  r   r   rK   r(  rQ   rH   r   r   r  rY  )rt   r   r  r   r  r  const_repeatsr  
unsqueezedonehotrepeats_per_dimtileds               r6   -_repeat_interleave_single_value_repeat_helperr    sx    >g$$z5+;+;G+D$E&w/MGS)D  A%$$y'44
ELLRSQTDU4+VW 1C!G,J ,Z8LsQw$$z6$: aq!$DDELL1A*1M$N   DD!$$z5<<3D$EwWX  	
 "!Q2DD5E1S'**r?   c                T   S nUb   [        U5      (       am  [        U5      (       a]  U" X!U/5      (       a  [        R                  R                  nO`[        R                  R                  [        R                  " 5       5      nO-[        U[        5      (       d   e[        R                  " U5      nU(       a  U R                  SX&R                  5       S9OS nU(       a  U R                  SXR                  5       S9OS nU(       a  U R                  SX6R                  5       S9OS nXaX#4$ )Nc                   U  H{  n[         R                  R                  U[         R                  R                  5      nU[         R                  R                  :w  d  M[  U[         R                  R                  :w  d  M{    g   g)NFT)r   r   r   r   r*  )scalarsrL  r   s      r6   _is_all_integral-_arange_cast_helper.<locals>._is_all_integral  se    F%33>>11;;K {88>>>;#<#<#F#FF  r?   r  r   )r&   r   r   r   r*  r  rK   get_default_dtyper9   r+   r   r"  )rt   endstartstepr  r  r   s          r6   _arange_cast_helperr    s    
  }5))huooU.//%3399K%33>>'')K %%%%%!//6AFADD%:%:%<D=DE=@!$$vs!6!6!8$
9dC?C144#8#8#:4;DU((r?   c                N    U R                   S::  a  SSKJn  OSSKJn  U" U /UQ76 $ )NrR  r   )arange)r5  r6  r  r  )rt   ru   r  s      r6   _arange_helperr    s#    ww"}56!dr?   c           
         U R                  SU5      nSSKJn  U" XU R                  S[        R                  " S/5      S9U5      $ )NrQ  r   )selectr   r   )r   r6  r   rK   r   )rt   r   r   
full_shaper   s        r6   _size_helperr  #  s<    gt$J1!j%,,s:K!LcRRr?   c           
        SSK Jn  U R                  S::  a  SSK Jn  OSSKJn  UR                  5       R                  5       c  [        SS5      $ UR                  5       R                  5       n[        US5      nUS:  a  Xv-  n[        X[        U5       Vs/ s H  oU:w  d  M
  UPM     sn5      n	U" X R                  SU5      S[        XS/5      U R                  SU5      5      n
U" X	U
S 5      nX4$ s  snf )	Nr   )expandrR  r  
index_fillzinput rank not accessibler   rQ  )r6  r  r5  r  r  r   r   r   r7   ry  r  r   )rt   r   r   r   r  r  self_dim	dim_valuer   unsqueezed_indexexpanded_index_shapeexpanded_indexs               r6   _index_fill_reshape_helperr  *  s     2ww"}6 	8yy{ l,GHHyy{ H3$I1}	(	eHo@oi1o@ #	44#4QaS#A144QVCW A1EtLN// As   	C3#C3c                ,   [        US5      n[        U5      (       d$  U R                  S[        R                  " U5      S9nU R
                  S::  a4  US:X  a  [        S[        R                  SU5        U R                  SX5      $ U R                  SXUS	9$ )
Nr   r   r   rt  rk   zReshape with allowzero=1   r  )allowzero_i)	rH   r&   r   rK   r(  r5  r  r   r  )rt   r8  rM   	allowzeros       r6   r)  r)  M  s    UD)EUZ)9)9%)@Aww"}>#*G,M,MrSX ttIu,,ttIutCCr?   c                   SSK Jn  [        US5      n[        US5      nUb  [        U5      (       an  Uc  [        R
                  " SU5      e[        R                  " S/U-  [        R                  R                  U5      R                  5       S9n	U R                  SU	S9nUb  [        U5      (       an  Uc  [        R
                  " SU5      e[        R                  " S	/U-  [        R                  R                  U5      R                  5       S9n
U R                  SU
S9nUb#  [        U5      (       d  Ub  [        U5      (       a  Ub  Uc   e[        U UU R                  S[        R                  " XxS
/[        R                  S9S95      nU R                  SU/ SQS9nU" U UU R                  S[        R                  " SS/[        R                  S9S9SS5      u  pTX#XE4$ )Nr   )	_var_meanrk   z@Unsupported: ONNX export of batch_norm for unknown channel size.r  rJ  r   r   g        r   	Transpose)r   rU  rk   )perm_iF)r6  r  r   r   r   r/   rK   r   r   r   r   r  r   r)  rY  )rt   r8  weightbiasrunning_meanrunning_varr  
batch_sizechannel_sizeweight_value
bias_value
reshape_intrans_ins                r6   _batchnorm_helperr  [  s    5%eQ/J'q1L~&))++R  ||EL ++66u=CCE
 j,7|x~~++R  \\EL ++66u=CCE

 ttJ
t3 	L!!K  %,*BBB$DDj%C5;;W  

 44Z	4B$-DDU\\1a&%LDM%
! 22r?   c                    U(       a.  UR                  5       R                  5       S:w  a  [        US5        [        U " U5      5      $ )Nr   divisor_override)r'   r)   r   rZ   )tuple_fnpaddingkernel_sizestrider   r  s         r6   _avgpool_helperr%    s=     ,11388:>NNt/0'"##r?   c                ~   [         R                  [        R                  R                  :X  a  gU (       a  [        R                  R
                  nO[        R                  R                  nU[         R                  :X  a  gS[        U 5       3n[        R                  " S[         R                   SU SU SU S3	5        g)zMWarns the user if the model's training mode and the export mode do not agree.Nztrain=zONNX export mode is set to z, but operator 'z' is set to z. Exporting with r  )
r   training_moder   TrainingModePRESERVETRAININGEVALr-   rn  ro  )op_train_moder   op_mode_enumop_mode_texts       r6   check_training_moder/    s     4 4 = ==++44++00w,,,D/01L MM
%g&;&;%<<LWI V!N"3L>	Dr?   c           	        U R                  SU5      n[        XS/S/U/S9nX`R                  S[        R                  " S/[        R                  S9S9/nX4S-
  :  aG  [        XS/US-   /U/S9nUU R                  S[        R                  " S/[        R                  S9S9U/nU R                   " S	/UQ7S
S06n	SSKJn
  U
" XU	5      $ )NrQ  r   )r9  r:  r;  r   r   rJ  r   rk   r  r'  )_reshape_from_tensor)r   r?  rK   r   rx  r6  r1  )rt   r8  	start_dimend_dimr   r  slice1slicesslice3final_shaper1  s              r6   _flatten_helperr8    s    gu%J1sA3i[QFdd:u||RD

/SdTUFqWq[M
 DDU\\2$ejj%IDJ
 $$x3&33K?+66r?   c                r    Uc  g[        U 5      (       a#  U R                  5       R                  5       S:w  a  gg)NFr!   Trf   )split_size_or_sizesrl   s     r6   _is_split_staticr;    s8    %&&$$&++-1AAr?   c                    U R                  S5      nUR                  [        R                  R	                  5       5        U$ )Nr   )r   setTyper   OptionalTypeofTensor)rt   ns     r6   r  r    s/    	AIIboo&&()Hr?   c                   ^ [        T5      nUb4  [        U4S j[        U5       5       5      (       a  U R                  UTSS9$ U R                  UTSS9$ )Nc              3  B   >#    U  H  n[        TU5      S :H  v   M     g7f)r   N)r   )r   r   r   s     r6   r   *_handle_reduce_dim_none.<locals>.<genexpr>  s!       4?qT1%*Ks   rk   r  r   )r   r   r  r   )rt   r   r   r  s    `  r6   _handle_reduce_dim_nonerE    s\    D!DC  49$K   
 ttGTat0044!4,,r?   c                2   [        U5      nUSS u  pEn[        U5      S:  a  US   OSn[        USS5      n[        R                  R                  U5      n	Uc.  U	b  U	R                  5       nO[        R                  R                  nU R                  SXBS9n
U R                  SU[        R                  R                  S9nU R                  SXbS9nUb1  [        R                  S:  a  [        S	[        R                  SS
U5        U R                  S	XXhS9UUU4$ )aj  Appends to graph `g` ONNX nodes that dequantizes `qtensor` into `tensor`.

Args:
    g: Graph, the ONNX IR graph that is under construction.
    qtensor: torch._C.Value, either a tuple of (quantized_tensor, scale, zero_point)
        for per tensor quantization, or
        (quantized_tensor, scale, zero_point, axis) for per channel quantization,
        representing the quantized tensor.
    qdtype: torch.onnx.TensorProtoDataType default None, if not None, represents the
        data type of quantized tensor. It must be either
        torch.onnx.TensorProtoDataType.UINT8 or torch.onnx.TensorProtoDataType.INT8.
Nr_   r`   r   axisr  r   rt  DequantizeLinear Attribute axis is not supported.r&  )rd   rb   rR   r   r   r   r"  r   r,  UINT8r   rA  r   r  r  )rt   qtensorqdtypeunpacked_qtensorsr   r   r   rG  r'  input_qdtyper"   s              r6   r   r     s$   " 19 1"1 5F:#&'8#9Q#>QDDc6*F,,77?L~#!++-F0066FDDD-EDDW%@%@%F%FDGEfj6Jg??"D(--.	
 	
zI	 r?   c                x   UbA  [        U5      (       d1  [        R                  S:  a  [        S[        R                  SSU5        Uc   e[        R
                  R                  U[        R
                  R                  5      [        R
                  R                  :w  a)  U R                  SU[        R                  R                  S9nUc   e[        R
                  R                  U[        R
                  R                  5      [        R
                  R                  [        R
                  R                  1;  a)  U R                  SU[        R                  R                  S9nU R                  SUUU[        USS5      S9nXRU/nUb!  [        U5      (       d  UR                  U5        U R                  " S	/UQ76 $ )
aA  Appends to graph `g` ONNX nodes that quantizes `tensor` based on `scale`, `zero_point` and `axis`.

Args:
    g: Graph, the ONNX IR graph that is under construction.
    tensor: torch._C.Value, representing the tensor to be quantized.
    scale: torch._C.Value, quantized scale.
    zero_point: torch._C.Value, quantized zero point.
    axis: Optional[torch._C.Value] default None, if None, represents per tensor quantization.
        Otherwise, represents per channel quantization, along given axis.

Returns:
    A TupleConstruct storing information of the quantized tensor.
rt  QuantizeLinearrI  r  r   r   rG  r&  r   )r   r   r  r  r   r   r   r   rA  r   r   r,  rJ  INT8rR   r   )rt   r   r   r   rG  r   ru   s          r6   r   r     s   * 	--2(--.	
 !!,,UK4M4M4W4WX$$**	+ VU)D)D)J)JK!!!  ++K--77 	!!''!!&& TT&*73N3N3T3TTU
TT$V,  F :&DD44&...r?   c           	        U R                  SX25      nU R                  SU5      nU R                  SU[        R                  " S/[        R                  S9S9nU R                  SU R                  SX5      [        R
                  R                  S	9n/ n	Ub!  [        U5      (       d  U	R                  U5        U R                   " S
XU/U	Q76 $ )aT  In PyTorch, bias is float and is quantized to int32 implicitly inside the quantized ATen op kernel.
In ONNX we need to make the quantization explicit because operators expect all of their inputs to be quantized.
Since int32 is not a supported output type by ONNX operator `QuantizeLinear`, quantization is exported using
regular operators.
MulrQ  r  r   rJ  r   r  r  r   r   )	r   rK   r   r+   r   r,  INT32r   r   )
rt   r  input_scaleweight_scalerG  
bias_scalebias_scale_shapebias_zero_pointq_bias	axis_argss
             r6   requantize_bias_helperr\  W  s     e\7JttGZ0dd+U\\1#UYY5W  O TTUD-G4O4O4U4U  F I44&OXiXXr?   c                   ^ U (       d   e[         R                  R                  U S   5      m[        U4S jU  5       5      nU$ )Nr   c              3  h   >#    U  H'  n[         R                  R                  U5      T:H  v   M)     g 7frE   )r   r   r   )r   elem
base_dtypes     r6   r   'args_have_same_dtype.<locals>.<genexpr>p  s*      MQT!!,,T2j@Ts   /2)r   r   r   all)ru   has_same_dtyper`  s     @r6   args_have_same_dtyperd  m  sC    K4**55d1g>J MQ N r?   c           
        UR                  SS5      nUR                  S[        R                  R                  5      n[	        U5      n[        R                  R                  US   5      n[        US   5      (       + =(       a    USL =(       d    [        R                  U:  nU(       a  U Hz  n	U	R                  5       (       d  M  [        R                  R                  U	5      n
X:w  d  M@  [        R                  " SU SUR                  5        SU
R                  5        3U	5      e   [        U5       HO  u  pU	R                  5       (       d  M  [        U	5      (       a  M.  U R                  SU	UR                  5       S	9Xk'   MQ     U R                  " U/UQ70 UD6nU(       a  U R                  SXR                  5       S	9nU$ )
a  Some PyTorch operators (e.g., Clip/Min/ReLU/Pad) are super set of ONNX in terms of data types.
This function maximizes the exportability of PyTorch-ONNX by allowing ONNX-unsupported PyTorch
operator data type. For example, `Cast<int>(Clip<float>(Cast<float>(INPUT)))` can be used to mimic
`Clip<int>(INPUT)` (opset version < 12).

Args:
    g (torch._C.Graph): graph to write the ONNX representation into.
    op_name (str): operator name in ONNX.
    *args (tuple): operands to the operator.
    **kwargs (dict): attributes to the operator along with "opset_before" (optional, None by default)
        indicating the smallest opset version to trigger such casting behavior and "target_float_t"
        (optional, torch.onnx.JitScalarType.FLOAT by default) indicating the data type of internal operator.

Returns:
    Optional[torch._C.Value, Tuple[torch._C.Value, ...]]: output(s) of the operator.
opset_beforeNtarget_float_tr   z
Inputs of z must have same dtype.Got z and r  r   )popr   r   rA  rT   r   rE  r   r  isCompleteTensorr   r/   r   r   r   r"  )rt   r   ru   rv   rf  rg  r0   dtype_0require_castr8  input_scalar_typer   r   s                r6   _op_with_optional_float_castrm  v  s   " ::nd3LZZ 0+2K2K2Q2QRN$ZF''226!9=GfQi(( P A AL P  E%%''$/$=$=$H$H$O!$/ 33$WI .&2245U;L;X;X;Z:[] 	  "&)HA%%''uDD'113 ! 	 * 44+&+F+DttFD'8'8':t;Kr?   c                `   [         R                  R                  U[         R                  R                  5      nU[         R                  R                  :w  aW  [	        U5      (       dG  U[         R                  R
                  :w  a)  U R                  SU[        R                  R
                  S9nU$ r  )	r   r   r   r   rE  r*  r   r   r,  )rt   r   r   s      r6   _maybe_cast_reduce_op_inputro    s    ++66k''11K k//999 d||{/H/H/N/N N447+F+F+L+L4MDKr?   c                    ^ ^ U U4S jnU$ )z_Returns a decorator that calls the decorated (higher-order) function with the given parameters.c                   > U " T0 TD6$ rE   rJ   )r~   ru   rv   s    r6   _apply_apply_params.<locals>._apply  s    4"6""r?   rJ   )ru   rv   rr  s   `` r6   _apply_paramsrt    s    # Mr?   c                   ^ ^ SUU 4S jjnU$ )Nc                  > [        X5      nUb  US:X  a  [        XT5      $ [        USS5      nU R                  S:  a4  T(       a  SOSn[        X$S5      nT(       a  UOU/nU R	                  TXUS9$ [        U5      (       a  UnOkT(       a2  U R	                  S[        R                  " U[        R                  S	9S
9nO2U R	                  S[        R                  " U/[        R                  S	9S
9nU R	                  TXUS9$ )NrJ   r   r     r   r   r  r   rJ  r   rD  )	ro  rE  rR   r5  r   r&   rK   r   rx  )	rt   r   r   r  r1   dim_listr9  allow_multi_dim_supportonnx_op_names	          r6   symbolic,_reduce_op_symbolic_helper.<locals>.symbolic  s    *13;#) +1LAA !#y9Gww|6tC E2"93uttL$GtTTS>>D. tt&S

0S  $    !tt&cU%**0U  $   ttL$tIIr?   NNrJ   )rz  ry  r{  s   `` r6   _reduce_op_symbolic_helperr~    s    J J: Or?   c                F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                   > T" U /UQ76 nU H2  nUR                   n[        U5      [        U5      :X  d  M)  U" U /UQ76 s  $    [        STR                   3S[        U5       S35      $ )Nzaten::zwith z
 arguments)r   rb   r   rm   )rt   ru   	overloadsoverloadr}   r~   s        r6   r   '_overload_by_arg_count.<locals>.wrapper  sm    qL4L	!H&77O?#s4y0)D)) " r{{m4c$i[
6STTr?   r   )r~   r   s   ` r6   _overload_by_arg_countr    s'    __RU U Nr?   c                B   ^^^ [        U TS9m[        UUU4S j5       nU$ )N)ry  c                   > [        S5      [        SS5      UU4S j5       5       nT(       a  SOSn[        S5      [        SUSS5      UU4S j5       5       nX54$ )NTr   r   c                  > S nUR                  5       R                  5       S:X  aB  [        USS5      n[        R                  " U5      R                  5       nU R                  SXS9nO/UR                  5       R                  5       S:w  a  [        TSU5      $ T" X5      nUbB  [        R                  R                  U5      R                  5       nXS:w  a  U R                  SXCS9nU$ Nr!   r   r  r  r   r   	r'   r)   rR   r   r   r"  r   r   r   )rt   r   r  
dtype_onnxresultresult_dtype_onnxr  r{  s         r6   reduce_nodim?_reduce_with_dtype_helper.<locals>.reduce.<locals>.reduce_nodim  s     Jzz|  "&66"5#w7(66u=GGI
ttFDt:""$(88%dGU;;a&F%$/$=$=$H$H%)+ " %2TT&&TBFMr?   r   r   c                  > S nUR                  5       R                  5       S:X  aB  [        USS5      n[        R                  " U5      R                  5       nU R                  SXS9nO/UR                  5       R                  5       S:w  a  [        TSU5      $ T	" XX#5      nUbB  [        R                  R                  U5      R                  5       nXu:w  a  U R                  SXeS9nU$ r  r  )
rt   r   r   r  r  r  r  r  r  r{  s
           r6   
reduce_dim=_reduce_with_dtype_helper.<locals>.reduce.<locals>.reduce_dim  s     Jzz|  "&66"5#w7(66u=GGI
ttFDt:""$(88%dGU;;as4F%$/$=$=$H$H%)+ " %2TT&&TBFMr?   )r   r   )	rt   ru   rv   r  dim_descr  ry  r  r{  s	         r6   reduce)_reduce_with_dtype_helper.<locals>.reduce  si    			C	 	 
! 
	" 34			C3	/	 
0 
	" ''r?   )r~  r  )rq  r  ry  r  r{  s    `` @r6   _reduce_with_dtype_helperr    s1     *)@H )( )(V Mr?   c                v   Uc  Uc  U R                  SUSS9$ Uc  [        U SXSS9$ [        USS5      n[        USS	5      nU R                  S
:  a  U R                  SX/US9nOCU R                  S[        R
                  " U/[        R                  S9S9nU R                  SXUS9nU R                  SXUS9nXW4$ )N	ReduceMaxr   rD  Maxr  rf  r   r  r   rw  r  r   rJ  r   ArgMaxr  r   rm  rR   r5  rK   r   rx  )rt   r   dim_or_yr  r   maxr9  indicess           r6   _max_helperr  $      GOttK!t44+AudSUVV Wc953.77R<$${D7$KC44
ELL#ejj,Q4RD$${D7$CC$$xg$F|r?   c                v   Uc  Uc  U R                  SUSS9$ Uc  [        U SXSS9$ [        USS5      n[        USS	5      nU R                  S
:  a  U R                  SX/US9nOCU R                  S[        R
                  " U/[        R                  S9S9nU R                  SXUS9nU R                  SXUS9nXW4$ )N	ReduceMinr   rD  Minr  r  r   r  r   rw  r  r   rJ  r   ArgMinr  r  )rt   r   r  r  r   minr9  r  s           r6   _min_helperr  8  r  r?   c                H    U R                  SU5      nU R                  SUSS9$ )NrQ  
ReduceProdr   rD  )r   )rt   r   rM   s      r6   _numel_helperr  L  s'    DD$E44e422r?   r   r   r   c           
     ~   U R                   S:  Gas  Uc  U R                  SUSS9nUn[        X5      nOyU R                  SXUS9nU R                  SXSS9nU R                  SU5      nU R                  SUU R                  S	[        R                  " U5      S
9SS9nU R                  SUSS9nU R                  SX5      n	U R                  SX5      n
Uc  SOUnU R                  SXUS9nUc  SnUS:w  a  U R                  SU[
        R                  R                  S9nU R                  S	[        R                  " U[        R                  S9S
9nU R                  SX5      nU R                  SXR                  SX}5      5      nX4$ S nUc  U R                  SUSS9nUn[        X5      nOU R                  S	[        R                  " U[        R                  S9S
9nU R                  SXUS9nU R                  SXSS9nU R                  SU5      nU R                  SUU R                  S	[        R                  " U5      S
9SS9nU R                  SUSS9nU R                  SX5      n	U R                  SX5      n
Uc  SOUnUc  U R                  SXS9nOU R                  SXUS9nUc  SnUS:w  a  U R                  SU[
        R                  R                  S9nU R                  S	[        R                  " U[        R                  S9S
9nU R                  SX5      nU R                  SXR                  SX}5      5      nX4$ )Nrw  
ReduceMeanr   rD  r  rk   rQ  r%  r   r   r&  r  SubrS  r  r   rJ  r  )
r5  r   r  rK   r   r   r,  rA  r,   rx  )rt   r8  r   
correctionr  meant_meannum_elementsredudced_dimssub_vsqr_subkeepdim_meanvaronemulr9  s                   r6   _var_mean_helperr  Q  s:   ww|;44e4:DF(2L44eG4LDTT,aTHFDD%0MDDZc):;	 ! M 44m4JLUE*$$ue+KqWdd<dNJ?447+F+F+L+L   L $$z5<<
%+++V$WC$$uc0C$$uc44|#ABCy;44e4:DF(2L44
ELLEJJ,O4PD44eg4FDTT,TBFDD%0MDDZc):;	 ! M 44m4JLUE*$$ue+KqW<$$|W$FC$$|W|$LCJ?447+F+F+L+L   L $$z5<<
%+++V$WC$$uc0C$$uc44|#ABCyr?   c
                   U(       a   [         R                  (       a  [        S5      $ U	b  U	S:  a  [        S5      eU R	                  S[
        R                  " S5      S9n
U R	                  SU
[        R                  R                  S9n
U R	                  S[
        R                  " S/5      S9n[        U [        XU R	                  S[
        R                  " S5      S95      S/5      nU(       d  X</nU R                  " S	/UQ7S
S06n[        XS/S/[        R                  /S/S9n[        XS/S/[        R                  /S/S9n[        XU R	                  S[
        R                  " S5      S95      n[        R                   " U SXSS9u  nu  nnUR"                  n[$        R&                  " U5      n[$        R&                  " U5        UR	                  SUUSS9nUR	                  SUUSS9n[        UUS/5      n[        UUS/5      nUR	                  SUUUU5      nUR	                  SUUSS9n[)        U5      (       d6  UR	                  SUUUU5      n[        UUS/5      nUR	                  SUU5      nUS:X  a  [+        UUS/SS9nOUS:X  ai  UR,                  S:  a  UR	                  SUS/SS9nOUR	                  S[
        R                  " S/[
        R.                  S9S9nUR	                  SUUSS9nOhUR,                  S:  a  UR	                  SUS/SS9nODUR	                  S[
        R                  " S/[
        R.                  S9S9nUR	                  SUUSS9nUR	                  SU
[        R                  R                  S9n[$        R0                  " UU5        [$        R0                  " UU5        UR3                  5       R5                  5       S S S 4$ )Nz7embedding_bag with scale_grad_by_freq for training moder   zembedding_bag with padding_idxr   rk   r   r  r   r  r'  )r9  r:  r;  r<  Loop)n_blocksr%  r&  SlicerS  r  rw  r  rJ  rD  r  )r   export_trainingr   RuntimeErrorr   rK   r   r   r,  rG  ry  r  r?  r  r  r   add_op_with_blocksblockr   _add_input_to_blockr   r  r5  rx  _add_output_to_blockr'   r   )rt   embedding_matrixr  r  scale_grad_by_freqr  sparseper_sample_weightsinclude_last_offsetpadding_idxloop_conditionzeroindices_lenoffsets_startsoffsets_endsloop_lenlooploop_contextr   
loop_blockblock_input_iterindices_startindices_endindices_row
embeddingsper_sample_weights_rowr9  cond_outs                               r6   _embedding_bag_helperr    s    g55 E
 	
 ;!#3;<<TT*ell1oT>NTT&.w7R7R7W7WTXN44
ELL!$546D#	Qj%,,q/!JK	
K
 ($$x4'4!4
 #	!aS}QCN !	!aS}QCL AQTT*ellSToT-VWH(;;	68a D/<1 ##J 00<	j) OO."21 $ M //(L:JST/UK%lMA3GM#L+sCK//'7M;PTUK+;[QRSJ&''!-'T"
 "301#"
 "__UJ8NO
qy&*aSQ

 
"%j! ) J  ??ELL!EJJ$G # D &z4TUVJ"%Z ) J  ??ELL!EJJ$G # D &j$STUJW%@%@%E%E  H 
z84	z:6 99;tT11r?   c                $   S n[        U5      (       a  [        XS/5      nSnOAU R                  S:  a1  U R                  S[        R
                  " U[        R                  S9S9nU[        R                  :X  a|  U R                  S:  a#  U R                  SU R                  SU5      X4S	9nGOUc#  U R                  SU R                  SU5      US
9nGOU R                  SU R                  SU5      XdS
9nGOU[        R                  * :X  a|  U R                  S:  a#  U R                  SU R                  SU5      X4S	9nGOUc#  U R                  SU R                  SU5      US
9nGO^U R                  SU R                  SU5      XdS
9nGO;US:X  a  U R                  S:  a  [        SSSSU5      $ Uc?  [        U UU R                  S[        R
                  " S/[        R                  S9S95      nSnU R                  SU R                  SXR                  S[        R                  " S/5      S95      5      nU R                  SU[        R                  R                  U5      R                  5       S9n[!        XX4S	9$ US:X  aH  U R                  S:  a  [#        S5      " XX4S9nGO!Uc  [#        S5      " XUS9nGO[#        S5      " XXdS9nOUS:X  aF  U R                  S:  a  [#        S5      " XX4S9nOUc  [#        S5      " XUS9nO[#        S5      " XXdS9nOU R                  S[        R
                  " U[        R$                  S9S9n	[!        X R                  SU R                  SU5      U	5      X4S	9nU R                  SUU R                  SU R                  S[        R
                  " S[        R$                  S9S9U	5      5      n[        U5      (       d@  ['        USS5      nU R                  SU[        R                  " U5      R                  5       S9nU$ )Nr   Frw  r   rJ  r   r  Absr  rD  r  r   r  linalg_vector_normr3  zord=0 not supportedNotEqualr  r   rk   ReduceL1)r   r  )r  rU  ReduceL2Powr  r   r  )r   r)  r5  r   rK   r   rx  mathinfr  rY  r(  r   r   r   r"  r  r~  r  rR   )
rt   r   ordr   r  r  r9  r  cond_opord_ops
             r6   _linalg_vector_norm_helperr    s    D}}q-	
BttJS

(KtL
dhh77R<TTQTT%.s  F |k144t+<Qk144t+<dW			77R<TTQTT%.s  F |k144t+<Qk144t+<dW	77R<3$a-BD  {&DDU\\2$ekk-RDS
  ddWdDDU=M=Mqc=RD$STG dd ..99$?IIK  G
 %QPP	77R</
;SF |3J?W 4J?T 
77R</
;SF |3J?W 4J?T j%,,s%--*PQ"ttE144t,f5c
 DDZau}})MN
 E??5#w/ff;+D+DU+K+U+U+WXMr?   )ByteCharDoubleFloatHalfIntLongShortBoolComplexFloatComplexDoubleBFloat16	Undefinedr  r  r  r  r  r  r  r  r  r  r  QInt8QUInt8QInt32r  )uint8_tint8_trK  r,   halfr+   int64_tint16_tr-   	complex64
complex128qint8quint8qint32bfloat16)r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  zset[int]_quantized_opsr}  )r1   _ValueDescriptorr2   
str | Noner3   r  )r'   z_C.Noder=   r.   )r"   _C.Value)r"   z2_C.Value | torch.Tensor | Number | Sequence | NonerG   r  )rU   r  r   zlist[_C.Value])r[   r  r   ztuple[_C.Value, ...])rU   r   r   r-   )r}   r  r   zRCallable[[Callable[_Concatenate[_U, _P], _T]], Callable[_Concatenate[_U, _P], _T]])
r   r-   r   zfloat | Noner   
int | Noner   r-   r   z.Callable[[Callable[_P, _T]], Callable[_P, _T]])r   r   r   zNumber | None)r   r   r   r-   )r"   r   r   r-   )r   r  r   r-   )r   z
_C.JitTyper   z_C.ListType | None)r   r  r   r  )T)r   r  r   r-   )r   r  r   r+   r   r  )r   r  r   r  rE   )r   r.   r   r.   r"   _C.Value | Noner   None)r   r.   r"   r  r   r   )
r   r.   r  r+   r  r+   r"   r  r   r   )r   r.   r  r+   r  r+   r
  r.   r"   r  r   r   )r  r.   )r   z _type_utils.JitScalarType | None)r   _type_utils.JitScalarType)rt   jit_utils.GraphContextr   r
  )rt   r  )r   r-   )TN)TFN)Nrk   r   )
rt   r  r8  torch._C.Valuer   r  r  r-   r   r.   )NNN)rt   r  r   zStuple[_type_utils.JitScalarType, _C.Value | None, _C.Value | None, _C.Value | None])r   )r!  zCallable[[Any], Sequence[int]]r"  zint | Sequence[int]r   ztuple[int, ...])r,  r+   r   r.   r   r	  )rt   r  rK  r  rL  z"_C_onnx.TensorProtoDataType | Noner   z4tuple[_C.Value, _C.Value, _C.Value, _C.Value | None])rt   r  r   r  r   r  r   r  rG  r  r   r  )rq  r.   r  r.   ry  r-   )rt   r  r   r  r  r,   r   zSequence[int] | Noner  r-   r  r  )
__future__r   r   rn   r  r  r   rn  r   r   r   r   r   _TypeVartyping_extensionsr	   _Concatenater
   
_ParamSpecrK   torch._C._onnxr   _onnxr   
torch.onnxr   r   r   r   torch.onnx._globalsr   torch.onnx._internalr   TYPE_CHECKINGcollections.abcr   torch.typesr   r   r   r   r  r7   r*   rC   rH   rO   rR   rW   r]   rd   rg   r   r   r   r   r   r&   rQ   r   r   r   r   r   rY   r   r   r   r   r   r   r   r  r  r  r  r  r#  r1  r?  rE  rH  rM  r^  rd  rl  rr  ry  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r)  r  r%  r/  r8  r;  r  rE  r   r   r\  rd  rm  ro  rt  r~  r  r  r  r  r  r  r  r  r,  rJ  rQ  rB  rA  FLOAT16rT  r*  INT16rG  r   r   rD  r   cast_pytorch_to_onnxscalar_name_to_pytorchuint8int8shortr+   rY  r  r,   rK  	complex32r  r  r-   r  r   r  r  scalar_type_to_pytorch_typepytorch_name_to_typescalar_type_to_onnxsetr  __annotations__rJ   r?   r6   <module>r'     s   "    
   H H R       > = ' * 
("d^d^ 
     	B
B B 	BJ#3
	=	 	#$&.WO&OWOh ! 	GGG G 	G
 4GT&V#5
/>
)5)
1,$ "	,,, , 	,
 ,. ",,, , 	,
 , ,&
;0I3< ==$*:$
( OS

*%"/"*8 NN>4 !"  F#=#=#= 
#= 	#=
 #=L@F`
`
F*-2'+'+V BF')')')TS0FD6363r
$,
$ 
$ 
$27(- 26--- /- :	-j !:/:/:/ :/ 	:/
 :/ :/| FJYY,2j	B
 >B3336:3l((3
 CsC E !EPg2g2Tjj
j 
j 
	j
 j jb ''--'',,))00((..''//&&,,''--((..'',,//9900;;++44,,66 $ ! 0 
KK	JJ	KK	II	KK	JJ	KK	LL	OO	OO		JJ	KK	LL	LL	NN! . KKJJll[[JJ99KK[[JJOO%%[[llll *   !  !"%()   $! * 5  r?   