
    ¯wg@                   ,   U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
mZmZmZ d dlmZmZ d dlZd dlmc mZ d dlmZ d dlmZmZmZmZ d dlmZ d dl m!Z! ejD                  rd d	l#m$Z$  ed
      Z% ed      Z& ed      Z'e
d   Z(	 	 d	 	 	 	 	 ddZ)ddZ*ddZ+	 	 	 	 ddZ,d Z-d Z.ddZ/ddZ0ddZ1ddZ2	 	 	 	 ddZ3dddd	 	 	 	 	 	 	 	 	 ddZ4ddZ5d Z6ddZ7ddZ8dd Z9dd!Z:dd"Z;dd#Z<dd$Z=dd%Z>dd&Z?dd'Z@dd(ZAddd)ZBdd*ZCdd+ZDddd,ZEddd-ZF	 d	 	 	 	 	 	 	 	 	 dd.ZG	 d	 	 	 	 	 	 	 	 	 	 	 dd/ZHdd0ZIdd1ZJdd2ZK	 	 	 	 dd3ZLddd4ZM	 d	 dd5ZNdd6ZOdd7ZPdd8ZQddd9ZR	 d	 dd:ZSdd;ZTd< ZUdd=ZVdd>ZW	 	 	 d	 dd?ZXdd@ZYddAZZddBZ[ddCZ\	 	 ddDZ]	 	 	 	 	 	 	 	 	 	 ddEZ^dF Z_	 	 ddGZ`ddHZaddIZbddJZc	 	 ddKZd	 d	 	 	 ddLZeddMZfddNZgddOZhdddPZi	 	 ddQZj	 	 	 	 	 	 ddRZkddSZlddTZmdU ZndV ZoddWZp	 d	 	 	 	 	 	 	 ddXZq	 d	 	 	 	 	 	 	 	 	 	 	 ddYZr	 d	 ddZZsd[ Ztdd\Zudd]Zvd^ Zwdd_Zxd` Zy	 d	 	 	 	 	 ddaZzdddbZ{dddcZ|dddZ} e3dedfdgdg      ddh       Z~	 	 ddiZ	 	 	 	 	 	 	 	 	 	 	 	 ddjZej                  j                  ej                  j                  ej                  j                  ej                  j
                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  dkZdldmdndodpdqdrdsdtdudvdwdxdydzd{Zej"                  ej$                  ej&                  ej(                  ej*                  ej,                  ej.                  ej0                  ej2                  ej4                  ej6                  ej8                  ej:                  ej<                  ej>                  ej@                  gZej"                  ej$                  ej0                  ej.                  ej,                  ej(                  ej*                  ej&                  ej8                  ej4                  ej6                  ej:                  ej<                  ej>                  ej@                  d|Zedl   edm   eds   edq   edr   edp   edo   edn   ed}   edu   edv   edt   edm   edl   edq   edz   gZ e       Zd~ed<   y)    )annotationsN)AnyCallableLiteralNoReturnSequenceTypeVar)Concatenate	ParamSpec)_C)
_constants_type_utilserrorsutils)GLOBALS)	jit_utils)Number_T_U_P)	viisffsbstnonec           	     n   |dk(  r| S |dk(  st        |       s| S | j                         }|j                         ry |j                         dk(  rt	        |d      }|dk(  rt        |      S |dk(  rt        |      S |dk(  rt        |      S |dk(  rt        |      S |d	k(  r|S |d
k(  r|D cg c]  }t        |       c}S |dk(  r|D cg c]  }t        |       c}S t        j                  d| d| d|       |j                         dk(  r|d
k(  r|j                         D ]B  }|j                         }|j                         dk7  s't        j                  d| d| d|        | j                         j                         D cg c]%  }t        t	        |j                         d            ' c}S t        j                  d| d|       ||(t        j                  d|j                          d|       t        j                  d| d| d|j                          d|       c c}w c c}w c c}w )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           _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/onnx/symbolic_helper.py
_parse_argr7   /   sj    v~s{)E*::<Dyy{&&T7+3;x= S[?"S[>!S[x= S[OT\$,-qCF--T\&./E!H//++Gv N..2V27 
 
-	-4<[[] 	 vvx$$&*:: 333L> B))- /\]  	 @Ezz|?R?R?TU!C	!&&(G45UU++//3fA7  9,''8RH
 	

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

 eT""r?   c                    | j                         }|j                         dk7  rt        j                  d| d|       t	        |j                               S )Nr$   z;ONNX symbolic expected node type prim::ListConstruct, got 'r#   )r'   r)   r   r/   listr0   )
list_value	list_nodes     r6   _unpack_listrW      s[    !I~~00'';b"
 	

 	  "##r?   c                    | j                         }t        |       s(t        j                  d|j	                          d|       t        |j                               S )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                    | j                         }t        |       s;t        j                  d| d|j	                          dt
        j                   |       t        |j                               }t        |      dk(  st        |      dk(  sJ |S )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                ^    t        |       xr! | j                         j                         dk(  S )Nr$   r&   r'   r)   )rU   s    r6   _is_packed_listrg      s(    Z VZ__%6%;%;%=AV%VVr?   c                      	 	 	 	 d fd}|S )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                V      _         t        j                         d fd       }|S )Nc                   d}t        
      t        |      k\  s/J dt        |       dt        
       dj                   d|        	 t        j                        }t	        |j
                  j                               dd  }j                  }t        |
|      D 	cg c]  \  }}}	t        |||	|       }}}}	t        |      dk  sJ dj                   d|        t        |      dk(  rd	|v sJ dj                   d
|         | g|i |S # t        $ r d gt        |      z  }d }Y w xY wc c}	}}w )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   wrapperz.parse_args.<locals>.decorator.<locals>.wrapper   s   l 
 '3t94 >s4yk J&&)/&:%;;]^`^i^i]jjm."4''+ !4!4!67;	++ 034)/T +C8 3(G<D 
 v;!# $R[[M 2$."# 6{a!V+ ( 65#n&+
 a)$)&))-   "FSY.		
s   AD #D:D76D7)rt   r   ru   z_P.argsrv   z	_P.kwargsreturnr   )_arg_descriptors	functoolswraps)r~   r   r}   s   ` r6   	decoratorz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                      fd}|S )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                L     t        j                          fd       }|S )Nc                
   '| j                  dt        j                              }nd }'| j                  dt        j                              }nd }dt        |      t              z
  z  z   }t	        t        ||            }d }g }|D ]d  \  }	}
t        |
      r<|
j                         j                         D ]  }|j                   ||	|              M|j                   ||	|
             f t        |      s | g|i |S g }|D ]  \  }	}
 ||	|
      r,t        | |
      \  }}}}|j                  |       ||}|8|};t        |
      ri|
j                         j                         D ]6  } ||	|      st        | |      \  }}}}||}||}|j                  |       8 |j                  |
       |j                  |
         | g|i |}|J d       |J d       rt        | |||      S |S )NConstantrN   )Fc                :    | xr t        |      xr t        |      S rE   )r&   rY   )rG   r{   s     r6   _is_arg_quantizedzMquantized_args.<locals>.decorator.<locals>.wrapper.<locals>._is_arg_quantizedc  s    !QinQ9LS9QQr?   z-Bug: Scale must be set for quantized operatorz2Bug: Zero point must be set for quantized operator)oprK   tensorrb   rZ   rs   rg   r'   r0   appendanydequantize_helperreplaceAllUsesWithquantize_helper)rt   ru   rv   _scale_zero_pointarg_q_descriptors_extendeddescriptor_argsr   is_quantizedrG   r{   	arg_inputnon_quantized_argsdequantized_arg	arg_scalearg_zero_point_outputarg_q_descriptorsr~   r   r   r   s                     r6   r   z2quantized_args.<locals>.decorator.<locals>.wrapperP  sw     j%,,u2EF%dd:u||J7OdP" *;XD	C 122> *& $C(BD$IJOR L#2 L
C"3'%(XXZ%6%6%8 V	$++,=j),TUV !''(9*c(JKL |$!-d-f-- "$#2  3
C$Z5DU3EAOY '--o>~!*"*&4$S)%(XXZ%6%6%8 J	,ZC !2!Y ? / ) . !  &~)2*2.<%88IJ '--c2 '--c2A 3F 9.9&9F%V'VV%'DCD' &q&&+FFMr?   r   r   )r~   r   r   r   r   r   s   ` r6   r   z!quantized_args.<locals>.decoratorO  s(    		P	 
P	d r?   rJ   )r   r   r   r   r   s   ```` r6   quantized_argsr      s    ^Tl r?   c                v    t        | t        j                        r| j                  dk(  r| j	                         S y)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                F   t        | t        j                        r| S t        j                  j                  |t        j                  j                        }|t        j                  j                  k7  r/|j                         j                         } t        | |             S | S )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$$&,,. wtR ""Kr?   c                    | d u xs: t        | t        j                        r| j                         j	                         S dS NF)r9   r   rF   r'   r(   r   s    r6   _is_noner     s2    9U*Q2I,,.UuUr?   c                6    t        | t        j                        S rE   )r9   r   rF   r   s    r6   r&   r&     s    a""r?   c                ^    t        |        xs  | j                         j                         dv S )N>   r!   prim::Constantrf   rB   s    r6   rQ   rQ     s2     5::<#4#4#6 ; $ r?   c                x    | j                         j                  t        j                  j	                               S rE   )typeisSubtypeOfr   
TensorTypegetr   s    r6   
_is_tensorr     s&    668 1 1 344r?   c                <    t        | t        j                        r| S y rE   )r9   r   ListType)jit_types    r6   _as_list_typer     s    (BKK(r?   c                8    t        | j                               d uS rE   )r   r   r   s    r6   _is_listr     s    "$..r?   c                    t        | j                               }|yt        |j                         t        j
                        S r   )r   r   r9   getElementTyper   r   r   x_types     r6   _is_tensor_listr     s4    1668$F~f++-r}}==r?   c                    t        | j                               }|yt        j                  j	                  |       }|j                         S )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    | j                         j                         dk(  S )Nprim::TupleConstructrA   r   s    r6   rY   rY     s    668==?444r?   c                &   t        |       sJ t        j                  j                  | t        j                  j                        t        j                  j
                  t        j                  j                  t        j                  j                  hv S rE   )r&   r   r   r   r   	COMPLEX32	COMPLEX64
COMPLEX128r   s    r6   is_complex_valuer     sq    Q<<$$//	;$$.. 	!!++!!++!!,,
 r?   c                    t        |       r| j                         y | j                         }t        j                  t        j
                  |      }|j                         S rE   )r   r   typingcastr   r   dimr   s     r6   _get_tensor_rankr     sB    a=AFFH,VVXF[[/F::<r?   c                    t        |       r| j                         y | j                         }t        j                  t        j
                  |      }|r|j                         S |j                         S rE   )r   r   r   r   r   r   varyingSizessizes)r   allow_nonstaticr   s      r6   _get_tensor_sizesr     sX    a=AFFH,VVXF[[/F ""$$ <<>r?   c                *    t        |       }|r||   S d S rE   )r   )r   r   r   s      r6   _get_tensor_dim_sizer     s    a E5:(D(r?   c                    |dk(  rt        |       }|J ||z   S |.t        |       }|J t        |      D ]  \  }}|	|dk(  s|c S  |S )Nr_   )r   r   	enumerate)r   r   tensor_rankr   indexsizes         r6   _get_dim_for_crossr     su    
by&q)&&&[  
{!!$   $U+ 	KE4DAI	 Jr?   c                ~    t         j                  t        j                  j                  k(  rt        |  d| |       y y )Nz, )r   operator_export_type_C_onnxOperatorExportTypesONNX_onnx_unsupported)r   msgr"   s      r6   _unimplementedr   -  s6    ##w'B'B'G'GGRD3%.%0 Hr?   c                    d|  dt         j                   }t        |t        j                        rt        j                  ||      t        j                  |      )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   3  sa    
/y 9(BBC	E 
 %"''
 	
 
"
"7
++r?   c                    d|  d| d| d}t        |t        j                        rt        j                  ||      t        j
                  |      )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   A  se     'wiz- I$$3#4A	7  %"''
 	
 
"
"7
++r?   c           	         d|  d| d| d| d	}t        |t        j                        rt        j                  ||      t        j
                  |      )Nr   r   z. r   r   r   )r   r   r   reasonr"   r   s         r6    _onnx_opset_unsupported_detailedr  S  sm     'wi 0r&)D_DUUV	X  %"''
 	
 
"
"7
++r?   c                      fd}|S )Nc                 X    t        j                  d dt        j                   d      )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_fnz)_block_list_in_opset.<locals>.symbolic_fng  s6    &&$TF*O001 277
 	
r?   rJ   )r  r  s   ` r6   _block_list_in_opsetr  f  s    
 r?   c                     | D ][  }t         j                  j                  |t         j                  j                        }|t         j                  j                  k7  sY|c S  y rE   )r   r   r   r   )ru   r{   r   s      r6   _try_get_scalar_typer	  q  sY     !//::**44
 +33=== r?   c                 p   t         j                  j                  }| D cg c]  }t        |       }}t	        |      dk(  r|S t	        |      dk(  r|d   S |d   j                         }|D ]&  }t        j                  ||j                               }( t         j                  j                  |      S c c}w )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:;s%c*;I;
9~
9~|!""$I >''	1779=	>$$//	:: <s   B3c                    t         j                  j                  |t         j                  j                        |k7  r"| j	                  d||j                               S |S 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           
     h   t        |      }t        |      }t        |      s(| j                  dt	        j
                  |g            }n;|9|r7|dk(  r2t        | || j                  dt	        j
                  dg                  }t        j                  j                  |t        j                  j                        }|t        j                  j                  t        j                  j                  hvr,| j                  d|t        j                  j                        }| j                  d|||      S )	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_helperr&    s   #E*K 'I[!Z)9)9;-)HI		=>#5!$$z53C3CQC3H$IE $11<<{((22 !!''!!%%!  VU)D)D)J)JK44$c422r?   c                j    | j                   dk  rddlm}  || ||||      S ddlm}  || |||||      S )N	   r   )_slice)opsettorch.onnx.symbolic_opset9r)  torch.onnx.symbolic_opset10)rt   inputaxesstartsendssteps_slice9_slice10s           r6   _slice_helperr4    s=     	ww!|@q%vt44B5$e<<r?   c                >   t         j                  j                  | t         j                  j                        t         j                  j                  t         j                  j
                  t         j                  j                  t         j                  j                  hv S rE   )r   r   r   r   FLOATDOUBLEHALFBFLOAT16rB   s    r6   _is_fpr:    st    $$//{((22 	!!''!!((!!&&!!**	
 r?   c                    t         j                  j                  | t         j                  j                        t         j                  j                  hv S rE   )r   r   r   r   BOOLrB   s    r6   _is_boolr=    sC    $$//{((22

#
#
(
(	)* *r?   c                   t        |t        j                        rJ t        |t              r6| j	                  dt        j
                  |t        j                              S | j	                  dt        j
                  |            S )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_numberrB    s`     &%,,///&% ttJV5<<(PtQQ44
ELL$8499r?   c                f   |t        dd       | j                  d|      }| j                  d|| j                  dt        j                  |gt        j                                    }| j
                  dk  r$|st        dd	       | j                  d
|||d      S | j                  d
||||d      S )NSortOut parameter is not supportedShaper  r   r?  r   
   Ascending is not supportedTopK   r  outputs)r  	largest_irL  )r   r   rK   r   int64r*  )rt   r-  r   	decendingoutshape_	dim_size_s          r6   _sort_helperrS    s    
v?@TT'5!F	Zse5;;!GHI
 	ww"}6#?@ttFE9S!tDDttE9SIq  
 	
r?   c           
     B   |t        dd       t        |      s8| j                  dt        j                  |gt        j
                              }nt        | || j                  dt        j                  dg                  }t        |      t        j                  j                  k7  r,| j                  d|t        j                  j                        }| j                  d	k  r$|st        dd
       | j                  d|||d      S | j                  d|||||d      S )NrI  rE  r   r?  r   rk   r  r  rG  rH  rJ  rK  )r  rM  sorted_irL  )r   r&   r   rK   r   rN  r  r	  r   r   r  r   r!  r*  )rt   r-  kr   largestsortedrP  s          r6   _topk_helperrY    s     v?@Q<DDU\\1#U[[%IDJAq!$$z5<<;L$"MN"k&?&?&E&EEVQW%@%@%F%FGAww"}6#?@ttFE1S!t<<ttE1SGfVW  
 	
r?   c                `    | j                   dk  rddlm}  || ||      S ddlm}  || ||      S )N   r   )lt)r*  torch.onnx.symbolic_opset8r\  r+  )rt   r-  other_lt8_lt9s        r6   
_lt_helperra    s1    ww!|9Aue$$9Aue$$r?   c                    t         j                  dk\  rdnd}t        j                  d|z   dz   t	        t         j                        z   dz          y )NrG  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_warningrg    s]     ::b@o  MM2
		 223	47P	P
r?   c                l   t        |d         rl| j                  dk\  rI| j                  dt        j                  |t        j
                              }| j                  d||      S | j                  d||      S | j                  dk  rt        j                  d|      | j                  d||d         S )	Nr      r   r?  r   	Unsqueezeaxes_iz<Opset version must be >= 13 for Unsqueeze with dynamic axes.)rQ   r*  r   rK   r   longr   r/   )rt   r-  rl  r.  s       r6   _unsqueeze_helperrn  (  s    F1I77b=44
ELLuzz,R4SD44UD11ttKvt66ww|''JE
 	
 44UF1I..r?   c                   t        |d         rl| j                  dk\  rI| j                  dt        j                  |t        j
                              }| j                  d||      S | j                  d||      S | j                  dk  rt        j                  d|      |d   }t        |      }|J |d	kD  rt        j                  d
|      |dk(  r!t        | |dg      }| j                  d||      S | j                  d||      S )Nr   ri  r   r?  r   Squeezerk  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   r*  r   rK   r   rm  r   r/   r   rn  )rt   r-  rl  r.  axes_t	axes_ranks         r6   _squeeze_helperrs  6  s   F1I77b=44
ELLuzz,R4SD44	5$//ttIuVt44ww|''H%
 	
 AYF (I   1}''QSX
 	
 
a"1fqc2ttIuf--44	5&))r?   c                >   t        |d      }| j                  dk\  rn|rWt        |      s6| j                  dt	        j
                  |t        j                              }| j                  d||||      S | j                  d|||      S | j                  d|||      S )	Nr   ri  r   r?  r   	ReduceSum)
keepdims_inoop_with_empty_axes_irl  rv  )rH   r*  r&   r   rK   r   rm  )rt   r-  rl  rv  rw  s        r6   _reducesum_helperry  O  s     "*c2Jww"}V$V5::(N   44%'=    tt!#9	  
 	
 ttKv*tMMr?   c           	     >   t        |d      }t        |      rd}| j                  dt        j                  |t        j
                              }| j                  d|t        j                  j                        }t        | | j                  d|      d	gt        j                  g|g
      }| j                  d|t        j                  j                        }| j                  d||      }| j                  d||d	      }	|	S t        d	|      D 
cg c]J  }
|
dk  rdn@t        |||
z
            t        |j                         j                         ||
z
            z  L }}
| j                  dt        j                   |t        j
                              }	|	S c c}
w )Nr   rJ  r   r?  r   r  r  rF  r   r.  r0  r/  DivConcatr        ?)rH   r&   r   rK   onesfloat32r   r!  r6  r4  sysmaxsizeranger,   r   r   r   )rt   r-  output_sizer   offsetoffsetsdividenddivisor
scale_dimsscalesr   scales_constants               r6   _interpolate_size_to_scalesr  n  s{   ";5K$$z5::fEMM+R$S44'2M2M2S2S4TqttGU#1#S[[M6(
 $$vwW-H-H-N-N$OTT%73
hA> M 1c]

  1u {S1W:./EJJL&&(374566
 
 _EMM R  
 M
s   AFc           	     N   t        |d   d      dk7  xr t        |d          }|sy | j                  dt        j                  dt        j
                              }| j                  dt        j                  t        |d   d                  }| j                  d||d	      }|S )
Nr   r   r   r   rJ  r?  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                P    |dk(  rd }|dd  }n
|d   }|dd  }t        | |      }||fS )Nnearestr   rk   )r  )rt   moderu   align_cornersr  s        r6   _get_interpolate_attributesr    sE    yabQab1!V<F=  r?   c                   | j                  dt        j                  dt        j                              }t	        |      }t        |j                         t        j                        s||dkD  r| j                  d||d      S t        | |dg      }| j                  d|t        j                  j                  	      }t        |dz
        D cg c]  }| }} | j                   d|g|d
di}|S c c}w )Nr   rJ  r?  r   r   r}  r  r  r  r  )r   rK   r  r  r   r9   r   r   r   rn  r   r!  r6  r  )rt   scale_factorr   r  scale_factor_rankr   r  s          r6   _interpolate_get_scalesr    s    dd:uzz!5=='IdJG(6,##%r{{3%*;a*?ttHg|At>>(L1#>ttLw'B'B'H'H  
 ).cAg71,77144'=F=1=L 8s   	C4c                   t        |d      }d|v rd}d|v rd}t        |       t        |d      }t        |t              r|rt	        dd      S |j                         j                         st	        dd      S |j                         j                         }t        |      st        | ||      }||fS t        |      s{t        |      s^t        |d      j                         d	k(  }|r?t        | |d	g      }t        |d
z
        D cg c]  }| }} | j                  dg|dd	i}t        | |||      }||fS t	        dd      S c c}w )Nr   linearcubicr   interpolatezalign_corners == Truemissing input shaper   r   rJ  r}  r  z.Both size and scales are None in __interpolate)rH   rg  r9   r-   r   r   r   r   r  rg   rn  r  r   r  )	rt   r-  r   r  r  r  r   	is_scalarr   s	            r6    _interpolate_get_scales_and_moder    sX    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(D1#6&+C!Gn555qttH6t6A621eT3G
  K
 	
	 6s   	Ec           
          fd}t        |      rt         | j                  dt        j                  dg                  } ||dd      }|ro j                  d|      } j                  d|      }	 j                  d	|	t        j                  d
gt        j
                              }
 j                  d||
      }|S t        |d      } ||||      S )Nc                x    j                   dk\  rj                  | ||d      S j                  | ||      S )N   F)r  rv  select_last_index_ir  rv  )r*  r   )r-  r  rv  rt   r   s      r6   
op_wrapperz)_argmin_argmax_helper.<locals>.op_wrapper  sL    77b=44%$)    ttGU6jtIIr?   r   r   r   r   Fr  rF  ConstantOfShaperk   r?  Reshaper   )r   r  r   rK   r   rN  r7   )rt   r-  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)VY7F
S#
CeCG<<r?   c                2    t        ddd      fd       }|S )NTFc                R   t        | |      \  }}t        |      }dk(  rdn|rdnd}|| j                  d|      }t        | |dgdgdg      }| j                  d	|t        j
                  j                  
      }| j                  d||d      }| j                  dk\  rt        |       }	t        |       }
nl| j                  dt        j                  g t        j                              }	| j                  dt        j                  g t        j                              }
| j                  d||	|
||dd	      S | j                  dk\  rt        |       }	n6| j                  dt        j                  g t        j                              }	| j                  d||	||dd      S )Nr  
asymmetricr  
half_pixelrF  r   rJ  r{  r  r  r}  r  ri  r   r?  r   Resize      floor coordinate_transformation_mode_scubic_coeff_a_fmode_snearest_mode_s)r  rO   r   r4  r   r!  r  r*  "_optional_input_placeholder_tensorrK   r   r  )rt   r-  r  ru   r  r  coordinate_transformation_mode
input_sizeinput_size_beg	empty_roiempty_scalesre  s              r6   r  z(_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   re  r  s     ` r6   _interpolate_helperr    s'    D%'< (<| r?   c                d   t        |d      }d|v rd}d|v rd}t        |d      }t        |t              sdn|}|dk(  rdn|rdnd	}t        |      s| j	                  d
|      }t        | |dgdgdg      }	 t        |       xr t        |d      j                         dk(  }	|	rXt        |      }
|
t        dd      S t        | |dg      }t        |
dz
        D cg c]  }| }} | j                  dg|ddi}| j	                  d|t        j                   j"                        }| j	                  d||d      }| j$                  dk\  rt'        |       }t'        |       }nl| j	                  dt)        j*                  g t(        j,                              }| j	                  dt)        j*                  g t(        j,                              }| j	                  d|||||d|d	      S t        |      }
|
t        dd       S | j$                  dk\  rt'        |       }n6| j	                  dt)        j*                  g t(        j,                              }t/        | ||
      }| j	                  d||||d|d      S # t        $ r' t        |       }	|	st        j                  d       Y w xY wc c}w )!Nr   r  r  r   Fr  r  r  r  rF  r   rJ  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  ri  r   r?  r   r  r  r  r  zinterpolate (with scales)r  )rH   r9   r-   r   r   r4  rg   r   AttributeErrorrc  rd  r   r   rn  r  r   r!  r  r*  r  rK   r   r  r  )rt   r-  r   r  r  r  recompute_scale_factorr  r  r  rankr   r  r  r  s                  r6   __interpolate_helperr  @  s    D#&D4$$]C8M!+M4!@EmM 9 	   # D>TT'5)
"1js!aSQ


	+D11  s+//1Q6  #E*D|%=U  %Qqc2D"'q/2QD2D21442422DttFDw'B'B'H'HtIttHj$qt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(L$?tt-K!"  	
 		
i  	+D11IU	  3s   8+I: 	J-:,J*)J*c                |    | j                   dk  rddlm} n| j                   dk  rddlm} nddlm}  || |||      S )N   r   )unbindr  )r*  r+  r  torch.onnx.symbolic_opset11torch.onnx.symbolic_opset13)rt   r   r   rl   r  s        r6   _unbind_helperr    s2    ww|5	
B66!T3))r?   c                R    | j                   dk  rddlm} nddlm}  || ||||      S )NrG  r   scatter)r*  r+  r  r  )rt   r   r   r   srcr  s         r6   _scatter_helperr    s(    ww"}6 	81dC,,r?   c                    | j                   dk  r| j                  d|dg|z  ||      }n=ddlm} | j                  dt	        j
                  dg|z              } || ||||	      }|dkD  r|S |gS )
Nr  Splitrk   )split_ir  rL  r   )splitr   r   )rl   )r*  r   r  r  rK   r   )rt   r   repsr   	split_outr  repeatss          r6   _repeat_interleave_split_helperr    su    ww"}DD$d
3PTDU	5$$z5<<d
+C$D!T7C$?	q91yk1r?   c                @   ddl m}m} t        |      s&| j	                  dt        j                  |            }t        |      }t        |d      }t        |      dk(  r8| j	                  d|| j	                  dt        j                  dg                  } || ||dz         }|rJt        j                  t        |      t
        j                        }	||	|dz   <   | j	                  d|	      }
n| j	                  d	 || |dz   d      | j	                  dt        j                  t        |                  | j	                  d
| j	                  dt        j                  dg            |d            }	 || |	dd      }
| j	                  d||
      } || |||dz         S )Nr   )flatten	unsqueezer   r   r   r  rk   r?  OneHotr}  r  Tile)r+  r  r  r   r   rK   r  rQ   rH   r   r   r  rN  )rt   r   r  r   r  r  const_repeatsr  
unsqueezedonehotrepeats_per_dimtileds               r6   -_repeat_interleave_single_value_repeat_helperr    s    >g$$z5+;+;G+D$E&w/MGS)D  A%$$y'144
ELLRSQTDU4+VW 1dC!G,J ,Z8LsQw$$z6$: aq!$DDELL1A*1M$N   DD!$$z5<<3D$EwWX  	
 "!VQ2DD_5E1eS#'**r?   c                &   d }|t        |      rct        |      rX ||||g      rt        j                  j                  }nYt        j                  j                  t        j                               }n't        |t              sJ t        j                  |      }|r"| j                  d||j                               nd }|r"| j                  d||j                               nd }|r"| j                  d||j                               nd }||||fS )Nc                    | D ]w  }t         j                  j                  |t         j                  j                        }|t         j                  j                  k7  sY|t         j                  j                  k7  sw y y)NFT)r   r   r   r   r  )scalarsrA  r   s      r6   _is_all_integralz-_arange_cast_helper.<locals>._is_all_integral  sj     	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)huoUC./%3399K%33>>'')K %%%%!//6AFADD[%:%:%<D=DE=@!$$vs!6!6!8$
9dC?C144;#8#8#:4;DUD((r?   c                L    | j                   dk  rddlm} nddlm}  || g| S )NrG  r   )arange)r*  r+  r  r  )rt   ru   r  s      r6   _arange_helperr    s#    ww"}56!dr?   c           
         | j                  d|      }ddlm}  || || j                  dt        j                  dg            |      S )NrF  r   )selectr   r   )r   r+  r  rK   r   )rt   r   r   
full_shaper  s        r6   _size_helperr    s>    gt$J1!Zj%,,s:K!LcRRr?   c           
        ddl m} | j                  dk  rddl m} nddlm} |j                         j                         t        dd      S |j                         j                         }t        |d      }|dk  r||z  }t        | |t        |      D cg c]
  }||k7  s	| c}      }	 || | j                  d|      dt        | |dg      | j                  d|            }
 || |	|
d       }|
|fS c c}w )	Nr   )expandrG  r  
index_fillzinput rank not accessibler   rF  )r+  r  r*  r  r  r   r   r   r7   rn  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}X	(	5eHo@i1@ #	144#4QaS#A144QVCW A/1EtLN// As   
C2 C2c                ,   t        |d      }t        |      s&| j                  dt        j                  |            }| j
                  dk  r4|dk(  rt        dt        j                  d|       | j                  d||      S | j                  d|||	      S )
Nr   r   r   ri  rk   zReshape with allowzero=1   r  )allowzero_i)	rH   r&   r   rK   r  r*  r   r   r  )rt   r-  rM   	allowzeros       r6   r  r  G  s    UD)EUZ)9)9%)@Aww"}>#*G,M,MrSX ttIue,,ttIuetCCr?   c                   ddl m} t        |d      }t        |d      }|t        |      rq|t	        j
                  d|      t        j                  dg|z  t        j                  j                  |      j                               }	| j                  d|	      }|t        |      rq|t	        j
                  d|      t        j                  d	g|z  t        j                  j                  |      j                               }
| j                  d|
      }|t        |      s|t        |      r||J t        | || j                  dt        j                  ||d
gt        j                                    }| j                  d|g d      } || || j                  dt        j                  ddgt        j                              dd      \  }}||||fS )Nr   )	_var_meanrk   z@Unsupported: ONNX export of batch_norm for unknown channel size.r~  r?  r   r   g        r   	Transpose)r   rJ  rk   )perm_iF)r+  r  r   r   r   r/   rK   r   r   r   r   r  r   r  rN  )rt   r-  weightbiasrunning_meanrunning_varr  
batch_sizechannel_sizeweight_value
bias_value
reshape_intrans_ins                r6   _batchnorm_helperr  U  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%
!\ 4{22r?   c                    |r-|j                         j                         dk7  rt        |d       t         | |            S )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                n   t         j                  t        j                  j                  k(  ry| rt        j                  j
                  }nt        j                  j                  }|t         j                  k(  rydt        |        }t        j                  dt         j                   d| d| d| d	       y)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-   rc  rd  )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                   | j                  d|      }t        | |dgdg|g      }|| j                  dt        j                  dgt        j                              g}||dz
  k  rPt        | |dg|dz   g|g      }|| j                  dt        j                  dgt        j                              |g} | j                   d	g|d
di}	ddlm}
  |
| ||	      S )NrF  r   )r.  r/  r0  r   r   r?  r   rk   r}  r  )_reshape_from_tensor)r   r4  rK   r   rm  r+  r#  )rt   r-  	start_dimend_dimr   r  slice1slicesslice3final_shaper#  s              r6   _flatten_helperr*    s    gu%J1jsA3i[QFadd:u||RD

/SdTUFqzWq[M
 DDU\\2$ejj%IDJ
 !$$x3&33K?5+66r?   c                d    |yt        |       r"| j                         j                         dk7  ryy)NFr!   Trf   )split_size_or_sizesrl   s     r6   _is_split_staticr-    s5    %&$$&++-1AAr?   c                    | j                  d      }|j                  t        j                  j	                                |S )Nr   )r   setTyper   OptionalTypeofTensor)rt   ns     r6   r  r    s/    	AIIboo&&()Hr?   c                    t              }|1t        fdt        |      D              r| j                  |d      S | j                  |d      S )Nc              3  <   K   | ]  }t        |      d k(    yw)r   N)r   ).0r   r   s     r6   	<genexpr>z*_handle_reduce_dim_none.<locals>.<genexpr>  s#       /0T1%* s   rk   rv  r   )r   r   r  r   )rt   r   r   r  s    `  r6   _handle_reduce_dim_noner8    sW    D!DC  49$K  
 ttGTat0044!4,,r?   c                J   t        |      }|dd \  }}}t        |      dk\  r|d   nd}t        |dd      }t        j                  j                  |      }	|-|	|	j                         }nt        j                  j                  }| j                  d||      }
| j                  d|t        j                  j                        }| j                  d||      }|0t        j                  dk  rt        d	t        j                  dd
|       | j                  d	|
|||      |||fS )a  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  ri  DequantizeLinear Attribute axis is not supported.r  )rd   rb   rR   r   r   r   r  r   r!  UINT8r   r6  r   r  r  )rt   qtensorqdtypeunpacked_qtensorsr   r   r   r:  r  input_qdtyper"   s              r6   r   r     s.   " 19 1"1 5FE:#&'8#9Q#>QDDc6*F,,77?L~#!++-F0066FDDfD-EDDW%@%@%F%FDGEfjv6Jg??"D(--.	
 	
z&I	 r?   c                h   |;t        |      s0t        j                  dk  rt        dt        j                  dd|       |J t        j
                  j                  |t        j
                  j                        t        j
                  j                  k7  r,| j                  d|t        j                  j                        }|J t        j
                  j                  |t        j
                  j                        t        j
                  j                  t        j
                  j                  hvr,| j                  d|t        j                  j                        }| j                  d|||t        |dd            }|||g}|t        |      s|j                  |        | j                  d	g| S )
ai  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.
    ri  QuantizeLinearr<  r  r  r   r:  r  r   )r   r   r  r  r   r   r   r   r6  r   r   r!  r=  INT8rR   r   )rt   r   r   r   r:  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 E:&DD144&...r?   c                   | j                  d||      }| j                  d|      }| j                  d|t        j                  dgt        j                              }| j                  d| j                  d||      t        j
                  j                  	      }g }	|t        |      s|	j                  |        | j                   d
|||g|	 S )ad  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.
    MulrF  r  r   r?  r   r  r|  r  r   )	r   rK   r   r+   r   r!  INT32r   r   )
rt   r  input_scaleweight_scaler:  
bias_scalebias_scale_shapebias_zero_pointq_bias	axis_argss
             r6   requantize_bias_helperrO  Q  s     e\;7JttGZ0dd+U\\1#UYY5W  O TTUD*-G4O4O4U4U  F I144&
OXiXXr?   c                |    | sJ t         j                  j                  | d         t        fd| D              }|S )Nr   c              3  b   K   | ]&  }t         j                  j                  |      k(   ( y wrE   )r   r   r   )r5  elem
base_dtypes     r6   r6  z'args_have_same_dtype.<locals>.<genexpr>j  s,      EI!!,,T2j@s   ,/)r   r   r   all)ru   has_same_dtyperS  s     @r6   args_have_same_dtyperV  g  sC    K4**55d1g>J MQ N r?   c           
     <   |j                  dd      }|j                  dt        j                  j                        }t	        |      }t        j                  j                  |d         }t        |d          xr |du xs t        j                  |k  }|r|D ]r  }	|	j                         st        j                  j                  |	      }
|
|k7  s9t        j                  d| d|j                          d|
j                          |	       t        |      D ]G  \  }}	|	j                         st        |	      r#| j                  d|	|j                         	      ||<   I  | j                  |g|i |}|r"| j                  d||j                         	      }|S )
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   r6  rT   r   r:  r   r  isCompleteTensorr   r/   r   r   r   r  )rt   r   ru   rv   rX  rY  r0   dtype_0require_castr-  input_scalar_typer   r   s                r6   _op_with_optional_float_castr_  p  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:[] 		 "&) 	HAu%%'uDD'113 ! q		 144+&+F+DttFDw'8'8':t;Kr?   c                X   t         j                  j                  |t         j                  j                        }|t         j                  j                  k7  rTt	        |      sI|t         j                  j
                  k7  r,| j                  d|t        j                  j
                        }|S r  )	r   r   r   r   r:  r  r   r   r!  )rt   r   r   s      r6   _maybe_cast_reduce_op_inputra    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                       fd}|S )z_Returns a decorator that calls the decorated (higher-order) function with the given parameters.c                     | i S rE   rJ   )r~   ru   rv   s    r6   _applyz_apply_params.<locals>._apply  s    4"6""r?   rJ   )ru   rv   rd  s   `` r6   _apply_paramsre    s    # Mr?   c                     d fd	}|S )Nc                    t        | |      }||dk(  rt        | |      S t        |dd      }| j                  dk  r/rdnd}t        ||d      }r|n|g}| j	                  |||      S t        |      r|}npr7| j	                  dt        j                  |t        j                  	      
      }n7| j	                  dt        j                  |gt        j                  	      
      }| j	                  |||      S )NrJ   r   r     r   r   rx  r   r?  r   r7  )	ra  r8  rR   r*  r   r&   rK   r   rm  )	rt   r   r   r  r1   dim_listr.  allow_multi_dim_supportonnx_op_names	          r6   symbolicz,_reduce_op_symbolic_helper.<locals>.symbolic  s    *1d3;#) +1dLAA !#y9Gww|6tC dE2"93uttL$xGtTTS>D. tt&S

0S  $    !tt&cU%**0U  $   ttL$tIIr?   NNrJ   )rk  rj  rl  s   `` r6   _reduce_op_symbolic_helperrn    s    J: Or?   c                B     t        j                          fd       }|S )Nc                     | g| }|D ]0  }|j                   }t        |      t        |      k(  s' || g| c S  t        dj                   dt        |       d      S )Nzaten::zwith z
 arguments)r   rb   r   rm   )rt   ru   	overloadsoverloadr}   r~   s        r6   r   z'_overload_by_arg_count.<locals>.wrapper  sr    qL4L	! 	*H&77O?#s4y0)D))	* r{{m4c$i[
6STTr?   r   )r~   r   s   ` r6   _overload_by_arg_countrs    s'    __RU U Nr?   c                F    t        |       t        fd       }|S )N)rj  c                    t        d      t        dd      fd              }rdnd}t        d      t        d|dd      fd              }||fS )NTr   r   c                   d }|j                         j                         dk(  rEt        |dd      }t        j                  |      j                         }| j                  d||      }n.|j                         j                         dk7  rt        d|      S  | |      }|Ft        j                  j                  |      j                         }||k7  r| j                  d||      }|S Nr!   r   r  r  r  r   	r'   r)   rR   r   r   r  r   r   r   )rt   r   r  
dtype_onnxresultresult_dtype_onnxr  rl  s         r6   reduce_nodimz?_reduce_with_dtype_helper.<locals>.reduce.<locals>.reduce_nodim  s     Jzz|  "&66"5#w7(66u=GGI
ttFDzt:""$(88%dGU;;a&F%$/$=$=$H$H%)+ " %
2TT&&zTBFMr?   r   r   c                   d }|j                         j                         dk(  rEt        |dd      }t        j                  |      j                         }| j                  d||      }n.|j                         j                         dk7  rt        d|      S  	| |||      }|Ft        j                  j                  |      j                         }||k7  r| j                  d||      }|S rw  rx  )
rt   r   r   r  r  ry  rz  r{  r  rl  s
           r6   
reduce_dimz=_reduce_with_dtype_helper.<locals>.reduce.<locals>.reduce_dim  s     Jzz|  "&66"5#w7(66u=GGI
ttFDzt:""$(88%dGU;;asG4F%$/$=$=$H$H%)+ " %
2TT&&zTBFMr?   )r   r   )	rt   ru   rv   r|  dim_descr~  rj  r  rl  s	         r6   reducez)_reduce_with_dtype_helper.<locals>.reduce  sk    			C	 	 
! 
	" 34			C3	/	 
0 
	" Z''r?   )rn  rs  )rf  r  rj  r  rl  s    `` @r6   _reduce_with_dtype_helperr    s1     *)@H )( )(V Mr?   c                   ||| j                  d|d      S |t        | d||d      S t        |dd      }t        |dd	      }| j                  d
k  r| j                  d||g|      }nL| j                  dt	        j
                  |gt        j                              }| j                  d|||      }| j                  d|||      }||fS )N	ReduceMaxr   r7  Maxr  rX  r   r  r   rh  rx  r   r?  r   ArgMaxr  r   r_  rR   r*  rK   r   rm  )rt   r   dim_or_yr  r   maxr.  indicess           r6   _max_helperr        GOttK!t44+AudHSUVV Wc953.77R<$${D#7$KC44
ELL#ejj,Q4RD$${D$7$CC$$xcg$FG|r?   c                   ||| j                  d|d      S |t        | d||d      S t        |dd      }t        |dd	      }| j                  d
k  r| j                  d||g|      }nL| j                  dt	        j
                  |gt        j                              }| j                  d|||      }| j                  d|||      }||fS )N	ReduceMinr   r7  Minr  r  r   r  r   rh  rx  r   r?  r   ArgMinr  r  )rt   r   r  r  r   minr.  r  s           r6   _min_helperr  2  r  r?   c                N    | j                  d|      }| j                  d|d      S )NrF  
ReduceProdr   r7  )r   )rt   r   rM   s      r6   _numel_helperr  F  s'    DD$E44e422r?   r   r   r   c           
        | j                   dk  r|#| j                  d|d      }|}t        | |      }n| j                  d|||      }| j                  d||d      }| j                  d|      }| j                  d|| j                  d	t        j                  |      
      d      }| j                  d|d      }| j                  d||      }	| j                  d|	|	      }
|dn|}| j                  d|
||      }|d}|dk7  r| j                  d|t
        j                  j                        }| j                  d	t        j                  |t        j                        
      }| j                  d||      }| j                  d|| j                  d||            }||fS d }|#| j                  d|d      }|}t        | |      }n| j                  d	t        j                  |t        j                        
      }| j                  d|||      }| j                  d||d      }| j                  d|      }| j                  d|| j                  d	t        j                  |      
      d      }| j                  d|d      }| j                  d||      }	| j                  d|	|	      }
|dn|}|| j                  d|
|      }n| j                  d|
||      }|d}|dk7  r| j                  d|t
        j                  j                        }| j                  d	t        j                  |t        j                        
      }| j                  d||      }| j                  d|| j                  d||            }||fS )Nrh  
ReduceMeanr   r7  rx  rk   rF  r  r   r   r  r  SubrF  r  r  r?  r|  )
r*  r   r  rK   r   r   r!  r6  r,   rm  )rt   r-  r   
correctionr  meant_meannum_elementsredudced_dimssub_vsqr_subkeepdim_meanvaronemulr.  s                   r6   _var_mean_helperr  K  sd   ww|;44e4:DF(E2L44eCG4LDTT,caTHFDD%0MDDZc):;	 ! M 44m4JLUE6*$$ueU+KqWdd<dNJ?447+F+F+L+L   L $$z5<<
%+++V$WC$$uc<0C$$uc144|S#ABCDy;44e4:DF(E2L44
ELLEJJ,O4PD44eTg4FDTT,tTBFDD%0MDDZc):;	 ! M 44m4JLUE6*$$ueU+KqW<$$|W$FC$$|Wd|$LCJ?447+F+F+L+L   L $$z5<<
%+++V$WC$$uc<0C$$uc144|S#ABCDyr?   c
                   |rt         j                  rt        d      S |	|	dk\  rt        d      | j	                  dt        j                  d            }
| j	                  d|
t        j                  j                        }
| j	                  dt        j                  dg            }t        | t        | || j	                  dt        j                  d                  dg      }|s||g} | j                  d	g|d
di}t        | |dgdgt        j                  gdg      }t        | |dgdgt        j                  gdg      }t        | || j	                  dt        j                  d                  }t        j                   | d||
d      \  }\  }}|j"                  }t%        j&                  |      }t%        j&                  |      }|j	                  d||d      }|j	                  d||d      }t        ||dg      }t        ||dg      }|j	                  d||||      }|j	                  d||d      }t)        |      s6|j	                  d||||      }t        ||dg      }|j	                  d||      }|dk(  rt+        ||dgd      }n|dk(  rs|j,                  dk  r|j	                  d|dgd      }n|j	                  dt        j                  dgt
        j.                              }|j	                  d||d      }nr|j,                  dk  r|j	                  d|dgd      }nL|j	                  dt        j                  dgt
        j.                              }|j	                  d||d      }|j	                  d|
t        j                  j                        }t%        j0                  ||       t%        j0                  ||       |j3                         j5                         d d d fS )Nz7embedding_bag with scale_grad_by_freq for training moder   zembedding_bag with padding_idxr   rk   r   r  r  r}  r  )r.  r/  r0  r1  Loop)n_blocksr  r  SlicerF  rx  rh  r  r?  r7  r  )r   export_trainingr   RuntimeErrorr   rK   r   r   r!  r<  rn  r  r4  r  r  r   add_op_with_blocksblockr   _add_input_to_blockr   ry  r*  rm  _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condindices_startindices_endindices_row
embeddingsper_sample_weights_rowr.  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
 K(!$$x4'4!4
 #	7!aS}QCN !	7!aS}QCL A|QTT*ellSToT-VWH(;;	68^a D/<1 ##J 00<$$Z0D 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                   d }t        |      rt        | |dg      }d}nE| j                  dk\  r6| j                  dt	        j
                  |t        j                              }|t        j                  k(  r| j                  dk  r'| j                  d| j                  d|      ||	      }nV|&| j                  d| j                  d|      |
      }n.| j                  d| j                  d|      ||
      }n|t        j                   k(  r| j                  dk  r'| j                  d| j                  d|      ||	      }n|&| j                  d| j                  d|      |
      }n| j                  d| j                  d|      ||
      }nn|dk(  r| j                  dk  rt        dddd|      S |Dt        | || j                  dt	        j
                  dgt        j                                    }d}| j                  d| j                  d|| j                  dt	        j                  dg                        }| j                  d|t        j                  j                  |      j                               }t!        | |||	      S |dk(  rU| j                  dk  r t#        d      | |||      }nD| t#        d      | ||      }n, t#        d      | |||      }n|dk(  rR| j                  dk  r t#        d      | |||      }n| t#        d      | ||      }n t#        d      | |||      }n| j                  dt	        j
                  |t        j$                              }	t!        | | j                  d| j                  d|      |	      ||	      }| j                  d|| j                  d| j                  dt	        j
                  dt        j$                              |	            }t        |      sBt'        |dd      }| j                  d|t        j                  |      j                               }|S )Nr   Frh  r   r?  r   r  Absrx  r7  r  r   r  linalg_vector_normr(  zord=0 not supportedNotEqualr  r  rk   ReduceL1)r   r  )r  rJ  ReduceL2Powr|  r   r  )r   r  r*  r   rK   r   rm  mathinfr  rN  r  r   r   r   r  ry  rn  r  rR   )
rt   r   ordr   r  r  r.  rz  cond_opord_ops
             r6   _linalg_vector_norm_helperr    s    D}q$-	
BttJS

(KtL
dhh77R<TTQTT%.sw  F |k144t+<Qk144t+<dwW			77R<TTQTT%.sw  F |k144t+<Qk144t+<dwW	77R<3$a-BD  {&DDU\\2$ekk-RDS
  ddWdADDU=M=Mqc=RD$STG dd ..99$?IIK  G
 %QPP	77R<;/
;4S'F |?3J?tW @3J?tT7 
77R<;/
;4S'F |?3J?tW @3J?tT7 j%,,s%--*PQ"qttE144t,f5cg
 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_tr@  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_opsrm  )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  r-  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  r>  r  r?  z"_C_onnx.TensorProtoDataType | Noner   z4tuple[_C.Value, _C.Value, _C.Value, _C.Value | None])rt   r  r   r  r   r  r   r  r:  r  r   r  )rf  r.   r  r.   rj  r-   )rt   r  r   r  r  r,   r   zSequence[int] | Noner  r-   r  r  )
__future__r   r   rn   r  r  r   rc  r   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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  r&  r4  r:  r=  rB  rS  rY  ra  rg  rn  rs  ry  r  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   rO  rV  r_  ra  re  rn  rs  r  r  r  r  r  r  r  r!  r=  rD  r7  r6  FLOAT16rG  r  INT16r<  r   r   r9  r   cast_pytorch_to_onnxscalar_name_to_pytorchuint8int8shortr+   rN  r  r,   r@  	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   "    
   R R R       > = ' * 
"d^d^ 
     	B
B B 	BJ#3
	=	 	#$&.WO&OWOh ! 	EEE E 	E
 4EP&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?   