o
    wi                     @  s   d dl mZ d dlmZmZ d dlZd dlmZm	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)convert_from_ml_dtypesconvert_to_ml_dtypes)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 t|}z| |}W n# ty6 } ztdddd |fD  d| jjd|d}~ww 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      | ]	}t t|V  qd S Nstrtype.0_ r   S/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/onnx/reference/ops/_op.py	<genexpr>%       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          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(   3   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t|}t|}z| 	||}W n& t
tfyz } zt
dd
dd ||fD  d| jjd|d}~ww 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  r   r   r   r   r   r   r   r   `   r   z"OpRunBinary.run.<locals>.<genexpr>z (binary operator r   r   )r   r   r   RuntimeErrorr   r1   r   shaper   r   r    
ValueErrorr!   r   r"   r#   )r$   r%   yr&   r'   r   r   r   r(   J   s@   "
zOpRunBinary.runNr)   r   r   r   r   r2   D   r-   r2   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   r9   h   s    r9   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: r3   z or z (operator z
) type(x)=z	 type(y)=)r2   r(   r1   r   r   r   r   r#   )r$   r%   r8   r&   r   r   r   r(   r   s   
zOpRunBinaryNum.runNr)   r   r   r   r   r:   l   r-   r:   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   )r:   __init__r<   )r$   r<   r=   r>   r   r   r   r@      s   
zOpRunBinaryNumpy.__init__c                 C  s6   t |}t |}| ||f}t|d f}| |S )Nr   )r   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   r;   ~   s    
r;   c                   @  s:   e Zd ZdZ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=   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/   rD   npndarrayr"   r6   tupler0   )r$   r=   r>   r   r   r   r@      s   
 

zOpRunReduceNumpy.__init__c                 C  s   |d u S r   r   r$   rD   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 u rd S t |ttfr|S t |tjs.tdt| dt|jdkr9t|S d|jv r@d S t|	 
 S )Nr   zaxes must be an array, not .)r/   rH   r"   intrF   rG   r    r   r6   raveltolistrI   r   r   r   handle_axes   s   

zOpRunReduceNumpy.handle_axesc                 C  s   t j|||djS )N)axiskeepdims)rF   sumr6   )r$   datarD   rQ   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.)r1   )rT   rF   fullr1   )r$   rS   	const_valrD   rQ   rT   r   r   r   reduce_constant   s   z OpRunReduceNumpy.reduce_constantN)r=   r	   r>   r?   )	r   r*   r+   r,   r@   rJ   rO   rT   rW   r   r   r   r   rC      s    
rC   )
__future__r   typingr   r   numpyrF   #onnx.reference.custom_element_typesr   r   onnx.reference.op_runr   r   onnx.onnx_pbr	   r   r.   r2   r9   r:   r;   rC   r   r   r   r   <module>   s   $