
     Ti                        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J	r	  \R                  " \5      rSS jr " S S\R                  R                  5      rS	S	S	S
.         SS jjrg)z0Shape inference pass using onnx.shape_inference.    )annotationsShapeInferencePassinfer_shapesN)_c_api_utilsc                   [         R                  R                  U5      nSn[        U R	                  5       UR	                  5       5       GH   u  pE[         R
                  R                  U5      n[         R
                  R                  U5      nUR                  5        H  u  pX;   a~  Xx   n
U	R                  U
R                  :w  a   U
R                  b  U
R                  U	l        SnU	R                  U
R                  :w  a$  U
R                  b  U
R                  U	l	        SnM  M  M  [        R                  SXR                  5        M     GM     U$ )a  Merge the shape inferred model with the original model.

Args:
    model: The original IR model.
    inferred_proto: The ONNX model with shapes and types inferred.

Returns:
    A tuple containing the modified model and a boolean indicating whether the model was modified.
FTz'Value %s not found in inferred graph %s)irserdedeserialize_modelzipgraphsconveniencecreate_value_mappingitemsshapedtypeloggerwarningname)modelinferred_protoinferred_modelmodifiedoriginal_graphinferred_graphoriginal_valuesinferred_valuesr   valueinferred_values              d/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx_ir/passes/common/shape_inference.py_merge_funcr       s    XX//?NH*-ellnn>S>S>U*V&..==nM..==nM*002KD&!0!6;;."6"66>;O;O;["0"6"6EK#H;;."6"66>;O;O;["0"6"6EK#H <\6 =tEXEX 3 +W  O    c                  N   ^  \ 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   5   z0This pass performs shape inference on the graph.c                F   > [         TU ]  5         Xl        X l        X0l        g)a#  Initialize the shape inference pass.

If inference fails, the model is left unchanged.

Args:
    check_type: If True, check the types of the inputs and outputs.
    strict_mode: If True, use strict mode for shape inference.
    data_prop: If True, use data propagation for shape inference.
N)super__init__
check_typestrict_mode	data_prop)selfr'   r(   r)   	__class__s       r   r&   ShapeInferencePass.__init__8   s      	$&"r!   c                (  ^  SU 4S jjn [         R                  " X!5      n[        X5      n[
        R                  R                  XS9$ ! [         a>  n[        R	                  SUS9  [
        R                  R                  US5      s S nA$ S nAff = f)Nc                   > [         R                  R                  U TR                  TR                  TR
                  S9$ )Nr'   r(   r)   )onnxshape_inferencer   r'   r(   r)   )protor*   s    r   partial_infer_shapes5ShapeInferencePass.call.<locals>.partial_infer_shapesJ   s;    ''44?? ,,..	 5  r!   z3Shape inference failed: %s. Model is left unchanged)exc_infoF)r   )r2   onnx.ModelProtoreturnr6   )	r   call_onnx_api	Exceptionr   r   r   passes
PassResultr    )r*   r   r3   inferred_model_protoer   s   `     r   callShapeInferencePass.callI   s~    		6#/#=#=>R#Z 
 u;yy##E#==  	6NNP[\N]99''u55	6s   A	 	
B3BBB)r'   r)   r(   )TTT)r'   boolr(   r@   r)   r@   r7   None)r   ir.Modelr7   zir.passes.PassResult)	__name__
__module____qualname____firstlineno____doc__r&   r>   __static_attributes____classcell__)r+   s   @r   r   r   5   sA    : TX##48#LP#	# #"> >r!   Tr/   c               6    [        XUS9" U 5      R                  $ )a\  Perform shape inference on the model.

Args:
    model: The model to perform shape inference on.
    check_type: If True, check the types of the inputs and outputs.
    strict_mode: If True, use strict mode for shape inference.
    data_prop: If True, use data propagation for shape inference.

Returns:
    The model with shape inference applied.
r/   )r   r   )r   r'   r(   r)   s       r   r   r   \   s#    $ )Ur!   )r   rB   r   r6   r7   r@   )
r   rB   r'   r@   r(   r@   r)   r@   r7   rB   )rG   
__future__r   __all__loggingr0   onnx_irr   onnx_ir.passes.commonr   	getLoggerrC   r   r    r:   InPlacePassr   r    r!   r   <module>rS      s    7 " 
    .			8	$>$>.. $>T   	
  r!   