o
    Uôj8f  ã                   @   sž  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 	d5dededeeeeef   deeeee f  fdd	„Z						d6d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d„Z										d7d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d„Z								d8d 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*d+„Z								d9d,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.d/„Z	d:d ed0ed(ee defd1d2„Z	d:d,e	d0ed(ee de	fd3d4„ZdS );é    )ÚDictÚListÚMutableMappingÚOptionalÚSetÚTuple)Ú
GraphProtoÚ
ModelProtoÚTensorProtoÚcheckerÚhelperÚutilsNÚg1Úg2Úio_mapÚreturnc                 C   sr  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d	td
ttt  dtt fdd„}g }|s=g }dd„ |D ƒ}||| ƒ|||d�ƒ}|rX| d|f¡ |dd„ | jD ƒdd„ |jD ƒƒ}|rr| d|f¡ |dd„ | j	D ƒdd„ |j	D ƒƒ}|rŒ| d|f¡ |dd„ | j
D ƒdd„ |j
D ƒƒ|dd„ | j
D ƒdd„ |j
D ƒƒ }|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                 S   s   t t| ƒt|ƒ@ ƒS ©N)ÚlistÚset)r   r   © r   ú_/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/onnx/compose.pyÚ_overlapping   s   z-check_overlapping_names.<locals>._overlappingNÚgraphÚexcludec                 S   sn   |d u rt ƒ }g }| jD ](}|jD ]}|dkr ||vr | |¡ q|jD ]}|dkr3||vr3| |¡ q$q|S )NÚ )r   ÚnodeÚinputÚappendÚoutput)r   r   ÚedgesÚnÚiÚor   r   r   Ú_edge_names   s   


€

€þz,check_overlapping_names.<locals>._edge_namesc                 S   ó   h | ]}|d  ’qS ©é   r   )Ú.0Úelemr   r   r   Ú	<setcomp>0   ó    z*check_overlapping_names.<locals>.<setcomp>)r   Úedgec                 S   ó   g | ]}|j ‘qS r   ©Úname©r+   Úer   r   r   Ú
<listcomp>8   ó    z+check_overlapping_names.<locals>.<listcomp>Ú
value_infoc                 S   r0   r   r1   r3   r   r   r   r5   >   r6   Úinitializerc                 S   ó   g | ]}|j j‘qS r   ©Úvaluesr2   r3   r   r   r   r5   D   r.   c                 S   r9   r   r:   r3   r   r   r   r5   E   r.   c                 S   r9   r   ©Úindicesr2   r3   r   r   r   r5   G   r.   c                 S   r9   r   r<   r3   r   r   r   r5   H   r.   Úsparse_initializerr   )Útyper   Ú
ValueErrorr   Ústrr   r   r!   r7   r8   r>   )r   r   r   r   r'   ÚresultZio_map_inputsÚoverlapr   r   r   Úcheck_overlapping_names   sF   "$ÿÿþþýrD   ÚinputsÚoutputsÚprefix1Úprefix2r2   Ú
doc_stringc	                     sô  t | ƒtur
tdƒ‚t |ƒturtdƒ‚ˆsˆrFˆr*tƒ }	|	 | ¡ |	} t| ˆd�} ˆr<tƒ }
|
 |¡ |
}t|ˆd�}‡‡fdd„|D ƒ}dd„ |D ƒ‰dd„ |D ƒ‰d	d
„ |D ƒ}dd„ | jD ƒ}dd„ |jD ƒ}|sp|�r|sƒdd„ | jD ƒ}dd„ |jD ƒ}nt|ƒ‰ ‡ fdd„| jD ƒ}‡ ‡fdd„|jD ƒ}|s¯dd„ | jD ƒ}dd„ |jD ƒ}nt|ƒ‰‡‡fdd„| jD ƒ}‡fdd„|jD ƒ}t|ƒt| jƒk sÚt|ƒt| jƒk rét	 
t | ¡¡}| ||¡j} t|ƒt|jƒk süt|ƒt|jƒk �rt	 
t |¡¡}| ||¡j}|D ]\}}||v�rtd|› d�ƒ‚||v�r+td|› d�ƒ‚�qt| ||ƒ}t|ƒdk�rQ|d \}}td|› d�d |¡ d d ƒ‚tƒ }|j | j¡ t|jƒ}|j |j¡ t|jƒ}t||ƒD ]}|j| }t|jƒD ]\}}||v �r�|| |j|< �q}�qq|�rµt|ƒ‰ |j ‡ fdd„| jD ƒ¡ |j ‡ fd d„|jD ƒ¡ n|j | j¡ |j ‡fd!d„|jD ƒ¡ |�rît|ƒ‰|j ‡fd"d„| jD ƒ¡ |j ‡fd#d„|jD ƒ¡ n|j ‡fd$d„| jD ƒ¡ |j |j¡ |j | j¡ |j ‡fd%d„|jD ƒ¡ |j | j¡ |j ‡fd&d„|jD ƒ¡ |j | j¡ |j ‡fd'd„|jD ƒ¡ |d(u�rI|nd) | j|jg¡|_|d(u �rud*| j› d+|j› d�| j d, | j d, |j d, |j }||_|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                    ó<   g | ]}ˆ rˆ |d   n|d  ˆrˆ|d  n|d f‘qS ©r   r*   r   ©r+   Úio©rG   rH   r   r   r5   ‡   ó    ýþÿz merge_graphs.<locals>.<listcomp>c                 S   r(   )r   r   rN   r   r   r   r-   �   r.   zmerge_graphs.<locals>.<setcomp>c                 S   r(   r)   r   rN   r   r   r   r-   �   r.   c                 S   s   i | ]\}}||“qS r   r   )r+   Zout_nameZin_namer   r   r   Ú
<dictcomp>‘   s    z merge_graphs.<locals>.<dictcomp>c                 S   ó   h | ]}|j ’qS r   r1   ©r+   r&   r   r   r   r-   ’   r6   c                 S   rS   r   r1   ©r+   r%   r   r   r   r-   “   r6   c                 S   r0   r   r1   rU   r   r   r   r5   ˜   r6   c                 S   r0   r   r1   rU   r   r   r   r5   ™   r6   c                    ó   g | ]
}|j ˆ v r|j ‘qS r   r1   rU   ©Ú	input_setr   r   r5   œ   ó    c                    s&   g | ]}|j ˆ v s|j ˆv r|j ‘qS r   r1   rU   )rX   Úio_map_g2_insr   r   r5   �   ó
    þc                 S   r0   r   r1   rT   r   r   r   r5   ¤   r6   c                 S   r0   r   r1   rT   r   r   r   r5   ¥   r6   c                    s&   g | ]}|j ˆv s|j ˆ v r|j ‘qS r   r1   rT   )Úio_map_g1_outsÚ
output_setr   r   r5   ¨   r[   c                    rV   r   r1   rT   ©r]   r   r   r5   ­   rY   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 r|‘qS r   r1   rU   rW   r   r   r5   Ú   ó    c                    ra   r   r1   rU   rW   r   r   r5   Û   rb   c                    ó   g | ]	}|j ˆ vr|‘qS r   r1   rU   ©rZ   r   r   r5   Þ   rb   c                    ra   r   r1   rT   r^   r   r   r5   â   rb   c                    ra   r   r1   rT   r^   r   r   r5   ã   rb   c                    rc   r   r1   rT   )r\   r   r   r5   å   rb   c                    rc   r   r1   ©r+   Úinitrd   r   r   r5   ê   rb   c                    s   g | ]
}|j jˆ vr|‘qS r   r:   re   rd   r   r   r5   ï   s
    þc                    rc   r   r1   )r+   Úvird   r   r   r5   ÷   rb   NÚ_zGraph combining ú and z

)r?   r   r@   ÚCopyFromÚadd_prefix_graphr"   r    r   Úlenr   Z	Extractorr   Ú
make_modelZextract_modelr   rD   Újoinr   ÚextendÚrangeÚ	enumerater8   r>   r7   r2   rI   ) r   r   r   rE   rF   rG   rH   r2   rI   Zg1_copyZg2_copyZreversed_io_mapZg1_outsZg2_insZ	g1_inputsZ	g2_inputsZ
g1_outputsZ
g2_outputsÚe1Úe2Zg1_out_nameZ
g2_in_nameZoverlapping_namesÚcategoryÚnamesÚgZg2_nodes_beginZg2_nodes_endÚnode_idxr   ÚindexÚname_r   )rX   r\   rZ   r]   rG   rH   r   Úmerge_graphsP   sô   &

û
þþ$&

ÿÿþýüÿ



€þÿ
þÿ"
ÿþýüûúùÿ
rz   úonnx.compose.merge_modelsú1.0r   r*   Úm1Úm2Úproducer_nameÚproducer_versionÚdomainÚmodel_versionc              	      s6  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ƒ }|D ]$}|j|v rV||j }|j|krUtd| j› d|j› �ƒ‚q8|j||j< q8ˆ saˆr�ˆ rstƒ }| | ¡ |} t	| ˆ d�} ˆr…tƒ }| |¡ |}t	|ˆd�}‡ ‡fd	d
„|D ƒ}t
| j|j|||||d�}tj||	|
||||d�}i }| jD ]}|j||j< q­|jD ](}|j|v rÛ||j }||jkrÚtd|j› d|› d|j› d�ƒ‚q¹|j||j< q¹t ||¡ tdd„ | jD ƒdd„ |jD ƒ@ ƒ}|�rtdd |¡ ƒ‚|j | j¡ |j |j¡ t |¡ |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: ri   rJ   c                    rL   rM   r   rN   rP   r   r   r5   b  rQ   z merge_models.<locals>.<listcomp>)rE   rF   r2   rI   )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                 S   rS   r   r1   )r+   Úfr   r   r   r-   �  r6   zmerge_models.<locals>.<setcomp>zPCan't merge models with overlapping local function names. Found in both graphs: r_   )r?   r	   r@   r„   r   Úopset_importr�   Úversionrj   Ú
add_prefixrz   r   r   rm   Zmetadata_propsÚvalueÚkeyZset_model_propsÚ	functionsrn   Ú	MergeFromr   Úcheck_model)r}   r~   r   rE   rF   rG   rH   r2   rI   r   r€   r�   r‚   r„   Zopset_import_maprƒ   ÚentryZfound_versionZm1_copyZm2_copyr   ÚmodelZmodel_propsZ
meta_entryrŠ   Zfunction_overlapr   rP   r   Úmerge_models  s°   1ÿ


ÿÿÿÿ

ûù	ù




ÿÿÿÿÿÿÿÿ
r‘   TFr   rK   Úrename_nodesÚrename_edgesÚrename_inputsÚrename_outputsÚrename_initializersÚrename_value_infosÚinplaceÚname_mapc
                 C   sè  t | ƒtur
tdƒ‚|stƒ }
|
 | ¡ n| }
dtdtdtfdd„}|	du r(i }	|rJ|
jD ]}|jD ]	}|||ƒ|	|< q2|jD ]	}|||ƒ|	|< q?q-|r[|
jD ]}|||jƒ|	|j< qO|rl|
jD ]}|||jƒ|	|j< q`|r�|
jD ]}|||jƒ|_|j	D ]}|j
r‹t|j
|d|	d	� q}qq|rº|
jD ]}|||jƒ|	|j< q’|
jD ]}|||jjƒ|	|jj< |||jjƒ|	|jj< q¡|rË|
jD ]}|||jƒ|	|j< q¿|
jD ]2}t|jƒD ]\}}|j| |	v rç|	| |j|< qÕt|jƒD ]\}}|j| |	v rÿ|	| |j|< qíqÎ|
jD ]}|j|	v �r|	|j |_�q|
jD ]}|j|	v �r%|	|j |_�q|
jD ]}|j|	v �r8|	|j |_�q*|
jD ]!}|jj|	v �rN|	|jj |j_|jj|	v �r]|	|jj |j_�q=|
jD ]}|j|	v �rp|	|j |_�qb|
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 graphrK   r2   r   c                 S   s   t |ƒdkr
| | S |S )Nr   )rl   )rK   r2   r   r   r   Ú	_prefixedÈ  s   z#add_prefix_graph.<locals>._prefixedNT)r˜   r™   )r?   r   r@   rj   rA   r   r    r"   r2   Ú	attributerv   rk   r8   r>   r;   r=   r7   rq   )r   rK   r’   r“   r”   r•   r–   r—   r˜   r™   rv   r›   r$   r4   r�   rœ   rf   Zsparse_initr%   r"   Úinput_Úin_descÚout_descr8   r>   r7   r   r   r   rk   �  sœ   "


ÿ




ÿ€þ

ÿÿ

€€þ
€
€
€
€
€rk   r�   Úrename_functionsc
                 C   sÌ   t | ƒtur
tdƒ‚|	stƒ }
|
 | ¡ |
} t| j|||||||dd�	 |rdi }| jD ]}||j }|||j< ||_q+| jD ]}|jD ]}|j	|v rP||j	 |_	qCq>| jjD ]}|j	|v rc||j	 |_	qV| 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˜   )
r?   r	   r@   rj   rk   r   rŒ   r2   r   Úop_type)r�   rK   r’   r“   r”   r•   r–   r—   r    r˜   ÚmZ
f_name_mapr†   Z
new_f_namer$   r   r   r   r‰     sD   "
÷





€þ
€r‰   Údim_idxc                 C   s�  t | ƒtur
tdƒ‚|stƒ }| | ¡ n| }dd„ |jD ƒ}|jD ]2}t|jƒD ]\}}||v r;|d|› � |j|< q)t|jƒD ]\}}||v rS|d|› � |j|< qAq"|jd }	|j 	t
jdg |	g|	› d�t
j|	› d�tjd	g|gd
�d�¡ tt|jƒƒD ]E}
|j d¡}|jd|› � }|j 	t
jd||	g|jgd|j› �d�¡ dd„ |j jjjD ƒ}| |d	¡ |j 	t
 |j|j jj|¡¡ q€|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                 S   r0   r   r1   )r+   r"   r   r   r   r5   y  r6   z(expand_out_dim_graph.<locals>.<listcomp>Z_collapsed_dim_Z_expand_out_dim_idxÚConstantz	-constantz-valuer*   )r2   Ú	data_typeÚdimsÚvals)rE   rF   r2   rŠ   r   Ú	Unsqueezez
unsqueeze-)rE   rF   r2   c                 S   r0   r   )Ú	dim_value)r+   Údr   r   r   r5   ¢  r6   )r?   r   r@   rj   r"   r   rq   r    r2   r!   r   Z	make_nodeÚmake_tensorr
   ÚINT64rp   rl   ÚpopÚtensor_typeÚshapeÚdimÚinsertZmake_tensor_value_infoÚ	elem_type)r   r¤   r˜   rv   Zorig_out_namesr$   r%   ÚoutÚinpZexpand_dim_krh   r&   Úprev_outputÚ	new_shaper   r   r   Úexpand_out_dim_graph\  sj   
€€þ
ÿÿúûÿ
üÿÿÿr¸   c                 C   s@   t | ƒtur
tdƒ‚|stƒ }| | ¡ |} t| j|d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˜   )r?   r	   r@   rj   r¸   r   )r�   r¤   r˜   r£   r   r   r   Úexpand_out_dim¬  s   
ýr¹   r   )NNNNNN)
NNNNNNr{   r|   r   r*   )TTTTTTFN)TTTTTTTF)F)Útypingr   r   r   r   r   r   Úonnxr   r	   r
   r   r   r   rA   rD   rz   Úintr‘   Úboolrk   r‰   r¸   r¹   r   r   r   r   Ú<module>   sD    ÿÿÿÿ
þI÷ÿþý
ü
ûúùø	÷

ö @óÿþý
ü
ûúùø	÷
öõôó
ò öÿþýüûúùø	÷
ö
õxöÿþýüûúùø	÷
ö
õMýÿþý
üSýÿþýü