U
    qh                  
   @  s   d Z ddlmZ ddlZddlmZ ddlZddlm  m	Z
 ddlmZmZmZmZ d$ddddd	d
ddZd%dddddddddZdddejfdddddddddddZdddd d!d"d#Ze
jZdS )&zIonnx shape inference. Shape inference is not guaranteed to be
complete.

    )annotationsN)Sequence)AttributeProtoFunctionProto
ModelProto	TypeProtoFzModelProto | bytesboolr   )model
check_typestrict_mode	data_propreturnc                 C  sf   t | ttfr>t | tr| n|  }t||||}t|S t | trPt	dt	dt
|  dS )a  Apply shape inference to the provided ModelProto.

    Inferred shapes are added to the value_info field of the graph.

    If the inferred values conflict with values already provided in the
    graph, that means that the provided values are invalid (or there is a
    bug in shape inference), and the result is unspecified.

    Arguments:
        model: ModelProto.
        check_type: Checks the type-equality for input and output.
        strict_mode: Stricter shape inference, it will throw errors if any;
            Otherwise, simply stop if any error.
        data_prop: Enables data propagation for limited operators to perform shape computation.

    Returns:
        (ModelProto) model with inferred shape information
    zhinfer_shapes only accepts ModelProto or bytes,you can use infer_shapes_path for the model path (String).z?infer_shapes only accepts ModelProto or bytes, incorrect type: N)
isinstancer   bytesSerializeToStringCinfer_shapesonnxZload_from_stringstr	TypeErrortype)r	   r
   r   r   Z	model_strZinferred_model_str r   8/tmp/pip-unpacked-wheel-xnis5xre/onnx/shape_inference.pyr      s        

r    zstr | os.PathLikeNone)
model_pathoutput_pathr
   r   r   r   c              
   C  s   t | trtdzt| } W n6 tk
rV } ztdt|  |W 5 d}~X Y nX zt|}W n6 tk
r } ztdt| |W 5 d}~X Y nX |dkr| }t| |||| dS )zTake model path for shape_inference.

    This function is the same as :func:`infer_shape` but supports >2GB models.
    The function outputs the inferred model to the `output_path`. The original model path
    is used if not specified.
    z_infer_shapes_path only accepts model Path (String),you can use infer_shapes for the ModelProto.z^infer_shapes_path only accepts model path as a string or PathLike, incorrect model path type: Nz`infer_shapes_path only accepts output path as a string or PathLike, incorrect output path type: r   )r   r   r   osfspathr   r   infer_shapes_path)r   r   r
   r   r   expr   r   r   r   =   s.    
r   zonnx.defs.OpSchemazonnx.NodeProtozdict[str, onnx.TypeProto]z"dict[str, onnx.TensorProto] | Nonez(dict[str, onnx.SparseTensorProto] | Nonez$list[onnx.OperatorSetIdProto] | Noneint)schemanodeinput_types
input_datainput_sparse_dataopset_imports
ir_versionr   c                   s   | j s
i S  d kri  d kr"i |d kr0i }ndd |D }fdd|jD }D ]}	|	|krV|	  ||	< qV fdd|jD }
fdd|jD }| | ||
|||}dd | D S )Nc                 S  s   i | ]}|j |jqS r   )domainversion).0Zopsetr   r   r   
<dictcomp>u   s      z&infer_node_outputs.<locals>.<dictcomp>c                   s"   i | ]}|d kr| |   qS )r   r   r+   key)r$   r   r   r,   x   s      c                   s"   i | ]}| kr| |   qS r   r-   r.   )r%   r   r   r,      s    c                   s"   i | ]}| kr| |   qS r   r-   r.   )r&   r   r   r,      s    c                 S  s   i | ]\}}|t j|qS r   )r   r   Z
FromString)r+   r/   outr   r   r   r,      s      )Z%has_type_and_shape_inference_functioninputr   Z_infer_node_outputsitems)r"   r#   r$   r%   r&   r'   r(   Zpassed_opset_importsZpassed_input_typesr/   Zpassed_input_dataZpassed_sparse_input_dataoutputsr   )r%   r&   r$   r   infer_node_outputsc   s<    	


r4   r   zSequence[TypeProto]zSequence[AttributeProto]zlist[TypeProto])functionr$   
attributesr   c                   sF   t |  dd |D dd |D }dddd  fdd|D S )	zxApply type-and-shape-inference to given function body, with given input types
    and given input attribute values.
    c                 S  s   g | ]}|  qS r   r-   r+   xr   r   r   
<listcomp>   s     z/infer_function_output_types.<locals>.<listcomp>c                 S  s   g | ]}|  qS r   r-   r7   r   r   r   r9      s     r   )r   c                 S  s   t  }||  |S )N)r   r   ZParseFromString)r8   Z
type_protor   r   r   to_type_proto   s    
z2infer_function_output_types.<locals>.to_type_protoc                   s   g | ]} |qS r   r   r7   r:   r   r   r9      s     )r   infer_function_output_typesr   )r5   r$   r6   resultr   r;   r   r<      s    r<   )FFF)r   FFF)__doc__
__future__r   r   typingr   r   Z'onnx.onnx_cpp2py_export.shape_inferenceZonnx_cpp2py_exportZshape_inferencer   r   r   r   r   r   r   Z
IR_VERSIONr4   r<   ZInferenceErrorr   r   r   r   <module>   s,      +    *2