o
    ;ήc                     @   sz   U d Z ddlZzddlZW n ey   edw ddlmZ dZe Zej	e
 ed< dejfdd	Zd
d Zdd ZdS )zm
Support for registering ONNX Runtime's built-in contrib ops with
PyTorch-ONNX exporter (torch.onnx.export).
    NzdThis module is only useful in combination with PyTorch. To install PyTorch see https://pytorch.org/.)symbolic_helper   _registered_opssymbolic_fnc                 C   s(   d| j  }tj|| t t| d S )Nz::%s)__name__torchonnxregister_custom_op_symbolic_OPSET_VERSIONr   add)r   name r   S/tmp/pip-target-vg8gfxp4/lib/python/onnxruntime/tools/pytorch_export_contrib_ops.py_reg   s   
r   c                  C   st   dd } t |  dd }t | tjjddddtjjd	tfd
d}t | dd }t | dd }t | dS )zaRegister ONNX Runtime's built-in contrib ops.

    Should be run before torch.onnx.export().
    c                 S   sV   t |d}t |d}g d| }g d| }tt |d}| jd|||||dS )Ni)bilinearnearestbicubic)zerosborder
reflectionbzcom.microsoft::GridSample)mode_spadding_mode_salign_corners_i)r   _maybe_get_constintop)ginputgridmodepadding_modealign_cornersmode_strpadding_mode_strr   r   r   grid_sampler$   s   		zregister.<locals>.grid_samplerc                 S   s   |  d|| S )Nzcom.microsoft::Inverser   setTypetype)r   selfr   r   r   inverseE   s   zregister.<locals>.inversevsnoner*   approximatec                 S   s0   |dkr|  d|| S tjj| ||S )Nr.   zcom.microsoft::Gelu)r   r(   r)   r   r   symbolic_opset9gelu)r   r*   r/   r   r   r   r1   J   s   zregister.<locals>.geluc                 S      | j d||dd| S )Ncom.microsoft::Trilur   upper_ir'   r   r*   diagonalr   r   r   triuS      zregister.<locals>.triuc                 S   r2   )Nr3   r   r4   r'   r6   r   r   r   trilX   r9   zregister.<locals>.trilN)r.   )r   r   r   r   
parse_args_CValuestr)r&   r+   r1   r8   r:   r   r   r   register   s   r?   c               
   C   s~   t D ]:} z	tj| t W q ty<   ddlm} | d\}}t	j
D ]}|tkr9||||r9|j||f |= q$Y qw dS )z/Unregister ONNX Runtime's built-in contrib ops.r   )symbolic_registryz::N)r   r   r   unregister_custom_op_symbolicr
   AttributeError
torch.onnxr@   splitr   _onnx_stable_opsetsis_registered_op	_registry)r   r@   	namespacekindversionr   r   r   
unregister^   s   
rK   )__doc__typingr   ModuleNotFoundErrorrC   r   r
   setr   AbstractSetr>   __annotations__Callabler   r?   rK   r   r   r   r   <module>   s   @