U
    qh                     @  s  d Z ddlmZ ddddddd	d
dddddgZddlZddlZddlmZmZm	Z	 ddl
mZ ddlZddlm  mZ ddlZddlmZmZmZmZmZmZmZmZmZ dZe Zee_dej ! ie_"e# Z$e	dedef dZ%ddddddZ&efdd dd!d"d
Z'efd#d dd$d%d	Z(ee$fd&d d'dd(d)dZ)ee$fd*d d'dd+d,dZ*de$fd-d.d'dd/d0dZ+ee$fd1d d'dd2d3dZ,efd4d dd5d6dZ-d<d8d9d9d9dd:d;dZ.ej/Z/dS )=zDGraph utilities for checking whether an ONNX proto message is legal.    )annotationscheck_attributecheck_functioncheck_graphcheck_model
check_nodecheck_sparse_tensorcheck_tensorcheck_value_infoDEFAULT_CONTEXTLEXICAL_SCOPE_CONTEXTValidationErrorCMAXIMUM_PROTOBUFN)AnyCallableTypeVar)Message)	
IR_VERSIONAttributeProtoFunctionProto
GraphProto
ModelProto	NodeProtoSparseTensorProtoTensorProtoValueInfoProtoi 5w FuncType.)boundr   ztype[Message]None)proto
proto_typereturnc                 C  s    t | |std|j dd S )Nz'The proto message needs to be of type '')
isinstance	TypeError__name__)r!   r"    r(   0/tmp/pip-unpacked-wheel-xnis5xre/onnx/checker.py_ensure_proto_typeA   s    
r*   r   zC.CheckerContext)
value_infoctxr#   c                 C  s   t | t t|  |S N)r*   r   r   r
   SerializeToString)r+   r,   r(   r(   r)   r
   H   s    
r   )tensorr,   r#   c                 C  s   t | t t|  |S r-   )r*   r   r   r	   r.   )r/   r,   r(   r(   r)   r	   O   s    
r   zC.LexicalScopeContext)attrr,   lexical_scope_ctxr#   c                 C  s   t | t t|  ||S r-   )r*   r   r   r   r.   )r0   r,   r1   r(   r(   r)   r   T   s    
r   )noder,   r1   r#   c                 C  s   t | t t|  ||S r-   )r*   r   r   r   r.   )r2   r,   r1   r(   r(   r)   r   ]   s    
r   zC.CheckerContext | None)functionr,   r1   r#   c                 C  sV   t | t |d kr@t }tjj| jdd|_dd | jD |_	t
|  || d S )NT)Zignore_unknownc                 S  s   i | ]}|j |jqS r(   )domainversion).0Zdomain_versionr(   r(   r)   
<dictcomp>q   s    z"check_function.<locals>.<dictcomp>)r*   r   r   CheckerContextonnxhelperZfind_min_ir_version_forZopset_import
ir_versionopset_importsr   r.   )r3   r,   r1   r(   r(   r)   r   f   s    
 r   )graphr,   r1   r#   c                 C  s   t | t t|  ||S r-   )r*   r   r   r   r.   )r=   r,   r1   r(   r(   r)   r   x   s    
r   )sparser,   r#   c                 C  s   t | t t|  | d S r-   )r*   r   r   r   r.   )r>   r,   r(   r(   r)   r      s    
Fz&ModelProto | str | bytes | os.PathLikebool)model
full_checkskip_opset_compatibility_checkcheck_custom_domainr#   c                 C  sh   t | ttjfr(tt| ||| n<t | tr6| n|  }t	
|tkrTtdt|||| dS )ar  Check the consistency of a model.

    An exception will be raised if the model's ir_version is not set
    properly or is higher than checker's ir_version, or if the model
    has duplicate keys in metadata_props.

    If IR version >= 3, the model must specify opset_import.
    If IR version < 3, the model cannot have any opset_import specified.

    Args:
        model: Model to check. If model is a path, the function checks model
            path first. If the model bytes size is larger than 2GB, function
            should be called using model path.
        full_check: If True, the function also runs shape inference check.
        skip_opset_compatibility_check: If True, the function skips the check for
            opset compatibility.
        check_custom_domain: If True, the function will check all domains. Otherwise
            only check built-in domains.
    zZThis protobuf of onnx model is too large (>2GB). Call check_model with model path instead.N)r%   strosPathLiker   Zcheck_model_pathfspathbytesr.   sys	getsizeofr   
ValueErrorr   )r@   rA   rB   rC   Zprotobuf_stringr(   r(   r)   r      s&    )FFF)0__doc__
__future__r   __all__rE   rI   typingr   r   r   Zgoogle.protobuf.messager   Z	onnx.defsr9   Zonnx.onnx_cpp2py_export.checkerZonnx_cpp2py_exportcheckerr   Zonnx.shape_inferencer   r   r   r   r   r   r   r   r   r   r8   r   r;   ZdefsZonnx_opset_versionr<   ZLexicalScopeContextr   r   r*   r
   r	   r   r   r   r   r   r   r   r(   r(   r(   r)   <module>   sh   ,
	   3