
    Yji`                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZm	Z	m
Z
mZmZmZ d dlmZmZ d dlmZ  ej        e          Zd ZddZdd	Zd
 Zg dZdgddgddgdZ G d d          Z	 	 	 	 	 	 	 	 	 ddZddZdS )    N)AttributeProto
GraphProto
ModelProto	NodeProtoTensorProtohelpernumpy_helper)infer_shapesinfer_shapes_path)versionc                     d | D             S )z|
    Convert numpy float16 to python int.

    :param np_list: numpy float16 list
    :return int_list: python int list
    c           	          g | ]M}t          t          |                    d                     dd                             d          d          NS )H   N   )intbinviewzfill).0_s     ]/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/onnxruntime/transformers/float16.py
<listcomp>z%_npfloat16_to_int.<locals>.<listcomp>$   sK    DDDqCAFF3KK  $**2..22DDD     )np_lists    r   _npfloat16_to_intr      s     EDGDDDDr   "\o>     @c           	         d }| t          j        | dk                       j        d         dk    r| t          j        | dk                                                       }| t          j        | dk                                                       }||k    r t
                              d| d|            ||k    r t
                              d| d|            | t          j        | dk                        j        d         dk    r| t          j        | dk                                                        }| t          j        | dk                                                        }|| k    r!t
                              d| d|             || k    r!t
                              d| d|             t          j         |d| |          ||           } t          j         || | d          | |           } t          j         ||| t          d                    ||           } t          j         |t          d          | |           | |           } t          j        |           S )a?  
    Convert float32 numpy array to float16 without changing sign or finiteness.
    Positive values less than min_positive_val are mapped to min_positive_val.
    Positive finite values greater than max_finite_val are mapped to max_finite_val.
    Similar for negative values. NaN, 0, inf, and -inf are unchanged.
    c                 <    t          j        | |k     ||k               S N)nplogical_and)abcs      r   betweenz&convert_np_to_float16.<locals>.between/   s    ~a!eQU+++r   r   zthe float32 number z will be truncated to infz-inf)	r#   whereshapemaxminloggerdebugfloatfloat16)np_arraymin_positive_valmax_finite_valr(   positive_maxpositive_minnegative_maxnegative_mins           r   convert_np_to_float16r9   '   sj   , , , A&&'-a0144A 6 67;;==A 6 67;;==>))LLc|ccSaccddd+++LLe|eeSceefffA&&'-a0144A 6 67;;==A 6 67;;==N?**LLd|ddTbSbddeee,,,,LLf|ffTdSdffgggx8-=>>@PRZ[[Hx!1 18Q??BRART\]]Hx%,,GGYabbHxfx.IIN?\deeH:hr   c                    t          | t                    st          dt          |                      | j        t          j        k    rt          j        | _        | j        rOt          t          j
        | j                  ||          }t          |          }|| j        dd<   g | j        dd<   | j        rEt          j        | j        d          }t          |||          }|                                | _        | S )a  Convert tensor float to float16.

    Args:
        tensor (TensorProto): the tensor to convert.
        min_positive_val (float, optional): minimal positive value. Defaults to 1e-7.
        max_finite_val (float, optional): maximal finite value. Defaults to 1e4.

    Raises:
        ValueError: input type is not TensorProto.

    Returns:
        TensorProto: the converted tensor.
    3Expected input type is an ONNX TensorProto but got Nfloat32dtype)
isinstancer   
ValueErrortype	data_typeFLOATFLOAT16
float_datar9   r#   arrayr   
int32_dataraw_data
frombuffertobytes)tensorr3   r4   float16_dataint_listfloat32_listfloat16_lists          r   convert_tensor_float_to_float16rP   I   s     fk** _]tTZ||]]^^^;,,,&. 	&0&:K1L1LN^`nooL(66H#+Faaa #%Faaa ? 	5=	JJJL0?OQ_``L*2244FOMr   c                 t    t          j        |           j        }t          j        | j        | j        |          S r"   )r	   to_arrayr+   r   make_tensor_value_infonamerB   )rK   r+   s     r   make_value_info_from_tensorrU   n   s/    !&))/E(f6FNNNr   )ArrayFeatureExtractor	BinarizerCastMapCategoryMapperDictVectorizerFeatureVectorizerImputerLabelEncoderLinearClassifierLinearRegressor
NormalizerOneHotEncoderRandomUniformLikeSVMClassifierSVMRegressorScalerTreeEnsembleClassifierTreeEnsembleRegressorTreeEnsembleZipMapNonMaxSuppressionTopKRoiAlignRangeCumSumMinMaxUpsampler      )Resize	GroupNormSkipGroupNormc                   *    e Zd ZdZdefdZdefdZdS )InitializerTrackerz'Class for keeping track of initializer.initializerc                 0    || _         g | _        g | _        d S r"   )rx   
fp32_nodes
fp16_nodes)selfrx   s     r   __init__zInitializerTracker.__init__   s    &r   nodec                 v    |r| j                             |           d S | j                            |           d S r"   )rz   appendr{   )r|   r~   is_node_blockeds      r   add_nodezInitializerTracker.add_node   sB     	)O""4(((((O""4(((((r   N)__name__
__module____qualname____doc__r   r}   r   r   r   r   r   rw   rw      sP        11K    
)Y ) ) ) ) ) )r   rw   Fc
                 
   |dk    s
J d            |t          t          j        t          j                  j                  k    s
J d            |i n|}
t          | t                    r| }t          j        t          j
                  t          j        d          k    rz|sxt          j        t          j                            |                    5 }|j        }t#          ||           t          j        |          } d}ddd           n# 1 swxY w Y   nt          j        |          } t          | t&                    st)          dt+          |                      d}|sBt          j        t          j
                  t          j        d	          k    r	 t,          }n# w xY w|t.          }|g }t1          |          }t1          |          }t	          d
 | j        D             d          }t5          t6                    }|dk    rdg|d<   t8                              d| d| d d| d| d| d|            g }g }g }g }| ||           } |                    |            i }t1                      }t1                      }d | j        j         D             }d | j        j!        D             }t          tD                    rfd|D             }fd|D             }nsg }g }tG          | j        j                   D ]!\  }}|j        |v rdt          |          z   }|||j        <   |$                    |j                   dt          |          z   }| j        j%        $                                }|&                    |           ||_        tN          j(        |j        j)        _*        tW          j,        d|j        g|gtN          j(        |          g}| j        j-        .                    |           |                    |           |$                    |           #tG          | j        j!                  D ]\  }}|j        |v rdt          |          z   } | ||j        <   |$                    |j                   d t          |          z   }| j        j%        $                                }|&                    |           | |_        tN          j(        |j        j)        _*        tW          j,        d| g|j        gd|          g}| j        j-        .                    |           |                    |           |$                    |           i }!|rg }"|D ]}#t          |#t&                    r|"                    |#j                   t          |#t^                    r|#j0        D ]9}|j1        tN          j2        k    r"|j        |!vsJ tg          |          |!|j        <   :|#j-        D ]e}|j        |v rti          tk          |j                             D ],}|j         |         |v r||j         |                  |j         |<   -ti          tk          |j!                            D ],}|j!        |         |v r||j!        |                  |j!        |<   -|j6        |v p|j        |v }$tG          |j                   D ]a\  }} | |!v rX|$p9||7                    |j6        g           v o||
7                    |j6        g           v}%|!|          8                    ||%           b|$r|                    |           J|j6        dk    r=|j9        D ]5}&|&j        d!k    r(|&j:        tN          j2        k    rtN          j(        |&_:         n6|j6        d"v rd#}'|j9        D ]5}&|&j        d$k    r(d}'|&j:        tN          j2        k    rtN          j(        |&_:        6|j6        d%v r:|'s8|j9        .                    tW          j;        d$tN          j(                  g           |j6        |vs	|j6        |
v r!|j9        D ]}&|"                    |&           P|                    |           gt          |#tx                    r|"                    |#j=                   |#j>        D ]}|"                    |           |#j?        &                    t          |#j?        ||                     |#jA        D ]}t          |||          }t          |#t^                    rt          jC        |#j         |#j!        |#j%                  D ]}|j        j)        j*        tN          j2        k    r9|j        |vr0tN          j(        |j        j)        _*        |                    |           |j        D                    d&          rl|j        jE        j*        j)        j*        tN          j2        k    rC|j        |vr:tN          j(        |j        jE        j*        j)        _*        |                    |           |"}||!F                                D ]x}(|s|(jG        rmt          |(j0        ||          |(_0        |                    t          |(j0                             |(jI        r$|s"t8          J                    d'|(jG                    y|D ] })tG          |)j                   D ]\  }} |||)j6                 vs||
7                    |)j6        g           v r2|D ]}*| |*j        k    r| j        j%        $                                }|&                    |*           | d(z   }||_        tN          j2        |j        j)        _*        | d)z   }tW          j,        d| g|gd|          g}| j        j-        .                    |           ||)j         |<    n|	rtN          jK        ntN          j2        }+|D ]})ti          tk          |)j                             D ]}|)j         |         } |D ]}*| |*j        k    r| j        j%        $                                }|&                    |*           | d(z   }||_        |+|j        j)        _*        | d)z   }tW          j,        d| g|g|+|          g}| j        j-        .                    |           ||)j         |<    nti          tk          |)j!                            D ]}|)j!        |         },|D ]}*|,|*j        k    r| j        j%        $                                }|&                    |*           |,d*z   }-|-|_        |+|j        j)        _*        |,d+z   }tW          j,        d|-g|,gd|          g}| j        j-        .                    |           |-|)j!        |<    n| S ),a  Convert tensor float type in the input ONNX model to tensor float16.

    Args:
        model (ModelProto or str): The ONNX model or path of the model to convert.
        min_positive_val (float, optional): minimal positive value. Defaults to 5.96e-08.
        max_finite_val (float, optional): maximal finite value of float16. Defaults to 65504.
        keep_io_types (Union[bool, List[str]], optional): It could be boolean or a list of float32 input/output names.
                                                          If True, model inputs/outputs should be left as float32.
                                                          Defaults to False.
        disable_shape_infer (bool, optional): Skips running onnx shape/type inference.
                                              Useful if shape inference has been done. Defaults to False.
        op_block_list (List[str], optional): List of op types to leave as float32.
                                             Defaults to None, which will use `float16.DEFAULT_OP_BLOCK_LIST`.
        node_block_list (List[str], optional): List of node names to leave as float32. Defaults to None.
        force_fp16_initializers(bool): force converting all float initializers to float16.
                                       Default to false, which will convert only the one needed to avoid precision loss.
        force_fp16_inputs(Dict[str, List[int]]): Force the conversion of the inputs of some operators to float16, even if
                                                 this script's preference it to keep them in float32.
    Raises:
        ValueError: input type is not ModelProto.

    Returns:
        ModelProto: converted model.
    r   zginvalid min_positive_val. smallest positive float16 value: subnormal 5.96e-08, and normalized 6.104e-05z4invalid max_finite_val. largest float16 value: 65504Nz1.8.0)dirTz$Expected an ONNX ModelProto but got z1.2.0c              3   6   K   | ]}|j         d v |j        V  dS )) zai.onnxN)domainr   )r   os     r   	<genexpr>z+convert_float_to_float16.<locals>.<genexpr>   s0      [[Aqx??Z?Zai?Z?Z?Z?Z[[r      )default
   rr   rs   z"fp16 parameters: min_positive_val=z max_finite_val=z keep_io_types=z disable_shape_infer=z op_block_list=z node_block_list=z force_fp16_initializers=c                 Z    g | ](}|j         j        j        t          j        k    !|j        )S r   rA   tensor_type	elem_typer   rC   rT   r   ns     r   r   z,convert_float_to_float16.<locals>.<listcomp>  s0    jjja8J8TXcXi8i8i168i8i8ir   c                 Z    g | ](}|j         j        j        t          j        k    !|j        )S r   r   r   s     r   r   z,convert_float_to_float16.<locals>.<listcomp>  s0    lllq!&:L:VZeZk:k:kAF:k:k:kr   c                     g | ]}|v |	S r   r   r   r   keep_io_typess     r   r   z,convert_float_to_float16.<locals>.<listcomp>  s#    DDDQm1C1Cq1C1C1Cr   c                     g | ]}|v |	S r   r   r   s     r   r   z,convert_float_to_float16.<locals>.<listcomp>  s#    FFFa13E3E3E3E3Er   graph_input_cast_graph_input_castCast)torT   graph_output_cast_graph_output_castr   )EyeLikeMultinomialRandomNormalRandomNormalLikeRandomUniformrb   SequenceEmpty	BernoulliFr>   )r   r   r   sequence_typezXinitializer is used by both fp32 and fp16 nodes. Consider add these nodes to block list:_cast_to_fp32_cast_to_fp32_node_cast_to_fp16_cast_to_fp16_node)Lr0   r#   finfor1   r,   r?   strr   parseonnx__version__tempfileNamedTemporaryFileospathdirnamerT   r   loadr   r@   rA   r
   DEFAULT_OP_BLOCK_LISTsetopset_importdictALWAYS_FLOAT_INPUTSr.   r/   r   graphinputoutputlist	enumerateadd
value_infoCopyFromr   rD   r   r   r   	make_noder~   extendr   rx   rB   rC   rw   rangelenop_typegetr   	attributeimake_attributer   ggraphstrP   tensors	itertoolschainHasFieldr   valuesr{   rU   rz   infoBFLOAT16).modelr3   r4   r   disable_shape_inferop_block_listnode_block_listforce_fp16_initializersforce_fp16_inputs#use_bfloat16_as_blocked_nodes_dtypeforce_fp16_inputs_dict
model_pathtmpfileshape_infer_model_pathfunc_infer_shape
onnx_opsetalways_float_inputsqueuevalue_info_list	node_listmixed_float_type_node_listname_mappinggraph_io_to_skipio_castsfp32_inputsfp32_outputsr   r   output_name	node_namenew_value_infonew_node
input_namefp32_initializers
next_levelqr   use_fp32_weightattr	has_dtypevaluer~   r   accuracy_typer   output_cast_names.      `                                          r   convert_float_to_float16r      s   H x'''q ('' U28BJ#7#7#;<<<<<>t<<<#4#<RRBS% *
=)**gmG.D.DDDM`D,1L1LMMM +QX)0&!*.DEEE	"899&*#+ + + + + + + + + + + + + + + Ij))EeZ(( OMUMMNNN 7=1A#B#BgmT[F\F\#\#\	+DDDD -&&M/**O
 [[);[[[eghhhJ233R)*H%
LL 	Z-=  	Z  	Z~  	Z  	Zn{  	Z  	Z  Sf  	Z  	Z  wD  	Z  	Z  Wf  	Z  	Z  AX  	Z  	Z  
 EOI
 "$ #  ''	LLLuuuuHjj5;#4jjjKllEK$6lllL-&& DDDD+DDDFFFF<FFF %++,, $ $16[  -A6K#.L   (((*SVV3I"[37799N##A&&&"-N8C8KN+5(!&K=[M`gpqqqrHK##H---"">222LL###%+,-- $ $16\!!-A6J#-L   (((+c!ff4I"[37799N##A&&&",N8C8KN+5(*xAT]^^^_HK##H---"">222LL###79
 b
 ^	: ^	:A!Z(( +!!!'***!Z(( @A J JA{k&777 v->>>>>4Fq4I4I)!&1 :A :AA v)) "3qw<<00 B B71:55)5agaj)AAGAJ"3qx==11 D D8A;,66*6qx{*CAHQK&'i=&@&]AFoD]O)217);); W W:%):::.= / !%8%<%<QY%K%K K !W$%-C-G-G	SU-V-V$V , .j9BB1oVVV& $A!((++++9..() * *#'9#4#4;CT9T9T-8-@DF$)E9 	) 	 	 ).I() E E#'9#7#704I'+v1B'B'B1<1D !"	-_ _ _ir _ ! 2 2F4I'S^Sf4g4g3h i i i 9,???19PfCfCf() 8 8 * 1 1$ 7 7 7 78 7==a@@@@ !^,, ]!!!#&&& ) )A%%a((((<QSBRTbccddd ] ]A7;K^\\AA!Z(( : #!(ALII 	: 	:Av)3{7HHH6)999;F;NAF.8+221555v77 :6/9EOS^Sddd v-===WbWj 4 > J T / 6 6q 9 9 9E  bH #))++  " 	e&6 	 ?@QSces t tE""#>u?P#Q#QRRR (?  Boto  B  B  
 +  &tz22 	 	MAz+DL999QBXB\B\]a]ikmBnBn=n=n-  
00%*[%;%?%?%A%AN"++J777",">K*5N'@K@QN'3= *-A AI & 0*}YZaj k k klHK$++H555$/DJqME 1		& -PfK((U`UfM ' ' s4:'' 	 	AAJ-  
00%*[%;%?%?%A%AN"++J777",">K*5N'@MN'3= *-A AI & 0*}Yfmv w w wxHK$++H555$/DJqME 1  s4;''(( 	 	A[^F-  
Z_,,%*[%;%?%?%A%AN"++J777'-'?$*:N'@MN'3= &)= =I & 0:J9KfXZ\cl m m mnHK$++H555%5DKNE -	" Ls   .DDDF$ $F&c                    t          | t                    st          dt          |                      | j        t          j        k    rt          d          d}| j        rt          j        | j                  }| j	        rt          j
        | j	        d          }|t          d          t          |||          }t          j        t          j        |t          j        |          z
                      S )zSMeasure the maximum absolute difference after converting a float tensor to float16.r;   z#Expected tensor data type is float.Nr<   r=   zexternal data not loaded!)r?   r   r@   rA   rB   rC   rE   r#   rF   rH   rI   RuntimeErrorr9   amaxabsr<   )rK   r3   r4   float32_datarL   s        r   float_to_float16_max_diffr    s    fk** _]tTZ||]]^^^;,,,>???L 3x 122 G}V_IFFF6777(7GXXL726,L)A)AABBCCCr   )r   r   )	r   r   FFNNFNF)r   loggingr   r   numpyr#   r   r   r   r   r   r   r   r	   onnx.shape_inferencer
   r   	packagingr   	getLoggerr   r.   r   r9   rP   rU   r   r   rw   r   r  r   r   r   <module>r
     s        				       e e e e e e e e e e e e e e e e e e @ @ @ @ @ @ @ @      		8	$	$E E E       D" " " "JO O O
   D #$Aq6QPQFSS ) ) ) ) ) ) ) )" !(-A A A AH
D D D D D Dr   