U
    qhe                     @  s   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 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)AnyN)	NodeProto)convert_from_ml_dtypesconvert_to_ml_dtypes)OpRunRuntimeTypeErrorc                   @  s   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 t|}z| |}W nP tk
rv } z2tdddd |fD  d| jjd|W 5 d}~X Y nX t|d	 f}|  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  s   | ]}t t|V  qd S Nstrtype.0_ r   :/tmp/pip-unpacked-wheel-xnis5xre/onnx/reference/ops/_op.py	<genexpr>#   s     z!OpRunUnary.run.<locals>.<genexpr>z (unary operator ).Nr   -- done %s.run -> %d outputs)
_log	__class____name__r   _run	TypeErrorjoinr   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	      s   r	   c                   @  s   e Zd ZdZdd ZdS )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kr(|S t|d tsn|d j|jkrnt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$   1   s    "zOpRunUnaryNum.runNr%   r   r   r   r   r)   +   s   r)   c                   @  s   e Zd ZdZdd ZdS )OpRunBinaryznAncestor to all binary operators in this subfolder.

    Checks that input and output types are the same.
    c                 C  s.  |  d| jj |dks |dkrHtdt| dt| dt|  d|j|jkrtd|j d|j d| jjd	|j d
|j dt|}t|}z| 	||}W nV t
tfk
r } z4t
dd
dd ||fD  d| jjd|W 5 d}~X Y nX t|d f}|  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  s   | ]}t t|V  qd S r   r   r   r   r   r   r   ^   s     z"OpRunBinary.run.<locals>.<genexpr>z (binary operator r   r   )r   r   r   RuntimeErrorr   r,   r   shaper   r   r   
ValueErrorr   r   r   r   )r    r!   yr"   r#   r   r   r   r$   H   s,    ".(zOpRunBinary.runNr%   r   r   r   r   r-   B   s   r-   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   r4   f   s   r4   c                   @  s   e Zd ZdZdd ZdS )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r`t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: r.   z or z (operator z
) type(x)=z	 type(y)=)r-   r$   r,   r   r   r   r   r   )r    r!   r3   r"   r   r   r   r$   p   s    <zOpRunBinaryNum.runNr%   r   r   r   r   r5   j   s   r5   c                   @  s*   e Zd ZdZddddddZdd	 Zd
S )OpRunBinaryNumpyzI*numpy_fct* is a binary numpy function which
    takes two matrices.
    r   r   dict[str, Any])	numpy_fct	onnx_node
run_paramsc                 C  s   t | || || _d S r   )r5   __init__r8   )r    r8   r9   r:   r   r   r   r;      s    zOpRunBinaryNumpy.__init__c                 C  s6   t |}t |}| ||f}t|d f}| |S )Nr   )r   r8   r   r   )r    abr"   r   r   r   r      s
    zOpRunBinaryNumpy._runN)r   r&   r'   r(   r;   r   r   r   r   r   r6   |   s   r6   c                   @  s@   e Zd ZdZdddddZdd Zd	d
 Zdd Zdd ZdS )OpRunReduceNumpyzFImplements the reduce logic.
    It must have a parameter *axes*.
    r   r7   )r9   r:   c                 C  s   t | || t| drt| jtjr\t| jjdksF| jjd dkrNd | _qt	| j| _n.| jg dfkrrd | _nt| jt
rt	| j| _d S )Naxesr   r   )r   r;   hasattrr*   r?   npndarrayr   r1   tupler+   )r    r9   r:   r   r   r   r;      s    
 zOpRunReduceNumpy.__init__c                 C  s   |d kS r   r   r    r?   r   r   r   is_axes_empty   s    zOpRunReduceNumpy.is_axes_emptyc                 C  s   t |trt|dkrd S |S |d kr*d S t |ttfr<|S t |tjs\tdt| dt|jdkrrt|S d|jkrd S t|	 
 S )Nr   zaxes must be an array, not .)r*   rC   r   intrA   rB   r   r   r1   ZraveltolistrD   r   r   r   handle_axes   s    

zOpRunReduceNumpy.handle_axesc                 C  s   t j|||djS )N)Zaxiskeepdims)rA   sumr1   )r    datar?   rJ   r   r   r   output_shape   s    zOpRunReduceNumpy.output_shapec                 C  s"   |  |||}tj|||jdfS )z<Special case reduction where the output value is a constant.)r,   )rM   rA   fullr,   )r    rL   Z	const_valr?   rJ   rM   r   r   r   reduce_constant   s    z OpRunReduceNumpy.reduce_constantN)	r   r&   r'   r(   r;   rE   rI   rM   rO   r   r   r   r   r>      s   r>   )
__future__r   typingr   ZnumpyrA   Zonnx.onnx_pbr   Z#onnx.reference.custom_element_typesr   r   Zonnx.reference.op_runr   r   r	   r)   r-   r4   r5   r6   r>   r   r   r   r   <module>   s   $