
    h!:                     (   S SK JrJrJr  S SKrS SKJrJrJ	r	J
r
JrJrJr  S SKJrJrJrJrJrJrJrJrJr  S SKJr  S\S\S\S	\\   4S
 jrS\\	\4   S	\\	\4   4S jrS\\	\4   S\S	\\	\4   4S jr    SS\\\	\
4   S\S\\   S\S\4
S jjr g)    )ListOptionalUnionN)AttributeProtoFunctionProto
GraphProto
ModelProto	NodeProtoSparseTensorProtoTensorProto)	make_attributemake_function
make_graph
make_model	make_nodemake_tensormake_tensor_value_infoset_model_propstensor_dtype_to_np_dtype)
from_arraynode	thresholdvalue_constant_of_shapereturnc                    U R                   S:w  a  [        SU R                   < S35      eU R                   GH*  nUR                  S:X  a  [	        SU R                  < S35      eUR                  S:X  a  UR
                  nUR                   S3nUR                  n[        R                  " U5      nXq::  a  U /s  $ [        [        R                  " [        U5      [        R                  S	9US
9n[        UR                  5      n	[        S/ U/US9n
[        SU/U R                   [        [        R                  " U/U	S	95      S9nX/s  $ [	        SUR                  < 35      e   U /$ )z
Replaces a Constant node with a large tensor (with more than threshold elements)
by a sequence of nodes that produces a dummy constant of same shape as original tensor.
Constantz!Node type must be 'Constant' not .sparse_valuezEThis feature is not yet implemented for a sparse constant (node name=).value__SHAPEdtypenamer    ConstantOfShapez'Replacement of constant with attribute )op_type	TypeError	attributer%   NotImplementedErrortdimsnpprodr   arraylistint64r   	data_typer   output)r   r   r   attr    new_namer-   sizeinitr#   
node_shapenew_nodes               V/var/www/fran/franai/venv/lib/python3.13/site-packages/onnx/tools/replace_constants.py_replace_constantr<      sV    ||z!;DLL;K1MNN~~88~%%"ii]".  88wEEE**W-H::D774=D vbhhtDzBRD,U__=E"
	J !!
 +B*C5!QR	H ))!5chh\B
 	
9 > 6M    onxc           	        ^ [        U [        5      (       a  [        U R                  5      nOC[        U [        5      (       a  [        U R                  5      nO[        S[        U 5       S35      e[        5       mU H3  nT[        UR                  5      -  mT[        UR                  5      -  mM5     U4S jn[        S/ U" S5      /SS9n[        S/ U" S5      /S	S9n0 n[        U5       GHu  u  prUR                  S
:w  a  M  UR                  S   n[        SU/U" U S35      /5      n	[        SUR                  S   U	R                  S   UR                  S   /U" U S35      /5      n
[        UR                  5      S	:X  a$  UR                  S   R                  R                   nO["        R$                  n[        SU
R                  S   /U" U S35      /US9n[        SU	R                  S   /U" U S35      /US9n[        SUR                  S   UR                  S   /U" U S35      /5      n[        SUR                  S   U/UR                  5      nXXX/Xg'   GMx     ['        UR)                  5       SS9 H  u  nnUXUS	-   & M     UR+                  SU5        UR+                  S	U5        [        U [        5      (       aB  [-        UU R.                  U R                  U R                  U R0                  U R2                  S9nU$ [        U [        5      (       aB  [5        U R6                  U R.                  U R                  U R                  UU R8                  S9nU$ [        S[        U 5       S35      e)z
Replaces all *ConstantOfShape* by node *Range* to avoid constant tensors.
The function is not recursive. The recursivity is done by
*replace_initializer_by_constant_of_shape*.
Not implemented for type r   c                    > U T;  a  TR                  U 5        U $ Sn U  SU 3nUT;  a  TR                  U5        U$ US-  nM'  )N   _   )addRuntimeError)prefixir%   existing_namess      r;   
_find_name9_replace_constant_of_shape_with_range.<locals>._find_name_   s^    'v&MXQqc?D>)""4(FA r=   r   zeror   )	value_intonerD   r'   
ReduceProd_NRange_RANGECast_RANGEf)to_NfDiv_FLATReshapeT)reverseinitializersparse_initializeropset_imports)
isinstancer   r1   r   r   r)   typesetinputr4   r   	enumerater(   lenr*   r,   r3   r   FLOATsorteditemsinsertr   r%   r\   r]   r   domainopset_import)r>   nodesr   rJ   cst0cst1updateinodeshapenarU   accldreshupgraphnew_onxrI   s                      @r;   %_replace_constant_of_shape_with_ranger{   J   s+    #z""SXX	C	'	'SXX3DI;a@AAUN#djj/)#dkk** N Zj&8%9QGDZj&7%8AFDF '<<,,

1lUGjE7".F-GH[[^QXXa[$++a.95'()*

 t~~!#"$$..B""Bv}zUG7:K/L.MRTUv}zUG3-/H.IbQBIIaL"))A,/*we_2M1N
 QXXa[%$8$++Frq/+ (. FLLND9	r#%eai  :	LLD	LLD#z""HHIIJJ"55
 #}%%JJHHIIJJ**
 
/S	{!<
==r=   c           	         [        U [        5      (       a  [        U R                  5      nOC[        U [        5      (       a  [        U R                  5      nO[        S[        U 5       S35      e[        5       nU H3  nU[        UR                  5      -  nU[        UR                  5      -  nM5     0 n[        U5       H  u  pdUR                  S:w  a  M  UR                  S   R                  n[        UR                  UR                   S/U/5      n[#        SUR                  UR                  5      n	[%        UR                  S   R                  US9n
U	R                  R'                  U
5        XU'   M     UR)                  5        H	  u  pkXU'   M     [        U [        5      (       aB  [+        UU R                  U R                  U R                  U R,                  U R.                  S9nU$ [        U [        5      (       aB  [1        U R2                  U R                  U R                  U R                  UU R4                  S9nU$ [        S[        U 5       S35      e)	ze
Replaces all fill value of all nodes *ConstantOfShape*.
*replace_initializer_by_constant_of_shape*.
r@   r   r'   r   rD   r&   r[   r^   )r`   r   r1   r   r   r)   ra   rb   rc   r4   rd   r(   r*   r,   r   r%   r3   r   r   appendrh   r   r\   r]   r   rj   rk   )r>   r   rl   rI   r   ro   rp   tensor
new_tensorr:   r5   rx   ry   rz   s                 r;    _replace_constant_of_shape_valuer      s    #z""SXX	C	'	'SXX3DI;a@AAUN#djj/)#dkk**  F '<<,,"$$ KK))A31H0I

 .

DKKHT^^A.33:F!!#& u ( \\^	e $ #z""HHIIJJ"55
 #}%%JJHHIIJJ**
 
/S	{!<
==r=   
ir_version	use_rangec                    [        U [        5      (       a  Sn/ nU R                   HT  nUR                  S:X  a0  [	        XqU5      n[        U5      S:X  a  SnUR                  U5        MC  UR                  U5        MV     U(       ae  [        U R                  U R                  U R                  U R                  UU R                  S9n	U(       a  [        U	5      $ US:w  a  [        X5      $ U	$ U(       a  [        U 5      $ US:w  a  [        X5      $ U $ [        U [         5      (       Ga  [#        U R$                  U=(       d    U R&                  UUUS9n
U R(                   Vs/ s H#  n[#        UUU=(       d    U R&                  UUS9PM%     nn[+        U
UU R,                  U R.                  U=(       d    U R&                  U R0                  U R                  U R2                  S	9n[        U R4                  5      S
:  a;  U R4                   Vs0 s H  oR6                  UR8                  _M     nn[;        X5        UR                  SS2	 U R                   H  nUR                  R=                  5       nUR                  S:X  a0  UR>                  S:  a   U(       a  [A        SUR>                   S35      eUR                  S:X  a)  UR>                  S:  a  [A        SUR>                   S35      eUR                  Ul	        UR>                  Ul        M     U$ [        U [B        5      (       d  [E        S[G        U 5       S35      eS
n/ n[I        5       n/ n/ nU RJ                   GHX  n[M        URN                  5      n[P        RR                  " U5      nUU::  a  UR                  U5        MH  US-  nUR                   S3nUR                  [U        [P        RV                  " [Y        U5      [P        RZ                  S9US95        []        UR^                  5      n[a        SU/UR                  /[U        [P        RV                  " S/US95      S9nUR                  U5        UR=                  UR                  5        Uc  GM  US::  d  GM$  UR                  [c        U[d        Rf                  [        U5      /5      5        GM[     / nU Rh                   H  n[M        URN                  5      n[P        RR                  " U5      nUU::  a  UR                  U5        MG  [k        SURl                  R                  < SURn                  R                  < S35      e   U R                   GHs  nUR                  S:X  a3  [	        XqU5      n[        U5      S:X  a  US-  nUR                  U5        MG  Sn/ nURp                   H  nURF                  [r        Rt                  :X  ao  [w        US5      (       a^  URx                  bQ  [#        URx                  UUUUS9n [{        U 5      [{        URx                  5      :w  a  Sn[}        UR                  U 5      nUR                  U5        M     U(       a_  [a        UR                  UR                  UR                  5      n!U!Rp                  R                  U5        UR                  U!5        US-  nGMb  UR                  U5        GMv     US
:  ax  [        UU R                  U R                   V"s/ s H  n"U"R                  U;  d  M  U"PM     sn"U-   U R                  UUS9n#U(       a  [        U#5      $ US:w  a  [        U#U5      $ U#$ U(       a  [        U 5      $ US:w  a  [        X5      $ U $ s  snf s  snf s  sn"f )a  
Replace initializers or constant node by nodes *ConstantOfShape* to reduce
the size. This reduce the cost to write a unit test about
a specific graph structure.

:param onx: ModelProto
:param threshold: every initializer under this threshold is not impacted
:param ir_version: initializer must be specified as input for `ir_version <= 3`,
    this must be specified if onx is :class:`FunctionProto` or :class:`GraphProto`
:param use_range: if uses operator *Range* instead of *ConstantOfShape* to avoid
    constant tensors
:param value_constant_of_shape: value to use as a value for all nodes *ConstantOfShape*,
    a high value may produce nan or inf predictions
:return: onx, modified ModelProto

The function is designed so that the function can be reapplied on a modified model
and either replace *ConstantOfShape* with *Range* operators, either replace the fill value
for every *ConstantOfShape*.
Fr   rB   Tr^   rD   )r   r   r   r   )r   r   r   r   )	functionsproducer_nameproducer_versionr   
doc_stringrj   model_versionr   N    z.Range was introduced in opset 11 but opset is r   	   z7ConstantOfShape was introduced in opset 9 but opset is z-onx should be a GraphProto at this stage not r!   r"   r$   r'         ?r&      zKThis feature is not yet implemented for a sparse initializer (indices.name=z, values.name=r   gr[   )@r`   r   r   r(   r<   re   extendr}   r   rj   r%   rc   r4   rk   r{   r   r	   (replace_initializer_by_constant_of_shapery   r   r   r   r   r   r   r   metadata_propskeyr    r   rE   versionrF   r   r)   ra   rb   r\   tupler-   r.   r/   r   r0   r1   r2   r   r3   r   r   r   INT64r]   r+   indicesvaluesr*   r   GRAPHhasattrr   idr   r   )$r>   r   r   r   r   modified	new_nodesr   	cst_nodesrz   	new_graphfnew_functionsmodelpr   oimpop_setn_modificationsremovedadditional_inputs	new_initsr8   r-   r7   r6   r#   new_sparse_initssp_initshape_nodesattsr5   r   r:   rH   ry   s$                                       r;   r   r      s   4 #}%%%'	HHD||z)-d?VW	y>Q&#H  +T"  #

		

!..G <WEE&!+7  N8=="a'3CQQ
#z""<II!3S^^$;
	 ]]	
 # 5#%7#(? # 	 	
 #++ 11!3S^^~~::++	
 s!!"Q&.1.@.@A.@eeQWWn.@FAE*q!$$D''++-F{{b T\\B%69"DT\\NRST  {{b T\\A%5",,0LL><  !KKFM!\\FN % c:&&GS	{RSTUUOIeG#%ITYYwwt}9T"1ii[(rxxT
"((;(K	
 )8JYYKRXXse59:	
 	DII!jAo$$&x1B1BSYKP+  2 13))W\\"wwt}9##G,!$__114 5">>..15
 	
 * <<:%+D=TUK;1$1$[)>>CN000C%%EE%<EE')',C a5BsuuI%#H(15CKK! ""  tzz4;;GH%%d+X&q OT"A D HH		;	1QVV7%:Q	;>OOJJ!/
 8??"a'3E;RSS4S99!#/MMJW	
* BP <s   *]- ]2/]7]7)   NFr   )!typingr   r   r   numpyr.   onnxr   r   r   r	   r
   r   r   onnx.helperr   r   r   r   r   r   r   r   r   onnx.numpy_helperr   intfloatr<   r{   r   boolr    r=   r;   <module>r      s   ) (   
 
 
 )(
( #(>C(	)_(VV>	z=(	)V>
:}$%V>r8>	z=(	)8>DI8>
:}$%8>z  $%(N	}j*4	5NN N 	N
 #Nr=   