
    Siz9                        S SK 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 jr    SS jr      SS jr    S         SS	 jjrg)    )annotationsN)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_arrayc                   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 /$ )zReplaces 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)node	thresholdvalue_constant_of_shapeattr   new_namer'   sizeinitr   
node_shapenew_nodes               [/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx/tools/replace_constants.py_replace_constantr9      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    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       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      (       a@  [-        UU R.                  U R                  U R                  U R0                  U R2                  S9$ [        U [        5      (       a@  [5        U R6                  U R.                  U R                  U R                  UU R8                  S9$ [        S[        U 5       S35      e)zReplaces 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      r8   
_find_name9_replace_constant_of_shape_with_range.<locals>._find_name[   s^    'v&MXQqc?D>)""4(FA r:   r   zeror   )	value_intoner@   r!   
ReduceProd_NRange_RANGECast_RANGEf)to_NfDiv_FLATReshapeT)reverseinitializersparse_initializeropset_imports)
isinstancer   r+   r/   r   r#   typesetinputr.   r   	enumerater"   lenr$   r&   r-   r
   FLOATsorteditemsinsertr   r   rX   rY   r   domainopset_import)onxnodesr/   rF   cst0cst1updateinodeshapenarQ   accldreshuprE   s                    @r8   %_replace_constant_of_shape_with_rangerv   G   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      (       a@  [+        UU R                  U R                  U R                  U R,                  U R.                  S9$ [        U [        5      (       a@  [1        U R2                  U R                  U R                  U R                  UU R4                  S9$ [        S[        U 5       S35      e)	z7Replaces all fill value of all nodes *ConstantOfShape*.r<   r   r!   r   r@   r    rW   rZ   )r\   r   r+   r/   r   r#   r]   r^   r_   r.   r`   r"   r$   r&   r   r   r-   r   r   appendrd   r   rX   rY   r   rf   rg   )rh   r1   ri   rE   r/   rl   rm   tensor
new_tensorr7   r2   ru   s               r8    _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:   c                   [        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.

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

Returns:
    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   r>   TrZ   r@   )
ir_versionr0   	use_ranger1   )r0   r}   r~   r1   )	functionsproducer_nameproducer_versionr}   
doc_stringrf   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   grW   )@r\   r   r/   r"   r9   ra   extendrx   r   rf   r   r_   r.   rg   rv   r{   r   (replace_initializer_by_constant_of_shapegraphr}   r   r   r   r   r   r   metadata_propskeyr   r   rA   versionrB   r   r#   r]   r^   rX   tupler'   r(   r)   r   r*   r+   r,   r   r-   r   r   r
   INT64rY   r%   indicesvaluesr$   r   GRAPHhasattrr   idr   r   )$rh   r0   r}   r~   r1   modified	new_nodesr/   	cst_nodesnew_onx	new_graphfnew_functionsmodelpr   oimpop_setn_modificationsremovedadditional_inputs	new_initsr5   r'   r4   r3   r   new_sparse_initssp_initshape_nodesattsr2   r   r7   rD   r   s$                                       r8   r   r      s   > #}%%%'	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)r/   r   r0   intr1   floatreturnzlist[NodeProto])rh   GraphProto | FunctionProtor   r   )rh   r   r1   r   r   r   )   NFr   )
rh   z'FunctionProto | GraphProto | ModelProtor0   r   r}   z
int | Noner~   boolr1   r   )
__future__r   numpyr(   onnxr   r   r   r   r   r	   r
   onnx.helperr   r   r   r   r   r   r   r   r   onnx.numpy_helperr   r9   rv   r{   r    r:   r8   <module>r      s    #   
 
 
 )%
% #%>C%%PS>	#S>S>l2>	#2>>C2>2>n !%(S	0SS S 	S
 #Sr:   