o
    òôjÂ   ã                   @  sh   d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	m
Z
mZ G dd„ dƒZ	dddd„ZdS )é    )ÚannotationsN)ÚFunctionProtoÚ
ModelProtoÚ	NodeProtoÚTensorProtoÚValueInfoProtoc                   @  sx   e Zd Zd/dd„Zedd„ ƒZd	d
„ Zd0dd„Zd0dd„Zd1dd„Z	d2dd„Z
dd„ Zd3d!d"„Zd4d*d+„Zd5d,d-„Zd.S )6Ú	ExtractorÚmodelr   ÚreturnÚNonec                 C  s<   t j |¡| _| jj| _|  | jj¡| _|  | jj¡| _	d S ©N)
ÚonnxÚshape_inferenceÚinfer_shapesr	   ÚgraphÚ_build_name2obj_dictÚinitializerÚwmapÚ
value_infoÚvimap)Úselfr	   © r   ú]/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/onnx/utils.pyÚ__init__   s   
zExtractor.__init__c                 C  s   dd„ | D ƒS )Nc                 S  s   i | ]}|j |“qS r   )Úname)Ú.0Úobjr   r   r   Ú
<dictcomp>   ó    z2Extractor._build_name2obj_dict.<locals>.<dictcomp>r   )Úobjsr   r   r   r      s   zExtractor._build_name2obj_dictc           
        s|   |   |¡}t|ƒ}t|ƒ}||@ }|| }g }|D ]	}	| ||	 ¡ q|D ]
}	| | j|	 ¡ q%|   |¡‰ ‡ fdd„|D ƒS )Nc                   s   g | ]}ˆ | ‘qS r   r   )r   r   ©Znew_io_tensors_mapr   r   Ú
<listcomp>)   r   z2Extractor._collect_new_io_core.<locals>.<listcomp>)r   ÚsetÚappendr   )
r   Zoriginal_ioZio_names_to_extractZoriginal_io_mapZoriginal_io_namesZs_io_names_to_extractZio_names_to_keepZnew_io_names_to_addZnew_io_tensorsr   r   r    r   Ú_collect_new_io_core   s   

zExtractor._collect_new_io_coreÚnamesú	list[str]úlist[ValueInfoProto]c                 C  ó   |   | jj|¡S r   )r$   r   Úinput©r   r%   r   r   r   Ú_collect_new_inputs+   ó   zExtractor._collect_new_inputsc                 C  r(   r   )r$   r   Úoutputr*   r   r   r   Ú_collect_new_outputs.   r,   zExtractor._collect_new_outputsÚnode_output_nameÚstrÚgraph_input_namesÚreachable_nodesúlist[NodeProto]c                 C  sX   ||v rd S | j jD ]}||jvrq
||v rq
| |¡ |jD ]	}|  |||¡ qq
d S r   )r   Únoder-   r#   r)   Ú_dfs_search_reachable_nodes)r   r/   r1   r2   r4   r   r   r   r   r5   1   s   


ÿÿùz%Extractor._dfs_search_reachable_nodesÚinput_namesÚoutput_namesc                   s6   g ‰ |D ]	}|   ||ˆ ¡ q‡ fdd„| jjD ƒ}|S )Nc                   s   g | ]}|ˆ v r|‘qS r   r   )r   Ún©r2   r   r   r!   N   s    z6Extractor._collect_reachable_nodes.<locals>.<listcomp>)r5   r   r4   )r   r6   r7   r   Únodesr   r9   r   Ú_collect_reachable_nodesE   s
   z"Extractor._collect_reachable_nodesc                   s0   ‡ fdd„}g }|||ƒ}|r|||ƒ}|s|S )Nc                   sP   g }| D ]!‰ t ‡ fdd„ˆjjD ƒd ƒ}|r%||vr%| |¡ | |j¡ q|S )Nc                 3  s,   � | ]}|j ˆ jkr|jˆ jkr|V  qd S r   )r   Úop_typeÚdomain)r   Úf©r4   r   r   Ú	<genexpr>^   s   € þýz[Extractor._collect_referred_local_functions.<locals>.find_referred_funcs.<locals>.<genexpr>)Únextr	   Ú	functionsr#   Úextendr4   )r:   Úreferred_local_functionsÚ	new_nodesZmatch_function©r   r?   r   Úfind_referred_funcsY   s   
þú
€zHExtractor._collect_referred_local_functions.<locals>.find_referred_funcsr   )r   r:   rG   rD   rE   r   rF   r   Ú!_collect_referred_local_functionsQ   s   

ÿz+Extractor._collect_referred_local_functionsr:   ú.tuple[list[TensorProto], list[ValueInfoProto]]c                   s¤   t ƒ ‰ |D ]}ˆ  |j¡ ˆ  |j¡ q‡ ‡fdd„ˆjD ƒ}‡ ‡fdd„ˆjD ƒ}tˆjjƒ}|dkr<t	d|› d�ƒ‚tˆjj
ƒ}|dkrNt	d|› d�ƒ‚||fS )Nc                   ó   g | ]}|ˆ v rˆj | ‘qS r   )r   ©r   Út©Zall_tensors_namesr   r   r   r!   |   ó    z8Extractor._collect_reachable_tensors.<locals>.<listcomp>c                   rJ   r   )r   rK   rM   r   r   r!   }   rN   r   zlen_sparse_initializer is z, it must be 0.zlen_quantization_annotation is )r"   Úupdater)   r-   r   r   Úlenr   Úsparse_initializerÚ
ValueErrorZquantization_annotation)r   r:   r4   r   r   Zlen_sparse_initializerZlen_quantization_annotationr   rM   r   Ú_collect_reachable_tensorsr   s"   
ÿ
ÿz$Extractor._collect_reachable_tensorsÚinputsÚoutputsr   úlist[TensorProto]r   Úlocal_functionsúlist[FunctionProto]c           
      C  sR   d| j j d }tjj||||||d�}| jj| jjd|dœ}	tjj|fi |	¤ŽS )NzExtracted from {Ú})r   r   zonnx.utils.extract_model)Ú
ir_versionÚopset_importsÚproducer_namerB   )	r   r   r   ÚhelperÚ
make_graphr	   rZ   Úopset_importÚ
make_model)
r   r:   rT   rU   r   r   rW   r   r   Úmetar   r   r   Ú_make_modelŠ   s   	ÿüzExtractor._make_modelc           
      C  sP   |   |¡}|  |¡}|  ||¡}|  |¡\}}|  |¡}|  ||||||¡}	|	S r   )r+   r.   r;   rS   rH   rb   )
r   r6   r7   rT   rU   r:   r   r   rW   r	   r   r   r   Úextract_model    s   


ÿzExtractor.extract_modelN)r	   r   r
   r   )r%   r&   r
   r'   )r/   r0   r1   r&   r2   r3   r
   r   )r6   r&   r7   r&   r
   r3   )r:   r3   r
   rI   )r:   r3   rT   r'   rU   r'   r   rV   r   r'   rW   rX   r
   r   )r6   r&   r7   r&   r
   r   )Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr   r$   r+   r.   r5   r;   rH   rS   rb   rc   r   r   r   r   r      s    






!
r   TÚ
input_pathústr | os.PathLikeÚoutput_pathr6   r&   r7   Úcheck_modelÚboolr
   r   c                 C  s€   t j | ¡std| › �ƒ‚|stdƒ‚|stdƒ‚tj | ¡ t | ¡}t|ƒ}| 	||¡}t 
||¡ |r>tj |¡ dS dS )a5  Extracts sub-model from an ONNX model.

    The sub-model is defined by the names of the input and output tensors *exactly*.

    Note: For control-flow operators, e.g. If and Loop, the _boundary of sub-model_,
    which is defined by the input and output tensors, should not _cut through_ the
    subgraph that is connected to the _main graph_ as attributes of these operators.

    Arguments:
        input_path (str | os.PathLike): The path to original ONNX model.
        output_path (str | os.PathLike): The path to save the extracted ONNX model.
        input_names (list of string): The names of the input tensors that to be extracted.
        output_names (list of string): The names of the output tensors that to be extracted.
        check_model (bool): Whether to run model checker on the extracted model.
    zInvalid input model path: z%Output model path shall not be empty!z'Output tensor names shall not be empty!N)ÚosÚpathÚexistsrR   r   Úcheckerrk   Úloadr   rc   Úsave)rh   rj   r6   r7   rk   r	   ÚeZ	extractedr   r   r   rc   ±   s   
ÿrc   )T)rh   ri   rj   ri   r6   r&   r7   r&   rk   rl   r
   r   )Ú
__future__r   rm   Zonnx.checkerr   Zonnx.helperÚonnx.shape_inferencer   r   r   r   r   r   rc   r   r   r   r   Ú<module>   s    )û