U
    qh?!                     @  sn   d dl mZ d dlZd dlZd dlmZmZmZmZm	Z	 d dl
mZmZ G dd dejZedkrje  dS )    )annotationsN)
ModelProtoTensorProtoTensorShapeProtoValueInfoProtohelper)
make_modelmake_tensor_value_infoc                   @  sx   e Zd ZddddddZdddd	d
ZddddddZddddZddddZddddZddddZ	dS )TestSymbolicShaper   zlist[ValueInfoProto]None)
onnx_modelvalue_infosreturnc           	      C  s   |D ]}|  ||j}|dk	s(t| |jdr@|jjj}n|jdrV|jjj}t|j	t|j	kstt| t
|j	D ]D\}}|j	| }|jdkr|jst| q~|j|jks~t| q~qdS )zAssert onnx_model.value_info should be the same as expected value_infos
        Instead of exact symbol, use -1 to represent symbolic shape in expected value_infos
        Ntensor_typesparse_tensor_type)_get_shape_from_namenameAssertionErrortypeHasFieldr   shaper   lendim	enumerateZ	dim_value	dim_param)	selfr   r   Zexpected_vir   Zexpected_shapeZdim_ir   Zexpected_dim r   A/tmp/pip-unpacked-wheel-xnis5xre/onnx/test/symbolic_shape_test.py_assert_valueinfo_shape   s    


z)TestSymbolicShape._assert_valueinfo_shapeint)r   r   c                 C  sh   t  }t|jj}t|jj}t|jj}|| | D ](}|jjjj	D ]}|j
rF||j
 qFq6t|S )z0Return the total number of unique symbolic shape)setlistgraphinputoutput
value_infor   r   r   r   r   addr   )r   r   Zsymbol_shape_setinputsoutputs
valueinfosvr   r   r   r   _count_unique_dim_param_number%   s    z0TestSymbolicShape._count_unique_dim_param_numberstrzTensorShapeProto | None)r   r   r   c                 C  sx   t |jj}t |jj}t |jj}|| | D ]B}|j|kr0|jdrX|jjj	  S |jdr0|jj
j	  S q0dS )zHGet shape from tensor_type or sparse_tensor_type according to given namer   r   N)r"   r#   r$   r%   r&   r   r   r   r   r   r   )r   r   r   r(   r)   r*   r+   r   r   r   r   1   s    
z&TestSymbolicShape._get_shape_from_name)r   c              
   C  s   t jdddgdgddd}tj jddgdgtjd	}t jd
||gt dtjddgt dtjddggt dtjdd ggd}t|}tjj	|dd}| 
|tdtjdg | |d| |dkstd S )NConcatABC   r(   r)   r   ZaxisCastr%   r(   r)   to
test_graph      r   Znodesr(   r)   TZstrict_moder8   r   r   	make_nodeonnxr   FLOAT
make_graphr	   r   shape_inferenceinfer_shapesr   r   r   r   concatcast	graph_defr   inferred_modelr   r   r   test_concat_enable_symbolic@   sD             
z-TestSymbolicShape.test_concat_enable_symbolicc                 C  s  t jdddgdgddd}t jdddgdgddd}tj jd	dgd
gtjd}t jd|||gt dtjddgt dtjddgt dtjddggt d
tjdd ggd}t|}tjj	|dd}| 
|tdtjdtdtjdg | |d| |d
kstd S )Nr.   r/   r0   r1   r2   r3   DEr4   r%   r5   r7   r8   r9   r:   Tr;   r<   r=   )r   concat1concat2rF   rG   r   rH   r   r   r   test_two_symbolic_concat]   sX                
z*TestSymbolicShape.test_two_symbolic_concatc           	      C  s   t jdddgdgddd}t jdddgdgddd}tj jd	dgd
gtjd}t jd|||gt dtjddgt dtjddgt dtjddggt d
tjddggd}t|}| |}tj	j
|dd}| |}||d kst| | d S )Nr.   r/   r0   r1   r2   r3   rJ   rK   r4   r%   r5   r7   r8   Zunk__0r9   Zunk__1r:   Tr;   )r   r>   r?   r   r@   rA   r	   r   r,   rB   rC   r   )	r   rL   rM   rF   rG   r   Zoriginal_countrH   Zinferred_countr   r   r   test_duplicate_symbolic_shape   sN                 

z/TestSymbolicShape.test_duplicate_symbolic_shapec              
   C  s   t jdddgdgddd}tj jddgdgtjd	}t jd
||gt dtjdd gt dtjdd ggt dtjdd ggd}t|}tjj	|dd}| 
|tdtjdg | |d| |dkstd S )Nr.   r/   r0   r1   r2   r3   r4   r%   r5   r7   r9   r:   Tr;   )r9   r   r=   rD   r   r   r   test_unknown_shape   sL               
z$TestSymbolicShape.test_unknown_shapeN)
__name__
__module____qualname__r   r,   r   rI   rN   rO   rP   r   r   r   r   r
      s   %#r
   __main__)
__future__r   ZunittestZonnx.shape_inferencer?   r   r   r   r   r   Zonnx.helperr   r	   ZTestCaser
   rQ   mainr   r   r   r   <module>   s    :