o
    XiH                     @  sh   d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 er"ddl
Z
eeZdZedZdddZdS )z+Utilities for interfacing with onnx C APIs.    )annotationsN)TYPE_CHECKINGCallableTypeVar)iri  _RfuncCallable[[onnx.ModelProto], _R]modelir.Modelreturnc           	   
   C  sj  t |jj }dd |D }t|jj}|D ]B}|jdus J |jdu r*|jj|_|jdu r4|jj|_||jjvrA|jj	| |jj
tkrYd|_|jdusQJ |jj|j qtj|}z-| |}W |D ]}||j |_|j| qh|jjd| }|jj  |jj| |S |D ]}||j |_|j| q|jjd| }|jj  |jj| w )a  Call an ONNX C API function by temporarily removing initializers.

    This is necessary because the ONNX C API does not support large models
    with initializers that have large tensor values. The input model is left
    unchanged no matter the call succeeds or not.

    Args:
        func: Partially applied function that takes a model proto and returns anything.
        model: The IR model to pass to the API function.

    Returns:
        The resulting ModelProto that contains the result of the API call.
    c                 S  s   i | ]}|j |jqS  )nameconst_value).0vr   r   ]/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/version_converter/_c_api_utils.py
<dictcomp>(   s    z!call_onnx_api.<locals>.<dictcomp>N)tuplegraphinitializersvaluesleninputsr   shapedtypeappendsize_BIG_TENSOR_SIZE_LIMITr   popr   serdeserialize_modelregister_initializerclearextend)	r   r
   initializer_valuestensorsoriginal_inputs_leninitializerprotoresultr   r   r   r   call_onnx_api   s@   




r+   )r   r	   r
   r   r   r   )__doc__
__future__r   loggingtypingr   r   r   
onnxscriptr   onnx	getLogger__name__loggerr   r   r+   r   r   r   r   <module>   s   
