
     Ti5                        S r SSKJr  SS/rSSKrSSKrSSKrSSKr	\R                  " \5      rS
S jrS r " S S\	R                  R                   5      r " S	 S\	R                  R                   5      rg)z>Pass for removing duplicated initializer tensors from a graph.    )annotationsDeduplicateInitializersPass!DeduplicateHashedInitializersPassNc                ^   U R                  5       (       d  U R                  5       (       a!  [        R                  SU R                  5        gU R
                  nUc!  [        R                  SU R                  5        gUR                  U:  a"  [        R                  SU R                  U5        gg)z=Check if the initializer should be skipped for deduplication.zJSkipped deduplication of initializer '%s' as it is a graph input or outputTzqSkipped deduplication of initializer '%s' as it has no constant value. The model may contain invalid initializerszDSkipped initializer '%s' as it exceeds the size limit of %d elementsF)is_graph_inputis_graph_outputloggerwarningnameconst_valuesizedebug)initializer
size_limit	const_vals      n/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx_ir/passes/common/initializer_deduplication.py_should_skip_initializerr      s    !!##{'B'B'D'DX	
 ''I	
 ~~
"R	

     c                    U R                   R                  5       (       a2  [        R                  " U R	                  5       5      R                  5       $ U R                  5       $ )a  StringTensor does not support tobytes. Use 'string_data' instead.

However, 'string_data' yields a list of bytes which cannot be hashed, i.e.,
cannot be used to index into a dict. To generate keys for identifying
tensors in initializer deduplication the following converts the list of
bytes to an array of fixed-length strings which can be flattened into a
bytes-string. This, together with the tensor shape, is sufficient for
identifying tensors for deduplication, but it differs from the
representation used for serializing tensors (that is string_data) by adding
padding bytes so that each string occupies the same number of consecutive
bytes in the flattened .tobytes representation.
)dtype	is_stringnparraystring_datatobytes)vals    r   _tobytesr   2   sB     yyxx)*2244;;=r   c                  >   ^  \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	r   D   aO  Remove duplicated initializer tensors from the main graph and all subgraphs.

This pass detects initializers with identical shape, dtype, and content,
and replaces all duplicate references with a canonical one.

Initializers are deduplicated within each graph. To deduplicate initializers
in the model globally (across graphs), use :class:`~onnx_ir.passes.common.LiftSubgraphInitializersToMainGraphPass`
to lift the initializers to the main graph first before running pass.

.. versionadded:: 0.1.3
.. versionchanged:: 0.1.7
    This pass now deduplicates initializers in subgraphs as well.
c                .   > [         TU ]  5         Xl        g Nsuper__init__r   selfr   	__class__s     r   r$   $DeduplicateInitializersPass.__init__S   s    $r   c                |   SnUR                  5        GH	  n0 n[        UR                  R                  5       5       H  n[	        XPR
                  5      (       a  M  UR                  nUc   eUR                  [        UR                  5      [        U5      4nXt;   ax  SnXG   nUR                  U5        UR                  c   eUR                  R                  UR                  5        [        R                  SUR                  UR                  5        M  XTU'   M     GM     [        R                   R#                  XS9$ )NFT8Replaced initializer '%s' with existing initializer '%s'modelmodified)graphstupleinitializersvaluesr   r   r   r   shaper   replace_all_uses_withr   popr	   infoirpasses
PassResult)	r&   r,   r-   graphr0   r   r   keyinitializer_to_keeps	            r   call DeduplicateInitializersPass.callW   s   \\^EWYL$U%7%7%>%>%@A+KII'33	 ,,, ioo(>@ST&#H*6*;'556IJ&++777&&**;+;+;<KKR#((+00 )4%)  B $0 yy##%#CCr   r   )i   r   intr,   zir.Modelreturnzir.passes.PassResult	__name__
__module____qualname____firstlineno____doc__r$   r<   __static_attributes____classcell__r'   s   @r   r   r   D   s    % %D Dr   c                  >   ^  \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	r   u   a  Remove duplicated initializer tensors (using a hashed method) from the graph.

This pass detects initializers with identical shape, dtype, and hashed content,
and replaces all duplicate references with a canonical one.

This pass should have a lower peak memory usage than :class:`DeduplicateInitializersPass`
as it does not store the full tensor data in memory, but instead uses a hash of the tensor data.

.. versionadded:: 0.1.7
c                .   > [         TU ]  5         Xl        g r!   r"   r%   s     r   r$   *DeduplicateHashedInitializersPass.__init__   s    $r   c                   SnUR                  5        GH  n0 n[        UR                  R                  5       5       GHi  n[	        XPR
                  5      (       a  M   UR                  nUc   e[        R                  " 5       nUR                  5       nUR                  U5        UR                  5       n	[        UR                  R                  5       5      n
UR                  X4nX;   a  [        XK   R                  5      [        U5      :w  a  [        R!                  SXK   U5        M  SnXK   nUR#                  U5        UR$                  c   eUR                  R'                  UR$                  5        [        R)                  SUR$                  UR$                  5        GMe  XTU'   GMl     GM     [*        R,                  R/                  XS9$ )NFzVInitializer deduplication failed: hashes match but values differ with values %s and %sTr*   r+   )r.   r/   r0   r1   r   r   r   hashlibsha512numpyupdate	hexdigestr2   r   r   r	   r
   r3   r   r4   r5   r6   r7   r8   )r&   r,   r-   r9   r0   r   r   hashedtensor_datatensor_digesttensor_dimsr:   r;   s                r   r<   &DeduplicateHashedInitializersPass.call   s   \\^EUWL$U%7%7%>%>%@A+KII'33	 ,,, !)'oo/k* & 0 0 2#IOO$9$9$;< C& 1 = =>(9BUUS(-'	 !#H*6*;'556IJ&++777&&**;+;+;<KKR#((+00 )4%K  B $T yy##%#CCr   r>   )l        r?   rA   rC   rK   s   @r   r   r   u   s    	% %
-D -Dr   )r   zir.Valuer   r@   rB   bool)rH   
__future__r   __all__rQ   loggingrS   r   onnx_irr6   	getLoggerrD   r	   r   r   r7   InPlacePassr   r    r   r   <module>rc      sr    E "(*M
N    			8	$<$.D"))"7"7 .Db>D		(=(= >Dr   