U
    qhR-                     @  s   d dl mZ d dl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dd	d
dddZddddddZddd
dddZdS )    )annotationsN)FunctionProto
ModelProto	NodeProtoTensorProtoValueInfoProtoc                   @  s   e Zd ZdddddZedd Zdd	 Zd
ddddZd
ddddZdddddddddZ	d
d
ddddZ
dd Zddddd Zdddd!dd"dd#d$d%Zd
d
ddd&d'Zd(S ))	Extractorr   None)modelreturnc                 C  s<   t j|| _| jj| _| | jj| _| | jj| _	d S N)
onnxZshape_inferenceZinfer_shapesr
   graph_build_name2obj_dictinitializerwmap
value_infovimap)selfr
    r   ./tmp/pip-unpacked-wheel-xnis5xre/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>   s      z2Extractor._build_name2obj_dict.<locals>.<dictcomp>r   )objsr   r   r   r      s    zExtractor._build_name2obj_dictc                   sp    |t}t|}||@ }|| }fdd|D }|fdd|D   |  fdd|D S )Nc                   s   g | ]} | qS r   r   r   r   )original_io_mapr   r   
<listcomp>!   s     z2Extractor._collect_new_io_core.<locals>.<listcomp>c                 3  s   | ]} j | V  qd S r   r   r   r   r   r   	<genexpr>#   s     z1Extractor._collect_new_io_core.<locals>.<genexpr>c                   s   g | ]} | qS r   r   r   )new_io_tensors_mapr   r   r   '   s     )r   setextend)r   Zoriginal_ioZio_names_to_extractZoriginal_io_namesZs_io_names_to_extractZio_names_to_keepZnew_io_names_to_addZnew_io_tensorsr   )r#   r   r   r   _collect_new_io_core   s    

zExtractor._collect_new_io_core	list[str]zlist[ValueInfoProto])namesr   c                 C  s   |  | jj|S r   )r&   r   inputr   r(   r   r   r   _collect_new_inputs)   s    zExtractor._collect_new_inputsc                 C  s   |  | jj|S r   )r&   r   outputr*   r   r   r   _collect_new_outputs,   s    zExtractor._collect_new_outputsstrzset[str]zlist[NodeProto]zset[int])node_output_namegraph_input_namesnodes	reachableunreachabler   c           	   	     sn    |krdS  fdd|D }|D ]}| | || q$|D ]&}| jD ]}| |||| qPqBdS )a  Helper function to find nodes which are connected to an output

        Arguments:
            node_output_name (str): The name of the output
            graph_input_names (set of string): The names of all inputs of the graph
            nodes (list of nodes): The list of all nodes of the graph
            reachable (set of int): The set of indexes to reachable nodes in `nodes`
            unreachable (set of int): The set of indexes to unreachable nodes in `nodes`
        Nc                   s   g | ]} | j kr|qS r   )r,   )r   indexr/   r1   r   r   r   E   s     z9Extractor._dfs_search_reachable_nodes.<locals>.<listcomp>)addremover)   _dfs_search_reachable_nodes)	r   r/   r0   r1   r2   r3   Znodes_to_search
node_indexr   r   r5   r   r8   /   s"    
    z%Extractor._dfs_search_reachable_nodes)input_namesoutput_namesr   c                   s`   t |}t| jj t  }t tt }|D ]}| || || q. fddt|D   S )Nc                   s   g | ]} | qS r   r   )r   r9   r1   r   r   r   c   s     z6Extractor._collect_reachable_nodes.<locals>.<listcomp>)r$   listr   noderangelenr8   sorted)r   r:   r;   Z_input_namesr2   r3   r   r   r<   r   _collect_reachable_nodesU   s        z"Extractor._collect_reachable_nodesc                   s.    fdd}g }|||}|r*|||}q|S )Nc                   sP   g }| D ]B t  fddjjD d }|r||kr|| ||j q|S )Nc                 3  s*   | ]"}|j  jkr|j jkr|V  qd S r   )r   Zop_typedomain)r   fr>   r   r   r"   s   s    z[Extractor._collect_referred_local_functions.<locals>.find_referred_funcs.<locals>.<genexpr>)nextr
   	functionsappendr%   r>   )r1   referred_local_functions	new_nodesZmatch_functionr!   rE   r   find_referred_funcsn   s    

zHExtractor._collect_referred_local_functions.<locals>.find_referred_funcsr   )r   r1   rK   rI   rJ   r   r!   r   !_collect_referred_local_functionsf   s    
z+Extractor._collect_referred_local_functionsz.tuple[list[TensorProto], list[ValueInfoProto]])r1   r   c                   s   t   |D ]} |j  |j q
 fddjD } fddjD }tjj}|dkrxt	d| dtjj
}|dkrt	d| d||fS )Nc                   s   g | ]}| krj | qS r   )r   r   tZall_tensors_namesr   r   r   r      s      z8Extractor._collect_reachable_tensors.<locals>.<listcomp>c                   s   g | ]}| krj | qS r   r    rM   rO   r   r   r      s      r   zlen_sparse_initializer is z, it must be 0.zlen_quantization_annotation is )r$   updater)   r,   r   r   r@   r   Zsparse_initializer
ValueErrorZquantization_annotation)r   r1   r>   r   r   Zlen_sparse_initializerZlen_quantization_annotationr   rO   r   _collect_reachable_tensors   s"    

z$Extractor._collect_reachable_tensorszlist[TensorProto]zlist[FunctionProto])r1   inputsoutputsr   r   local_functionsr   c           
      C  sN   d| j j d }tjj||||||d}| jj| jjd|d}	tjj|f|	S )NzExtracted from {})r   r   zonnx.utils.extract_model)
ir_versionZopset_importsZproducer_namerG   )	r   r   r   helperZ
make_graphr
   rW   Zopset_importZ
make_model)
r   r1   rS   rT   r   r   rU   r   r   metar   r   r   _make_model   s    	     zExtractor._make_modelc           
      C  sP   |  |}| |}| ||}| |\}}| |}| ||||||}	|	S r   )r+   r-   rB   rR   rL   rZ   )
r   r:   r;   rS   rT   r1   r   r   rU   r
   r   r   r   extract_model   s    


     zExtractor.extract_modelN)__name__
__module____qualname__r   staticmethodr   r&   r+   r-   r8   rB   rL   rR   rZ   r[   r   r   r   r   r      s   
&!r   Tzstr | os.PathLiker'   boolr	   )
input_pathoutput_pathr:   r;   check_modelr   c                 C  s|   t j| std|  |s&td|s2tdtj|  t| }t|}|	||}t
|| |rxtj| 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existsrQ   r   checkerrc   loadr   r[   save)ra   rb   r:   r;   rc   r
   eZ	extractedr   r   r   r[      s    
r[   ztarfile.TarFilezlist[tarfile.TarInfo])tarbaser   c                 C  s   g }| D ]r}t j||j}t j|}t j|}||sPtd| dn | s`| rptd| d|	| q|S )zCheck that the content of ``tar`` will be extracted safely

    Args:
        tar: The tarball file
        base: The directory where the tarball will be extracted

    Returns:
        list of tarball members
    zThe tarball member z^ in downloading model contains directory traversal sequence which may contain harmful payload.zP in downloading model contains symbolic links which may contain harmful payload.)
rd   re   joinr   abspath
startswithRuntimeErrorissymislnkrH   )rk   rl   resultmemberZmember_pathabs_baseZ
abs_memberr   r   r   _tar_members_filter   s    


rv   )model_tar_pathlocal_model_with_data_dir_pathr   c              	   C  sH   t | 4}tt dr&|j|dd n|j|t||d W 5 Q R X dS )aQ  Safely extracts a tar file to a specified directory.

    This function ensures that the extraction process mitigates against
    directory traversal vulnerabilities by validating or sanitizing paths
    within the tar file. It also provides compatibility for different versions
    of the tarfile module by checking for the availability of certain attributes
    or methods before invoking them.

    Args:
        model_tar_path: The path to the tar file to be extracted.
        local_model_with_data_dir_path: The directory path where the tar file
      contents will be extracted to.
    Zdata_filterdata)re   filter)re   membersN)tarfileopenhasattr
extractallrv   )rw   rx   Zmodel_with_data_zippedr   r   r   _extract_model_safe  s    
  r   )T)
__future__r   rd   r|   Zonnx.checkerr   Zonnx.helperZonnx.shape_inferencer   r   r   r   r   r   r[   rv   r   r   r   r   r   <module>   s    = (