U
    qh/i                     @  s  d dl mZ d dlZd dlmZmZmZ d dlZd dl	m
Z
 d dlmZmZmZmZmZmZmZ d dlmZmZmZ d dlmZmZmZ d dlmZ d d	lmZmZm Z m!Z! d
d Z"G dd de#Z$G dd de#Z%G dd de&Z'G dd dZ(G dd dZ)ddddZ*e* Z+G dd dZ,G dd dZ-dddd d!Z.d"d#d$d%d&Z/G d'd( d(Z0G d)d* d*ej1Z2G d+d, d,e2Z3G d-d. d.e2Z4G d/d0 d0e4Z5dS )1    )annotationsN)AnyClassVarIterable)TensorProto)bfloat16float8e4m3fnfloat8e4m3fnuz
float8e5m2float8e5m2fnuzint4uint4)get_all_schemas_with_history
get_schemaonnx_opset_version)	make_nodemake_tensor_type_protonp_dtype_to_tensor_dtypeto_array)AttributeProto
GraphProto	NodeProto	TypeProtoc                 C  sT   d| krL| j ddd\}}zt|}W n tk
rB   | d f Y S X ||fS | d fS )N_   )maxsplit)rsplitint
ValueError)nameprefixversv r$   9/tmp/pip-unpacked-wheel-xnis5xre/onnx/reference/op_run.py_split_class_name   s    r&   c                   @  s   e Zd ZdZdS )RuntimeTypeErrorz/Raised when a type of a variable is unexpected.N__name__
__module____qualname____doc__r$   r$   r$   r%   r'   &   s   r'   c                   @  s   e Zd ZdZdS )RuntimeContextErrorzfRaised when the context is missing but an context dependent implementation is defined for an operator.Nr(   r$   r$   r$   r%   r-   *   s   r-   c                   @  s   e Zd ZdZdS )RuntimeImplementationErrorz8Raised when no implementation was found for an operator.Nr(   r$   r$   r$   r%   r.   .   s   r.   c                   @  s   e Zd ZdZdS )DefaultNonezjDefault value for parameters when the parameter is not set but the operator has a default behavior for it.Nr(   r$   r$   r$   r%   r/   2   s   r/   c                   @  s,   e Zd ZdZddddZddddZd	S )
RefAttrNamez}Implements a link between a parameter of a function and an attribute in node.

    Args:
        name: name of the input
    strr    c                 C  s
   || _ d S Nr2   )selfr    r$   r$   r%   __init__=   s    zRefAttrName.__init__returnc                 C  s   | j j d| jdS )N())	__class__r)   r    r4   r$   r$   r%   __repr__@   s    zRefAttrName.__repr__N)r)   r*   r+   r,   r5   r<   r$   r$   r$   r%   r0   6   s   r0   zdict[str, type]r6   c               	   C  s   i } t  D ]}|j| krp|j| |j jkrRtd|jd|jd| |j jd|j| |j jkrz|| |j< n
|| |j< || |jd t|j < q
| S )Nz7This function assumes every operator has a unique name z even accross multiple domains z and .r   )r   r    domainNotImplementedErrorZsince_versionr1   )resschemar$   r$   r%   _build_schemasD   s    

"
rB   c                   @  s(   e Zd ZddddZddddZd	S )
OnnxTyper   
type_protoc                 C  s(   t |tstdt| d|| _d S )Nztype_proto z must be of type TypeProto.)
isinstancer   	TypeErrortyperE   )r4   rE   r$   r$   r%   r5   [   s    
zOnnxType.__init__r1   r6   c                 C  s   d| j dS )Nz	OnnxType(r9   rD   r;   r$   r$   r%   r<   `   s    zOnnxType.__repr__N)r)   r*   r+   r5   r<   r$   r$   r$   r%   rC   Z   s   rC   c                   @  s6   e Zd ZdZdddddddZedd	d
dZdS )SparseTensorzcSimple representation of a sparse tensor.
    It is based on numpy but does not require scipy.
    
np.ndarrayz
tuple[int]None)valuesindicesshaper7   c                 C  s   || _ || _|| _d S r3   )rL   rM   rN   )r4   rL   rM   rN   r$   r$   r%   r5   i   s    zSparseTensor.__init__r   r6   c                 C  s   | j jS r3   )rL   dtyper;   r$   r$   r%   rO   p   s    zSparseTensor.dtypeN)r)   r*   r+   r,   r5   propertyrO   r$   r$   r$   r%   rI   d   s   rI   r   )attr7   c                 C  s,   t dd | jD }tt| jt| j|S )zHosts a sparse tensor.c                 s  s   | ]
}|V  qd S r3   r$   ).0dr$   r$   r%   	<genexpr>w   s     z#to_sparse_tensor.<locals>.<genexpr>)tupleZdimsrI   r   rL   rM   )rQ   rN   r$   r$   r%   to_sparse_tensoru   s    rV   r   rJ   )tensorr7   c                 C  s   t | S )zAlias for :func:`to_array`.r   )rW   r$   r$   r%   to_array_extended{   s    rX   c                   @  s    e Zd ZdZdddddZdS )Graphgr   rK   )r[   r7   c                 C  s
   || _ d S r3   rZ   )r4   r[   r$   r$   r%   r5      s    zGraph.__init__N)r)   r*   r+   	__slots__r5   r$   r$   r$   r%   rY      s   rY   c                   @  s  e Zd ZU dZdZejdd ejdd ejdd ej	dd ej
dd ejd	d ejd
d ejdd ejdd ejdd ejdd ejdd ejdd ejdd iZded< dUddddddZdd ZdVddddd d!Zed"d# Zd$d%d&d'Zed(d)d*d+d,Zed-d%d.d/Zed-d%d0d1Zed2d%d3d4Z ed2d%d5d6Z!d7d%d8d9Z"d2d%d:d;Z#e$j%d<d= Z&d>d>d?d@dAZ'dddBdCdDZ(e)dEdF Z*e)dWdGdGdddHdIdJZ+e)dXdGdGdLdddMdNdOZ,e)ddKdPdQdGdLdddRdSdTZ-dS )YOpRuna0  Ancestor to all operators in this subfolder.

    Args:
        onnx_node: `onnx` node
        run_params: additional parameters such as `verbose`, `opsets`
            (it can be more than one if the operator has a subgraph),
            `log` for a logging function
        schema: operator schema
     c                 C  s   t | jS r3   )npfloat32frQ   r$   r$   r%   <lambda>       zOpRun.<lambda>c                 C  s   dd | j D S )Nc                 S  s   g | ]}t |qS r$   )r_   r`   )rR   ra   r$   r$   r%   
<listcomp>   s     "OpRun.<lambda>.<locals>.<listcomp>)Zfloatsrb   r$   r$   r%   rc      rd   c                 C  s
   t | jS r3   )rY   r[   rb   r$   r$   r%   rc      rd   c                 C  s   dd | j D S )Nc                 S  s   g | ]}t |qS r$   )rY   )rR   r[   r$   r$   r%   re      s     rf   )Zgraphsrb   r$   r$   r%   rc      rd   c                 C  s
   t | jS r3   )r   irb   r$   r$   r%   rc      rd   c                 C  s   dd | j D S )Nc                 S  s   g | ]}t |qS r$   )r   rR   rg   r$   r$   r%   re      s     rf   )Zintsrb   r$   r$   r%   rc      rd   c                 C  s
   t | jS r3   )rV   Zsparse_tensorrb   r$   r$   r%   rc      rd   c                 C  s   dd | j D S )Nc                 S  s   g | ]}t |qS r$   )rV   rR   tr$   r$   r%   re      s    rf   )Zsparse_tensorsrb   r$   r$   r%   rc      s   c                 C  s   | j dS )Nutf-8)sdecoderb   r$   r$   r%   rc      rd   c                 C  s   dd | j D S )Nc                 S  s   g | ]}| d qS )rk   )rm   )rR   rl   r$   r$   r%   re      s     rf   )stringsrb   r$   r$   r%   rc      rd   c                 C  s
   t | jS r3   )rX   rj   rb   r$   r$   r%   rc      rd   c                 C  s   dd | j D S )Nc                 S  s   g | ]}t |qS r$   )rX   ri   r$   r$   r%   re      s     rf   )Ztensorsrb   r$   r$   r%   rc      rd   c                 C  s
   t | jS r3   )rC   tprb   r$   r$   r%   rc      rd   c                 C  s   dd | j D S )Nc                 S  s   g | ]}t |qS r$   )rC   ri   r$   r$   r%   re      s     rf   )Ztype_protosrb   r$   r$   r%   rc      rd   zClassVar[dict[Any, Any]]_attribute_conversion_functionsNr   dict[str, Any]r   )	onnx_node
run_paramsrA   c                 C  s   t |tstdt| ddD ]&}||kr"td|dt| dq"d|krZtd|| _|| _|d krt	| j
d	r| j
j| _q| j
jtkrt| j
j | _q|jtkrt|j | _qd | _n|| _d
| _|   d S )Nz$run_params must be a dictionary not r=   )opsetsnew_ops
Attribute z must be in run_params, only z was found.logz#run_params must contains key 'log'.	op_schemaF)rF   dictrG   rH   RuntimeErrorsortedKeyErrorrr   rs   hasattrr:   rx   _schemar)   _schemasop_typehas_subgraph_load_attributes)r4   rr   rs   rA   rQ   r$   r$   r%   r5      s,    

zOpRun.__init__c                 G  s   | j d |f|  d S )Nrw   )rs   )r4   patternargsr$   r$   r%   _log   s    z
OpRun._logr   zAttributeProto | None)rQ   ref_attr7   c                 C  s8  |j tjkr| jdd}d| jkr8t| jd  }nd}| jdd}|dk	s`td| ||j| jd t	d| jddd	 |dkrdn
t| |d
S |j t
jkrt
j|j  |S |dkrtd|jd|j d| jjd| jjd| dtd|jd|j d| jjd| jjd| d| ddS )z0Converts an attribute value into a python value.ru   NZexisting_functionsevaluator_clsz0evaluator_cls must be specified to evaluate att=rt   r   verbose   )rt   r   ru   	functionszUnable to convert attribute z type z from node type z	, domain 
r=   z$Unable to convert default value for )rH   r   GRAPHrs   getlistrL   AssertionErrorr[   maxr]   rp   AttributeErrorr    rr   r   r>   )r4   rQ   r   ru   r   r   r$   r$   r%   _extract_attribute_value   s4    
.4zOpRun._extract_attribute_valuec                 C  s   |j d | pi |dS )N
attributes)runcontextvaluer   r$   r$   r%   _evaluate_subgraph   s    zOpRun._evaluate_subgraphrK   r6   c                 C  s`  d| _ g }| jjD ]}|j}|jr4t|j}d| _ n
| |}t| || || |j	t
jkrd| _|  j |j O  _ t| d|j |dfdd q| jrR| jjdkrR| jj D ]\}}t| |st|ddrtd	|d
| jjdt|dr|jj	dks&|jj	dkr,|jjjdkr,d}n| |j|}t| || || qt|| _dS )zChecks and loads attributes.FTZ_run_Nc                 S  s   t | ||S r3   )r]   r   r   r$   r$   r%   rc     s     z(OpRun._load_attributes.<locals>.<lambda>>   Constantrequiredrv   z3 is expected based on ONNX specifications for node r=   default_valuer      )has_linked_attributerr   	attributer    Zref_attr_namer0   r   setattrappendrH   r   r   r   r~   r   r   itemsr}   getattrrz   r   rj   Z	data_typesetattributes_names_)r4   Zadded_attributesrQ   r    r   kr#   r$   r$   r%   r      sH    






zOpRun._load_attributesr   z	list[str])graphr7   c           	      C  s   t | tstdt| dt }t }| jD ]}||j q0| jD ]}||j qH| j	D ]}||j q`| j
D ]8}|jD ]}|| q|j	D ]}||kr|| qqxt|S )zReturns all varibles not registered as inputs and not produced by
        an node inside the graph. This inputs are part of the context
        existing in the graph calling this one.
        zUnexpected type r=   )rF   r   rG   rH   r   Zinitializeraddr    Zsparse_initializerinputnodeoutputr   )	r   localZknowninitZsparse_initinpr   org   r$   r$   r%   implicit_inputs  s"    






zOpRun.implicit_inputszIterable[str]c                 C  s   | j jS )zReturns node attribute `input`.)rr   r   r;   r$   r$   r%   r   3  s    zOpRun.inputc                 C  s   | j jS )z Returns node attribute `output`.)rr   r   r;   r$   r$   r%   r   8  s    zOpRun.outputr1   c                 C  s   | j jS )z!Returns node attribute `op_type`.)rr   r   r;   r$   r$   r%   r   =  s    zOpRun.op_typec                 C  s   | j jS )z Returns node attribute `domain`.)rr   r>   r;   r$   r$   r%   r>   B  s    zOpRun.domainboolc                 C  s   dS )zTells the runtime if this node needs the context
        (all the results produced so far) as it may silently access
        one of them (operator Scan, If, Loop).
        The default answer is `False`.
        Fr$   r;   r$   r$   r%   need_contextG  s    zOpRun.need_contextc                 C  s   | j jd d| jj g}t| j D ]R\}}|dkr:q(d|d   krRdkr(n q(|d dkr(|d	| d
| d q(|d d|S )Nr8   z    op_type=>   descrr   ar   zr   z    =,r9   r   )	r:   r)   rr   r   r{   __dict__r   r   join)r4   Zattsr   r#   r$   r$   r%   __str__O  s    (
zOpRun.__str__c                 O  s   t d| jjddS )a  Should be overwritten.

        Args:
            *args: operator inputs
            **kwargs: optional inputs and overriden attributes, an
                attribute may be overridden if it belongs to a function,
                in this case, the same instance of OpRun can be called
                with different values of the same attribute.

        Returns:
            outputs
        z/Method '_run' must be overwritten for operator r=   N)r?   r:   r)   )r4   r   kwargsr$   r$   r%   _runY  s    z
OpRun._runztuple[Any, ...])r@   r7   c                 C  s   t |ts(td| jjdt| d|s@td| jjdtdd |D rzdd |D }td	| jjd
|dtdd |D }tdd |D rdd |D }td	| jjd|d|S )z-Checks the output are from the expected type.zMethod '_run' of class z does not return a tuple but 'z'.z does not return any result.c                 s  s   | ]}t |tV  qd S r3   )rF   rU   ri   r$   r$   r%   rT   u  s     z/OpRun._check_and_fix_outputs.<locals>.<genexpr>c                 S  s   g | ]}t |qS r$   rH   ri   r$   r$   r%   re   v  s     z0OpRun._check_and_fix_outputs.<locals>.<listcomp>z6One of the results returned by method '_run' of class zg is a tuple, this is no ONNX corresponding type (Map, List, Tensor, SparseTensor). All returned types: r=   c                 s  s&   | ]}t |rt |n|V  qd S r3   )r_   Zisscalararray)rR   xr$   r$   r%   rT   |  s    c                 s  s,   | ]$}t |tjttfp t|d  V  qdS )ZtodenseN)rF   r_   Zndarrayr   ry   r}   ri   r$   r$   r%   rT     s   c                 S  s   g | ]}t |qS r$   r   ri   r$   r$   r%   re     s     zr has an unexpected type, this is no ONNX correponding type (Map, List, Tensor, SparseTensor). All returned types: )rF   rU   rG   r:   r)   rH   r   any)r4   r@   Zdtypesr$   r$   r%   _check_and_fix_outputsk  s0    
zOpRun._check_and_fix_outputs)linked_attributesr   c          
      G  sz  |   r&|dkrBtdt|  dn|dk	rBtdt|  d| jrd|dkrdtdt|  d| js|dk	rtdt|  dt| di }| jr|dkrtd	t|  d| jD ]R}t| |}t	|t
r|j|krtd
|d|dt|  d||j ||< q| d| jjt| i }| jD ]H}||krDq2t| |sjtd|d| jjdt| |||< q2| jr| jr|stdt|  d||d< |dk	r||d< z(|r| j|||}n| j||}W nb ttfk
rJ }	 z>tddd |D  dt| dt| d| jjd	|	W 5 d}	~	X Y nX | d| jj|dk	rjt|nd | |S )a  Calls method ``_run``, catches exceptions,
        displays a longer error message.

        Args:
            *args: inputs
            linked_attributes: used if this has an attriute linked to
                the attribute of the function it belongs to
            context: if this node is part of the subgraph, `context` is
                a dictionary with the values this node may use

        Returns:
            tuple of results
        NzThis node if type z needs context to be filled.z+ does not need any contextbut one is given.z
This node zK has linked attributes but None are given in parameter 'linked_attributes'.zM has no linked attribute but some are given in parameter 'linked_attributes' r=   zBOne attribute is linked but no linked value is provided, in class z,Unable to find a value for linked attribute z in z	 in node z-- begin %s.run(%d inputs)rv   z is missing in operator z6A subgraph has linked attribute but none was given to r   r   zIssues with types c                 S  s   g | ]}t |qS r$   r   )rR   r   r$   r$   r%   re     s     zOpRun.run.<locals>.<listcomp>z and attributes z and linked attributes=z (operator ).z-- done %s.run -> %d outputsr   )r   rz   rH   r   r   r   r   r   r   rF   r0   r    r   r:   r)   lenr}   	NameErrorr   r   rG   r{   r   )
r4   r   r   r   Zoverridden_attributesrQ   r#   r   r@   er$   r$   r%   r     s    





2z	OpRun.runc                 C  s^   | j }d|kr|t fS |dd\}}zt|}W n  tk
rT   | j t f Y S X ||fS )Nr   r   )r)   r   r   r   r   )clsr    r"   Zi_versr$   r$   r%   
infer_name  s    
zOpRun.infer_namez
int | None)n_inputs	n_outputsr   r7   c                 K  s   |   \}}| j}d}|dkr8|dkr2t|||}|j}|dkrZ|dkrTt|||}|j}dd t|D }dd t|D }	t|||	f|}
|
S )a  Creates an ONNX node for this class based on the given information.

        Args:
            n_inputs: number of inputs (default is defined by the
                operator schema)
            n_outputs: number of outputs (default is defined by the
                operator schema)
            verbose: verbosity
            **kwargs: node attributes

        Returns:
            NodeProto

        Method :meth:`eval <onnx.reference.op_run.OpRun.eval>` creates an onnx node
        returned by method :meth:`make_node <onnx.reference.op_run.OpRun.make_node>`.

        .. exec_code::

            import numpy as np
            from onnx.reference.ops._op_list import Celu

            onnx_node = Celu.make_node(alpha=0.5)
            print(onnx_node)
        Nc                 S  s   g | ]}d | qS )r   r$   rh   r$   r$   r%   re     s     z#OpRun.make_node.<locals>.<listcomp>c                 S  s   g | ]}d | qS )yr$   rh   r$   r$   r%   re     s     )r   	op_domainr   Z	min_inputZ
min_outputranger   )r   r   r   r   r   Zopsetr>   rA   Znames_inZ	names_outr   r$   r$   r%   r     s    zOpRun.make_noder   r   )r   r   r   r   r7   c           	        sH   dddd fdd}| j ||f|} |ddt id	}| ||}|S )
a  Instantiates this class based on the given information.

        Args:
            n_inputs: number of inputs (default is defined by the
                operator schema)
            n_outputs: number of outputs (default is defined by the
                operator schema)
            verbose: verbosity
            **kwargs: node attributes

        Returns:
            NodeProto
        r1   r   rK   )r   r   r7   c                   s    dkrt | t|  d S )Nr   )printrU   )r   r   r   r$   r%   log_function2  s    z"OpRun.create.<locals>.log_functionNr^   )r   rw   ru   rt   )r   r   )	r   r   r   r   r   r   r   rs   clr$   r   r%   create  s    
zOpRun.creater   r   z	list[Any])r   r   r   r   r7   c                O  s>   | j t|f||d|}|j| }t|dkr:|d S |S )a  Evaluates this operator.

        Args:
            *args: inputs
            n_outputs: number of outputs (default is defined by the
                operator schema)
            verbose: verbosity
            **kwargs: node attributes

        Returns:
            NodeProto
        r   r   r   )r   r   r   )r   r   r   r   r   instr@   r$   r$   r%   eval@  s
    
z
OpRun.eval)N)N)NN)NNr   ).r)   r*   r+   r,   r   r   FLOATZFLOATSr   ZGRAPHSINTZINTSZSPARSE_TENSORZSPARSE_TENSORSSTRINGZSTRINGSZTENSORZTENSORSZ
TYPE_PROTOZTYPE_PROTOSrp   __annotations__r5   r   r   staticmethodr   r   r   rP   r   r   r   r>   r   r   abcabstractmethodr   r   r   classmethodr   r   r   r   r$   r$   r$   r%   r]      s   

                #
-

 U
  /   #r]   c                   @  s,   e Zd ZdZdddddddZd	d
 ZdS )OpRunExpandz.Class any operator to avoid must inherit from.Nr   rq   r   )rr   rs   implc                 C  s   t dt|  dd S Nz5The reference implementation must not use this node (r   rz   rH   )r4   rr   rs   r   r$   r$   r%   r5   ^  s    zOpRunExpand.__init__c                 O  s   t dt|  dd S r   r   r4   inputsr   r$   r$   r%   r   h  s    zOpRunExpand._run)Nr)   r*   r+   r,   r5   r   r$   r$   r$   r%   r   [  s    
r   c                   @  s6   e Zd ZdZddddddddZd	d
 Zdd ZdS )
OpFunctionzRuns a custom function.Nr   dict[str, Any] | Noner   )rr   rs   r   r   c                   sX   |d kr"t d|jd|jdt || | _ fddt jd|D  _d S )Nz"impl cannot be None for node type  from domain r=   c                   s   i | ]}|t  |qS r$   )r   )rR   r    r;   r$   r%   
<dictcomp>  s    z'OpFunction.__init__.<locals>.<dictcomp>attributes_)rz   r   r>   r]   r5   impl_r   r   )r4   rr   rs   r   r   r$   r;   r%   r5   q  s    
zOpFunction.__init__c                 O  s   | j | jf||S r3   )	_run_implr   r   r$   r$   r%   r     s    zOpFunction._runc              
   O  s   t |jt |krBtdt | dt |j d| jd| jd	tt|j|}| j	 }|
| |jd ||d}t |jt |krtdt | dt |j d| jd| jd	t|S )	Nz.Mismatch lengths between the number of inputs z# and the expected number of inputs z
 for node r   r=   r   z/Mismatch lengths between the number of outputs z$ and the expected number of outputs )r   Zinput_namesrz   r   r   r>   ry   zipr   copyupdater   Zoutput_namesrU   )r4   r   r   r   Zfeedsr   resultsr$   r$   r%   r     s    *

*zOpFunction._run_impl)NN)r)   r*   r+   r,   r5   r   r   r$   r$   r$   r%   r   n  s     r   c                   @  s,   e Zd ZdZdddddddZd	d
 ZdS )OpFunctionContextDependantzThe function can be instantiated but only at execution time.
    An instance of OpFunction is created everytime to node is executed.
    This is needed when the schema of an operator defines a context dependant function.
    Nr   r   r   )rr   rs   parentc                 C  s<   t j| ||| i d || _|j|j }t|j||j| _d S )N)r   r   )r   r5   r   rt   r>   r   r   Zschema_)r4   rr   rs   r   versionr$   r$   r%   r5     s    z#OpFunctionContextDependant.__init__c              	   O  s   g }|D ]}zt |j}W n tk
r   |jtkr<tj}nn|jtkrNtj}n\|jtkr`tj	}nJ|jt
krrtj}n8|jtkrtj}n&|jtkrtj}n|jtkrtj}n Y nX |t||j q| j| j|}|| j| j}| j|jf||S r3   )r   rO   r|   r   r   ZFLOAT8E4M3FNr	   ZFLOAT8E4M3FNUZr
   Z
FLOAT8E5M2r   ZFLOAT8E5M2FNUZr   ZBLOFAT16r   ZUINT4r   ZINT4r   r   rN   r   Z
_load_implrr   rs   r   r   )r4   r   r   typesrj   ttyper   r   r$   r$   r%   r     s0    






zOpFunctionContextDependant._run)Nr   r$   r$   r$   r%   r     s   	 r   )6
__future__r   r   typingr   r   r   Znumpyr_   Zonnxr   Zonnx._custom_element_typesr   r   r	   r
   r   r   r   Z	onnx.defsr   r   r   Zonnx.helperr   r   r   Zonnx.numpy_helperr   Zonnx.onnx_pbr   r   r   r   r&   rz   r'   r-   r?   r.   r/   r0   rB   r   rC   rI   rV   rX   rY   ABCr]   r   r   r   r$   r$   r$   r%   <module>   s:   $	
   W/