
    /;jih                        d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZ erd dlmZ 	 d<d=dZ	 	 	 	 	 	 d>d?dZ	 	 	 	 	 	 	 	 	 	 d@dAd$Z	 	 	 	 	 	 	 	 dBdCd4Z	 	 	 	 	 	 	 	 dDdEd7Z	 dFdGd:Z	 dFdHd;ZdS )I    )annotations)TYPE_CHECKING)AttributeProto
GraphProto
ModelProtoTensorProtocheckerhelperutils)MutableMappingNg1r   g2io_maplist[tuple[str, str]] | Nonereturnlist[tuple[str, list[str]]]c                &   t          | t                    st          d          t          |t                    st          d          dd}dd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	list[str]c2r   c                Z    t          t          |           t          |          z            S N)listset)r   r   s     F/root/voice-cloning/.venv/lib/python3.11/site-packages/onnx/compose.py_overlappingz-check_overlapping_names.<locals>._overlapping'   s!    CGGc"gg%&&&    Ngraphr   excludeset[str] | Nonec                    |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>;   s    000T!W000r   )r!   edgec                    g | ]	}|j         
S r1   namer2   es     r   
<listcomp>z+check_overlapping_names.<locals>.<listcomp>C   s    '''A'''r   c                    g | ]	}|j         
S r1   r7   r9   s     r   r;   z+check_overlapping_names.<locals>.<listcomp>C   s    )H)H)HQ!&)H)H)Hr   
value_infoc                    g | ]	}|j         
S r1   r7   r9   s     r   r;   z+check_overlapping_names.<locals>.<listcomp>I   s    (((A(((r   c                    g | ]	}|j         
S r1   r7   r9   s     r   r;   z+check_overlapping_names.<locals>.<listcomp>I   s    *J*J*Ja16*J*J*Jr   initializerc                &    g | ]}|j         j        S r1   valuesr8   r9   s     r   r;   z+check_overlapping_names.<locals>.<listcomp>O       6661666r   c                &    g | ]}|j         j        S r1   rB   r9   s     r   r;   z+check_overlapping_names.<locals>.<listcomp>P   rD   r   c                &    g | ]}|j         j        S r1   indicesr8   r9   s     r   r;   z+check_overlapping_names.<locals>.<listcomp>R       777A777r   c                &    g | ]}|j         j        S r1   rG   r9   s     r   r;   z+check_overlapping_names.<locals>.<listcomp>S   rI   r   sparse_initializer)r   r   r   r   r   r   r   )r    r   r!   r"   r   r   )
isinstancer   	TypeErrorr'   r=   r@   rK   )r   r   r   r   r-   resultio_map_inputsoverlaps           r   check_overlapping_namesrQ      s1    b*%% <:;;;b*%% <:;;;' ' ' '     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   list[tuple[str, str]]inputslist[str] | Noneoutputsprefix1
str | Noneprefix2r8   
doc_stringc	                t    t          | t                    st          d          t          |t                    s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                  }d2 fd# |||           |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         d0|j         d| j        z   d1z   | j        z   d1z   |j        z   d1z   |j        z   }||_        |S )3a  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 underscore 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   r0   r1   r2   iorV   rX   s     r   r;   z merge_graphs.<locals>.<listcomp>   ]     
 
 

  $+5"Q%1#*5"Q%1
 
 
r   c                    h | ]
}|d          S )r   r1   r2   r`   s     r   r4   zmerge_graphs.<locals>.<setcomp>   s    ---be---r   c                    h | ]
}|d          S r/   r1   rc   s     r   r4   zmerge_graphs.<locals>.<setcomp>   s    ,,,rRU,,,r   c                    i | ]\  }}||	S r1   r1   )r2   out_namein_names      r   
<dictcomp>z merge_graphs.<locals>.<dictcomp>   s    III->XwwIIIr   c                    h | ]	}|j         
S r1   r7   r2   r,   s     r   r4   zmerge_graphs.<locals>.<setcomp>   s    )))!qv)))r   c                    h | ]	}|j         
S r1   r7   r2   r+   s     r   r4   zmerge_graphs.<locals>.<setcomp>   s    '''af'''r   c                    g | ]	}|j         
S r1   r7   rl   s     r   r;   z merge_graphs.<locals>.<listcomp>       222A222r   c                    g | ]	}|j         
S r1   r7   rl   s     r   r;   z merge_graphs.<locals>.<listcomp>   rn   r   c                0    g | ]}|j         v |j         S r1   r7   r2   r+   	input_sets     r   r;   z merge_graphs.<locals>.<listcomp>   s'    IIIAQVy5H5H5H5H5Hr   c                B    g | ]}|j         v s	|j         v |j         S r1   r7   )r2   r+   rr   io_map_g2_inss     r   r;   z merge_graphs.<locals>.<listcomp>   s>       6Y&&!&M*A*A *A*A*Ar   c                    g | ]	}|j         
S r1   r7   rj   s     r   r;   z merge_graphs.<locals>.<listcomp>       444Q!&444r   c                    g | ]	}|j         
S r1   r7   rj   s     r   r;   z merge_graphs.<locals>.<listcomp>   rv   r   c                B    g | ]}|j         v s	|j         v |j         S r1   r7   )r2   r,   io_map_g1_outs
output_sets     r   r;   z merge_graphs.<locals>.<listcomp>   s>       6Z''16^+C+C +C+C+Cr   c                0    g | ]}|j         v |j         S r1   r7   r2   r,   rz   s     r   r;   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.	sub_graphr   startintendr   Nonec           	     4   t          ||          D ]}| j        |         }|j        D ]@}|j        t          j        k    r) |j        dt          |j        j                             At          |j	                  D ]\  }}|	v r	|         |j	        |<   d S Nr   )
ranger%   	attributetyper   GRAPHglen	enumerater&   )
r   r   r   node_idxr%   attrindexname_
connect_ioreversed_io_maps
           r   r   z merge_graphs.<locals>.connect_io   s    eS)) 	? 	?H>(+D < <9 444Jtvq#dfk*:*:;;; )$* 5 5 ? ?uO++(7(>DJu%?	? 	?r   c                &    g | ]}|j         v |S r1   r7   rq   s     r   r;   z merge_graphs.<locals>.<listcomp>   %    CCCaqv/B/B/B/B/Br   c                &    g | ]}|j         v |S r1   r7   rq   s     r   r;   z merge_graphs.<locals>.<listcomp>   r   r   c                &    g | ]}|j         v|S r1   r7   )r2   r+   rt   s     r   r;   z merge_graphs.<locals>.<listcomp>   s%    KKKaqv]/J/J/J/J/Jr   c                &    g | ]}|j         v |S r1   r7   r|   s     r   r;   z merge_graphs.<locals>.<listcomp>   %    FFFq:1E1E1E1E1Er   c                &    g | ]}|j         v |S r1   r7   r|   s     r   r;   z merge_graphs.<locals>.<listcomp>   r   r   c                &    g | ]}|j         v|S r1   r7   )r2   r,   ry   s     r   r;   z merge_graphs.<locals>.<listcomp>   s%    NNNq~1M1M1M1M1Mr   c                &    g | ]}|j         v|S r1   r7   r2   initrt   s     r   r;   z merge_graphs.<locals>.<listcomp>   s%    KKK$DI],J,J,J,J,Jr   c                0    g | ]}|j         j        v|S r1   rB   r   s     r   r;   z merge_graphs.<locals>.<listcomp>  s3     	
 	
 	
{}44 444r   c                &    g | ]}|j         v|S r1   r7   )r2   virt   s     r   r;   z merge_graphs.<locals>.<listcomp>
  s%    TTTrwm7S7S7S7S7Sr   N_zGraph combining  and z

)r   r   r   r   r   r   r   r   )rL   r   rM   CopyFromadd_prefix_graphr(   r&   r   r   r   	Extractorr
   
make_modelextract_modelr    
ValueErrorrQ   joinr%   extendr@   rK   r=   r8   rY   )!r   r   r   rS   rU   rV   rX   r8   rY   g1_copyg2_copyg1_outsg2_ins	g1_inputs	g2_inputs
g1_outputs
g2_outputse1e2g1_out_name
g2_in_nameoverlapping_namescategorynamesr   g2_nodes_beging2_nodes_endr   rr   ry   rt   rz   r   s!        ``                    @@@@@@r   merge_graphsr   [   s   L b*%% <:;;;b*%% <:;;;  
' 
 	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	? 	? 	? 	? 	? 	? 	? Jq.,/// 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$   r0   m1r   m2producer_nameproducer_versiondomainmodel_version
int | Nonec           
        t          | t                    st          d          t          |t                    s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 underscore 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   r[   c                d    g | ],}r|d          z   n|d          r|d         z   n|d         f-S r^   r1   r_   s     r   r;   z merge_models.<locals>.<listcomp>u  ra   r   )rS   rU   r8   rY   )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 r1   r7   r2   fs     r   r4   zmerge_models.<locals>.<setcomp>  s    &&&A&&&r   c                    h | ]	}|j         
S r1   r7   r   s     r   r4   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: r}   )rL   r   rM   r   r   r   opset_importr   versionr   
add_prefixr   r    r
   r   metadata_propsvaluekeyset_model_props	functionsr   	MergeFromr	   check_model)r   r   r   rS   rU   rV   rX   r8   rY   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 b*%% <:;;;b*%% <:;;;	}%%;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    r\   strrename_nodesbool | Nonerename_edgesrename_inputsrename_outputsrename_initializersrename_value_infosinplacename_mapdict[str, str] | Nonec
                \   t          | t                    st          d          |s$t                      }
|
                    |            n| }
dd}|	i }	|r8d |
j        D             }|
j        D ]}|j        D ]}||vr |||          |	|<    |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 graphr\   r   r8   r   c                6    t          |          dk    r| |z   n|S r   )r   )r\   r8   s     r   	_prefixedz#add_prefix_graph.<locals>._prefixed  s     #D		Av}}47r   Nc                    h | ]	}|j         
S r1   r7   rj   s     r   r4   z#add_prefix_graph.<locals>.<setcomp>  s    777af777r   T)r   r   )r\   r   r8   r   r   r   )rL   r   rM   r   r(   r%   r&   r8   r   r   r   r@   rK   rC   rH   r=   r   )r    r\   r   r   r   r   r   r   r   r   r   r   graph_output_namesr*   r:   r   r   r   sparse_initr+   r(   input_in_descout_descr@   rK   r=   s                              r   r   r     sR   D eZ(( ?=>>> LL	

58 8 8 8  7 87ah777 	7 	7AX 7 7..."+)FA"6"6HQK7  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                    s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   )
rL   r   rM   r   r   r    r   r8   r%   op_type)r   r\   r   r   r   r   r   r   r   r   m
f_name_mapr   
new_f_namer*   s                  r   r   r   )  s>   D eZ(( ?=>>> LL	

5!!#%/-
 
 
 
  2
 	  	 A!&J!+JqvAFF  	6 	6AV 6 69
** *19 5AI6 ! 	2 	2AyJ&&&qy1	Lr   dim_idxr   c                Z   t          | t                    s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 r1   r7   )r2   r(   s     r   r;   z(expand_out_dim_graph.<locals>.<listcomp>  s    999ffk999r   _collapsed_dim__expand_out_dim_idxConstantz	-constantz-valuer0   )r8   	data_typedimsvals)rS   rU   r8   r   r   	Unsqueezez
unsqueeze-)rS   rU   r8   c                    g | ]	}|j         
S r1   )	dim_value)r2   ds     r   r;   z(expand_out_dim_graph.<locals>.<listcomp>  s    GGGQQ[GGGr   )rL   r   rM   r   r(   r%   r   r&   r8   r'   r
   	make_nodemake_tensorr   INT64r   r   popr   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  s   ( eZ(( ?=>>> 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                    s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   )rL   r   rM   r   r  r    )r   r   r   r   s       r   expand_out_dimr    sv    ( eZ(( ?=>>> LL	

5   
 Lr   r   )r   r   r   r   r   r   r   r   )NNNNNN)r   r   r   r   r   rR   rS   rT   rU   rT   rV   rW   rX   rW   r8   rW   rY   rW   r   r   )
NNNNNNr   r   r$   r0   )r   r   r   r   r   rR   rS   rT   rU   rT   rV   rW   rX   rW   r8   rW   rY   rW   r   rW   r   rW   r   rW   r   r   r   r   )TTTTTTFN)r    r   r\   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )TTTTTTTF)r   r   r\   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )F)r    r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )
__future__r   typingr   onnxr   r   r   r   r	   r
   r   collections.abcr   rQ   r   r   r   r   r  r  r1   r   r   <module>r#     s   # " " " " "                               /...... LPB B B B BR  $ $!@ @ @ @ @N  $ $! ;#( !O O O O Oj !% $!%"&'+&* &*v v v v vx !% $!%"&'+&*$( G G G G GZ !M M M M Mf !! ! ! ! ! ! !r   