
    
Ng8f                        d dl mZmZmZmZmZmZ d dlmZm	Z	m
Z
mZmZmZ 	 d.dededeeeeef                           deeeee         f                  fdZ	 	 	 	 	 	 d/dededeeeef                  d	eee                  d
eee                  dee         dee         dee         dee         defdZ	 	 	 	 	 	 	 	 	 	 d0de	de	deeeef                  d	eee                  d
eee                  dee         dee         dee         dee         dee         dee         dee         dee         de	fdZ	 	 	 	 	 	 	 	 d1dededee         d ee         d!ee         d"ee         d#ee         d$ee         d%ee         d&eeeef                  defd'Z	 	 	 	 	 	 	 	 d2d(e	dedee         d ee         d!ee         d"ee         d#ee         d$ee         d)ee         d%ee         de	fd*Z	 d3ded+ed%ee         defd,Z	 d3d(e	d+ed%ee         de	fd-ZdS )4    )DictListMutableMappingOptionalSetTuple)
GraphProto
ModelProtoTensorProtocheckerhelperutilsNg1g2io_mapreturnc                    t          |           t          urt          d          t          |          t          urt          d          dt          t                   dt          t                   dt          t                   fd}ddt          d	t
          t          t                            dt          t                   fd
}g }|sg }d |D             } | ||            |||                    }|r|                    d|f            |d | j        D             d |j        D                       }|r|                    d|f            |d | j	        D             d |j	        D                       }|r|                    d|f            |d | j
        D             d |j
        D                        |d | j
        D             d |j
        D                       z   }|r|                    d|f           |S )a  Checks whether there are name collisions between two graphs

    Returns a list of tuples where the first element represents the member containing overlapping names
    (One of: "node", "edge", "value_info", "initializer", "sparse_initializer"), and the
    second element contains a list of names that appear in both graphs on that category.

    Optionally, it takes an io_map, representing the output/inputs to be connected. It provided, overlapping
    present in the io_map argument will be ignored.
     g1 argument is not an ONNX graph g2 argument is not an ONNX graphc1c2r   c                 Z    t          t          |           t          |          z            S N)listset)r   r   s     H/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/compose.py_overlappingz-check_overlapping_names.<locals>._overlapping   s!    CGGc"gg%&&&    Ngraphexcludec                     |t                      }g }| j        D ]T}|j        D ]!}|dk    r||vr|                    |           "|j        D ]!}|dk    r||vr|                    |           "U|S )N )r   nodeinputappendoutput)r   r    edgesnios         r   _edge_namesz,check_overlapping_names.<locals>._edge_names   s    ?eeG 	$ 	$AW $ $77q//LLOOOX $ $77q//LLOOO$ r   c                     h | ]
}|d          S     ).0elems     r   	<setcomp>z*check_overlapping_names.<locals>.<setcomp>0   s    000T!W000r   )r    edgec                     g | ]	}|j         
S r/   namer0   es     r   
<listcomp>z+check_overlapping_names.<locals>.<listcomp>8   s    '''A'''r   c                     g | ]	}|j         
S r/   r5   r7   s     r   r9   z+check_overlapping_names.<locals>.<listcomp>8   s    )H)H)HQ!&)H)H)Hr   
value_infoc                     g | ]	}|j         
S r/   r5   r7   s     r   r9   z+check_overlapping_names.<locals>.<listcomp>>   s    (((A(((r   c                     g | ]	}|j         
S r/   r5   r7   s     r   r9   z+check_overlapping_names.<locals>.<listcomp>>   s    *J*J*Ja16*J*J*Jr   initializerc                 &    g | ]}|j         j        S r/   valuesr6   r7   s     r   r9   z+check_overlapping_names.<locals>.<listcomp>D       6661666r   c                 &    g | ]}|j         j        S r/   r@   r7   s     r   r9   z+check_overlapping_names.<locals>.<listcomp>E   rB   r   c                 &    g | ]}|j         j        S r/   indicesr6   r7   s     r   r9   z+check_overlapping_names.<locals>.<listcomp>G       777A777r   c                 &    g | ]}|j         j        S r/   rE   r7   s     r   r9   z+check_overlapping_names.<locals>.<listcomp>H   rG   r   sparse_initializerr   )typer	   
ValueErrorr   strr   r   r%   r;   r>   rI   )r   r   r   r   r+   resultio_map_inputsoverlaps           r   check_overlapping_namesrP      ss    Bxxz!!;<<<Bxxz!!;<<<'c 'S	 'd3i ' ' ' ' : S0B dSVi     F 00000M l;;r??KKM,R,R,RSSG )vw'(((l''''')H)H"-)H)H)H G  /|W-...l(((((*J*J2>*J*J*J G  0}g.///l66 566666 5666  	77!677777!6777	 	G  7+W5666Mr   inputsoutputsprefix1prefix2r6   
doc_stringc	                    !"# t          |           t          urt          d          t          |          t          urt          d          srr6t                      }	|	                    |            |	} t	          |           } r6t                      }
|
                    |           |
}t	          |          }fd|D             }d |D             !d |D             "d |D             }d | j        D             }d	 |j        D             }|s|r|s#d
 | j        D             }d |j        D             }n6t          |            fd| j        D             } "fd|j        D             }|s#d | j        D             }d |j        D             }n6t          |          #!#fd| j        D             }#fd|j        D             }t          |          t          | j                  k     s%t          |          t          | j                  k     rAt          j
        t          j        |                     }|                    ||          j        } t          |          t          |j                  k     s%t          |          t          |j                  k     rAt          j
        t          j        |                    }|                    ||          j        }|D ]3\  }}||vrt          d| d          ||vrt          d| d          4t          | ||          }t          |          dk    r:|d         \  }}t          d| dd                    |          z   dz   dz             t                      }|j                            | j                   t          |j                  }|j                            |j                   t          |j                  }t'          ||          D ]=}|j        |         }t)          |j                  D ]\  }}||v r||         |j        |<   >|rft          |           |j                             fd| j        D                        |j                             fd|j        D                        nJ|j                            | j                   |j                            "fd|j        D                        |rft          |          #|j                            #fd| j        D                        |j                            #fd |j        D                        nJ|j                            !fd!| j        D                        |j                            |j                   |j                            | j                   |j                            "fd"|j        D                        |j                            | j                   |j                            "fd#|j        D                        |j                            | j                   |j                            "fd$|j        D                        ||n d&                    | j        |j        g          |_        |<d'| j         d(|j         d| j        z   d)z   | j        z   d)z   |j        z   d)z   |j        z   }||_        |S )*a  Combines two ONNX graphs into a single one.

    The combined graph is defined by connecting the specified set of outputs/inputs. Those inputs/outputs
    not specified in the io_map argument will remain as inputs/outputs of the combined graph.

    Arguments:
        g1 (GraphProto): First graph
        g2 (GraphProto): Second graph
        io_map (list of pairs of string): The pairs of names [(out0, in0), (out1, in1), ...]
                                          representing outputs of the first graph and inputs of the second
                                          to be connected
        inputs (list of string): Optional list of inputs to be included in the combined graph
                                 By default, all inputs not present in the ``io_map`` argument will be
                                 included in the combined model
        outputs (list of string): Optional list of outputs to be included in the combined graph
                                  By default, all outputs not present in the ``io_map`` argument will be
                                  included in the combined model
        prefix1 (string): Optional prefix to be added to all names in g1
        prefix2 (string): Optional prefix to be added to all names in g2
        name (string): Optional name for the combined graph
                       By default, the name is g1.name and g2.name concatenated with an undescore delimiter
        doc_string (string): Optional docstring for the combined graph
                             If not provided, a default docstring with the concatenation of g1 and g2 docstrings is used

    Returns:
        GraphProto
    r   r   prefixc                 d    g | ],}r|d          z   n|d          r|d         z   n|d         f-S r   r.   r/   r0   iorS   rT   s     r   r9   z merge_graphs.<locals>.<listcomp>   ]     
 
 

  $+5"Q%1#*5"Q%1
 
 
r   c                     h | ]
}|d          S )r   r/   r0   r\   s     r   r2   zmerge_graphs.<locals>.<setcomp>   s    ---be---r   c                     h | ]
}|d          S r-   r/   r_   s     r   r2   zmerge_graphs.<locals>.<setcomp>   s    ,,,rRU,,,r   c                     i | ]\  }}||	S r/   r/   )r0   out_namein_names      r   
<dictcomp>z merge_graphs.<locals>.<dictcomp>   s    III->XwwIIIr   c                     h | ]	}|j         
S r/   r5   r0   r*   s     r   r2   zmerge_graphs.<locals>.<setcomp>   s    )))!qv)))r   c                     h | ]	}|j         
S r/   r5   r0   r)   s     r   r2   zmerge_graphs.<locals>.<setcomp>   s    '''af'''r   c                     g | ]	}|j         
S r/   r5   rh   s     r   r9   z merge_graphs.<locals>.<listcomp>       222A222r   c                     g | ]	}|j         
S r/   r5   rh   s     r   r9   z merge_graphs.<locals>.<listcomp>   rj   r   c                 0    g | ]}|j         v |j         S r/   r5   r0   r)   	input_sets     r   r9   z merge_graphs.<locals>.<listcomp>   s'    IIIAQVy5H5H5H5H5Hr   c                 B    g | ]}|j         v s	|j         v |j         S r/   r5   )r0   r)   rn   io_map_g2_inss     r   r9   z merge_graphs.<locals>.<listcomp>   s>       6Y&&!&M*A*A *A*A*Ar   c                     g | ]	}|j         
S r/   r5   rf   s     r   r9   z merge_graphs.<locals>.<listcomp>       444Q!&444r   c                     g | ]	}|j         
S r/   r5   rf   s     r   r9   z merge_graphs.<locals>.<listcomp>   rr   r   c                 B    g | ]}|j         v s	|j         v |j         S r/   r5   )r0   r*   io_map_g1_outs
output_sets     r   r9   z merge_graphs.<locals>.<listcomp>   s>       6Z''16^+C+C +C+C+Cr   c                 0    g | ]}|j         v |j         S r/   r5   r0   r*   rv   s     r   r9   z merge_graphs.<locals>.<listcomp>   s'    LLLQqv7K7K!&7K7K7Kr   zOutput z is not present in g1zInput z is not present in g2r   z=Cant merge two graphs with overlapping names. Found repeated z names: , 
zYConsider using ``onnx.compose.add_prefix`` to add a prefix to names in one of the graphs.c                 &    g | ]}|j         v |S r/   r5   rm   s     r   r9   z merge_graphs.<locals>.<listcomp>   %    CCCaqv/B/B/B/B/Br   c                 &    g | ]}|j         v |S r/   r5   rm   s     r   r9   z merge_graphs.<locals>.<listcomp>   r|   r   c                 &    g | ]}|j         v|S r/   r5   )r0   r)   rp   s     r   r9   z merge_graphs.<locals>.<listcomp>   s%    KKKaqv]/J/J/J/J/Jr   c                 &    g | ]}|j         v |S r/   r5   rx   s     r   r9   z merge_graphs.<locals>.<listcomp>   %    FFFq:1E1E1E1E1Er   c                 &    g | ]}|j         v |S r/   r5   rx   s     r   r9   z merge_graphs.<locals>.<listcomp>   r   r   c                 &    g | ]}|j         v|S r/   r5   )r0   r*   ru   s     r   r9   z merge_graphs.<locals>.<listcomp>   s%    NNNq~1M1M1M1M1Mr   c                 &    g | ]}|j         v|S r/   r5   r0   initrp   s     r   r9   z merge_graphs.<locals>.<listcomp>   s%    KKK$DI],J,J,J,J,Jr   c                 0    g | ]}|j         j        v|S r/   r@   r   s     r   r9   z merge_graphs.<locals>.<listcomp>   s3     	
 	
 	
{}44 444r   c                 &    g | ]}|j         v|S r/   r5   )r0   virp   s     r   r9   z merge_graphs.<locals>.<listcomp>   s%    TTTrwm7S7S7S7S7Sr   N_zGraph combining  and z

)rJ   r	   rK   CopyFromadd_prefix_graphr&   r$   r   lenr   	Extractorr   
make_modelextract_modelr   rP   joinr#   extendrange	enumerater>   rI   r;   r6   rU   )$r   r   r   rQ   rR   rS   rT   r6   rU   g1_copyg2_copyreversed_io_mapg1_outsg2_ins	g1_inputs	g2_inputs
g1_outputs
g2_outputse1e2g1_out_name
g2_in_nameoverlapping_namescategorynamesgg2_nodes_beging2_nodes_endnode_idxr#   indexname_rn   ru   rp   rv   s$        ``                         @@@@r   merge_graphsr   P   s   L Bxxz!!;<<<Bxxz!!;<<<  
' 
 	6 llGR   B!"W555B 	6 llGR   B!"W555B
 
 
 
 

 
 
 
 .-f---N,,V,,,MII&IIIO))ry)))G''bh'''F  ? ? 
	22222I22222IIFIIIIIIIII      I  
	M44")444J44")444JJWJ      J
 MLLL")LLLJy>>CMM))S__s29~~-M-M!22!6!677B!!)Z88>By>>CMM))S__s29~~-M-M!22!6!677B!!)Z88>B $* I IZg%%I{IIIJJJV##GjGGGHHH $ 0B??
!!+A.%1&1 1 1ii  j	j
 
 	
 	AFMM"'[[NFMM"'qv;;L .,77 ; ;vh%dj11 	; 	;LE5''$3E$:
5!	;  MKK		CCCC28CCCDDD	CCCC28CCCDDDD	rx   	KKKK28KKKLLL #\\
	FFFFBIFFFGGG	FFFFBIFFFGGGG	NNNNBINNNOOO		"""M(((MKKKK".KKK    5666	
 	
 	
 	
-	
 	
 	
   L&&&LTTTTbmTTTUUU%TT388RWbg4F+G+GAF8rw88RW888g m 	
 g  m 	 ALHr   onnx.compose.merge_models1.0r"   r.   m1m2producer_nameproducer_versiondomainmodel_versionc           
         t          |           t          urt          d          t          |          t          urt          d          | j        |j        k    r t          d| j         d|j         d          | j        }i }t	          | j                  t	          |j                  z   }|D ]R}|j        |v r8||j                 }|j        |k    rt          d| j         d|j                   C|j        ||j        <   Ssrr6t                      }|                    |            |} t          |           } r6t                      }|                    |           |}t          |          }fd	|D             }t          | j        |j        |||||
          }t          j        ||	|
||||          }i }| j        D ]}|j        ||j        <   |j        D ]V}|j        |v r<||j                 }||j        k    r#t          d|j         d| d|j         d          G|j        ||j        <   Wt          j        ||           t	          d | j        D             d |j        D             z            }|r%t          dd                    |          z             |j                            | j                   |j                            |j                   t+          j        |           |S )a=  Combines two ONNX models into a single one.

    The combined model is defined by connecting the specified set of outputs/inputs.
    Those inputs/outputs not specified in the io_map argument will remain as
    inputs/outputs of the combined model.

    Both models should have the same IR version, and same operator sets imported.

    Arguments:
        m1 (ModelProto): First model
        m2 (ModelProto): Second model
        io_map (list of pairs of string): The pairs of names [(out0, in0), (out1, in1), ...]
                                          representing outputs of the first graph and inputs of the second
                                          to be connected
        inputs (list of string): Optional list of inputs to be included in the combined graph
                                 By default, all inputs not present in the ``io_map`` argument will be
                                 included in the combined model
        outputs (list of string): Optional list of outputs to be included in the combined graph
                                  By default, all outputs not present in the ``io_map`` argument will be
                                  included in the combined model
        prefix1 (string): Optional prefix to be added to all names in m1
        prefix2 (string): Optional prefix to be added to all names in m2
        name (string): Optional name for the combined graph
                       By default, the name is g1.name and g2.name concatenated with an undescore delimiter
        doc_string (string): Optional docstring for the combined graph
                             If not provided, a default docstring with the concatenation of g1 and g2 docstrings is used
        producer_name (string): Optional producer name for the combined model. Default: 'onnx.compose'
        producer_version (string): Optional producer version for the combined model. Default: "1.0"
        domain (string): Optional domain of the combined model. Default: ""
        model_version (int): Optional version of the graph encoded. Default: 1

    Returns:
        ModelProto
    z m1 argument is not an ONNX modelz m2 argument is not an ONNX modelzIR version mismatch z != z-. Both models should have the same IR versionzPCan't merge two models with different operator set ids for a given domain. Got: r   rW   c                 d    g | ],}r|d          z   n|d          r|d         z   n|d         f-S rZ   r/   r[   s     r   r9   z merge_models.<locals>.<listcomp>b  r]   r   )rQ   rR   r6   rU   )r   r   r   r   opset_imports
ir_versionzaCan't merge models with different values for the same model metadata property. Found: property = z, with values .c                     h | ]	}|j         
S r/   r5   r0   fs     r   r2   zmerge_models.<locals>.<setcomp>  s    &&&A&&&r   c                     h | ]	}|j         
S r/   r5   r   s     r   r2   zmerge_models.<locals>.<setcomp>  s    )G)G)GQ!&)G)G)Gr   zPCan't merge models with overlapping local function names. Found in both graphs: ry   )rJ   r
   rK   r   r   opset_importr   versionr   
add_prefixr   r   r   r   metadata_propsvaluekeyset_model_props	functionsr   	MergeFromr   check_model)r   r   r   rQ   rR   rS   rT   r6   rU   r   r   r   r   r   opset_import_mapr   entryfound_versionm1_copym2_copyr   modelmodel_props
meta_entryr   function_overlaps        ``                   r   merge_modelsr     s   b Bxxz!!;<<<Bxxz!!;<<<	}%%;2= ; ;bm ; ; ;
 
 	
 J13))D,A,AAM 	; 	;<+++,U\:M}-- DOD D24/D D   . .3]U\**  
' 
 	0 llGR   BBw///B 	0 llGR   BBw///B
 
 
 
 

 
 
 
 

  E #)##  E K' 7 7
&0&6JN##' 	; 	;
>[((
/E
((( h*4.h hHMh hT^Tdh h h   ) +5*:K
''
5+... &&&&&)G)G",)G)G)GG   
&(,		2B(C(CD
 
 	
 
Obl+++	Obl+++Lr   TFr   rX   rename_nodesrename_edgesrename_inputsrename_outputsrename_initializersrename_value_infosinplacename_mapc
                    t          |           t          urt          d          |s$t                      }
|
                    |            n| }
dt          dt          dt          fd}|	i }	|r<|
j        D ]4}|j        D ]} |||          |	|<   |j        D ]} |||          |	|<   5|r#|
j        D ]} |||j                  |	|j        <   |r#|
j        D ]} |||j                  |	|j        <   |rI|
j        D ]A} |||j                  |_        |j	        D ]!}|j
        rt          |j
        |d|	           "B|rs|
j        D ]} |||j                  |	|j        <   |
j        D ]H} |||j        j                  |	|j        j        <    |||j        j                  |	|j        j        <   I|r#|
j        D ]} |||j                  |	|j        <   |
j        D ]t}t#          |j                  D ]$\  }}|j        |         |	v r|	|         |j        |<   %t#          |j                  D ]$\  }}|j        |         |	v r|	|         |j        |<   %u|
j        D ]}|j        |	v r|	|j                 |_        |
j        D ]}|j        |	v r|	|j                 |_        |
j        D ]}|j        |	v r|	|j                 |_        |
j        D ]V}|j        j        |	v r|	|j        j                 |j        _        |j        j        |	v r|	|j        j                 |j        _        W|
j        D ]}|j        |	v r|	|j                 |_        |
S )	a  Adds a prefix to names of elements in a graph: nodes, edges, inputs, outputs,
    initializers, sparse initializer, value infos.

    It can be used as a utility before merging graphs that have overlapping names.
    Empty names are not prefixed.

    Arguments:
        graph (GraphProto): Graph
        prefix (str): Prefix to be added to each name in the graph
        rename_nodes (bool): Whether to prefix node names
        rename_edges (bool): Whether to prefix node edge names
        rename_inputs (bool): Whether to prefix input names
        rename_outputs (bool): Whether to prefix output names
        rename_initializers (bool): Whether to prefix initializer and sparse initializer names
        rename_value_infos (bool): Whether to prefix value info names
        inplace (bool): If True, mutates the graph directly.
                        Otherwise, a copy will be created
        name_map: (Dict): shared name_map in subgraph

    Returns:
        GraphProto
    #graph argument is not an ONNX graphrX   r6   r   c                 6    t          |          dk    r| |z   n|S )Nr   )r   )rX   r6   s     r   	_prefixedz#add_prefix_graph.<locals>._prefixed  s     #D		Av}}47r   NT)r   r   )rJ   r	   rK   r   rL   r#   r$   r&   r6   	attributer   r   r>   rI   rA   rF   r;   r   )r   rX   r   r   r   r   r   r   r   r   r   r   r(   r8   r   r   r   sparse_initr)   r&   input_in_descout_descr>   rI   r;   s                             r   r   r     sk   D E{{*$$>??? LL	

58# 8S 8S 8 8 8 8  3 	3 	3AW 3 3'i22X 3 3'i223  AW 	A 	AE#,9VUZ#@#@HUZ   AX 	A 	AE#,9VUZ#@#@HUZ    	 	AYvqv..AF[  	; $!VTH     	M 	? 	?D"+)FDI">">HTY/ 	 	K09	*/1 1H[',- 2;+02 2H[(-..  A\ 	A 	AE#,9VUZ#@#@HUZ  V . ."18,, 	/ 	/IAvx{h&&&v."17++ 	. 	.IAvwqzX%%%f-
	. 7 2 2<8###GL1GLH 4 4=H$$$X]3HM} : :x'''(89K2 X X$)X55-56H6O6T-U%*%*h66.67I7Q7V.W&+l 8 8
?h&&&z7JOHr   r   rename_functionsc
                    t          |           t          urt          d          |	s%t                      }
|
                    |            |
} t	          | j        |||||||d	  	         |ri }| j        D ]}||j        z   }|||j        <   ||_        | j        D ]'}|j        D ]}|j	        |v r||j	                 |_	        (| j        j        D ]}|j	        |v r||j	                 |_	        | S )a  Adds a prefix to names of elements in a graph: nodes, edges, inputs, outputs,
    initializers, sparse initializer, value infos, and local functions.

    It can be used as a utility before merging graphs that have overlapping names.
    Empty names are not _prefixed.

    Arguments:
        model (ModelProto): Model
        prefix (str): Prefix to be added to each name in the graph
        rename_nodes (bool): Whether to prefix node names
        rename_edges (bool): Whether to prefix node edge names
        rename_inputs (bool): Whether to prefix input names
        rename_outputs (bool): Whether to prefix output names
        rename_initializers (bool): Whether to prefix initializer and sparse initializer names
        rename_value_infos (bool): Whether to prefix value info nanes
        rename_functions (bool): Whether to prefix local function names
        inplace (bool): If True, mutates the model directly.
                        Otherwise, a copy will be created

    Returns:
        ModelProto
    #model argument is not an ONNX modelT)r   r   r   r   r   r   r   )
rJ   r
   rK   r   r   r   r   r6   r#   op_type)r   rX   r   r   r   r   r   r   r   r   m
f_name_mapr   
new_f_namer(   s                  r   r   r     s=   D E{{*$$>??? LL	

5!!#%/-
 
 
 
  2
 	  	 A!&J!+JqvAFF  	6 	6AV 6 69
** *19 5AI6 ! 	2 	2AyJ&&&qy1	Lr   dim_idxc                 \   t          |           t          urt          d          |s$t                      }|                    |            n| }d |j        D             }|j        D ]^}t          |j                  D ]\  }}||v r|d| z   |j        |<   t          |j                  D ]\  }}||v r|d| z   |j        |<   _|j        dz   }	|j        	                    t          j        dg |	g|	 dt          j        |	 dt          j        dg|g	          
                     t          t!          |j                            D ]}
|j                            d          }|j        d| z   }|j        	                    t          j        d||	g|j        gd|j                              d |j         j        j        j        D             }|                    |d           |j        	                    t          j        |j        |j         j        j        |                     |S )aE  Inserts an extra dimension with extent 1 to each output in the graph.

    Inserts an Unsqueeze node for each output. It can be used as a utility before merging graphs,
    for example when the second one expects a batch dimension.

    Arguments:
        graph (GraphProto): Graph
        dim_idx (int): Index of the dimension to be inserted.
                       A negative value means counting dimensions from the back.
        inplace (bool): If True, mutates the model directly.
                        Otherwise, a copy will be created

    Returns:
        GraphProto
    r   c                     g | ]	}|j         
S r/   r5   )r0   r&   s     r   r9   z(expand_out_dim_graph.<locals>.<listcomp>y  s    999ffk999r   _collapsed_dim__expand_out_dim_idxConstantz	-constantz-valuer.   )r6   	data_typedimsvals)rQ   rR   r6   r   r   	Unsqueezez
unsqueeze-)rQ   rR   r6   c                     g | ]	}|j         
S r/   )	dim_value)r0   ds     r   r9   z(expand_out_dim_graph.<locals>.<listcomp>  s    GGGQQ[GGGr   )rJ   r	   rK   r   r&   r#   r   r$   r6   r%   r   	make_nodemake_tensorr   INT64r   r   poptensor_typeshapediminsertmake_tensor_value_info	elem_type)r   r   r   r   orig_out_namesr(   r)   outinpexpand_dim_kr   r*   prev_output	new_shapes                 r   expand_out_dim_graphr  \  s   ( E{{*$$>??? LL	

599999NV ? ?)) 	@ 	@FAsn$$!$?g$?$??(( 	? 	?FAsn$$ #>W#>#>>
	? 611LFMM!N +++$$,,,%+ 	 	 		
 	
 	
  & 3qx==!! 
 
HLLOOf::::	#\2*!&**	  	
 	
 	
 HG!&*<*B*FGGG	!$$$	)*4i 	
 	
 	
 	

 Hr   c                     t          |           t          urt          d          |s%t                      }|                    |            |} t	          | j        |d           | S )aE  Inserts an extra dimension with extent 1 to each output in the graph.

    Inserts an Unsqueeze node for each output. It can be used as a utility before merging graphs,
    for example when the second one expects a batch dimension.

    Arguments:
        model (ModelProto): Model
        dim_idx (int): Index of the dimension to be inserted.
                       A negative value means counting dimensions from the back.
        inplace (bool): If True, mutates the model directly.
                        Otherwise, a copy will be created

    Returns:
        ModelProto
    r   T)r   )rJ   r
   rK   r   r  r   )r   r   r   r   s       r   expand_out_dimr    su    ( E{{*$$>??? LL	

5   
 Lr   r   )NNNNNN)
NNNNNNr   r   r"   r.   )TTTTTTFN)TTTTTTTF)F)typingr   r   r   r   r   r   onnxr	   r
   r   r   r   r   rL   rP   r   intr   boolr   r   r  r  r/   r   r   <module>r     s   D C C C C C C C C C C C C C C C L L L L L L L L L L L L L L L L OSB BB"B,4T%S/5J,KB	%T#Y
 B B B BR #'#'!! $x xxx sCx!x T#Y	x
 d3i x c]x c]x 3-x x x x x x~ #'#'!! $#>&+#$O OOO sCx!O T#Y	O
 d3i O c]O c]O 3-O O C=O smO SMO C=O O O O Oj $(#'$(%)*.)-#)-r rrr 4.r 4.	r
 D>r TNr "$r !r d^r tCH~&r r r r rp $(#'$(%)*.)-'+#G GGG 4.G 4.	G
 D>G TNG "$G !G tnG d^G G G G GZ $M MMM d^M 	M M M Mf $! !!! d^! 	! ! ! ! ! !r   