o
    toi\#                     @  sp   d dl mZ d dlZd dlmZ d dlZd dlmZ d dlm	Z	m
Z
mZ G dd deZedkr6e  dS dS )	    )annotationsN)TestShapeInferenceHelper)TensorProto)	make_nodemake_tensormake_tensor_value_infoc                   @  sp   e Zd ZdddZdddZdddZdd	d
ZdddZdddZdddZ	dddZ
dddZdddZdS )TestDataPropagationreturnNonec                 C  sl   |  dtjdfdtjdfgtddgdgtdddgdggg }| j|tdtjd	tdtjd
gdd d S )Nx)         y)r      r   ShapeshapeExpandzr   )r   r   r   T	data_prop)_make_graphr   INT32r   _assert_inferredr   INT64selfgraph r   S/home/ubuntu/.local/lib/python3.10/site-packages/onnx/test/data_propagation_test.pytest_expand_symbolic_input   s   
z.TestDataPropagation.test_expand_symbolic_inputc                 C  sp   |  dtjdfgtddgdgtddgdgtdtjdd	d
gg }| j|tdtjdtdtjdgdd d S )Nr   r   r      r   r   ConstantOfShaper   valuer   r   r%   r   Tr   	r   r   FLOATr   r   r   r   r   r   r   r   r   r    (test_constantofshape_with_symbolic_shape#   s&   	
z<TestDataPropagation.test_constantofshape_with_symbolic_shapec                 C  8   t jd}| j|tdtjdtdtjdgdd dS )	z8Infer the shape of z by propagating the value of xshape.z
            <ir_version: 7, opset_import: [ "" : 18]>
            agraph (float[4, 1, 16] x, float[1, 8, 16] y) => () {
                xshape = Shape (x)
                z = Expand (y, xshape)
            }
        xshaper   r   r         Tr   Nonnxparserparse_modelr   r   r   r   r*   r   modelr   r   r    test_model_data_propagation:   s   	
z/TestDataPropagation.test_model_data_propagationc                 C  r,   )	zTest value-propagation through function calls.
        Underlying core example is same as previous test_model_data_propagation.
        a  
            <ir_version: 7, opset_import: [ "" : 18, "local" : 1 ]>
            agraph (float[4, 1, 16] x, float[1, 8, 16] y) => () {
                xshape = local.GetShape (x)
                z = Expand (y, xshape)
            }
            <domain: "local", opset_import: [ "" : 18 ]>
            GetShape (x) => (shapeval) {
                shapeval = Shape(x)
            }
        r-   r   r   r.   Tr   Nr1   r5   r   r   r    test_data_prop_via_functionN   s   
z/TestDataPropagation.test_data_prop_via_functionc              	   C  sP   t jd}| j|tdtjdtdtjdtdtjdtdtjdgd	d
 dS )zTest value-propagation handles multiple calls to same function correctly.
        Underlying core example is same as previous test_model_data_propagation.
        a  
            <ir_version: 7, opset_import: [ "" : 18, "local" : 1 ]>
            agraph (float[4, 1, 16] x, float[1, 8, 16] y) => () {
                yshape = local.GetShape (y)
                xshape = local.GetShape (x)
                z = Expand (y, xshape)
                w = Expand (y, yshape)
            }
            <domain: "local", opset_import: [ "" : 18 ]>
            GetShape (x) => (shapeval) {
                shapeval = Shape(x)
            }
        yshaper   r-   r   r.   w)r   r/   r0   Tr   Nr1   r5   r   r   r    test_multiple_calls_to_functionh   s   
z3TestDataPropagation.test_multiple_calls_to_functionc                 C  s   |  dtjdfdtjdfgtddgdgtddgdgtdddgd	gtd
d	gdgtdtjdddgg }| j|tdtjdtdtjdtd	tjdtdtjdgdd d S )Nr   r"   r   )r   r   r   r   r-   r9   Addzshaper$   r   r%   r&   r'   r(   r   )r      r/   Tr   r)   r   r   r   r    test_shape_arithmetic   .   
z)TestDataPropagation.test_shape_arithmeticc                 C  s   |  dtjdfdtjdfgtddgdgtddgdgtdddgd	gtd
d	gdgtdtjdddgg }| j|tdtjdtdtjdtd	tjdtdtjdgdd d S )Nr   r"   r   r   r   r-   r9   r<   r=   r$   r   r%   r&   r'   r(   )r>      r/   Tr   r)   r   r   r   r    $test_shape_arithmetic_with_broadcast   r@   z8TestDataPropagation.test_shape_arithmetic_with_broadcastc                 C  s   |  dtjdfdtjdfgtddgdgtddgdgtdddgd	gtd
d	gdgtdtjdddgg }| j|tdtjdtdtjdtd	tjdtdtjdgdd d S )Nr   r   r   r   r   r-   r9   r<   r=   r$   r   r%   r&   r'   r(   )r   Tr   r)   r   r   r   r    )test_shape_arithmetic_with_zero_broadcast   r@   z=TestDataPropagation.test_shape_arithmetic_with_zero_broadcastc                 C  H   t jd}t j|ddd}|jjd }| |jj	j
jd jd dS )zZTest that a Concat with an empty tensor as input is handled correctly by data-propagation.z
            <ir_version: 7, opset_import: [ "" : 17]>
            agraph (float[256] y) => (float[N] z)
            <float[0] x = {}>
            {
                z = Concat <axis=0> (x, y)
            }
        Tr      Nr2   r3   r4   shape_inferenceinfer_shapesr   outputassertEqualtypetensor_typer   dim	dim_valuer   r6   inferred_modelrI   r   r   r    test_empty_tensor   s   
z%TestDataPropagation.test_empty_tensorc                 C  rD   )zTest that a Concat with an empty tensor as input is handled correctly by data-propagation.
        This time with a negative axis.
        z
            <ir_version: 7, opset_import: [ "" : 17]>
            agraph (float[256] y) => (float[N] z)
            <float[0] x = {}>
            {
                z = Concat <axis=-1> (x, y)
            }
        Tr   rE   NrF   rO   r   r   r    test_empty_tensor_negative_axis   s   
z3TestDataPropagation.test_empty_tensor_negative_axisN)r	   r
   )__name__
__module____qualname__r!   r+   r7   r8   r;   r?   rB   rC   rQ   rR   r   r   r   r    r      s    








r   __main__)
__future__r   unittestshape_inference_testr   onnx.parserr2   r   onnx.helperr   r   r   r   rS   mainr   r   r   r    <module>   s    k