o
    toiK                     @  s   d dl mZ d dlmZmZ d dlZd dlmZm	Z	 er"d dl
mZ G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZdS )    )annotations)TYPE_CHECKINGAnyN)OpRunRuntimeTypeError)	NodeProtoc                   @     e Zd ZdZdd ZdS )
OpRunUnaryzmAncestor to all unary operators in this subfolder.

    Checks that input and output types are the same.
    c              
   C  s   |  d| jj z| |}W n# ty2 } ztdddd |fD  d| jjd|d}~ww |  d	| jjt| | |S )
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                 s      | ]	}t t|V  qd S Nstrtype.0_ r   J/home/ubuntu/.local/lib/python3.10/site-packages/onnx/reference/ops/_op.py	<genexpr>        z!OpRunUnary.run.<locals>.<genexpr>z (unary operator ).N-- done %s.run -> %d outputs)_log	__class____name___run	TypeErrorjoinlen_check_and_fix_outputs)selfxreser   r   r   run   s   
zOpRunUnary.runNr   
__module____qualname____doc__r&   r   r   r   r   r	          r	   c                   @  r   )OpRunUnaryNumz{Ancestor to all unary and numerical operators in this subfolder.

    Checks that input and output types are the same.
    c                 C  sx   t | |}t|dks|d du r|S t|d ts7|d j|jkr7td|j d|d j d| jjd| 	|S )zCalls method ``OpRunUnary.run``.

        Catches exceptions, displays a longer error message.
        Checks that the result is not empty.
        r   NzOutput type mismatch: input 'z' != output 'z' (operator r   )
r	   r&   r    
isinstancelistdtyper   r   r   r!   )r"   r#   r$   r   r   r   r&   -   s   
zOpRunUnaryNum.runNr'   r   r   r   r   r,   '   r+   r,   c                   @  r   )OpRunBinaryznAncestor to all binary operators in this subfolder.

    Checks that input and output types are the same.
    c                 C  s  |  d| jj |du s|du r$tdt| dt| dt|  d|j|jkrDtd|j d|j d| jjd	|j d
|j dz| ||}W n& t	t
fyr } zt	dd
dd ||fD  d| jjd|d}~ww |  d| jjt| | |S )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                 s  r   r   r   r   r   r   r   r   X   r   z"OpRunBinary.run.<locals>.<genexpr>z (binary operator r   )r   r   r   RuntimeErrorr   r/   r   shaper   r   
ValueErrorr   r    r!   )r"   r#   yr$   r%   r   r   r   r&   D   s:   "
zOpRunBinary.runNr'   r   r   r   r   r0   >   r+   r0   c                   @  s   e Zd ZdZdS )OpRunBinaryComparisonzEAncestor to all binary operators in this subfolder comparing tensors.N)r   r(   r)   r*   r   r   r   r   r7   _   s    r7   c                   @  r   )OpRunBinaryNumznAncestor to all binary operators in this subfolder.

    Checks that input oud output types are the same.
    c                 C  sj   t | ||}|d j|jkr0td|j d|d j d|j d| jjdt| dt| | |S )zVCalls method ``OpRunBinary.run``, catches exceptions, displays a longer error message.r   zOutput type mismatch: r1   z or z (operator z
) type(x)=z	 type(y)=)r0   r&   r/   r   r   r   r   r!   )r"   r#   r6   r$   r   r   r   r&   i   s   
zOpRunBinaryNum.runNr'   r   r   r   r   r8   c   r+   r8   c                   @  s"   e Zd ZdZddd	Zd
d ZdS )OpRunBinaryNumpyzI*numpy_fct* is a binary numpy function which
    takes two matrices.
    	numpy_fctr   	onnx_noder   
run_paramsdict[str, Any]c                 C  s   t | || || _d S r   )r8   __init__r:   )r"   r:   r;   r<   r   r   r   r>   z   s   
zOpRunBinaryNumpy.__init__c                 C  s   |  ||f}| |S r   )r:   r!   )r"   abr$   r   r   r   r      s   
zOpRunBinaryNumpy._runN)r:   r   r;   r   r<   r=   )r   r(   r)   r*   r>   r   r   r   r   r   r9   u   s    
r9   c                   @  s<   e Zd ZdZdddZdd	 ZdddZdd Zdd ZdS )OpRunReduceNumpyzFImplements the reduce logic.
    It must have a parameter *axes*.
    r;   r   r<   r=   c                 C  s   t | || t| drJt| jtjr0t| jjdks#| jjd dkr(d | _d S t	| j| _d S | jg dfv r<d | _d S t| jt
rLt	| j| _d S d S d S )Naxesr   r   )r   r>   hasattrr-   rB   npndarrayr    r4   tupler.   )r"   r;   r<   r   r   r   r>      s   
 

zOpRunReduceNumpy.__init__c                 C  s   |d u p|j dkS )N)r   )r4   )r"   rB   r   r   r   is_axes_empty      zOpRunReduceNumpy.is_axes_emptyFc                 C  s   t |trt|dkr|sd S |S |d u r|rdS d S t |ttfr$|S t |tjs4tdt| dt|jdkr?t|S d|jv rJ|rHdS d S t|	 
 S )Nr   r   zaxes must be an array, not .)r-   rF   r    intrD   rE   r   r   r4   raveltolist)r"   rB   noop_with_empty_axesr   r   r   handle_axes   s&   

zOpRunReduceNumpy.handle_axesc                 C  s   t j|||djS )N)axiskeepdims)rD   sumr4   )r"   datarB   rP   r   r   r   output_shape   rH   zOpRunReduceNumpy.output_shapec                 C  s"   |  |||}tj|||jdfS )z<Special case reduction where the output value is a constant.)r/   )rS   rD   fullr/   )r"   rR   	const_valrB   rP   rS   r   r   r   reduce_constant   s   z OpRunReduceNumpy.reduce_constantN)r;   r   r<   r=   )F)	r   r(   r)   r*   r>   rG   rN   rS   rV   r   r   r   r   rA      s    

rA   )
__future__r   typingr   r   numpyrD   onnx.reference.op_runr   r   onnx.onnx_pbr   r	   r,   r0   r7   r8   r9   rA   r   r   r   r   <module>   s   !