U
    9%e                     @   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mZ ejejddZede	d	d	d
d
dejejdddZede	d	d
d	d	ejejdddZede	d	d
d	d	ddej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)	_beartype	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   Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/onnx/symbolic_opset16.py
<dictcomp>9   s      z grid_sampler.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   r   r   r   r   r   :   s      Z
GridSample)Zalign_corners_imode_spadding_mode_s)r   Z_get_tensor_rankZ_onnx_unsupportedr   itemsr   opint)r   inputgridZ	mode_enumZpadding_mode_enumZalign_cornersr   r   r   r   r   grid_sampler+   s    
r   zaten::scatter_addc           
      C   s*  t  r| jd||||ddS tj|tjj}t |}t |}t|t|krnt 	dd| d| dS ||ks~d |k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 )NZscattersrc)Zoverload_name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is_caffe2_aten_fallbackatr   ZJitScalarType
from_value	UNDEFINEDZ_get_tensor_sizeslenZ_unimplementedr   torchtensorZ_maybe_get_scalarZ	_is_valueZ	onnx_type)
r   selfdimindexr   Zsrc_typeZ	src_sizesZindex_sizesZadjusted_shapeZstartsr   r   r   r    E   sD     

	

r    zaten::scatter_reduces)r   r/   r0   r1   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|||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)r5   sumprodZaminZamaxSizer"   ZEqualr#   r   )Zdtyper$   If      )Zn_blocksoutputsZReshapeZIdentityr&   r(      ZSqueeze)r&   )r   ZOnnxExporterErrorr   r-   r.   Zint64r	   Zadd_op_with_blocksr   Z_add_output_to_blockblocknodeoutput)r   r/   r0   r1   r   r3   r4   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_reducex   sn              rH   )__doc__	functoolsr-   Ztorch.nn.functionalr   r   Z
torch.onnxr   r   r   r   Ztorch.onnx._internalr   r	   r
   partialZonnx_symbolicZ_onnx_symbolic
parse_argsZbeartypeZGraphContextr   r    Z_CValuer   strboolrH   r   r   r   r   <module>   s4   0