o
    wi                     @  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 er3ddlmZ 			d1d2ddZ				d3d4ddZdddejfd5d'd(Zd6d/d0Ze
jZdS )7zIonnx shape inference. Shape inference is not guaranteed to be
complete.

    )annotationsN)TYPE_CHECKING)AttributeProtoFunctionProto
ModelProto	TypeProto)SequenceFmodelModelProto | bytes
check_typeboolstrict_mode	data_propreturnr   c                 C  sb   t | ttfrt | tr| n|  }t||||}t|S t | tr(t	dt	dt
|  )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: )
isinstancer   bytesSerializeToStringCinfer_shapesonnxload_from_stringstr	TypeErrortype)r	   r   r   r   	model_strinferred_model_str r   Q/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/onnx/shape_inference.pyr      s   

r    
model_pathstr | os.PathLikeoutput_pathNonec              
   C  s   t | tr	tdzt| } W n ty& } z
tdt|  |d}~ww zt|}W n tyD } z
tdt| |d}~ww |dkrK| }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%   schemaonnx.defs.OpSchemanodeonnx.NodeProtoinput_typesdict[str, onnx.TypeProto]
input_data"dict[str, onnx.TensorProto] | Noneinput_sparse_data(dict[str, onnx.SparseTensorProto] | Noneopset_imports$list[onnx.OperatorSetIdProto] | None
ir_versionintc                   s   | j si S  d u ri  d u ri |d u ri }ndd |D }fdd|jD } D ]\}	}
|	|vr;|
 ||	< q- 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).0opsetr   r   r   
<dictcomp>x   s    z&infer_node_outputs.<locals>.<dictcomp>c                   s"   i | ]}|d kr| |   qS )r   r   r7   key)r+   r   r   r9   {   s    c                   "   i | ]}| v r| |   qS r   r:   r;   )r-   r   r   r9      
    c                   r=   r   r:   r;   )r/   r   r   r9      r>   c                 S  s   i | ]\}}|t j|qS r   )r   r   
FromString)r7   r<   outr   r   r   r9      s    )%has_type_and_shape_inference_functioninputitemsr   _infer_node_outputs)r'   r)   r+   r-   r/   r1   r3   passed_opset_importspassed_input_typesr<   valuepassed_input_datapassed_sparse_input_dataoutputsr   )r-   r/   r+   r   infer_node_outputsf   s>   	


rK   functionr   Sequence[TypeProto]
attributesSequence[AttributeProto]list[TypeProto]c                   sB   t |  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     g | ]}|  qS r   r:   r7   xr   r   r   
<listcomp>       z/infer_function_output_types.<locals>.<listcomp>c                 S  rQ   r   r:   rR   r   r   r   rT      rU   r   r   c                 S  s   t  }||  |S )N)r   r   ParseFromString)rS   
type_protor   r   r   to_type_proto   s   
z2infer_function_output_types.<locals>.to_type_protoc                   s   g | ]} |qS r   r   rR   rX   r   r   rT      rU   N)r   r   )r   infer_function_output_typesr   )rL   r+   rN   resultr   rY   r   rZ      s   
rZ   )FFF)
r	   r
   r   r   r   r   r   r   r   r   )r   FFF)r   r    r!   r    r   r   r   r   r   r   r   r"   )r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r   r,   )rL   r   r+   rM   rN   rO   r   rP   )__doc__
__future__r   r#   typingr   r   'onnx.onnx_cpp2py_export.shape_inferenceonnx_cpp2py_exportshape_inferencer   r   r   r   r   collections.abcr   r   r%   
IR_VERSIONrK   rZ   InferenceErrorr   r   r   r   <module>   s2   +*
2
