o
    q::j                     @  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mZ er5ddlmZ e
jZe
jZ			d1d2ddZ				d3d4ddZddde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)
IR_VERSIONAttributeProtoFunctionProto
ModelProto	TypeProto)SequenceFmodelModelProto | bytes
check_typeboolstrict_mode	data_propreturnr   c                 C  sh   t | ttfrt | tr| n|  }t||||}t|S t | tt	j
f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
    zminfer_shapes only accepts ModelProto or bytes, For Model paths (str or os.PathLike), use infer_shapes_path().z?infer_shapes only accepts ModelProto or bytes, incorrect type: )
isinstancer   bytesSerializeToStringCinfer_shapesonnxload_from_stringstrosPathLike	TypeErrortype)r
   r   r   r   	model_strinferred_model_str r   [/home/nk/hobo-godmode/plappi-mvp/.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   r   fspathr   r   infer_shapes_path)r"   r$   r   r   r   expr   r   r    r'   I   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    d u ri  d u ri |d u ri }ndd |D }fdd|j D } D ]\}	}
|	|vr6|
 ||	< 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>   s    z&infer_node_outputs.<locals>.<dictcomp>c                   s"   i | ]}|d kr| |   qS )r!   r   r9   key)r-   r   r    r;      s    c                   "   i | ]}| v r| |   qS r   r<   r=   )r/   r   r    r;      
    c                   r?   r   r<   r=   )r1   r   r    r;      r@   c                 S  s   i | ]\}}|t j|qS r   )r   r   
FromString)r9   r>   outr   r   r    r;      s    )inputitemsr   _infer_node_outputs)r)   r+   r-   r/   r1   r3   r5   passed_opset_importspassed_input_typesr>   valuepassed_input_datapassed_sparse_input_dataoutputsr   )r/   r1   r-   r    infer_node_outputso   s:   	


rL   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<   r9   xr   r   r    
<listcomp>       z/infer_function_output_types.<locals>.<listcomp>c                 S  rR   r   r<   rS   r   r   r    rU      rV   r   r   c                 S  s   t  }||  |S )N)r   r   ParseFromString)rT   
type_protor   r   r    to_type_proto   s   
z2infer_function_output_types.<locals>.to_type_protoc                   s   g | ]} |qS r   r   rS   rY   r   r    rU      rV   N)r   r   )r   infer_function_output_typesr   )rM   r-   rO   resultr   rZ   r    r[      s   
r[   )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/   r0   r1   r2   r3   r4   r5   r6   r   r.   )rM   r   r-   rN   rO   rP   r   rQ   )__doc__
__future__r   r   typingr   r   'onnx.onnx_cpp2py_export.shape_inferenceonnx_cpp2py_exportshape_inferencer   onnx.onnx_pbr   r   r   r   r   collections.abcr	   GraphInferencerInferenceContextr   r'   rL   r[   InferenceErrorr   r   r   r    <module>   s6   +*
0
