
    Χg@                      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j"        rd d	l#m$Z$  ed
          Z% ed          Z& ed          Z'e
d         Z(	 	 dddZ)ddZ*ddZ+ddZ,d Z-d  Z.dd$Z/dd'Z0dd(Z1dd+Z2dd.Z3ddd/d0dd8Z4dd;Z5d< Z6dd=Z7dd>Z8dd?Z9dd@Z:ddDZ;ddEZ<ddFZ=ddGZ>ddHZ?ddIZ@ddJZAdddLZBddOZCddPZDdddUZEdddXZF	 ddd[ZG	 ddd]ZHdd_ZIddaZJddcZKddfZLdddgZM	 dddhZNddiZOddjZPddkZQdddlZR	 dddnZSddoZTdp ZUddqZVddrZW	 	 	 dddtZXdduZYddvZZddwZ[ddxZ\ddyZ]dd}Z^d~ Z_ddZ`ddZaddZbddZcddZd	 dddZeddZfddZgddZhdddZiddZjddZkddZlddZmd Znd ZoddZp	 dddZq	 dddZr	 dddZsd ZtddZuddZvd ZwddZxd Zy	 dddZzdddZ{dddZ|ddZ} e3dddd          dd            Z~ddZdd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        dZddddddddddddddddǜZej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        gZej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        dȜZed         ed         ed         ed         ed         ed         ed         ed         ed         ed         ed         ed         ed         ed         ed         ed         gZ e            Zded<   dS )    )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nonedesc_ValueDescriptorarg_name
str | None	node_namec           	     h   |dk    r| S |dk    st          |           s| S |                                 }|                                rd S |                                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 |D             S |dk    rd |D             S t          j
        d| d| d|           |                                dk    r|d
k    r|                                D ]J}|                                }|                                dk    rt          j
        d| d| d|           Kd |                                                                 D             S t          j
        d| d|           ||+t          j
        d|                                 d|           t          j
        d| d| d|                                 d|           )Nr   r   onnx::Constantvaluer   r   r   r   r   r   c                ,    g | ]}t          |          S  )int.0r   s     V/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/onnx/symbolic_helper.py
<listcomp>z_parse_arg.<locals>.<listcomp>J   s    ---qCFF---    r   c                ,    g | ]}t          |          S r)   )floatr+   s     r-   r.   z_parse_arg.<locals>.<listcomp>L   s    ///E!HH///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.c                l    g | ]1}t          t          |                                d                     2S r'   )r*   	_node_getnoder+   s     r-   r.   z_parse_arg.<locals>.<listcomp>_   s2    UUU!C	!&&((G4455UUUr/   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_valuer7   
mustBeNonekindr6   r*   r1   boolstrr   SymbolicValueErrorinputs)r'   r    r"   r$   r7   node_valr   element_nodes           r-   
_parse_argrB   /   s    v~~s{{)E**{::<<D tyy{{&&&T7++3;;x== S[[??"S[[>>!S[[x== S[[OT\\--H----T\\//h////+7 7 7.27 7 7  
 
-	-	-4<<[[]] 	 	 vvxx$$&&*::: 3]L ] ])-] ] ]    ; VUuzz||?R?R?T?TUUUU+7/37 7 7   9,'HHHH
 
 	

 
#	Q!	Q 	Q.7	Q 	QAE	Q 	Q 	Q  r/   r7   _C.Nodekeyr=   c                    t          | t          j                  sJ |                     |          } t	          | |          |          S )z@Gets attributes of a node which is polymorphic over return type.)
isinstancer   NodekindOfgetattr)r7   rD   sels      r-   r6   r6   t   sE    dBG$$$$$
++c

C74c"""r/   r'   _C.Valuec                V    |                                                                  dk    S )z$Whether a Value is an ONNX constant.r&   r7   r;   r5   s    r-   _is_onnx_constantrN   {   s"    ::<<"222r/   2_C.Value | torch.Tensor | Number | Sequence | None
descriptorc                x    t          | t          j                  rt          |           rt	          | |          S | S N)rF   r   ValuerN   rB   )r'   rP   s     r-   _maybe_get_constrT      s=     %"" -'8'?'? -%,,,Lr/   c                t    t          | d          }t          |t          j                  r|j        dk    r|S | S )Nr   r)   )rT   rF   torchTensorshape)r'   value_ts     r-   _maybe_get_scalarrZ      s;    uc**G'5<(( W]b-@-@Lr/   c                x    t          |           st          j        d| d|  d|           t          | |          S )Nz0ONNX symbolic expected a constant value of the 'z' argument, got 'r8   )_is_constantr   r>   rB   )r'   r    r"   s      r-   
_get_constr]      s_     
'x    
 
 	

 eT"""r/   
list_valuereturnlist[_C.Value]c                    |                                  }|                                dk    rt          j        d| d|           t	          |                                          S )Nr3   z;ONNX symbolic expected node type prim::ListConstruct, got 'r2   )r7   r;   r   r>   listr?   )r^   	list_nodes     r-   _unpack_listrd      sr    !!I~~000'"" " "
 
 	

 	  ""###r/   tuple_valuetuple[_C.Value, ...]c                    |                                  }t          |           s+t          j        d|                                 d|           t          |                                          S )Nz>ONNX symbolic expected node type 'prim::TupleConstruct', got 'r2   )r7   _is_tuple_constructr   r>   r;   tupler?   )re   
tuple_nodes     r-   _unpack_tuplerk      sx    !!##J{++ 
'*OO%%* * *
 
 	

 ""$$%%%r/   c                R   |                                  }t          |           s:t          j        d| d|                                 dt
          j         |           t          |                                          }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       )
r7   rh   r   r>   r;   r   PYTORCH_GITHUB_ISSUES_URLri   r?   len)re   rj   unpackeds      r-   _unpack_quantized_tensorrr      s     !!##J{++ 
'gZ g g!!g g@J@dg g 	
 
 	
 Z&&(())Hx==AX!!3!3!3!3Or/   r   r<   c                t    t          |           o)|                                                                 dk    S )Nr3   r9   r7   r;   )r^   s    r-   _is_packed_listru      s1    Z  VZ__%6%6%;%;%=%=AV%VVr/   arg_descriptorsRCallable[[Callable[_Concatenate[_U, _P], _T]], Callable[_Concatenate[_U, _P], _T]]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
    fn"Callable[_Concatenate[_U, _P], _T]r_   c                Z      _         t          j                   d
 fd	            }|S )Ngr   args_P.argskwargs	_P.kwargsr_   r   c           	        d}t                    t          |          k    s5J dt          |           dt                     dj         d|             	 t          j                  }t	          |j                                                  dd          }j        n%# t          $ r d gt          |          z  }d Y nw xY wfdt          ||          D             }t          |          dk    sJ dj         d	|             t          |          dk    rd
|v sJ dj         d|              | g|R i |S )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'.    c                <    g | ]\  }}}t          |||          S r)   )rB   )r,   argarg_descr"   fn_names       r-   r.   zBparse_args.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>  s=       +C8 3(G<<  r/   zSymbolic function z4's '**kwargs' can contain a single key/value entry. _outputsz='s '**kwargs' can only contain '_outputs' key at '**kwargs'. )	rp   __name__inspect	signaturerb   
parameterskeys	Exceptionzip)	r|   r}   r   FILE_BUG_MSGsig	arg_namesr   rv   ry   s	         @r-   wrapperz.parse_args.<locals>.decorator.<locals>.wrapper   s   l 
 ''3t99444"s4yy " "&)/&:&:" "^`^i" "" " 544'++ !4!4!6!677;	+    "FSYY.		
   /24)/T/T  D
 v;;!###"R[ " "" " $## 6{{a!V+++& & &#& & ,++
 2a)$)))&)))s   A	B% %CC)r|   r   r}   r~   r   r   r_   r   )_arg_descriptors	functoolswraps)ry   r   rv   s   ` r-   	decoratorzparse_args.<locals>.decorator   sN     .			'	* '	* '	* '	* '	* '	* 
	'	*R r/   )ry   rz   r_   rz   r)   )rv   r   s   ` r-   
parse_argsr      s*    </ / / / / /b r/   T)scale
zero_pointquantize_outputarg_q_descriptorsr   float | Noner   
int | Noner   .Callable[[Callable[_P, _T]], Callable[_P, _T]]c                      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                P     t          j                    fd            }|S )Nc                   *|                      dt          j                            }nd }*|                      dt          j                            }nd }dt          |          t                    z
  z  z   }t	          t          ||                    }d }g }|D ]|\  }	}
t          |
          rI|
                                                                D ]!}|	                     ||	|                     "]|	                     ||	|
                     }t          |          s | g|R i |S g }|D ]\  }	}
 ||	|
          r3t          | |
          \  }}}}|	                    |           ||}||}Dt          |
          r}|
                                                                D ]@} ||	|          r2t          | |          \  }}}}||}||}|                    |           A|	                    |
           |	                    |
            | g|R i |}|
J d            |
J d            rt          | |||          S |S )NConstantrY   )Fc                B    | ot          |          ot          |          S rR   )r9   rh   )rP   r   s     r-   _is_arg_quantizedzMquantized_args.<locals>.decorator.<locals>.wrapper.<locals>._is_arg_quantizedc  s!    !QinnQ9LS9Q9QQr/   z-Bug: Scale must be set for quantized operatorz2Bug: Zero point must be set for quantized operator)oprV   tensorrp   ri   r   ru   r7   r?   appendanydequantize_helperreplaceAllUsesWithquantize_helper)r|   r}   r   _scale_zero_pointarg_q_descriptors_extendeddescriptor_argsr   is_quantizedrP   r   	arg_inputnon_quantized_argsdequantized_arg	arg_scalearg_zero_point_outputr   ry   r   r   r   s                     r-   r   z2quantized_args.<locals>.decorator.<locals>.wrapperP  s<     j%,u2E2EFF%dd:u|J7O7OdPP" *;XD		C 1222> *& $C(BD$I$IJJOR R R L#2 L L
C"3'' L%(XXZZ%6%6%8%8 V V	$++,=,=j),T,TUUUUV !''(9(9*c(J(JKKKK|$$ .r!-d---f--- "$#2  3  3
C$$Z55 3DU3E EAOY '--o>>>~!*"*&4$S)) 3%(XXZZ%6%6%8%8 J J	,,ZCC J !2!Y ? ? / ) . !  &~)2*2.<%88III&--c2222 '--c2222 R9.999&99F%%'V%%%''C (''  G&q&&+FFFMr/   r   r   )ry   r   r   r   r   r   s   ` r-   r   z!quantized_args.<locals>.decoratorO  sW    			P	 P	 P	 P	 P	 P	 P	 P	 
	P	d r/   r)   )r   r   r   r   r   s   ```` r-   quantized_argsr      s@    ^T T T T T T T Tl r/   xNumber | Nonec                x    t          | t          j                  r| j        dk    r|                                 S dS )z,Convert a scalar tensor into a Python value.r)   N)rF   rV   rW   rX   itemr   s    r-   _scalarr     s2    !U\"" qw"}}vvxx4r/   c                B   t          | t          j                  r| S t          j                            |t          j        j                  }|t          j        j        k    r>|                                                                } 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.
    )
rF   r   rS   r   JitScalarType
from_value	UNDEFINEDscalar_namelowerrI   )selfr   scalar_typetys       r-   _if_scalar_type_asr     s     $!! +66)3 K k/999$$&&,,.. wtR  """Kr/   c                    | d u pAt          | t          j                  r&|                                                                 ndS NF)rF   r   rS   r7   r:   r   s    r-   _is_noner     s;    9U*Q2I2IT,,...uUr/   c                6    t          | t          j                  S rR   )rF   r   rS   r   s    r-   r9   r9     s    a"""r/   c                r    t          |            p'|                                                                 dv S )N>   r&   prim::Constantrt   r5   s    r-   r\   r\     s:     5::<<#4#4#6#6 ; $ r/   c                    |                                                      t          j                                                  S rR   )typeisSubtypeOfr   
TensorTypegetr   s    r-   
_is_tensorr     s,    6688 1 1 3 3444r/   jit_type
_C.JitType_C.ListType | Nonec                >    t          | t          j                  r| S d S rR   )rF   r   ListType)r   s    r-   _as_list_typer     s     (BK(( 4r/   c                H    t          |                                           d uS rR   )r   r   r   s    r-   _is_listr     s    ""$..r/   c                    t          |                                           }|dS t          |                                t          j                  S r   )r   r   rF   getElementTyper   r   r   x_types     r-   _is_tensor_listr     s?    16688$$F~uf++--r}===r/   c                    t          |                                           }|dS t          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.
    NF)r   r   r   r   r   onnx_compatible)r   r   r   s      r-   _is_scalar_listr     sK     16688$$F~u+66q99K&&(((r/   c                V    |                                                                  dk    S )Nprim::TupleConstructrM   r   s    r-   rh   rh     s    6688==??444r/   c                    t          |           sJ t          j                            | t          j        j                  t          j        j        t          j        j        t          j        j        hv S rR   )r9   r   r   r   r   	COMPLEX32	COMPLEX64
COMPLEX128r   s    r-   is_complex_valuer     sa    Q<<$//	;$.  	!+!+!,
 r/   c                    t          |           r|                                 d S |                                 }t          j        t          j        |          }|                                S rR   )r   r   typingcastr   r   dimr   s     r-   _get_tensor_rankr     sP    a== AFFHH,tVVXXF[//F::<<r/   allow_nonstaticc                   t          |           r|                                 d S |                                 }t          j        t          j        |          }|r|                                S |                                S rR   )r   r   r   r   r   r   varyingSizessizes)r   r   r   s      r-   _get_tensor_sizesr     sn    a== AFFHH,tVVXXF[//F % ""$$$ <<>>r/   r   r*   c                8    t          |           }|r||         nd S rR   )r   )r   r   r   s      r-   _get_tensor_dim_sizer     s"    a  E(5::D(r/   c                    |dk    rt          |           }|J ||z   S |4t          |           }|J t          |          D ]\  }}|
|dk    r|c S |S )Nrm   )r   r   	enumerate)r   r   tensor_rankr   indexsizes         r-   _get_dim_for_crossr     s    
byy&q))&&&[  
{!!$$   $U++ 	 	KE4DAIIJr/   r   msg_C.Value | NoneNonec                r    t           j        t          j        j        k    rt          |  d| |           d S d S )Nz, )r   operator_export_type_C_onnxOperatorExportTypesONNX_onnx_unsupported)r   r  r'   s      r-   _unimplementedr
  -  s?    #w'B'GGGR..3..%00000 HGr/   op_namer   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   ro   rF   r   rS   r   r>   OnnxExporterError)r  r'   messages      r-   r	  r	  3  sq    	E 	E 	E(B	E 	E 
 %"" 
'
 
 	
 
"7
+
++r/   current_opsetsupported_opsetc                    d|  d| d| d}t          |t          j                  rt          j        ||          t          j        |          )NUnsupported: ONNX export of 
 in opset . Please try opset version .rF   r   rS   r   r>   r  )r  r  r  r'   r  s        r-   _onnx_opset_unsupportedr  A  sz    	7w 	7 	7- 	7 	7$3	7 	7 	7  %"" 
'
 
 	
 
"7
+
++r/   reasonc           	         d|  d| d| d| d	}t          |t          j                  rt          j        ||          t          j        |          )Nr  r  z. r  r  r  )r  r  r  r  r'   r  s         r-    _onnx_opset_unsupported_detailedr  S  s    	Xw 	X 	X	X 	X"(	X 	XET	X 	X 	X  %"" 
'
 
 	
 
"7
+
++r/   namec                      fd}|S )Nc                 N    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)r}   r   r  s     r-   symbolic_fnz)_block_list_in_opset.<locals>.symbolic_fng  s>    &7T 7 707 7 7
 
 	
r/   r)   )r  r  s   ` r-   _block_list_in_opsetr   f  s$    
 
 
 
 
 r/    _type_utils.JitScalarType | Nonec                     | D ]J}t           j                            |t           j        j                  }|t           j        j        k    r|c S Kd S rR   )r   r   r   r   )r}   r   r   s      r-   _try_get_scalar_typer#  q  s]      !/::*4
 
 +3=== >4r/   _type_utils.JitScalarTypec                 f   t           j        j        }d | D             }t          |          dk    r|S t          |          dk    r|d         S |d                                         }|D ])}t          j        ||                                          }*t           j                            |          S )Nc                ,    g | ]}t          |          S r)   )r#  )r,   r   s     r-   r.   z-_type_promote_from_values.<locals>.<listcomp>}  s!    ;;;s%c**;;;r/   r   r   )r   r   r   rp   dtyperV   promote_types
from_dtype)r}   undef	jit_types	new_dtyper   s        r-   _type_promote_from_valuesr-  {  s    %/E;;d;;;I
9~~
9~~|!""$$I > >'	17799==		$//	:::r/   r|   jit_utils.GraphContextc                    t           j                            |t           j        j                  |k    r*|                     d||                                          S |S NCastto_i)r   r   r   r   r   	onnx_type)r|   r'   r   s      r-   _maybe_cast_to_typer5    sf     	!,,UK4M4WXX	 	 tt##%%  
 
 	

 Lr/   c           
     ^   t          |          }t          |          }t          |          s+|                     dt	          j        |g                    }nC|A|r?|dk    r9t          | ||                     dt	          j        dg                              }t          j        	                    |t          j        j
                  }|t          j        j        t          j        j        hvr'|                     d|t          j        j                  }|                     d|||          S )	Nr   r   r   r   r1  r2  Gatheraxis_i)rZ   r   r9   r   rV   
LongTensor_reshape_helperr   r   r   r   INT64INTr  TensorProtoDataType)r|   r   r   r   apply_reshapeindex_const	index_dimindex_scalar_types           r-   _select_helperrC    s   #E**K ''I[!! Z)9;-)H)HII		=	>>#5!$$z53CQC3H3H$II E $1<<{(2  !'!%!   VU)D)JKK44$c4222r/   c                n    | j         dk    rddlm}  || ||||          S ddlm}  || |||||          S )N	   r   )_slice)opsettorch.onnx.symbolic_opset9rF  torch.onnx.symbolic_opset10)r|   inputaxesstartsendssteps_slice9_slice10s           r-   _slice_helperrQ    se     	w!||@@@@@@wq%vt444BBBBBBx5$e<<<r/   c                    t           j                            | t           j        j                  t           j        j        t           j        j        t           j        j        t           j        j        hv S rR   )r   r   r   r   FLOATDOUBLEHALFBFLOAT16r5   s    r-   _is_fprW    sX    $//{(2  	!'!(!&!*	
 r/   c                    t           j                            | t           j        j                  t           j        j        hv S rR   )r   r   r   r   BOOLr5   s    r-   _is_boolrZ    s9    $//{(2 

#
(	)* *r/   c                    t          |t          j                  rJ t          |t                    r5|                     dt          j        |t          j                            S |                     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   )rF   rV   rW   r1   r   r   double)r|   scalars     r-   _generate_wrapped_numberr_    sv     &%,/////&%   RttJV5<(P(P(PtQQQ44
EL$8$84999r/   c                   |t          dd           |                     d|          }|                     d||                     dt          j        |gt          j                                      }| j        dk    r,|st          dd	           |                     d
|||d          S |                     d
||||d          S )NSortOut parameter is not supportedShaper7  r   r\  r   
   Ascending is not supportedTopK   r9  outputs)r9  	largest_iri  )r
  r   rV   r   int64rG  )r|   rJ  r   	decendingoutshape_	dim_size_s          r-   _sort_helperrp    s    
v?@@@TT'5!!F	Zse5;!G!G!GHH I
 	w"}} 	A6#?@@@ttFE9S!tDDDttE9SIq  
 
 	
r/   Fc           
     \   |t          dd           t          |          s7|                     dt          j        |gt          j                            }nt          | ||                     dt          j        dg                              }t          |          t          j	        j
        k    r'|                     d|t          j        j
                  }| j        d	k    r,|st          dd
           |                     d|||d          S |                     d|||||d          S )Nrf  rb  r   r\  r   r   r1  r2  rd  re  rg  rh  )r9  rj  sorted_iri  )r
  r9   r   rV   r   rk  r;  r#  r   r   r<  r  r>  rG  )r|   rJ  kr   largestsortedrm  s          r-   _topk_helperrv    s"    v?@@@Q<< HDDU\1#U[%I%I%IDJJAq!$$z5<;L;L$"M"MNN""k&?&EEEVQW%@%FGGAw"}} 	A6#?@@@ttFE1S!t<<<ttE1SGfVW  
 
 	
r/   c                d    | j         dk    rddlm}  || ||          S ddlm}  || ||          S )N   r   )lt)rG  torch.onnx.symbolic_opset8ry  rH  )r|   rJ  other_lt8_lt9s        r-   
_lt_helperr~    sY    w!||999999tAue$$$999999tAue$$$r/   c                    t           j        dk    rdnd}t          j        d|z   dz   t	          t           j                  z   dz              d S )Nrd  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     r-   _interpolate_warningr    sq     :b@@o  M2
		 233	47P	P
 
 
 
 
r/   c                   t          |d                   ro| j        dk    rL|                     dt          j        |t          j                            }|                     d||          S |                     d||          S | j        dk     rt          j        d|          |                     d||d                   S )	Nr      r   r\  r   	Unsqueezeaxes_iz<Opset version must be >= 13 for Unsqueeze with dynamic axes.)r\   rG  r   rV   r   longr   r>   )r|   rJ  r  rK  s       r-   _unsqueeze_helperr  (  s    F1I 77b==44
ELuz,R,R,R4SSD44UD111ttKvt666w||'JE
 
 	
 44UF1I...r/   c                B   t          |d                   ro| j        dk    rL|                     dt          j        |t          j                            }|                     d||          S |                     d||          S | j        dk     rt          j        d|          |d         }t          |          }|J |d	k    rt          j        d
|          |dk    r)t          | |dg          }|                     d||          S |                     d||          S )Nr   r  r   r\  r   Squeezer  z:Opset version must be >= 13 for Squeeze with dynamic axes.r   zCFor Squeeze axses as input, the axes rank must be one in ONNX spec.)
r\   rG  r   rV   r   r  r   r>   r   r  )r|   rJ  r  rK  axes_t	axes_ranks         r-   _squeeze_helperr  6  s,   F1I 57b==44
ELuz,R,R,R4SSD44	5$///ttIuVt444w||'H%
 
 	
 AYF ((I   1}}'QSX
 
 	
 
a"1fqc22ttIuf---44	5&)))r/   r   c                \   t          |d          }| j        dk    ry|r^t          |          s5|                     dt	          j        |t          j                            }|                     d||||          S |                     d|||          S |                     d|||          S )	Nr   r  r   r\  r   	ReduceSum)
keepdims_inoop_with_empty_axes_ir  r  )rT   rG  r9   r   rV   r   r  )r|   rJ  r  r  r  s        r-   _reducesum_helperr  O  s     "*c22Jw"}} 	V$$ V5:(N(N(N    44%'=     tt!#9	  
 
 	
 ttKv*tMMMr/   c                   t          d          t                    rd}|                     dt          j        |t          j                            }|                     dt          j        j                  }t          | |                     d          d	gt          j        g|g
          }|                     d|t          j        j                  }|                     d||          }|                     d||d	          }	nSfdt          d	          D             }
|                     dt          j        |
t          j                            }	|	S )Nr   rg  r   r\  r   r1  r2  rc  r   rK  rM  rL  DivConcatr8  c                    g | ]a}|d k     rdnVt          |z
                      t                                                                          |z
                      z  bS )rg        ?)r1   r   r   )r,   r   r   rJ  output_sizes     r-   r.   z/_interpolate_size_to_scales.<locals>.<listcomp>{  s}     
 
 

  1uu C{S1W:.//EJJLL&&((374556
 
 
r/   )rT   r9   r   rV   onesfloat32r  r>  rS  rQ  sysmaxsizeranger   )r|   rJ  r  r   offsetoffsetsdividenddivisor
scale_dimsscalesscales_constants    ```       r-   _interpolate_size_to_scalesr  n  sc   ";55K 
$$z5:fEM+R+R+R$SS44'2M2S4TTqttGU##1#S[M6(
 
 
 $$vwW-H-N$OOTT%733
hA>>
 
 
 
 
 

 1c]]
 
 
 _EM R R R  
 
 Mr/   c           	        t          |d         d          dk    ot          |d                    }|sd S |                     dt          j        dt          j                            }|                     dt          j        t          |d         d                              }|                     d||d	          }|S )
Nr   r   r   r   rg  r\  r   r  r8  )rT   r   r   rV   r  r  r   )r|   r  available_scalesr  scales_lists        r-   $_interpolate_get_scales_if_availabler    s    'q	488B> xq	H H D  tdd:uz!5='I'I'IdJJG$$EL)9&)T)J)JKK   K TT(G[T;;FMr/   c                t    |dk    rd }|dd          }n|d         }|dd          }t          | |          }||fS )Nnearestr   r   )r  )r|   moder}   align_cornersr  s        r-   _get_interpolate_attributesr    sQ    yabbQabb1!V<<F=  r/   c                   |                      dt          j        dt          j                            }t	                    }t                                          t          j                  s||dk    r|                      d|d          S t          | dg          |                      dt          j        j        	          fd
t          |dz
            D             } | j         d|g|R ddiS )Nr   rg  r\  r   r   r  r8  r1  r2  c                    g | ]}S r)   r)   )r,   r   scale_factors     r-   r.   z+_interpolate_get_scales.<locals>.<listcomp>  s    7771,777r/   r9  )r   rV   r  r  r   rF   r   r   r   r  r  r>  rS  r  )r|   r  r   r  scale_factor_rankr  s    `    r-   _interpolate_get_scalesr    s   dd:uz!5='I'I'IdJJG(66,##%%r{33 	8%*;a*?*?ttHg|At>>>(L1#>>ttLw'B'H  
 
 8777cAg77714'=F===1==Lr/   c                   t          |d          }d|v rd}d|v rd}t          |           t          |d          }t          |t                    r|rt	          dd          S |                                                                st	          dd          S |                                                                }t          |          st          | ||          }nt                    st                    sit          d                                          d	k    }|rAt          | d	g          fd
t          |dz
            D              | j        dgR dd	it          | ||          }nt	          dd          S ||fS )Nr   linearcubicr   interpolatezalign_corners == Truemissing input shaper   r   c                    g | ]}S r)   r)   r,   r   r   s     r-   r.   z4_interpolate_get_scales_and_mode.<locals>.<listcomp>  s    555555r/   rg  r  r9  z.Both size and scales are None in __interpolate)rT   r  rF   r<   r
  r   r   r   r  ru   r  r  r   r  )r|   rJ  r   r  r  r  r   	is_scalars     `     r-    _interpolate_get_scales_and_moder    s    D#&&D4$$]C88M-&& F= Fm-DEEE::<< Dm-BCCC
**,,



CL!! 
.q,DDd^^ 
t$$ 	7(s337799Q>I 7(D1#665555eC!Gnn555qtH6t666A6621eT3GGK
 
 	
 r/   rJ  torch._C.Valuekeepdimc           
          fd}t          |          rt           |                     dt          j        dg                              } ||dd          }|rz                     d|          }                     d|          }	                     d	|	t          j        d
gt          j                            }
                     d||
          }|S t          |d          } ||||          S )Nc                    j         dk    r                    | ||d          S                     | ||          S )N   F)r9  r  select_last_index_ir9  r  )rG  r   )rJ  r9  r  r|   r  s      r-   
op_wrapperz)_argmin_argmax_helper.<locals>.op_wrapper  sT    7b==44%$)     ttGU6jtIIIr/   r   r   r   r   Fr  rc  ConstantOfShaper   r\  Reshaper   )r   r;  r   rV   r   rk  rB   )r|   rJ  r   r  r  r  	flattenedr   input_shapeinput_shape_shape	new_shapes   `   `      r-   _argmin_argmax_helperr    s'   	J 	J 	J 	J 	J 	J }} #uadd:u|RD/A/AdBB
 
	 IaEBBB 	8$$w..K !Wk : :!!aS<<<   I
 TT)VY77F
S#

C:eCG<<<<r/   c                @    t          ddd          fd            }|S )NTFc                   t          | |          \  }}t          |          }dk    rdn|rdnd}||                     d|          }t          | |dgdgdg          }|                     d	|t          j        j        
          }|                     d||d          }| j        dk    rt          |           }	t          |           }
nj|                     dt          j
        g t          j                            }	|                     dt          j
        g t          j                            }
|                     d||	|
||dd	  	        S | j        dk    rt          |           }	n5|                     dt          j
        g t          j                            }	|                     d||	||dd          S )Nr  
asymmetricr  
half_pixelrc  r   rg  r  r1  r2  r  r8  r  r   r\  r   Resize      floor coordinate_transformation_mode_scubic_coeff_a_fmode_snearest_mode_s)r  rZ   r   rQ  r  r>  r<  rG  "_optional_input_placeholder_tensorrV   r   r  )r|   rJ  r  r}   r  r  coordinate_transformation_mode
input_sizeinput_size_beg	empty_roiempty_scalesr  s              r-   r  z(_interpolate_helper.<locals>.symbolic_fn  s    ;A?OQU V V)-88  9,, L  	' >gu--J*:QCqc1#  N $$'*E*K   K $$xQ$OOKw"}}>qAA	A!DDDDRu}(M(M(M !  	  !ttRu}(M(M(M  $     441O %'&  
 
 
 w"}}>qAA		DDRu}(M(M(M !  	 441O %'&  	 	 	r/   )r   )r  r   r  r  s     ` r-   _interpolate_helperr    s<    D%''< < < < ('<| r/   c                   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|                     d
|          }t          | |dgdgdg          }	 t                     o%t          d                                          dk    }	n6# t          $ r) t                     }	|	st          j
        d           Y nw xY w|	rbt          |          }
|
t          dd          S t          | dg          fdt          |
dz
            D              | j        dgR ddi|                     dt          j        j                  |                     d|d          | j        dk    rt'          |           }t'          |           }nj|                     dt)          j        g t(          j                            }|                     dt)          j        g t(          j                            }|                     d||||d|d	  	        S t          |          }
|
t          d d!          S | j        dk    rt'          |           }n5|                     dt)          j        g t(          j                            }t/          | ||
          }|                     d||||d|d          S )"Nr   r  r  r   Fr  r  r  r  rc  r   rg  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)c                    g | ]}S r)   r)   r  s     r-   r.   z(__interpolate_helper.<locals>.<listcomp>s  s    222QD222r/   r  r9  r1  r2  r8  r  r   r\  r   r  r  r  r  zinterpolate (with scales)r  )rT   rF   r<   r   r   rQ  ru   r   AttributeErrorr  r  r   r
  r  r  r  r>  r<  rG  r  rV   r   r  r  )r|   rJ  r   r  r  r  recompute_scale_factorr  r  r  rankr  r  r  s     `           r-   __interpolate_helperr  @  ss    D#&&D4$$]C88M!+M4!@!@SEEmM 9 	 __ # D>> H
TT'5))
"1js!aSQQQ


	+D111  s++//11Q6 I  	 	 	+D111I U  	  		3#E**D|%=U   %Qqc22D2222%q//222D142422222DttFDw'B'HtIIttHj$qt997b==:1==I=a@@LLZb1V1V1VWWI44EL5=$I$I$I    L tt-K!"  

 

 
	
  &&<!"=?TUUU7b==:1==IIZb1V1V1VWWI(L$??tt-K!"  	
 	
 		
s   6C
 
0C=<C=c                r    | 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  )rG  rH  r  torch.onnx.symbolic_opset11torch.onnx.symbolic_opset13)r|   r   r   r   r  s        r-   _unbind_helperr    sj    w||5555555	
B66666666666666!T3)))r/   c                P    | j         dk    rddlm} nddlm}  || ||||          S )Nrd  r   scatter)rG  rH  r  r  )r|   r   r   r   srcr  s         r-   _scatter_helperr    sN    w"}}6666666 	87777771dC,,,r/   c                    | j         dk    r|                     d|dg|z  ||          }nCddlm} |                     dt	          j        dg|z                      } || ||||	          }|dk    r|n|gS )
Nr  Splitr   )split_ir9  ri  r   )splitr   r   )r   )rG  r   r  r  rV   r   )r|   r   repsr   	split_outr  repeatss          r-   _repeat_interleave_split_helperr    s    w"}}DD$d
3PTDUU		555555$$z5<d
+C+C$DDE!T7C$???	q99yk1r/   c                   ddl m}m} t          |          s)|                     dt          j        |                    }t          |          }t          |d          }t          |          dk    r?|                     d||                     dt          j
        dg                              } || ||dz             }|rMt          j        t          |          t
          j                  }	||	|dz   <   |                     d|	          }
n|                     d	 || |dz   d          |                     dt          j
        t          |                              |                     d
|                     dt          j
        dg                    |d                    }	 || |	dd          }
|                     d||
          } || |||dz             S )Nr   )flatten	unsqueezer   r   r   r  r   r\  OneHotr  r8  Tile)rH  r  r  r   r   rV   r:  r\   rT   r   r   r  rk  )r|   r   r  r   r  r  const_repeatsr  
unsqueezedonehotrepeats_per_dimtileds               r-   -_repeat_interleave_single_value_repeat_helperr    s    >=======g F$$z5+;G+D+D$EE&w//MGS))D   A%%$$y'144
ELRSQTDUDU4+V+VWW 1dC!G,,J  3,Z88LLLsQw$$z6$:: Iaq!$$DDEL1A*1M1M$N$N    DD!$$z5<3D3D$EEwWX   	
 	
 "'!VQ22DD_55E71eS#'***r/   Stuple[_type_utils.JitScalarType, _C.Value | None, _C.Value | None, _C.Value | None]c                `   d }|t          |          r`t          |          rQ ||||g          rt          j        j        }n\t          j                            t          j                              }n+t          |t                    sJ t          j        |          }|r*| 
                    d||                                          nd }|r*| 
                    d||                                          nd }|r*| 
                    d||                                          nd }||||fS )Nc                    | D ]^}t           j                            |t           j        j                  }|t           j        j        k    r|t           j        j        k    r dS _dS )NFT)r   r   r   r   r<  )scalarsr^  r   s      r-   _is_all_integralz-_arange_cast_helper.<locals>._is_all_integral  se     	 	F%3>>1; K {8>>>;#<#FFFuutr/   r1  r2  )r9   r   r   r   r<  r)  rV   get_default_dtyperF   r*   r   r4  )r|   endstartstepr'  r  r   s          r-   _arange_cast_helperr    s9   
 
 
  }5))}huoo}UC.// 	%39KK%3>>')) KK %%%%%%!/66AFPADD[%:%:%<%<D===DE=@
J!$$vs!6!6!8!8$
9
9
9dC?CM144;#8#8#:#:4;;;DUD((r/   c                D    | j         dk    rddlm} nddlm}  || g|R  S )Nrd  r   )arange)rG  rH  r  r  )r|   r}   r  s      r-   _arange_helperr    sL    w"}}55555556666666!dr/   c           
         |                      d|          }ddlm}  || ||                      dt          j        dg                    |          S )Nrc  r   )selectr   r   )r   rH  r  rV   r   )r|   r   r   
full_shaper  s        r-   _size_helperr    sY    gt$$J1111116!Zj%,s:K:K!L!LcRRRr/   c           
     4  
 ddl m} | j        dk    rddl m} nddlm} |                                                                t          dd          S |                                                                }t          |d          

dk     r
|z  
t          | |
fdt          |          D                       } || |                     d	|          dt          | |dg          |                     d	|                    } || ||d           }	||	fS )
Nr   )expandrd  r  
index_fillzinput rank not accessibler   c                     g | ]
}|k    |S r)   r)   )r,   r   	dim_values     r-   r.   z._index_fill_reshape_helper.<locals>.<listcomp>9  s    @@@i1r/   rc  )rH  r  rG  r  r  r   r   r
  rB   r  r  r   )r|   r   r   r   r  r  self_dimunsqueezed_indexexpanded_index_shapeexpanded_indexr  s             @r-   _index_fill_reshape_helperr"  $  sN    211111w"}}6666666 	877777yy{{ l,GHHHyy{{  H3$$I1}}X	(	5@@@@eHoo@@@  #7	144#4QaS#A#A144QVCWCW  VA/1EtLLN//r/   c                L   t          |d          }t          |          s)|                     dt          j        |                    }| j        dk    r9|dk    rt          dt          j        d|           |                     d||          S |                     d|||	          S )
Nr   r   r   r  r   zReshape with allowzero=1   r  )allowzero_i)	rT   r9   r   rV   r:  rG  r  r   r  )r|   rJ  rX   	allowzeros       r-   r;  r;  G  s    UD))EU BZ)9%)@)@AAw"}}>>#*G,MrSX   ttIue,,,ttIuetCCCr/   c                   ddl m} t          |d          }t          |d          }|t          |          rw|t	          j        d|          t          j        dg|z  t          j	        
                    |                                                    }	|                     d|	          }|t          |          rw|t	          j        d|          t          j        d	g|z  t          j	        
                    |                                                    }
|                     d|
          }| t          |          s|t          |          r||J t          | ||                     dt          j        ||d
gt          j                                      }|                     d|g d          } || ||                     dt          j        ddgt          j                            dd          \  }}||||fS )Nr   )	_var_meanr   z@Unsupported: ONNX export of batch_norm for unknown channel size.r  r\  r   r   g        r   	Transpose)r   rg  r   )perm_iF)rH  r(  r   r   r   r>   rV   r   r   r   r   r'  r   r;  rk  )r|   rJ  weightbiasrunning_meanrunning_varr(  
batch_sizechannel_sizeweight_value
bias_value
reshape_intrans_ins                r-   _batchnorm_helperr5  U  sJ    544444%eQ//J'q11L~&))~+R   |EL +66u==CCEE
 
 
 j,77|x~~|+R   \EL +66u==CCEE
 
 

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

 44Z			4BB$-IDDU\1a&%L%L%LDMM%
 %
!\ 4{22r/   tuple_fnCallable[[Any], Sequence[int]]paddingint | Sequence[int]tuple[int, ...]c                    |r:|                                                                 dk    rt          |d           t           | |                    S )Nr   divisor_override)r7   r;   r
  ri   )r6  r8  kernel_sizestrider<  r  s         r-   _avgpool_helperr?    sY      1,113388::>NNNt/000'""###r/   op_train_modec                0   t           j        t          j        j        k    rdS | rt          j        j        }nt          j        j        }|t           j        k    rdS dt          |            }t          j	        dt           j         d| d| d| d	           dS )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<   r  r  )r@  r  op_mode_enumop_mode_texts       r-   check_training_moderI    s     4 === 1+4+0w,,,1D//11L M	Dg&; 	D 	DW 	D 	D!	D 	D4@	D 	D 	D    r/   c                   |                      d|          }t          | |dgdg|g          }||                      dt          j        dgt          j                            g}||dz
  k     rSt          | |dg|dz   g|g          }||                      dt          j        dgt          j                            |g} | j         d	g|R d
di}	ddlm}
  |
| ||	          S )Nrc  r   )rK  rL  rM  r   r   r\  r   r   r  r9  )_reshape_from_tensor)r   rQ  rV   r   r  rH  rK  )r|   rJ  	start_dimend_dimr   r  slice1slicesslice3final_shaperK  s              r-   _flatten_helperrR    s   gu%%J1jsA3i[QQQFadd:u|RD
/S/S/SdTTUFqzWq[M
 
 
 DDU\2$ej%I%I%IDJJ
 !$x3&33333K??????5+666r/   c                    |dS t          |           r,|                                                                 dk    rdS dS )NFr&   Trt   )split_size_or_sizesr   s     r-   _is_split_staticrU    sL    u%&&$$&&++--1AAAu4r/   c                    |                      d          }|                    t          j                                                   |S )Nr   )r   setTyper   OptionalTypeofTensor)r|   ns     r-   r  r    s9    	AIIbo&&(()))Hr/   c                    t                    }|@t          fdt          |          D                       r|                     |d          S |                     |d          S )Nc              3  @   K   | ]}t          |          d k    V  dS )r   N)r   )r,   r   r   s     r-   	<genexpr>z*_handle_reduce_dim_none.<locals>.<genexpr>  sD          /0T1%%*           r/   r   r  r   )r   r   r  r   )r|   r   r  r  s    `  r-   _handle_reduce_dim_noner_    s    D!!DC        49$KK       
 ttGTat00044!4,,,r/   qtensorqdtype"_C_onnx.TensorProtoDataType | None4tuple[_C.Value, _C.Value, _C.Value, _C.Value | None]c                r   t          |          }|dd         \  }}}t          |          dk    r|d         nd}t          |dd          }t          j                            |          }	|(|	|	                                }nt          j        j	        }| 
                    d||          }
| 
                    d|t          j        j                  }| 
                    d||          }|-t          j        dk     rt          d	t          j        dd
|           | 
                    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.
    Nrm   rn   r   axisr1  r2  r  DequantizeLinear Attribute axis is not supported.r8  )rr   rp   r]   r   r   r   r4  r  r>  UINT8r   rS  r   r  r  )r|   r`  ra  unpacked_qtensorsr   r   r   re  r9  input_qdtyper'   s              r-   r   r     sK   " 199 1"1" 5FE:#&'8#9#9Q#>#>QDDc6**F,77??L~#!++--FF06FDDfD--EDDW%@%FDGGEfjv66Jg?"DD(-.	
 	
 	
 	
z&II	 r/   r   re  c                   |<t          |          s-t          j        dk     rt          dt          j        dd|           |J t          j                            |t          j        j                  t          j        j        k    r'| 	                    d|t          j        j                  }|J t          j                            |t          j        j                  t          j        j        t          j        j        hvr'| 	                    d|t          j        j                  }| 	                    d|||t          |dd          	          }|||g}|$t          |          s|                    |            | j	        d
g|R  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.
    Nr  QuantizeLinearrg  r1  r2  r   re  r8  r   )r   r   r  r  r   r   r   r   rS  r   r  r>  rh  INT8r]   r   )r|   r   r   r   re  r   r}   s          r-   r   r     s   * 	 	-22(-.	
 	
 	
 !,,UK4M4WXX$*	+ 	+ VU)D)JKK!!! ++K-7  	!'!&  TT&*73N3TTUU
TT$V,,   F E:&DD14&.....r/   c                   |                      d||          }|                      d|          }|                      d|t          j        dgt          j                            }|                      d|                      d||          t          j        j        	          }g }	|$t          |          s|	                    |            | j         d|||g|	R  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.
    Mulrc  r  r   r\  r   r1  r  r2  Nr   )	r   rV   r   r*   r  r>  INT32r   r   )
r|   r,  input_scaleweight_scalere  
bias_scalebias_scale_shapebias_zero_pointq_bias	axis_argss
             r-   requantize_bias_helperrx  Q  s     e\;77JttGZ00dd+U\1#UY5W5W5W   O TTUD*--G4O4U   F I14&
OXiXXXXr/   c                    | sJ t           j                            | d                   t          fd| D                       }|S )Nr   c              3  ^   K   | ]'}t           j                            |          k    V  (d S rR   )r   r   r   )r,   elem
base_dtypes     r-   r]  z'args_have_same_dtype.<locals>.<genexpr>j  sJ        EI!,,T22j@     r/   )r   r   r   all)r}   has_same_dtyper|  s     @r-   args_have_same_dtyper  g  sc    KKK*55d1g>>J    MQ    N r/   c           
        |                     dd          }|                     dt          j        j                  }t	          |          }t          j                            |d                   }t          |d                    o|du pt          j        |k     }|r|D ]}}	|		                                rgt          j                            |	          }
|
|k    rBt          j        d| d|                                 d|
                                 |	          ~t          |          D ]U\  }}	|		                                r<t          |	          s-|                     d|	|                                	          ||<   V | j        |g|R i |}|r*|                     d||                                	          }|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 r1  r2  )popr   r   rS  rb   r   rW  r   r  isCompleteTensorr   r>   r   r   r   r4  )r|   r  r}   r   r  r  r?   dtype_0require_castrJ  input_scalar_typer   r   s                r-   _op_with_optional_float_castr  p  s   " ::nd33LZZ 0+2K2QRRN$ZZF'226!9==GfQi((( P AL P    	 	E%%'' $/$=$H$H$O$O!$// 3]W ] ]&2244] ];L;X;X;Z;Z] ]  
 "&)) 	 	HAu%%'' u DD'1133 !  q	 14+&+++F++D <ttFDw'8'8':':t;;Kr/   c                $   t           j                            |t           j        j                  }|t           j        j        k    rKt	          |          s<|t           j        j        k    r'|                     d|t          j        j                  }|S r0  )	r   r   r   r   rW  r<  r   r  r>  )r|   r   r   s      r-   _maybe_cast_reduce_op_inputr    s{    +66k'1 K k/999 d|| 	N{/H/N N N447+F+L4MMDKr/   c                       fd}|S )z_Returns a decorator that calls the decorated (higher-order) function with the given parameters.c                     | i S rR   r)   )ry   r}   r   s    r-   _applyz_apply_params.<locals>._apply  s    r4"6"""r/   r)   )r}   r   r  s   `` r-   _apply_paramsr    s)    # # # # # # Mr/   c                     d fd	}|S )Nc                .   t          | |          }||dk    rt          | |          S t          |dd          }| j        dk     r7rdnd}t          ||d          }r|n|g}|                     |||          S t          |          r|}nnr6|                     dt          j        |t          j        	          
          }n6|                     dt          j        |gt          j        	          
          }|                     |||          S )Nr)   r   r     r   r   r  r   r\  r   r^  )	r  r_  r]   rG  r   r9   rV   r   r  )	r|   r   r   r  r    dim_listrK  allow_multi_dim_supportonnx_op_names	          r-   symbolicz,_reduce_op_symbolic_helper.<locals>.symbolic  s5   *1d33;#)) +1dLAAA !#y99Gw||6?ttC dE22"9D33uttL$xGtTTTS>> 
DD.  tt&S
0S0S0S  $      !tt&cU%*0U0U0U  $     ttL$tIIIr/   NNr)   )r  r  r  s   `` r-   _reduce_op_symbolic_helperr    s5    J J J J J J J: Or/   c                F     t          j                    fd            }|S )Nc                     | g|R  }|D ]4}|j         }t          |          t          |          k    r || g|R  c S 5t          dj         dt          |           d          S )Nzaten::zwith z
 arguments)r   rp   r
  r   )r|   r}   	overloadsoverloadrv   ry   s        r-   r   z'_overload_by_arg_count.<locals>.wrapper  s    BqL4LLL	! 	* 	*H&7O?##s4yy00x)D)))))) 14r{446Sc$ii6S6S6STTTr/   r   )ry   r   s   ` r-   _overload_by_arg_countr    s=    _RU U U U U Nr/   r  r  c                V    t          |           t          fd            }|S )N)r  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                0   d }|                                                                 dk    rPt          |dd          }t          j        |                                          }|                     d||          }n;|                                                                 dk    rt          d|          S  | |          }|Ot          j                            |                                          }||k    r|                     d||          }|S Nr&   r   r'  r1  r2  r   	r7   r;   r]   r   r   r4  r   r
  r   )r|   r   r'  
dtype_onnxresultresult_dtype_onnxr  r  s         r-   reduce_nodimz?_reduce_with_dtype_helper.<locals>.reduce.<locals>.reduce_nodim  s	    Jzz||  ""&666"5#w77(6u==GGII
ttFDzt::""$$(888%dGU;;;Xa&&F%$/$=$H$H% %)++ " %
22TT&&zTBBFMr/   r   r   c                4   d }|                                                                 dk    rPt          |dd          }t          j        |                                          }|                     d||          }n;|                                                                 dk    rt          d|          S  	| |||          }|Ot          j                            |                                          }||k    r|                     d||          }|S r  r  )
r|   r   r   r  r'  r  r  r  r  r  s
           r-   
reduce_dimz=_reduce_with_dtype_helper.<locals>.reduce.<locals>.reduce_dim  s    Jzz||  ""&666"5#w77(6u==GGII
ttFDzt::""$$(888%dGU;;;XasG44F%$/$=$H$H% %)++ " %
22TT&&zTBBFMr/   )r   r   )	r|   r}   r   r  dim_descr  r  r  r  s	         r-   reducez)_reduce_with_dtype_helper.<locals>.reduce  s    				C	 	 	 	 	 	 	 
!	  
		" 3;44				C3	/	/	 	 	 	 	 
0	/ 
		" Z''r/   )r  r  )r  r  r  r  r  s    `` @r-   _reduce_with_dtype_helperr    sY     *)@  H )( )( )( )( )( )( )(V Mr/   c                   |||                      d|d          S |t          | d||d          S t          |dd          }t          |dd	          }| j        d
k     r|                      d||g|          }nO|                      dt	          j        |gt          j                            }|                      d|||          }|                      d|||          }||fS )N	ReduceMaxr   r^  Maxr  r  r   r  r   r  r  r   r\  r   ArgMaxr  r   r  r]   rG  rV   r   r  )r|   r   dim_or_yr  r   maxrK  indicess           r-   _max_helperr        GOttK!t444+AudHSUVVVV Wc9553..7R<<$${D#7$KKCC44
EL#ej,Q,Q,Q4RRD$${D$7$CCC$$xcg$FFG|r/   c                   |||                      d|d          S |t          | d||d          S t          |dd          }t          |dd	          }| j        d
k     r|                      d||g|          }nO|                      dt	          j        |gt          j                            }|                      d|||          }|                      d|||          }||fS )N	ReduceMinr   r^  Minr  r  r   r  r   r  r  r   r\  r   ArgMinr  r  )r|   r   r  r  r   minrK  r  s           r-   _min_helperr  2  r  r/   c                ^    |                      d|          }|                      d|d          S )Nrc  
ReduceProdr   r^  )r   )r|   r   rX   s      r-   _numel_helperr  F  s-    DD$E44e4222r/   r   r   r   c           
        | j         dk     r|+|                     d|d          }|}t          | |          }n|                     d|||          }|                     d||d          }|                     d|          }|                     d||                     d	t          j        |          
          d          }|                     d|d          }|                     d||          }	|                     d|	|	          }
|dn|}|                     d|
||          }|d}|dk    r|                     d|t
          j        j                  }|                     d	t          j        |t          j                  
          }|                     d||          }|                     d||                     d||                    }||fS d }|+|                     d|d          }|}t          | |          }n|                     d	t          j        |t          j	                  
          }|                     d|||          }|                     d||d          }|                     d|          }|                     d||                     d	t          j        |          
          d          }|                     d|d          }|                     d||          }	|                     d|	|	          }
|dn|}||                     d|
|          }n|                     d|
||          }|d}|dk    r|                     d|t
          j        j                  }|                     d	t          j        |t          j                  
          }|                     d||          }|                     d||                     d||                    }||fS )Nr  
ReduceMeanr   r^  r  r   rc  r7  r   r   r8  r  Subro  r1  r2  r\  r  )
rG  r   r  rV   r   r  r>  rS  r1   r  )r|   rJ  r   
correctionr  meant_meannum_elementsredudced_dimssub_vsqr_subkeepdim_meanvaronemulrK  s                   r-   _var_mean_helperr  K  s   w||;44e4::DF(E22LL44eCG4LLDTT,caTHHFDD%00MDDZc):):;;	 !  M 44m4JJLUE6**$$ueU++KqqWdd<dNNJ??447+F+L    L $$z5<
%++V+V+V$WWC$$uc<00C$$uc144|S#A#ABBCDy;44e4::DF(E22LL44
ELEJ,O,O,O4PPD44eTg4FFDTT,tTBBFDD%00MDDZc):):;;	 !  M 44m4JJLUE6**$$ueU++KqqW<$$|W$FFCC$$|Wd|$LLCJ??447+F+L    L $$z5<
%++V+V+V$WWC$$uc<00C$$uc144|S#A#ABBCDyr/   c
                t   |rt           j        rt          d          S |	|	dk    rt          d          |                     dt          j        d                    }
|                     d|
t          j        j	                  }
|                     dt          j        dg                    }t          | t          | ||                     dt          j        d                              dg          }|s||g} | j        d	g|R d
di}t          | |dgdgt          j        gdg          }t          | |dgdgt          j        gdg          }t          | ||                     dt          j        d                              }t          j        | d||
d          \  }\  }}|j        }t%          j        |          }t%          j        |          }|                    d||d          }|                    d||d          }t          ||dg          }t          ||dg          }|                    d||||          }|                    d||d          }t)          |          sB|                    d||||          }t          ||dg          }|                    d||          }|dk    rt+          ||dgd          }n|dk    rv|j        dk     r|                    d|dgd          }n|                    dt          j        dgt
          j                            }|                    d||d          }nu|j        dk     r|                    d|dgd          }nO|                    dt          j        dgt
          j                            }|                    d||d          }|                    d|
t          j        j	                  }t%          j        ||           t%          j        ||           |                                                                d d d fS )Nz7embedding_bag with scale_grad_by_freq for training moder   zembedding_bag with padding_idxr   r   r   r1  r2  r  r9  )rK  rL  rM  rN  Loop)n_blocksr7  r8  Slicero  r  r  r  r\  r^  r  )r   export_trainingr	  RuntimeErrorr   rV   r   r  r>  rY  r  r  rQ  r  r  r   add_op_with_blocksblockr   _add_input_to_blockr   r  rG  r  _add_output_to_blockr7   r   )r|   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_rowrK  cond_outs                                r-   _embedding_bag_helperr    s     
g5 
 E
 
 	
 ;!#3#3;<<<TT*el1ooT>>NTT&.w7R7WTXXN44
EL!$5$5466D#	Qj%,q//!J!JKK	
 K
  5K(!$x4'444!44
 #	7!aS}QC  N !	7!aS}QC  L A|QTT*elSTooT-V-VWWH(;	68^a     D/<1 #J 0<<$Z00D OO."21 $  M //(L:JST/UUK%lMA3GGM#L+sCCK//'7M;PTUUK+;[QRSSJ&'' P!-'T"
 "
 "301#"
 "
 "__UJ8NOO
qyy&*aSQ
 
 


 
""%j! )  JJ  ??EL!EJ$G$G$G #  D &z4TUVVJJ""%Z )  JJ  ??EL!EJ$G$G$G #  D &j$STUUJW%@%E   H 
z8444	z:666 99;;tT11r/   r   ordr1   Sequence[int] | Noner'  c                	   d }t          |          rt          | |dg          }d}n@| j        dk    r5|                     dt	          j        |t          j                            }|t          j        k    r| j        dk     r/|                     d|                     d|          ||	          }n|.|                     d|                     d|          |
          }n|                     d|                     d|          ||
          }nk|t          j         k    r| j        dk     r/|                     d|                     d|          ||	          }n |.|                     d|                     d|          |
          }n|                     d|                     d|          ||
          }n|dk    r| j        dk     rt          dddd|          S |Gt          | ||                     dt	          j        dgt          j
                                      }d}|                     d|                     d||                     dt	          j        dg                                        }|                     d|t          j                            |                                                    }t!          | |||	          S |dk    rf| j        dk     r t#          d          | |||          }nw| t#          d          | ||          }nX t#          d          | |||          }n:|dk    rd| j        dk     r t#          d          | |||          }n| t#          d          | ||          }n t#          d          | |||          }n|                     dt	          j        |t          j                            }	t!          | |                     d|                     d|          |	          ||	          }|                     d||                     d|                     dt	          j        dt          j                            |	                    }t          |          sMt'          |dd          }|                     d|t          j        |                                                    }|S )Nr   Fr  r   r\  r   r  Absr  r^  r  r   r  linalg_vector_normrE  zord=0 not supportedNotEqualr1  r2  r   ReduceL1)r   r  )r  rg  ReduceL2Powr  r   r'  )r   r;  rG  r   rV   r   r  mathinfr  rk  r:  r   r   r   r4  r  r  r  r]   )
r|   r   r  r   r  r'  rK  r  cond_opord_ops
             r-   _linalg_vector_norm_helperr    s    D}} Mq$--	
BttJS
(K(K(KtLL
dh7R<<TTQTT%..sw   FF |k144t+<+<QQk144t+<+<dwWW				7R<<TTQTT%..sw   FF |k144t+<+<QQk144t+<+<dwWW	7R<<3$a-BD   {&DDU\2$ek-R-R-RDSS 
  ddWdADDU=Mqc=R=RD$S$STT G dd .99$??IIKK   G
 %QPPPP	7R<<;/
;;4S'  FF |?3J??tW   @3J??tT7   
7R<<;/
;;4S'  FF |?3J??tW   @3J??tT7   j%,s%-*P*P*PQQ"qttE144t,,f55cg
 
 
 DDZau})M)M)MNN 
 
 E?? Y5#w//ff;+DU+K+K+U+U+W+WX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]  r1   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  )r    r!   r"   r#   r$   r#   )r7   rC   rD   r=   )r'   rK   )r'   rO   rP   r!   )r^   rK   r_   r`   )re   rK   r_   rf   )r^   r   r_   r<   )rv   r!   r_   rw   )
r   r<   r   r   r   r   r   r<   r_   r   )r   r   r_   r   )r   r   r_   r<   )r'   r   r_   r<   )r   rK   r_   r<   )r   r   r_   r   )r   rK   r_   r   )T)r   rK   r   r<   )r   rK   r   r*   r_   r   )r   rK   r   r   rR   )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  r=   r'   r  r_   r   )r  r=   )r_   r!  )r_   r$  )r|   r.  r   r$  )r|   r.  )r_   r<   )TN)TFN)Nr   r   )
r|   r.  rJ  r  r   r  r  r<   r  r=   )NNN)r|   r.  r_   r  )r   )r6  r7  r8  r9  r_   r:  )r@  r*   r  r=   r_   r  )r|   r.  r`  rK   ra  rb  r_   rc  )r|   r.  r   rK   r   rK   r   rK   re  r  r_   rK   )r  r=   r  r=   r  r<   )r|   r.  r   r  r  r1   r   r  r  r<   r'  r  )
__future__r   r   r   r  r  r   r  r   r   r   r   r   r	   _TypeVartyping_extensionsr
   _Concatenater   
_ParamSpecrV   torch._C._onnxr   _onnxr  
torch.onnxr   r   r   r   torch.onnx._globalsr   torch.onnx._internalr   TYPE_CHECKINGtorch.typesr   r   r   r   r!   rB   r6   rN   rT   rZ   r]   rd   rk   rr   ru   r   r   r   r   r   r9   r\   r   r   r   r   r   rh   r   r   r   r   r   r
  r	  r  r  r   r#  r-  r5  rC  rQ  rW  rZ  r_  rp  rv  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  r;  r5  r?  rI  rR  rU  r  r_  r   r   rx  r  r  r  r  r  r  r  r  r  r  r  r  r  r>  rh  rm  rT  rS  FLOAT16rp  r<  INT16rY  r   r   rV  r   cast_pytorch_to_onnxscalar_name_to_pytorchuint8int8shortr*   rk  r  r1   r]  	complex32r  r  r<   r  r  r  r  scalar_type_to_pytorch_typepytorch_name_to_typescalar_type_to_onnxsetr  __annotations__r)   r/   r-   <module>r4     sj   " " " " " " "       



   R R R R R R R R R R R R R R R R R R R R R R R R                          > = = = = = = = = = = = ' ' ' ' ' ' * * * * * * 
 #""""""Xd^^Xd^^Z 
     	B B B B BJ# # # #3 3 3 3
	 	 	 	  # # #$ $ $ $& & & &   .W W W WO O O Oh ! 	E E E E E EP     &V V V V# # # #   5 5 5 5
   / / / /> > > >
) 
) 
) 
)5 5 5 5          ) ) ) )
   1 1 1 1 1, , , , ,$ "	, , , , ,. ", , , , ,&      
; 
; 
; 
;   3 3 3 3 3< = = = = =$   * * * *: : : :$
 
 
 
 
( OS
 
 
 
 
*% % % %  "/ / / /* * * *8 N N N N N>   4    ! ! ! !   "       F#= #= #= #=L@ @ @F`
 `
 `
 `
F* * * *- - - -2 2 2 2'+ '+ '+ '+V BF') ') ') ') ')T   S S S S0 0 0 0FD D D D D63 63 63 63r
$ 
$ 
$ 
$   27 7 7 7(    - - - - 26- - - - -j !:/ :/ :/ :/ :/| FJY Y Y Y Y,  2 2 2 2j	 	 	 	     B
 
 
 >B3 3 3 3 3l    (    (3 3 3 3
 CsC  E E E ! EPg2 g2 g2 g2Tj j j jb '-',)0(.'/&,'-(.',/90;+4,6  $ !  0 
K	J	K	I	K	J	K	L	O	O		J	K	L	L	N! . KJl[J9K[JO%[ll  *   !  !"%()   $! * 355            r/   