o
    ߥi                     @   s~   d dl Z d dlmZmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ ddlmZ e ZG d	d
 d
eZdS )    N)AnyCallableDictMapping)ModelOutputBase)	ModelFile)
get_logger)compare_arguments_nested   )Exporterc                   @   s   e Zd Zdeeef fddZddefddZdefdd	Zdefd
dZ						ddede
dedededefddZ			ddededefddZdS )TfModelExporterreturnc                 K   s   dS )zGenerate dummy inputs for model exportation to onnx or other formats by tracing.

        Returns:
            Dummy inputs that matches the specific model input, the matched preprocessor can be used here.
        N )selfkwargsr   r   Z/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/exporters/tf_model_exporter.pygenerate_dummy_inputs   s   z%TfModelExporter.generate_dummy_inputs   
output_dirc                 K   sH   d|vr| j n|d}tj|tj}| j||fd|i| d|iS )Nmodelopset)r   popospathjoinr   ONNX_MODEL_FILE_tf2_export_onnx)r   r   r   r   r   	onnx_filer   r   r   export_onnx   s   zTfModelExporter.export_onnxc                 K      t  NNotImplementedErrorr   r   r   r   r   r   export_saved_model       z"TfModelExporter.export_saved_modelc                 K   r   r    r!   r#   r   r   r   export_frozen_graph_def#   r%   z'TfModelExporter.export_frozen_graph_defTNoutputr   
validationrtolatol	call_funcc                 K   s   t d dd l}	dd l}
d|vr| jdi |n|d}|d u r&tddd | D }|
jj	|||d\}}|	
|| |rN| |||||| d S d S )	NzJImportant: This exporting function only supports models of tf2.0 or above.r   dummy_inputsz7Model property dummy_inputs,inputs,outputs must be set.c                 S   s    g | ]\}}t jj||d qS ))name)tf
TensorSpecfrom_tensor).0keytensorr   r   r   
<listcomp>;   s    z4TfModelExporter._tf2_export_onnx.<locals>.<listcomp>)r   r   )loggerinfoonnxtf2onnxr   r   r"   itemsconvert
from_kerassave_validate_model)r   r   r'   r   r(   r)   r*   r+   r   r7   r8   r,   input_signature
onnx_model_r   r   r   r   &   s:   	
z TfModelExporter._tf2_export_onnxc                    sD  z
dd l }dd l}W n ty   td Y d S w  fdd ||}	|jj|	dd |j|ddgd	}
|d ur>||n||}t	|t
tfrRt | }nt	|ttfr_t |}|
d  |} |}t	|trwt| }n	t	|trt|}i }|d ur||d
< |d ur||d< td||fi |stdd S )Nr   zgCannot validate the exported onnx file, because the installation of onnx or onnxruntime cannot be foundc                    sh   t | ttfrt|  fdd| D S t | tr$ fdd|  D S t | tjr2|  }|	 S | S )Nc                 3   s    | ]} |V  qd S r    r   )r1   ttensor_nested_numpifyr   r   	<genexpr>[   s    zQTfModelExporter._validate_model.<locals>.tensor_nested_numpify.<locals>.<genexpr>c                    s   i | ]	\}}| |qS r   r   )r1   krA   rB   r   r   
<dictcomp>^   s    zRTfModelExporter._validate_model.<locals>.tensor_nested_numpify.<locals>.<dictcomp>)

isinstancelisttupletyper   r9   r.   Tensorcpunumpy)tensorsrA   rB   r   r   rC   Y   s   

z>TfModelExporter._validate_model.<locals>.tensor_nested_numpifyT)
full_checkCUDAExecutionProviderCPUExecutionProvider)	providersr)   r*   zOnnx model output match failedz/export onnx failed because of validation error.)r7   onnxruntimeImportErrorr5   warnloadcheckercheck_modelInferenceSessionrG   r   r   rH   valuesrI   rundictr	   RuntimeError)r   r,   r   r'   r)   r*   r+   r7   ortr?   ort_sessionoutputs_originoutputstolsr   rB   r   r=   G   sf   	



zTfModelExporter._validate_model)r   )r   TNNN)NNN)__name__
__module____qualname__r   strr   r   r   r$   r&   intboolfloatr   r   r=   r   r   r   r   r      sB    
&r   )r   typingr   r   r   r   
tensorflowr.   modelscope.outputsr   modelscope.utils.constantr   modelscope.utils.loggerr   modelscope.utils.test_utilsr	   baser   r5   r   r   r   r   r   <module>   s   