
    eThN                         S SK r S SKJr  S SKJrJr  S SKJr  S SKJ	r	  S SK
JrJr  S SKJrJr  S SKJr  S S	KJrJrJr  \" S
5      r/ SQr " S S\5      rS\S\S\4S jrS\4S jrS rS\S\S\\\   \\   \\4   4S jr  S1S\S\S\S\	\   S\4
S jjr!S\S\"S\S\#4S jr$S\S\"S\4S  jr%   S2S\S\S\S\"S\	\   S\#S\4S! jjr&S"\S\4S# jr'S"\S\4S$ jr(S%\4S& jr)\*S':X  Gan  \" 5       r+\+RY                  5       r-\" \-R\                  5      R_                  5       \-l.         \0" S(5        \&" \-Rb                  \-Rd                  \-R\                  \-Rf                  \-Rh                  \-Rj                  \-R                  5        \-RP                  (       aV  \" \5        \-Rb                  S):X  a  \0" S*5        \0" S+5        \'" \-R\                  5      \-l6        \(" \-Rl                  5      \-l7        \-Rp                  (       a\  \0" S,5        \)" \-R\                  5        \9" \-S-5      (       a  \)" \-Rl                  5        \9" \-S.5      (       a  \)" \-Rn                  5        gggg! \: a  r;\0" S/\; 35        \<" S05         Sr;C;gSr;C;ff = f)3    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                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )OnnxConverterArgumentParser.   zS
Wraps all the script arguments supported to export transformers models to ONNX IR
c                   > [         TU ]  S5        U R                  S[        [        SS9  U R                  S[        SSS9  U R                  S	[        S
S9  U R                  S[        SS/SS9  U R                  S[
        SSS9  U R                  SSSS9  U R                  SSSS9  U R                  SSSS9  U R                  S5        g )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    Z/var/www/auris/envauris/lib/python3.13/site-packages/transformers/convert_graph_to_onnx.pyr%   $OnnxConverterArgumentParser.__init__3   s   )*'(	 	 	
 	G	 	 	
 	-c8rs4L2	 	 	
 	)#r@ST7 	 	

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

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

 	(#     )__name__
__module____qualname____firstlineno____doc__r%   __static_attributes____classcell__)r+   s   @r,   r   r   .   s    &$ &$r.   r   filename
identifierreturnc                     U R                   R                  U R                  U-   5      R                  U R                  5      $ )a-  
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)r7   r8   s     r,   generate_identified_filenamer@   \   s1     ??##HMMJ$>?KKHOO\\r.   minimum_versionc                      SSK n[        UR                  5      nU[        :  a  [	        SUR                   SU  S35      eg! [         a    [	        S5      ef = f)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)rA   rC   ort_versions      r,   check_onnxruntime_requirementsrH   i   s    
 K334 55<[=T=T<U V77F6G H[[  6  
,
 	

s   ?A Ac                    [        S5        U R                  R                  R                  n/ / pTUSS  H<  nXb;   a&  UR	                  U5        UR	                  X   5        M.  [        U S35          O   [        SU 35        U[        U5      4$ )z
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_inputrX      s     

01mm,,88O&("##AB'"&&x0f./XJJKL ( 
$%8$9
:;j 111r.   nlp	frameworkc                 b  ^ S[         S[        S[        4U4S jjmU R                  SUS9nUR                  R
                  S   nUS:X  a  U R                  " S0 UD6OU R                  U5      n[        U[        5      (       a  UR                  5       n[        U[        [        45      (       d  U4n[        UR                  5       5      nUR                  5        VVs0 s H  u  pgUT" XgS	U5      _M     nnn/ n	U HB  n
[        U
[        [        45      (       a  U	R                  U
5        M1  U	R                  U
5        MD     [!        [#        U	5      5       Vs/ s H  nS
U 3PM
     nn[%        X5       VVs0 s H  u  pgUT" XgSU5      _M     nnn['        U40 UD6nX\X4$ s  snnf s  snf s  snnf )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           	      v  > [        U[        [        45      (       a  U Vs/ s H  nT" XX#5      PM     sn$ [        UR                  5       VVs/ s H  u  pVUS:X  d  M  UPM     snnS   S0nU(       aA  [        UR                  5      S:X  a  SUS'   Ow[        S[        UR                  5       S35      e[        UR                  5       VV	s/ s H  u  pX:X  d  M  UPM     n
nn	UR                  [        R                  U
S5      5        [        SU(       a  S	OS
 SU  SU 35        U$ s  snf s  snnf s  sn	nf )NrJ   r   batch   sequencezUnable to infer tensor axes ()zFound inputr#    z with shape: )
isinstancerP   list	enumerateshapelen
ValueErrorupdatedictfromkeysrK   )r\   tensorr]   r^   taxisnumelaxesdimri   seq_axesbuild_shape_dicts              r,   rv   &infer_shapes.<locals>.build_shape_dict   s   fudm,,JPQ&Q$Th@&QQ .7v||-DS-DkdQR
T-DSTUVX_`Dv||$)(DG$'DSEVDWWX%YZZ2;FLL2I^2IJCUM]C2I^DMM(J?@(w94&dVTU R T _s   D*D/D/D5D5zThis is a sample output)return_tensorsr   Toutput_Fr/   )r'   boolr)   	tokenizer	input_idsri   rQ   rf   r   to_tuplerg   rP   keysitemsextendrO   rangerj   ziprm   )rY   rZ   rR   r^   outputs
input_varskvinput_dynamic_axesoutputs_flatr#   ioutput_namesoutput_dynamic_axesdynamic_axesrv   s                  @r,   infer_shapesr      s    s d S & ]]4Y]OF$$R(G%.$%6cii!&!CIIf<MG';''""$ge}--* fkkm$JOU||~^~tq!-aD'BB~^ Lfudm,,''	  ,1\1B+CD+CagaSM+CLDQTUaQpqQp1.qUGDDQpq *B.ABL\99! _ Eqs   F F&6F+pipeline_namerQ   r|   c                     Uc  UnUS:X  a  [        5       (       d  [        S5      eUS:X  a  [        5       (       d  [        S5      e[        SU SU S35        [	        XX1US9$ )	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

r   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: rc   )rQ   r|   rZ   model_kwargs)r   	Exceptionr   rK   r   )r   rZ   rQ   r|   models_kwargss        r,   load_graph_from_argsr      st      	 D!3!5!5fggD!2!2fgg	%eWM)A
FG M)gtuur.   opsetr#   use_external_formatc                 h   [        5       (       d  [        S5      eSSKnSSKJn  [        SUR                   35        UR                  5          [        U S5      u  pgp[        U R                  X5      u  pU" U R                  UUR                  5       U
UUSUS9  SSS5        g! , (       d  f       g= f)	ae  
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)frS   r   r   do_constant_foldingopset_version)r   r   torch
torch.onnxr   rK   rD   no_gradr   rX   rQ   as_posix)rY   r   r#   r   r   r   rS   r   r   rR   rV   rU   s               r,   convert_pytorchr     s     fgg!	%e&7&7%8
9:	:FsD:Q7<*<SYY*\'IIoo+%% $		
	 
s   AB##
B1c           	      ~   [        5       (       d  [        S5      e[        S5         SSKnSSKnSSKJn  [        SUR                  R                   SU 35        [        U S5      u  pgpU R                  R                  U	R                  5        U	R                  5        V
Vs/ s H  u  pUR                  R                  XS	9PM      nn
nUR                  R!                  U R                  XUR#                  5       S
9u  pgs  snn
f ! [$         a-  n[        SUR&                   SUR&                   SU 35      eSnAff = f)aZ  
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)rD   zUsing framework TensorFlow: z, tf2onnx: r   )r\   )r   output_pathzCannot import z6 required to convert TF model to ONNX. Please install z first. )r   r   rK   
tensorflowtf2onnxrD   versionVERSIONr   rQ   predictdatar   
TensorSpecfrom_tensorconvert
from_kerasr   rF   r\   )rY   r   r#   r   r   t2ovrS   r   r   rR   keyro   input_signaturemodel_proto_es                   r,   convert_tensorflowr   &  s*    fgg	
RS
/,RZZ-?-?,@D6RS ;GsD:Q7< 			&++&Z`ZfZfZhiZh;32==44V4FZhi 33IIAR 4 
Q j
  
QVVH$Z[\[a[aZbbjkljmn
 	

s*   A:D !%C?8D ?D 
D<(D77D<c                    [         R                  " S[        5        [        SU 35        [	        X`X40 UD6nUR
                  R                  5       (       d<  [        SUR
                   35        [        UR
                  R                  5       5        OW[        [        UR
                  R                  5       5      5      S:  a'  [        SUR
                  R                  5        S35      eU S:X  a  [        XX%5        g[        XU5        g)	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FutureWarningrK   r   r;   existsr   r   rj   r   r   r   r   )	rZ   rQ   r#   r   r|   r   r   r   rY   s	            r,   r   r   N  s    6 MM	
 
'w
/0 }
Z\
ZC==!! 01'')*	WV]]++-.	/!	3'&--"8"8":!;;]^__ DF@3v.r.   onnx_model_pathc                     SSK JnJn  [        U S5      nU" 5       nUR	                  5       Ul        U" U R	                  5       U5      n[        SU S35        [        S5        U$ )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. /!\)rC   r   r   r@   r   optimized_model_filepathrK   )r   r   r   opt_model_pathsess_optionr   s         r,   optimizer     sf     = 2/<PN "K+9+B+B+DK(113[AA	00@@V
WX	
ghr.   c                    SSK nSSKnSSKJn  SSKJn  SSKJn  SSKJ	n  UR                  U R                  5       5      n[        UR                  5      [        S5      :  a  [        S5        U" 5       nUR                  U5        [        UR                  5      [        S	5      :  a$  U" US
S
UR                   S
SS
SSS[#        U5      S9n	O#U" US
S
UR                   S
SS
SSS[#        U5      S9n	U	R%                  5         ['        U S5      n
[        SU
 S35        UR)                  U	R*                  R*                  U
R                  5       5        U
$ )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)rQ   per_channelreduce_rangemodestaticweight_qTypeinput_qTypetensors_rangenodes_to_quantizenodes_to_excludeop_types_to_quantize)rQ   r   r   r   r   r   activation_qTyper   r   r   r   z
-quantizedz$Quantized model has been written at r   )onnxrC   onnx.onnx_pbr   onnxruntime.quantizationr   'onnxruntime.quantization.onnx_quantizerr   !onnxruntime.quantization.registryr   loadr   r	   rD   rK   CopyFrom
IntegerOpsrg   quantize_modelr@   
save_modelrQ   )r   r   rC   r   r   r   r   
onnx_model
copy_model	quantizerquantized_model_paths              r,   quantizer     sO    '9ED ?3356JTw/6	
 J
# [$$%h7!!,,"!!%&8!9
	 "!,,""!!%&8!9
	  8V 
01E0FF\
]^OOIOO))+?+H+H+JKr.   pathc                     SSK JnJn  SSKJn  [        SU  S35         U" 5       nU" U R                  5       US/S9n[        SU  S	35        g ! U a  n[        S
U S35         S nAg S nAff = f)Nr   r   )RuntimeExceptionz"Checking ONNX model loading from: z ...CPUExecutionProvider)	providerszModel u    correctly loaded: ✔zError while loading the model u   : ✘)rC   r   r   +onnxruntime.capi.onnxruntime_pybind11_stater   rK   r   )r   r   r   r   onnx_optionsr   res          r,   verifyr     sz    <L	.tfD
9:I%'T]]_lG]F^_tfCDE I.rd2FGHHIs   -A A,A''A,__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: rJ   )N)NFr   )=r   argparser   osr   r   pathlibr   typingr   packaging.versionr   r	   transformers.pipelinesr
   r   transformers.tokenization_utilsr   transformers.utilsr   r   r   rE   r(   r   r'   r@   rH   rX   rP   rg   rm   r   r   r)   r{   r   r   r   r   r   r   r0   parser
parse_argsargsr#   absoluterK   rZ   rQ   r   r|   r   r   r   check_loadinghasattrr   r   exitr/   r.   r,   <module>r      s+    #     , 5 9 O O
  %W~ 
 +$. +$\
]4 
]S 
]T 
]
G 
:28=:h =:3 =:5cDItUb9b3c =:B PTvv#&v/2v?G}vv>"
 "
# "
t "
RV "
J%
H %
S %
$ %
Z  $ %-////// // 	//
 }// // //dd t 2H d H t H V
I 
I z(*FD t{{#,,.DK/89NNJJKKJJNN$$MM	
 ==*+GH ~~%w 9: %-T[[$9D! %-T-B-B$CD! @A4;;t/00t,,-t/00t,,- 1 U h  21#67Qs   (D.I I= I88I=