§
    1�Åj3  ã                  óª   — d dl mZ d dlZd dlZd dlmZ d dlmZ d dlZ	d dl
Z	d dlZ	erd dlmZmZmZmZmZ  G d„ d¦  «        Z	 	 ddd„Zdd„Zd d„ZdS )!é    )ÚannotationsN)Údeque)ÚTYPE_CHECKING)ÚFunctionProtoÚ
ModelProtoÚ	NodeProtoÚTensorProtoÚValueInfoProtoc                  ó~   — e Zd Zd'd„Zed(d„¦   «         Zed)d	„¦   «         Zd*d„Zd+d„Zd,d„Z	d-d„Z
d.d„Zd/d$„Zd0d%„Zd&S )1Ú	ExtractorÚmodelr   ÚreturnÚNonec                óà  — || _         | j         j        | _        |                      | j        j        ¦  «        | _        |                      | j        j        ¦  «        | _        | j                             |                      | j        j        ¦  «        ¦  «         | j                             |                      | j        j	        ¦  «        ¦  «         |  
                    | j        ¦  «        | _        d S ©N)r   ÚgraphÚ_build_name2obj_dictÚinitializerÚinitializersÚ
value_infoÚvalue_infosÚupdateÚinputÚoutputÚ_build_output_dictÚoutmap)Úselfr   s     ú;C:\Projects\Historical\venv\Lib\site-packages\onnx/utils.pyÚ__init__zExtractor.__init__   sÈ   € ØˆŒ
Ø”ZÔ%ˆŒ
Ø48×4MÒ4MØŒJÔ"ñ5
ô 5
ˆÔð 7;×6OÒ6OØŒJÔ!ñ7
ô 7
ˆÔð 	Ô×Ò × 9Ò 9¸$¼*Ô:JÑ KÔ KÑLÔLÐLØÔ×Ò × 9Ò 9¸$¼*Ô:KÑ LÔ LÑMÔMÐMØ&*×&=Ò&=¸d¼jÑ&IÔ&IˆŒˆˆó    Údictc                ó   — d„ | D ¦   «         S )Nc                ó   — i | ]
}|j         |“ŒS © )Úname)Ú.0Úobjs     r   ú
<dictcomp>z2Extractor._build_name2obj_dict.<locals>.<dictcomp>*   s   € Ð.Ð.Ð. #�”˜#Ð.Ð.Ð.r    r$   )Úobjss    r   r   zExtractor._build_name2obj_dict(   s   € à.Ð.¨Ð.Ñ.Ô.Ð.r    údict[str, int]c                óv   — i }t          | j        ¦  «        D ]!\  }}|j        D ]}|dk    rŒ	||vsJ ‚|||<   ŒŒ"|S )NÚ )Ú	enumerateÚnoder   )r   Úoutput_to_indexÚindexr.   Úoutput_names        r   r   zExtractor._build_output_dict,   sn   € à*,ˆÝ$ U¤ZÑ0Ô0ð 	5ð 	5‰KˆE�4Ø#œ{ð 5ð 5�Ø "Ò$Ð$ØØ"¨/Ð9Ð9Ð9Ð9Ø/4� Ñ,Ð,ð	5ð
 Ðr    Úio_names_to_extractú	list[str]úlist[ValueInfoProto]c                óŠ   ‡ — ˆ fd„|D ¦   «         }|r%t          dd                     |¦  «        › �¦  «        ‚ˆ fd„|D ¦   «         S )Nc                ó&   •— g | ]}|‰j         v¯|‘ŒS r$   ©r   ©r&   r%   r   s     €r   ú
<listcomp>z-Extractor._collect_new_io.<locals>.<listcomp>9   s-   ø€ ð 
ð 
ð 
Ø°DÀÔ@PÐ4PÐ4PˆDÐ4PÐ4PÐ4Pr    z3The following names were not found in value_infos: z, c                ó*   •— g | ]}‰j         |         ‘ŒS r$   r7   r8   s     €r   r9   z-Extractor._collect_new_io.<locals>.<listcomp>@   s!   ø€ ÐGÐGÐG¨4�Ô  Ô&ÐGÐGÐGr    )Ú
ValueErrorÚjoin)r   r2   Úmissing_namess   `  r   Ú_collect_new_iozExtractor._collect_new_io7   s}   ø€ ð
ð 
ð 
ð 
Ø0ð
ñ 
ô 
ˆð ð 	ÝØ`ÀdÇiÂiÐP]ÑF^ÔF^Ð`Ð`ñô ð ð HÐGÐGÐGÐ3FÐGÑGÔGÐGr    Únode_output_nameÚstrÚgraph_input_namesúset[str]Ú	reachableúset[int]c                óð   — |g}|rp|                      ¦   «         }||v rŒ|| j        v rJ| j        |         }||vr9|                     |¦  «         |d„ | j        j        |         j        D ¦   «         z  }|°ndS d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
            reachable (set of int): The set of indexes to reachable nodes in `nodes`
        c                ó   — g | ]
}|d k    ¯|‘ŒS )r,   r$   )r&   Ú
input_names     r   r9   z9Extractor._dfs_search_reachable_nodes.<locals>.<listcomp>[   s-   € ð ð ð à&Ø%¨Ò+Ð+ð #à+Ð+Ð+r    N)Úpopr   Úaddr   r.   r   )r   r?   rA   rC   ÚstackÚcurrent_output_namer0   s          r   Ú_dfs_search_reachable_nodesz%Extractor._dfs_search_reachable_nodesB   s¸   € ð "Ð"ˆØð 	Ø"'§)¢)¡+¤+Ðà"Ð&7Ð7Ð7Øà" d¤kÐ1Ð1ØœÐ$7Ô8�Ø 	Ð)Ð)à—M’M %Ñ(Ô(Ð(Øð ð à*.¬*¬/¸%Ô*@Ô*Fðñ ô ñ �Eð ð 	ð 	ð 	ð 	ð 	r    Úinput_namesÚoutput_namesúlist[NodeProto]c                ó¬   ‡ — t          |¦  «        }t          ¦   «         }|D ]}‰                      |||¦  «         Œˆ fd„t          |¦  «        D ¦   «         S )Nc                ó4   •— g | ]}‰j         j        |         ‘ŒS r$   )r   r.   )r&   r0   r   s     €r   r9   z6Extractor._collect_reachable_nodes.<locals>.<listcomp>k   s"   ø€ ÐFÐFÐF¨5�”
” Ô&ÐFÐFÐFr    )ÚsetrL   Úsorted)r   rM   rN   Ú_input_namesrC   r%   s   `     r   Ú_collect_reachable_nodesz"Extractor._collect_reachable_nodesa   si   ø€ õ
 ˜;Ñ'Ô'ˆÝ!™eœeˆ	Ø ð 	Lð 	LˆDØ×,Ò,¨T°<ÀÑKÔKÐKÐKàFÐFÐFÐFµF¸9Ñ4EÔ4EÐFÑFÔFÐFr    Únodesúlist[FunctionProto]c                ó\  — i }| j         j        D ]}|||j        |j        f<   Œg }t	          |¦  «        }|rv|                     ¦   «         }|j        |j        f|v rP|                     |j        |j        f¦  «        }|                     |¦  «         | 	                    |j
        ¦  «         |°v|S r   )r   Ú	functionsr%   Údomainr   ÚpopleftÚop_typerH   ÚappendÚextendr.   )r   rV   Úfunction_mapÚfunctionÚreferred_local_functionsÚqueuer.   s          r   Ú!_collect_referred_local_functionsz+Extractor._collect_referred_local_functionsm   sÆ   € ð >@ˆØœ
Ô,ð 	Fð 	FˆHØ=EˆL˜(œ-¨¬Ð9Ñ:Ð:Ø8:Ð Ý�e‘”ˆØð 	,Ø—=’=‘?”?ˆDà”˜dœkÐ*¨lÐ:Ð:Ø'×+Ò+¨T¬\¸4¼;Ð,GÑHÔH�Ø(×/Ò/°Ñ9Ô9Ð9Ø—’˜Xœ]Ñ+Ô+Ð+ð ð 	,ð (Ð'r    ú.tuple[list[TensorProto], list[ValueInfoProto]]c                ó´  ‡ ‡— t          ¦   «         Š|D ]6}‰                     |j        ¦  «         ‰                     |j        ¦  «         Œ7ˆˆ fd„‰ j        D ¦   «         }ˆˆ fd„‰ j        D ¦   «         }t          ‰ j        j        ¦  «        }|dk    rt          d|› d�¦  «        ‚t          ‰ j        j
        ¦  «        }|dk    rt          d|› d�¦  «        ‚||fS )Nc                ó2   •— g | ]}|‰v ¯‰j         |         ‘ŒS r$   )r   ©r&   ÚtÚall_tensors_namesr   s     €€r   r9   z8Extractor._collect_reachable_tensors.<locals>.<listcomp>Œ   s3   ø€ ð 
ð 
ð 
Ø%&¸qÐDUÐ?UÐ?UˆDÔ˜aÔ Ð?UÐ?UÐ?Ur    c                ó2   •— g | ]}|‰v ¯‰j         |         ‘ŒS r$   r7   rg   s     €€r   r9   z8Extractor._collect_reachable_tensors.<locals>.<listcomp>�   s3   ø€ ð 
ð 
ð 
Ø$%¸QÐBSÐ=SÐ=SˆDÔ˜QÔÐ=SÐ=SÐ=Sr    r   zlen_sparse_initializer is z, it must be 0.zlen_quantization_annotation is )rR   r   r   r   r   r   Úlenr   Úsparse_initializerr;   Úquantization_annotation)r   rV   r.   r   r   Úlen_sparse_initializerÚlen_quantization_annotationri   s   `      @r   Ú_collect_reachable_tensorsz$Extractor._collect_reachable_tensors„   s=  øø€ õ '*¡e¤eÐØð 	2ð 	2ˆDØ×$Ò$ T¤ZÑ0Ô0Ð0Ø×$Ò$ T¤[Ñ1Ô1Ð1Ð1ð
ð 
ð 
ð 
ð 
Ø*.Ô*;ð
ñ 
ô 
ˆð
ð 
ð 
ð 
ð 
Ø)-Ô)9ð
ñ 
ô 
ˆ
õ "% T¤ZÔ%BÑ!CÔ!CÐØ! QÒ&Ð&ÝØTÐ-CÐTÐTÐTñô ð õ '*¨$¬*Ô*LÑ&MÔ&MÐ#Ø&¨!Ò+Ð+ÝØ^Ð2MÐ^Ð^Ð^ñô ð ð ˜JÐ&Ð&r    ÚinputsÚoutputsr   úlist[TensorProto]r   Úlocal_functionsc                óÔ   — d| j         j        z   dz   }t          j                             ||||||¬¦  «        }| 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_namerY   )	r   r%   ÚonnxÚhelperÚ
make_graphr   rw   Úopset_importÚ
make_model)
r   rV   rq   rr   r   r   rt   r%   r   Úmetas
             r   Ú_make_modelzExtractor._make_modelž   s�   € ð " D¤J¤OÑ3°cÑ9ˆÝ”×&Ò&Ø�4˜ °kÈjð 'ñ 
ô 
ˆð œ*Ô/Ø!œZÔ4Ø7Ø(ð	
ð 
ˆõ Œ{Ô% eÐ4Ð4¨tÐ4Ð4Ð4r    c                ó  — |                       |¦  «        }|                       |¦  «        }|                      ||¦  «        }|                      |¦  «        \  }}|                      |¦  «        }|                      ||||||¦  «        S r   )r>   rU   rp   rc   r€   )	r   rM   rN   rq   rr   rV   r   r   rt   s	            r   Úextract_modelzExtractor.extract_model³   s�   € ð
 ×%Ò% kÑ2Ô2ˆØ×&Ò& |Ñ4Ô4ˆØ×-Ò-¨k¸<ÑHÔHˆØ"&×"AÒ"AÀ%Ñ"HÔ"HÑˆ�ZØ×@Ò@ÀÑGÔGˆØ×ÒØ�6˜7 K°¸_ñ
ô 
ð 	
r    N)r   r   r   r   )r   r!   )r   r*   )r2   r3   r   r4   )r?   r@   rA   rB   rC   rD   r   r   )rM   r3   rN   r3   r   rO   )rV   rO   r   rW   )rV   rO   r   rd   )rV   rO   rq   r4   rr   r4   r   rs   r   r4   rt   rW   r   r   )rM   r3   rN   r3   r   r   )Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr   r   r>   rL   rU   rc   rp   r€   r‚   r$   r    r   r   r      s   € € € € € ðJð Jð Jð Jð ð/ð /ð /ñ „\ð/ð ðð ð ñ „\ðð	Hð 	Hð 	Hð 	Hðð ð ð ð>
Gð 
Gð 
Gð 
Gð(ð (ð (ð (ð.'ð 'ð 'ð 'ð45ð 5ð 5ð 5ð*
ð 
ð 
ð 
ð 
ð 
r    r   TÚ
input_pathústr | os.PathLikeÚoutput_pathrM   r3   rN   Úcheck_modelÚboolÚinfer_shapesr   r   c                óP  — t           j                             | ¦  «        st          d| › �¦  «        ‚|st          d¦  «        ‚|st          d¦  «        ‚|st          d¦  «        ‚t	          |¦  «        t	          t          |¦  «        ¦  «        k    rt          d¦  «        ‚t	          |¦  «        t	          t          |¦  «        ¦  «        k    rt          d¦  «        ‚|rt          j                             | ¦  «         |rgt           j         	                    | ¦  «        t          j        j
        k    r5t          j                             | |¦  «         t          j        |¦  «        }n€|rjt          j        | d¬¦  «        }t          j                             |¦  «        }t           j                             | ¦  «        }t          j        ||¦  «         nt          j        | ¦  «        }t#          |¦  «        }|                     ||¦  «        }	|	                     ¦   «         t          j        j
        k    r;t           j                             |¦  «        d	z   }
t          j        |	|d
|
¬¦  «         nt          j        |	|¦  «         |r!t          j                             |¦  «         dS dS )a  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.

    Note: When the extracted model size is larger than 2GB, the extra data will be saved in "output_path.data".

    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 original model and the extracted model.
        infer_shapes (bool): Whether to infer the shapes of the original model.
    zInvalid input model path: z%Output model path shall not be empty!z&Input tensor names shall not be empty!z'Output tensor names shall not be empty!z0Duplicate names found in the input tensor names.z1Duplicate names found in the output tensor names.F)Úload_external_dataz.dataT)Úsave_as_external_dataÚlocationN)ÚosÚpathÚexistsr;   rk   rR   rz   ÚcheckerrŠ   ÚgetsizeÚMAXIMUM_PROTOBUFÚshape_inferenceÚinfer_shapes_pathÚloadrŒ   ÚdirnameÚload_external_data_for_modelr   r‚   ÚByteSizeÚbasenameÚsave)r‡   r‰   rM   rN   rŠ   rŒ   r   Úbase_dirÚeÚ	extractedr�   s              r   r‚   r‚   Â   sg  € õ4 Œ7�>Š>˜*Ñ%Ô%ð DÝÐB°jÐBÐBÑCÔCÐCØð BÝÐ@ÑAÔAÐAØð CÝÐAÑBÔBÐBØð DÝÐBÑCÔCÐCå
ˆ;ÑÔ�3�s ;Ñ/Ô/Ñ0Ô0Ò0Ð0ÝÐKÑLÔLÐLÝ
ˆ<ÑÔ�C¥ LÑ 1Ô 1Ñ2Ô2Ò2Ð2ÝÐLÑMÔMÐMàð -ÝŒ× Ò  Ñ,Ô,Ð,àð 	&�œŸš¨
Ñ3Ô3µd´lÔ6SÒSÐSÝÔ×.Ò.¨z¸;ÑGÔGÐGÝ”	˜+Ñ&Ô&ˆˆØ	ð &Ý”	˜*¸Ð?Ñ?Ô?ˆÝÔ$×1Ò1°%Ñ8Ô8ˆÝ”7—?’? :Ñ.Ô.ˆÝÔ)¨%°Ñ:Ô:Ð:Ð:å”	˜*Ñ%Ô%ˆå�%ÑÔ€AØ—’ ¨\Ñ:Ô:€Ià×ÒÑÔ�dœlÔ;Ò;Ð;Ý”7×#Ò# KÑ0Ô0°7Ñ:ˆÝŒ	�)˜[ÀÈxÐXÑXÔXÐXÐXåŒ	�)˜[Ñ)Ô)Ð)àð .ÝŒ× Ò  Ñ-Ô-Ð-Ð-Ð-ð.ð .r    Útarútarfile.TarFileÚbaseúlist[tarfile.TarInfo]c                ó  — g }t           j                             |¦  «        }| D ]ä}t           j                             ||j        ¦  «        }t           j                             |¦  «        }	 t           j                             ||g¦  «        |k    }n# t          $ r d}Y nw xY w|st          d|› d�¦  «        ‚|                     ¦   «         s| 	                    ¦   «         rt          d|› d�¦  «        ‚| 
                    |¦  «         Œå|S )zÖCheck 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
    Fz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.)r‘   r’   Úabspathr<   r%   Ú
commonpathr;   ÚRuntimeErrorÚissymÚislnkr]   )r¢   r¤   ÚresultÚabs_baseÚmemberÚmember_pathÚ
abs_memberÚis_within_bases           r   Ú_tar_members_filterr²     s<  € ð €FÝŒw�Š˜tÑ$Ô$€HØð ð ˆÝ”g—l’l 4¨¬Ñ5Ô5ˆÝ”W—_’_ [Ñ1Ô1ˆ
ð	#ÝœW×/Ò/°¸:Ð0FÑGÔGÈ8ÒSˆNˆNøÝð 	#ð 	#ð 	#Ø"ˆNˆNˆNð	#øøøàð 	ÝðS kð Sð Sð Sñô ð ð �<Š<‰>Œ>ð 	˜VŸ\š\™^œ^ð 	ÝðE kð Eð Eð Eñô ð ð 	�Š�fÑÔÐÐØ€Ms   Á+%BÂB ÂB Úmodel_tar_pathÚlocal_model_with_data_dir_pathc                ó  — t          j        | ¦  «        5 }t          t           d¦  «        r|                     |d¬¦  «         n%|                     |t	          ||¦  «        ¬¦  «         ddd¦  «         dS # 1 swxY w Y   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.
    Údata_filterÚdata)r’   Úfilter)r’   ÚmembersN)ÚtarfileÚopenÚhasattrÚ
extractallr²   )r³   r´   Úmodel_with_data_zippeds      r   Ú_extract_model_safer¿   (  sä   € õ  
Œ�nÑ	%Ô	%ð Ð)?å•7˜MÑ*Ô*ð 
	Ø"×-Ò-Ø3¸Fð .ñ ô ð ð ð #×-Ò-Ø3Ý+Ø*Ð,Jñô ð .ñ ô ð ðð ð ñ ô ð ð ð ð ð ð ð øøøð ð ð ð ð ð s   •AA5Á5A9Á<A9)TT)r‡   rˆ   r‰   rˆ   rM   r3   rN   r3   rŠ   r‹   rŒ   r‹   r   r   )r¢   r£   r¤   rˆ   r   r¥   )r³   rˆ   r´   rˆ   r   r   )Ú
__future__r   r‘   rº   Úcollectionsr   Útypingr   Úonnx.checkerrz   Úonnx.helperÚonnx.shape_inferenceÚonnx.onnx_pbr   r   r   r	   r
   r   r‚   r²   r¿   r$   r    r   ú<module>rÇ      sL  ðð #Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø €€€Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  à Ð Ð Ð Ø Ð Ð Ð Ø Ð Ð Ð àð ðð ð ð ð ð ð ð ð ð ð ð ð ð ðf
ð f
ð f
ð f
ð f
ñ f
ô f
ð f
ð\ Øð@.ð @.ð @.ð @.ð @.ðF ð  ð  ð  ðFð ð ð ð ð r    