a
    h
                  
   @   s   d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZ ejejddZede	d	d	d
d
dejdddZede	d	d
d	d	ejdddZede	d	d
d	d	ddejejjeejjejjeedddZdS )a  This file exports ONNX ops for opset 16.

Note [ONNX Operators that are added/updated in opset 16]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
https://github.com/onnx/onnx/blob/main/docs/Changelog.md#version-16-of-the-default-onnx-operator-set
New operators:
    GridSample https://github.com/onnx/onnx/pull/3557

Updated operators:
    Identity
    If
    LeakyRelu
    Loop
    PRelu
    RoiAlign
    Scan
    ScatterElements
    ScatterND
    Where
    GreaterOrEqual
    LessOrEqual
    N)GRID_SAMPLE_INTERPOLATION_MODESGRID_SAMPLE_PADDING_MODES)_type_utilserrorssymbolic_helperutils)	jit_utilsregistration   )Zopsetzaten::grid_samplervib)gc                 C   s^   t |dkrt dS dd t D | }dd t D | }| jd||t|||dS )N   z#GridSample with 5D volumetric inputc                 S   s   i | ]\}}||qS  r   .0kr   r   r   I/var/www/auris/lib/python3.9/site-packages/torch/onnx/symbolic_opset16.py
<dictcomp>:       z grid_sampler.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   r   r   r   r   r   ;   r   Z
GridSample)Zalign_corners_imode_spadding_mode_s)r   Z_get_tensor_rankZ_onnx_unsupportedr   itemsr   opint)r   inputZgridZ	mode_enumZpadding_mode_enumZalign_cornersr   r   r   r   r   grid_sampler-   s    
r   zaten::scatter_addc           
      C   s
  t j|t jj}t|}t|}t|t|krPtdd| d| dS ||ks`d |v r| d|}| jdt	
dgt| d}	| d	||	|}t|}t|r| jd
||||ddS t j||kr| jd|t j| d}| jd
||||ddS d S )Nscatter_addz	`index` (z0) should have the same dimensionality as `src` ()ShapeConstantr   Zvalue_tSliceScatterElementsaddZaxis_iZreduction_sZCast)Zto_i)r   ZJitScalarType
from_value	UNDEFINEDr   Z_get_tensor_sizeslenZ_unimplementedr   torchtensorZ_maybe_get_scalarZ	_is_valueZ	onnx_type)
r   selfdimindexsrcZsrc_typeZ	src_sizesZindex_sizesZadjusted_shapeZstartsr   r   r   r   H   s>    

	

r   zaten::scatter_reduces)r   r,   r-   r.   r/   reduceinclude_selfc              
   C   s  |dkrt d|s t ddddddd	}|| }| d
| d|}	| d|	| jdtjdtjdd}
tj| d|
ddd\}\}}}|jdtjdgtjdd}|d||}t	|j
| |d||}t	|j
| |d||}t	|j
| |d|}t	|j
| |d|}t	|j
| |d|}t	|j
| | jdg|R ||d}tj| d|
ddd\}\}}}|d|}t	|j
| |d|}t	|j
| |  }|S )Nmeanz7ONNX does not support mean reduction for scatter_reducez;ONNX does not support include_self=False for scatter_reducenoner%   mulminmax)r3   sumprodZaminZamaxSizer    ZEqualr!   r   )Zdtyper"   If      )Zn_blocksoutputsZReshapeZIdentityr$   r&      ZSqueeze)r   ZOnnxExporterErrorr   r*   r+   int64r   Zadd_op_with_blocksr   Z_add_output_to_blockblocknodeoutput)r   r,   r-   r.   r/   r1   r2   Zreduce_modeZonnx_reduceZ	self_rankZself_rank_is_zeroZif_opZ
if_contextZelse_context_Zneg_1Zself_reshapeZindex_reshapeZsrc_reshapeZself_identityZindex_identityeZsrc_identityresultZresult_squeezedZresult_identityZresult_finalr   r   r   scatter_reducew   sZ    

rG   )__doc__	functoolsr*   Ztorch.nn.functionalr   r   Z
torch.onnxr   r   r   r   Ztorch.onnx._internalr   r	   partialZonnx_symbolicZ_onnx_symbolic
parse_argsZGraphContextr   r   Z_CValuer   strboolrG   r   r   r   r   <module>   s.   -