
     `iN                        d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZmZ d dlmZ d d	lmZmZmZ  ed
          Zg dZ G d de          ZdededefdZdefdZd Zdededeee         ee         eef         fdZ 	 d3dededede	e         def
dZ!dede"dede#fdZ$dede"defd Z%	 	 	 d4dededede"de	e         de#defd#Z&d$edefd%Z'd$edefd&Z(d'efd(Z)e*d)k    rn e            Z+e+,                                Z- ee-j.                  /                                e-_.        	  e0d*            e&e-j1        e-j2        e-j.        e-j3        e-j4        e-j5        e-j                   e-j(        rV ee           e-j1        d+k    r e0d,            e0d-            e'e-j.                  e-_6         e(e-j6                  e-_7        e-j8        rU e0d.            e)e-j.                    e9e-d/          r e)e-j6                    e9e-d0          r e)e-j7                   dS dS dS # e:$ r$Z; e0d1e;             e<d2           Y dZ;[;dS dZ;[;ww xY wdS )5    N)ArgumentParser)listdirmakedirs)Path)Optional)Versionparse)Pipelinepipeline)BatchEncoding)ModelOutputis_tf_availableis_torch_availablez1.4.0)	feature-extractionnerzsentiment-analysisz	fill-maskzquestion-answeringztext-generationtranslation_en_to_frtranslation_en_to_detranslation_en_to_roc                   "     e Zd ZdZ fdZ xZS )OnnxConverterArgumentParserz[
    Wraps all the script arguments supported to export transformers models to ONNX IR
    c                 <   t                                          d           |                     dt          t          d           |                     dt          dd           |                     d	t          d
           |                     dt          ddgd           |                     dt
          dd           |                     ddd           |                     ddd           |                     ddd           |                     d           d S )NzONNX Converterz
--pipeliner   )typechoicesdefaultz--modelTz4Model's id or path (ex: google-bert/bert-base-cased))r   requiredhelpz--tokenizerz8Tokenizer's id or path (ex: google-bert/bert-base-cased))r   r   z--frameworkpttfzFramework for loading the model)r   r   r   z--opset   zONNX opset to use)r   r   r   z--check-loading
store_truez$Check ONNX is able to load the model)actionr   z--use-external-formatz!Allow exporting model >= than 2Gbz
--quantizez/Quantize the neural network to be run with int8output)super__init__add_argumentstrSUPPORTED_PIPELINESint)self	__class__s    v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/convert_graph_to_onnx.pyr$   z$OnnxConverterArgumentParser.__init__3   sm   )***'(	 	 	
 	
 	
 	G	 	 	
 	
 	
 	-c8rsss4L2	 	 	
 	
 	
 	)#r@STTT7 	 	
 	
 	

 	#4 	 	
 	
 	

 	B 	 	
 	
 	

 	(#####    )__name__
__module____qualname____doc__r$   __classcell__)r*   s   @r+   r   r   .   sB         &$ &$ &$ &$ &$ &$ &$ &$ &$r,   r   filename
identifierreturnc                 v    | j                             | j        |z                                 | j                  S )aE  
    Append a string-identifier at the end (before the extension, if any) to the provided filepath

    Args:
        filename: pathlib.Path The actual path object we would like to add an identifier suffix
        identifier: The suffix to add

    Returns: String with concatenated identifier at the end of the filename
    )parentjoinpathstemwith_suffixsuffix)r2   r3   s     r+   generate_identified_filenamer;   \   s1     ?##HMJ$>??KKHO\\\r,   minimum_versionc                     	 ddl }t          |j                  }|t          k     rt	          d|j         d|  d          dS # t          $ r t	          d          w xY w)z
    Check onnxruntime is installed and if the installed version match is recent enough

    Raises:
        ImportError: If onnxruntime is not installed or too old version is found
    r   Nz*We found an older version of onnxruntime (z&) but we require onnxruntime to be >= zp to enable all the conversions options.
Please update onnxruntime by running `pip install --upgrade onnxruntime`zonnxruntime doesn't seem to be currently installed. Please install the onnxruntime by running `pip install onnxruntime` and relaunch the conversion.)onnxruntimer	   __version__ORT_QUANTIZE_MINIMUM_VERSIONImportError)r<   r>   ort_versions      r+   check_onnxruntime_requirementsrC   i   s    
 K344 555[[=T [ [7F[ [ [   65  
 
 
,
 
 	

s   >A Ac                 8   t          d           | j        j        j        }g g }}|dd         D ]I}||v r1|                    |           |                    ||                    7t          | d            t          d|            |t          |          fS )a  
    Ensure inputs are presented in the correct order, without any Non

    Args:
        model: The model used to forward the input data
        tokens: BatchEncoding holding the input data
        input_names: The name of the inputs

    Returns: Tuple

    z$Ensuring inputs are in correct order   Nz, is not present in the generated input list.zGenerated inputs order: )printforward__code__co_varnamesappendtuple)modeltokensinput_namesmodel_args_name
model_argsordered_input_namesarg_names          r+   ensure_valid_inputrS      s     

0111m,8O&("#J#ABB'  {""&&x000fX.////XKKKLLL	
:%8
:
:;;;j 1 111r,   nlp	frameworkc                 :   dt           dt          dt          ffd|                     d|          }|j        j        d         |dk    r | j        di |n|                     |          }t          |t                    r|	                                }t          |t          t          f          s|f}t          |                                          }fd	|                                D             }g }|D ]I}t          |t          t          f          r|                    |           4|                    |           Jd
 t!          t#          |                    D             }fdt%          ||          D             }	t'          |fi |	}
|||
|fS )a?  
    Attempt to infer the static vs dynamic axes for each input and output tensors for a specific model

    Args:
        nlp: The pipeline object holding the model to be exported
        framework: The framework identifier to dispatch to the correct inference scheme (pt/tf)

    Returns:

        - List of the inferred input variable names
        - List of the inferred output variable names
        - Dictionary with input/output variables names as key and shape tensor as value
        - a BatchEncoding reference which was used to infer all the above information
    nameis_inputseq_lenc                     t          |t          t          f          r fd|D             S d t          |j                  D             d         di}rCt          |j                  dk    rd|d<   nst          dt          |j                   d	          fd
t          |j                  D             }|                    t          	                    |d                     t          drdnd d  d|            |S )Nc                 ,    g | ]} |          S  r\   ).0tbuild_shape_dictrX   rW   rY   s     r+   
<listcomp>z:infer_shapes.<locals>.build_shape_dict.<locals>.<listcomp>   s+    QQQQ$$T1h@@QQQr,   c                 $    g | ]\  }}|d k    |S )rE   r\   )r]   axisnumels      r+   r`   z:infer_shapes.<locals>.build_shape_dict.<locals>.<listcomp>   s"    SSSkdEQR

T


r,   r   batch   sequencerE   zUnable to infer tensor axes ()c                 &    g | ]\  }}|k    |S r\   r\   )r]   dimshaperY   s      r+   r`   z:infer_shapes.<locals>.build_shape_dict.<locals>.<listcomp>   s(    ^^^JCUV]M]M]CM]M]M]r,   zFound inputr"    z with shape: )
isinstancerK   list	enumeraterj   len
ValueErrorupdatedictfromkeysrF   )rW   tensorrX   rY   axesseq_axesr_   s   ` ``  r+   r_   z&infer_shapes.<locals>.build_shape_dict   s1   fudm,, 	AQQQQQQQ&QQQQ TSYv|-D-DSSSTUVX_`D Av|$$))(DGG$%YSEVEV%Y%Y%YZZZ^^^^)FL2I2I^^^DMM(J??@@@T(8wwTT4TTdTTUUUr,   zThis is a sample output)return_tensorsr   c           	      4    i | ]\  }}| ||d           S )Tr\   r]   kvr_   rY   s      r+   
<dictcomp>z infer_shapes.<locals>.<dictcomp>   s1    ^^^tq!!--aD'BB^^^r,   c                     g | ]}d | S )output_r\   )r]   is     r+   r`   z infer_shapes.<locals>.<listcomp>   s    DDDaMaMMDDDr,   c           	      4    i | ]\  }}| ||d           S )Fr\   r{   s      r+   r~   z infer_shapes.<locals>.<dictcomp>   s1    qqqA1..q!UGDDqqqr,   r\   )r&   boolr(   	tokenizer	input_idsrj   rL   rm   r   to_tuplern   rK   keysitemsextendrJ   rangerp   ziprs   )rT   rU   rM   outputs
input_varsinput_dynamic_axesoutputs_flatr"   output_namesoutput_dynamic_axesdynamic_axesr_   rY   s              @@r+   infer_shapesr      s    s d S      & ]]4Y]OOF$R(G%.$%6%6ici!!&!!!CIIf<M<MG';'' %""$$ge}-- * fkkmm$$J^^^^^v||~~^^^ L ( (fudm,, 	('''''''' ED5\1B1B+C+CDDDLqqqqqQTUacoQpQpqqq *BB.ABBL|\699r,   pipeline_namerL   r   c                     ||}|dk    rt                      st          d          |dk    rt                      st          d          t          d| d| d           t	          | ||||	          S )
a  
    Convert the set of arguments provided through the CLI to an actual pipeline reference (tokenizer + model

    Args:
        pipeline_name: The kind of pipeline to use (ner, question-answering, etc.)
        framework: The actual model to convert the pipeline from ("pt" or "tf")
        model: The model name which will be loaded by the pipeline
        tokenizer: The tokenizer name which will be loaded by the pipeline, default to the model's value

    Returns: Pipeline object

    Nr   LCannot convert because PyTorch is not installed. Please install torch first.r   LCannot convert because TF is not installed. Please install tensorflow first.zLoading pipeline (model: z, tokenizer: rg   )rL   r   rU   model_kwargs)r   	Exceptionr   rF   r   )r   rU   rL   r   models_kwargss        r+   load_graph_from_argsr      s      	 D!3!5!5fgggD!2!2fggg	
Fe
F
F)
F
F
FGGG M)ygtuuuur,   opsetr"   use_external_formatc                    t                      st          d          ddl}ddlm} t          d|j                    |                                5  t          | d          \  }}}}	t          | j
        |	|          \  }
} || j
        ||                                |
||d|           ddd           dS # 1 swxY w Y   dS )	a  
    Export a PyTorch backed pipeline to ONNX Intermediate Representation (IR

    Args:
        nlp: The pipeline to be exported
        opset: The actual version of the ONNX operator set to use
        output: Path where will be stored the generated ONNX model
        use_external_format: Split the model definition from its parameters to allow model bigger than 2GB

    Returns:

    r   r   N)exportzUsing framework PyTorch: r   T)frN   r   r   do_constant_foldingopset_version)r   r   torch
torch.onnxr   rF   r?   no_gradr   rS   rL   as_posix)rT   r   r"   r   r   r   rN   r   r   rM   rQ   rP   s               r+   convert_pytorchr     s8     hfgggLLL!!!!!!	
9e&7
9
9:::	 
 
:FsD:Q:Q7\<*<SYP[*\*\'ZIoo+%% $		
 		
 		
 		
	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   AB99B= B=c           	      <   t                      st          d          t          d           	 ddlddl}ddlm} t          dj        j         d|            t          | d          \  }}}}| j	        
                    |j                   fd	|                                D             }	|j                            | j	        |	||                                
          \  }
}dS # t           $ r'}t          d|j         d|j         d|           d}~ww xY w)av  
    Export a TensorFlow backed pipeline to ONNX Intermediate Representation (IR)

    Args:
        nlp: The pipeline to be exported
        opset: The actual version of the ONNX operator set to use
        output: Path where will be stored the generated ONNX model

    Notes: TensorFlow cannot export model bigger than 2GB due to internal constraint from TensorFlow

    r   zD/!\ Please note TensorFlow doesn't support exporting model > 2Gb /!\r   N)r?   zUsing framework TensorFlow: z, tf2onnx: r   c                 N    g | ]!\  }}j                             ||           "S ))rW   )
TensorSpecfrom_tensor)r]   keyru   r   s      r+   r`   z&convert_tensorflow.<locals>.<listcomp>C  s3    iii;3PV2=44V#4FFiiir,   )r   output_pathzCannot import z6 required to convert TF model to ONNX. Please install z first. )r   r   rF   
tensorflowtf2onnxr?   versionVERSIONr   rL   predictdatar   convert
from_kerasr   rA   rW   )rT   r   r"   r   t2ovrN   r   r   rM   input_signaturemodel_proto_er   s                @r+   convert_tensorflowr   &  sk     hfggg	
RSSS
//////RRZ-?RRDRRSSS ;GsD:Q:Q7\< 		&+&&&iiiiZ`ZfZfZhZhiii 33IeARAR 4 
 
QQQ  
 
 
nQVnn[\[annklnn
 
 	

s   B9C* *
D4"DDFr   c                 D   t          j        dt                     t          d|            t	          || ||fi |}|j                                        s>t          d|j                    t          |j                                                   nat          t          |j                                                            dk    r*t          d|j                                         d          | dk    rt          ||||           dS t          |||           dS )	a  
    Convert the pipeline object to the ONNX Intermediate Representation (IR) format

    Args:
        framework: The framework the pipeline is backed by ("pt" or "tf")
        model: The name of the model to load for the pipeline
        output: The path where the ONNX graph will be stored
        opset: The actual version of the ONNX operator set to use
        tokenizer: The name of the model to load for the pipeline, default to the model's name if not provided
        use_external_format:
            Split the model definition from its parameters to allow model bigger than 2GB (PyTorch only)
        pipeline_name: The kind of pipeline to instantiate (ner, question-answering, etc.)
        model_kwargs: Keyword arguments to be forwarded to the model constructor

    Returns:

    zoThe `transformers.convert_graph_to_onnx` package is deprecated and will be removed in version 5 of TransformerszONNX opset version set to: zCreating folder r   zFolder z" is not empty, aborting conversionr   N)warningswarnFutureWarningrF   r   r6   existsr   r   rp   r   r   r   r   )	rU   rL   r"   r   r   r   r   r   rT   s	            r+   r   r   N  s6   6 M	  
 

/
/
/000 }i	
Z
Z\
Z
ZC=!! `000111''))****	WV]++--..	/	/!	3	3^&-"8"8":":^^^___ DUF,?@@@@@3v.....r,   onnx_model_pathc                     ddl m}m} t          | d          } |            }|                                |_         ||                                 |          }t          d| d           t          d           |S )a>  
    Load the model at the specified path and let onnxruntime look at transformations on the graph to enable all the
    optimizations possible

    Args:
        onnx_model_path: filepath where the model binary description is stored

    Returns: Path where the optimized model binary description has been saved

    r   InferenceSessionSessionOptionsz
-optimizedz$Optimized model has been written at    : ✔zY/!\ Optimized model contains hardware specific operators which might not be portable. /!\)r>   r   r   r;   r   optimized_model_filepathrF   )r   r   r   opt_model_pathsess_optionr   s         r+   optimizer     s     =<<<<<<< 2/<PPN .""K+9+B+B+D+DK(1133[AAA	
W
W
W
WXXX	
ghhhr,   c                    ddl }ddl}ddlm} ddlm} ddlm} ddlm	} |
                    |                                           }t          |j                  t          d          k     rt          d            |            }|                    |           t          |j                  t          d	          k     r) ||d
d
|j        d
dd
dddt#          |                    }	n( ||d
d
|j        d
dd
dddt#          |                    }	|	                                 t'          | d          }
t          d|
 d           |                    |	j        j        |
                                           |
S )z
    Quantize the weights of the model from float32 to in8 to allow very efficient inference on modern CPU

    Args:
        onnx_model_path: Path to location the exported ONNX model is stored

    Returns: The Path generated for the quantized
    r   N)
ModelProto)QuantizationMode)ONNXQuantizer)IntegerOpsRegistryz1.5.0zpModels larger than 2GB will fail to quantize due to protobuf constraint.
Please upgrade to onnxruntime >= 1.5.0.z1.13.1FT)rL   per_channelreduce_rangemodestaticweight_qTypeinput_qTypetensors_rangenodes_to_quantizenodes_to_excludeop_types_to_quantize)rL   r   r   r   r   r   activation_qTyper   r   r   r   z
-quantizedz$Quantized model has been written at r   )onnxr>   onnx.onnx_pbr   onnxruntime.quantizationr   'onnxruntime.quantization.onnx_quantizerr   !onnxruntime.quantization.registryr   loadr   r	   r?   rF   CopyFrom
IntegerOpsrn   quantize_modelr;   
save_modelrL   )r   r   r>   r   r   r   r   
onnx_model
copy_model	quantizerquantized_model_paths              r+   quantizer     s    KKK''''''999999EEEEEEDDDDDD ?335566JTw//6	
 	
 	
 J
### [$%%h77!M!,"!!%&8!9!9
 
 
		 "M!,""!!%&8!9!9
 
 
	  8VV 

]1E
]
]
]^^^OOIO)+?+H+H+J+JKKKr,   pathc                    ddl m}m} ddlm} t          d|  d           	  |            } ||                                 |dg          }t          d|  d	           d S # |$ r}t          d
| d           Y d }~d S d }~ww xY w)Nr   r   )RuntimeExceptionz"Checking ONNX model loading from: z ...CPUExecutionProvider)	providerszModel u    correctly loaded: ✔zError while loading the model u   : ✘)r>   r   r   +onnxruntime.capi.onnxruntime_pybind11_stater   rF   r   )r   r   r   r   onnx_optionsr   res          r+   verifyr     s    <<<<<<<<LLLLLL	
9t
9
9
9:::I%~''T]]__lG]F^___DtDDDEEEEE I I IGrGGGHHHHHHHHHIs   >A# #B(BB__main__z'
====== Converting model to ONNX ======r   aV  	 Using TensorFlow might not provide the same optimization level compared to PyTorch.
	 For TensorFlow users you can try optimizing the model directly through onnxruntime_tools.
	 For more information, please refer to the onnxruntime documentation:
		https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/python/tools/transformers
z$
====== Optimizing ONNX model ======z+
====== Check exported ONNX model(s) ======optimized_outputquantized_outputz"Error while converting the model: rE   )N)NFr   )=r   argparser   osr   r   pathlibr   typingr   packaging.versionr   r	   transformers.pipelinesr
   r   transformers.tokenization_utilsr   transformers.utilsr   r   r   r@   r'   r   r&   r;   rC   rS   rK   rn   rs   r   r   r(   r   r   r   r   r   r   r   r-   parser
parse_argsargsr"   absoluterF   rU   rL   r   r   r   r   r   check_loadinghasattrr   r   exitr\   r,   r+   <module>r     sG    # # # # # #                             , , , , , , , , 5 5 5 5 5 5 5 5 9 9 9 9 9 9 O O O O O O O O O O
  %uW~~ 
 
 
 +$ +$ +$ +$ +$. +$ +$ +$\
]4 
]S 
]T 
] 
] 
] 
]
G 
 
 
 
:2 2 28=:h =:3 =:5cDItUb9b3c =: =: =: =:B PTv vv#&v/2v?G}vv v v v>"
 "
# "
t "
RV "
 "
 "
 "
J%
H %
S %
$ %
 %
 %
 %
Z  $ %-// ////// // 	//
 }// // // // // //dd t    2H d H t H  H  H  H V
I 
I 
I 
I 
I z((**FD $t{##,,..DK/8999NJKJN$M	
 	
 	
 = 	D**+GHHH ~%%w   E9::: %-HT[$9$9D! %-HT-B$C$CD!  	.E@AAAF4;wt/00 .t,---wt/00 .t,-----	. 	.. .    6166777Qi s   :C6H6 6I;II