
    bIge                        d dl mZ d dlmZ d dlZd dlmZ d dlm	Z	m
Z
 d dlmZmZ  G d de      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )annotations)AnyN)	NodeProto)convert_from_ml_dtypesconvert_to_ml_dtypes)OpRunRuntimeTypeErrorc                      e Zd ZdZd Zy)
OpRunUnaryzmAncestor to all unary operators in this subfolder.

    Checks that input and output types are the same.
    c                   | j                  d| j                  j                         t        |      }	 | j	                  |      }t        |d         f}| j                  d	| j                  j                  t        |             | j                  |      S # t
        $ rC}t        ddj                  d |fD               d| j                  j                  d      |d}~ww xY w)
z|Calls method ``_run``, catches exceptions, displays a longer error message.

        Supports only unary operators.
        z-- begin %s.run(1 input)Issues with types , c              3  D   K   | ]  }t        t        |              y wNstrtype.0_s     S/var/www/html/answerous/venv/lib/python3.12/site-packages/onnx/reference/ops/_op.py	<genexpr>z!OpRunUnary.run.<locals>.<genexpr>#   s     .Is47|.I    z (unary operator ).Nr   -- done %s.run -> %d outputs)
_log	__class____name__r   _run	TypeErrorjoinr   len_check_and_fix_outputs)selfxreses       r   runzOpRunUnary.run   s    
 			,dnn.E.EF #	))A,C &c!f-/		0$..2I2I3s8T**3//  	$TYY.IaS.I%I$J K##'>>#:#:"=RA 	s   B 	C >CC Nr   
__module____qualname____doc__r(        r   r   r      s    
0r.   r   c                      e Zd ZdZd Zy)OpRunUnaryNumz{Ancestor to all unary and numerical operators in this subfolder.

    Checks that input and output types are the same.
    c           	     X   t         j                  | |      }t        |      dk(  s|d   |S t        |d   t              s\|d   j
                  |j
                  k7  r@t        d|j
                   d|d   j
                   d| j                  j                  d      | j                  |      S )zCalls method ``OpRunUnary.run``.

        Catches exceptions, displays a longer error message.
        Checks that the result is not empty.
        r   zOutput type mismatch: input 'z' != output 'z' (operator r   )
r   r(   r"   
isinstancelistdtyper	   r   r   r#   )r$   r%   r&   s      r   r(   zOpRunUnaryNum.run1   s     nnT1%s8q=CFNJ#a&$'CFLLAGG,C"/yc!fll^ T!^^447r;  **3//r.   Nr)   r-   r.   r   r0   r0   +   s    
0r.   r0   c                      e Zd ZdZd Zy)OpRunBinaryznAncestor to all binary operators in this subfolder.

    Checks that input and output types are the same.
    c                4   | j                  d| j                  j                         ||0t        dt	        |       dt	        |       dt	        |        d      |j
                  |j
                  k7  rWt        d|j
                   d|j
                   d| j                  j                  d	|j                   d
|j                   d      t        |      }t        |      }	 | j                  ||      }t        |d         f}| j                  d| j                  j                  t        |             | j                  |      S # t        t        f$ rD}t        dd
j                  d ||fD               d| j                  j                  d      |d}~ww xY w)z}Calls method ``_run``, catches exceptions, displays a longer error message.

        Supports only binary operators.
        z-- begin %s.run(2 inputs)Nzx and y have different dtype:  != z ()zInput type mismatch: z (operator 'z
', shapes r   r   r   c              3  D   K   | ]  }t        t        |              y wr   r   r   s     r   r   z"OpRunBinary.run.<locals>.<genexpr>^   s     .Ls47|.Lr   z (binary operator r   r   )r   r   r   RuntimeErrorr   r4   r	   shaper   r   r    
ValueErrorr!   r   r"   r#   )r$   r%   yr&   r'   s        r   r(   zOpRunBinary.runH   s   
 			-t~~/F/FG9	0a	d1gYbdTUV  77agg"'yQWWI >"nn558 9''"QWWIR1 
 !# #	))Aq/C &c!f-/		0$..2I2I3s8T**3// :& 	$TYY.LaV.L%L$M N$$(NN$;$;#>bB 	s   "E F?FFNr)   r-   r.   r   r6   r6   B   s    
0r.   r6   c                      e Zd ZdZy)OpRunBinaryComparisonzEAncestor to all binary operators in this subfolder comparing tensors.N)r   r*   r+   r,   r-   r.   r   r@   r@   f   s    Or.   r@   c                      e Zd ZdZd Zy)OpRunBinaryNumznAncestor to all binary operators in this subfolder.

    Checks that input oud output types are the same.
    c                R   t         j                  | ||      }|d   j                  |j                  k7  rdt        d|j                   d|d   j                   d|j                   d| j                  j
                  dt        |       dt        |             | j                  |      S )zVCalls method ``OpRunBinary.run``, catches exceptions, displays a longer error message.r   zOutput type mismatch: r8   z or z (operator z
) type(x)=z	 type(y)=)r6   r(   r4   r	   r   r   r   r#   )r$   r%   r>   r&   s       r   r(   zOpRunBinaryNum.runp   s    oodAq)q6<<177""(	c!fll^4y Q!^^447 8 G9Id1gY8 
 **3//r.   Nr)   r-   r.   r   rB   rB   j   s    
	0r.   rB   c                  *    e Zd ZdZ	 	 	 	 	 	 ddZd Zy)OpRunBinaryNumpyzI*numpy_fct* is a binary numpy function which
    takes two matrices.
    c                @    t         j                  | ||       || _        y r   )rB   __init__	numpy_fct)r$   rH   	onnx_node
run_paramss       r   rG   zOpRunBinaryNumpy.__init__   s     	i<"r.   c                    t        |      }t        |      }| j                  ||      f}t        |d         f}| j                  |      S )Nr   )r   rH   r   r#   )r$   abr&   s       r   r   zOpRunBinaryNumpy._run   sK     # #~~a#%%c!f-/**3//r.   N)rH   r   rI   r   rJ   dict[str, Any])r   r*   r+   r,   rG   r   r-   r.   r   rE   rE   |   s)    ##)2#@N#0r.   rE   c                  0    e Zd ZdZddZd Zd Zd Zd Zy)	OpRunReduceNumpyzFImplements the reduce logic.
    It must have a parameter *axes*.
    c                   t        j                  | ||       t        | d      rt        | j                  t
        j                        rat        | j                  j                        dk(  s| j                  j                  d   dk(  rd | _        y t        | j                        | _        y | j                  g dfv rd | _        y t        | j                  t              rt        | j                        | _        y y y )Naxesr   r-   )r   rG   hasattrr2   rR   npndarrayr"   r<   tupler3   )r$   rI   rJ   s      r   rG   zOpRunReduceNumpy.__init__   s    tY
34 $))RZZ0tyy'1,		0Ba0G $DI %dii 0DIr2h& 	DIIt,!$)),	 - !r.   c                
    |d u S r   r-   r$   rR   s     r   is_axes_emptyzOpRunReduceNumpy.is_axes_empty   s    t|r.   c                   t        |t              rt        |      dk(  ry |S |y t        |t        t        f      r|S t        |t        j
                        st        dt        |       d      t        |j                        dk(  rt        |      S d|j                  v ry t        |j                         j                               S )Nr   zaxes must be an array, not .)r2   rV   r"   intrT   rU   r    r   r<   raveltolistrX   s     r   handle_axeszOpRunReduceNumpy.handle_axes   s    dE"4yA~K<dS%L)K$

+9$t*QGHHtzz?at9

?TZZ\((*++r.   c                F    t        j                  |||      j                  S )N)axiskeepdims)rT   sumr<   )r$   datarR   rb   s       r   output_shapezOpRunReduceNumpy.output_shape   s    vvd9???r.   c                n    | j                  |||      }t        j                  |||j                        fS )z<Special case reduction where the output value is a constant.)r4   )re   rT   fullr4   )r$   rd   	const_valrR   rb   re   s         r   reduce_constantz OpRunReduceNumpy.reduce_constant   s0    ((tX>itzzBDDr.   N)rI   r   rJ   rN   )	r   r*   r+   r,   rG   rY   r_   re   ri   r-   r.   r   rP   rP      s"    -,"@Er.   rP   )
__future__r   typingr   numpyrT   onnx.onnx_pbr   #onnx.reference.custom_element_typesr   r   onnx.reference.op_runr   r	   r   r0   r6   r@   rB   rE   rP   r-   r.   r   <module>rp      sv    #   " :0 020J 0.!0% !0HPK P0[ 0$0~ 0&,Eu ,Er.   