U
    q±ËhS  ã                   @  s´   d dl mZ d dlmZ d dlmZmZmZ d dlZd dl	Z
d dlm  mZ d dl
mZmZmZ G dd„ dƒZG dd	„ d	ƒZd
dddddœdd„ZG dd„ dƒZG dd„ dƒZdS )é    )Úannotations)Ú
namedtuple)ÚAnyÚNewTypeÚSequenceN)Ú
IR_VERSIONÚ
ModelProtoÚ	NodeProtoc                   @  s<   e Zd ZU dZedeƒZedƒZded< edƒZ	ded< dS )Ú
DeviceTypezDescribes device type.Ú_Typer   ÚCPUé   ÚCUDAN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úintr   r   Ú__annotations__r   © r   r   ú5/tmp/pip-unpacked-wheel-xnis5xre/onnx/backend/base.pyr
      s   

r
   c                   @  s    e Zd ZdZdddœdd„ZdS )ÚDevicezyDescribes device type and device id
    syntax: device_type:device_id(optional)
    example: 'CPU', 'CUDA', 'CUDA:1'
    ÚstrÚNone©ÚdeviceÚreturnc                 C  s>   |  d¡}tt|d ƒ| _d| _t|ƒdkr:t|d ƒ| _d S )Nú:r   r   )ÚsplitÚgetattrr
   ÚtypeZ	device_idÚlenr   )Úselfr   Úoptionsr   r   r   Ú__init__   s
    
zDevice.__init__N)r   r   r   r   r$   r   r   r   r   r      s   r   r   zSequence[str]r   ztype[tuple[Any, ...]])ÚtypenameÚfield_namesÚargsÚkwargsr   c                   sP   dd„ t |ƒD ƒ‰ | dd¡ t| |f|ž|Ž}ddddœ‡ fdd„}||_|S )	Nc                 S  s   i | ]\}}||“qS r   r   )Ú.0ÚiÚnr   r   r   Ú
<dictcomp>)   s      z"namedtupledict.<locals>.<dictcomp>ÚrenameTr   )r"   Úkeyr   c                   s&   t |tƒrˆ | }tt| ƒ| ƒ |¡S ©N)Ú
isinstancer   Úsuperr    Ú__getitem__)r"   r.   ©Zfield_names_mapr   r   Úgetitem.   s    
znamedtupledict.<locals>.getitem)Ú	enumerateÚ
setdefaultr   r2   )r%   r&   r'   r(   Údatar4   r   r3   r   Únamedtupledict&   s    r8   c                   @  s"   e Zd ZdZddddœdd„ZdS )Ú
BackendRepzÐBackendRep is the handle that a Backend returns after preparing to execute
    a model repeatedly. Users will then pass inputs to the run function of
    BackendRep to retrieve the corresponding results.
    r   útuple[Any, ...])Úinputsr(   r   c                 K  s   dS )zAbstract function.r/   r   )r"   r;   r(   r   r   r   Úrun=   s    zBackendRep.runN)r   r   r   r   r<   r   r   r   r   r9   7   s   r9   c                	   @  s’   e Zd ZdZeddddddœdd	„ƒZeddddd
dœdd„ƒZedddddddœdd„ƒZeddddddddœdd„ƒZedddœdd„ƒZ	dS ) ÚBackenda®  Backend is the entity that will take an ONNX model with inputs,
    perform a computation, and then return the output.

    For one-off execution, users can use run_node and run_model to obtain results quickly.

    For repeated execution, users should use prepare, in which the Backend
    does all of the preparation work for executing the model repeatedly
    (e.g., loading initializers), and returns a BackendRep handle.
    r   r   r   r   Úbool)Úmodelr   r(   r   c                 K  s   dS )NTr   ©Úclsr?   r   r(   r   r   r   Úis_compatibleM   s    zBackend.is_compatiblezBackendRep | Nonec                 K  s   t j |¡ d S r/   )ÚonnxÚcheckerZcheck_modelr@   r   r   r   ÚprepareT   s    zBackend.preparer:   )r?   r;   r   r(   r   c                 K  s&   | j ||f|Ž}|d k	st‚| |¡S r/   )rE   ÚAssertionErrorr<   )rA   r?   r;   r   r(   Úbackendr   r   r   Ú	run_model\   s    zBackend.run_modelNr	   z4Sequence[tuple[numpy.dtype, tuple[int, ...]]] | Nonezdict[str, Any]ztuple[Any, ...] | None)Únoder;   r   Úoutputs_infor(   r   c                 K  sD   d|kr4t  ¡ }t|_d|d i|_tj ||¡ ntj |¡ dS )a6  Simple run one operator and return the results.

        Args:
            node: The node proto.
            inputs: Inputs to the node.
            device: The device to run on.
            outputs_info: a list of tuples, which contains the element type and
                shape of each output. First element of the tuple is the dtype, and
                the second element is the shape. More use case can be found in
                https://github.com/onnx/onnx/blob/main/onnx/backend/test/runner/__init__.py
            kwargs: Other keyword arguments.
        Zopset_versionÚ N)Ú	c_checkerZCheckerContextr   Z
ir_versionZopset_importsrC   rD   Z
check_node)rA   rI   r;   r   rJ   r(   Zspecial_contextr   r   r   Úrun_noded   s    zBackend.run_noder   c                 C  s   dS )z…Checks whether the backend is compiled with particular device support.
        In particular it's used in the testing suite.
        Tr   )rA   r   r   r   r   Úsupports_device†   s    zBackend.supports_device)r   )r   )r   )r   N)
r   r   r   r   ÚclassmethodrB   rE   rH   rM   rN   r   r   r   r   r=   B   s    
 ÿ ÿ ÿ  ù!r=   )Ú
__future__r   Úcollectionsr   Útypingr   r   r   ZnumpyZonnx.checkerrC   Zonnx.onnx_cpp2py_export.checkerZonnx_cpp2py_exportrD   rL   r   r   r	   r
   r   r8   r9   r=   r   r   r   r   Ú<module>   s   