§
    ù¸¸j:1  ã                  ó¶   — 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	d dl	mZmZmZmZmZ erd dl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ÚValueInfoProto)ÚRepeatedCompositeFieldContainerc                  óŽ   — e Zd Zd,d„Zed-d„¦   «         Zed.d	„¦   «         Zd/d„Zd0d„Zd0d„Z	d1d„Z
d2d„Zd3d „Zd4d"„Zd5d)„Zd6d*„Zd+S )7Ú	ExtractorÚmodelr   ÚreturnÚNonec                ó  — || _         | j         j        | _        |                      | j        j        ¦  «        | _        |                      | j        j        ¦  «        | _        |                      | j        ¦  «        | _        d S ©N)	r   ÚgraphÚ_build_name2obj_dictÚinitializerÚwmapÚ
value_infoÚvimapÚ_build_output_dictÚoutmap)Úselfr   s     úZ/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/onnx/utils.pyÚ__init__zExtractor.__init__   sd   € ØˆŒ
Ø”ZÔ%ˆŒ
Ø×-Ò-¨d¬jÔ.DÑEÔEˆŒ	Ø×.Ò.¨t¬zÔ/DÑEÔEˆŒ
Ø×-Ò-¨d¬jÑ9Ô9ˆŒˆˆó    Ú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ÚnodeÚoutput)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   Úoriginal_ioú/RepeatedCompositeFieldContainer[ValueInfoProto]Úio_names_to_extractú	list[str]úlist[ValueInfoProto]c                ó¾   — |                       |¦  «        }g }|D ]B}||v r|                     ||         ¦  «         Œ"|                     | j        |         ¦  «         ŒC|S r   )r   Úappendr   )r   r1   r3   Úoriginal_io_mapÚnew_io_tensorsÚio_name_to_extracts         r   Ú_collect_new_io_corezExtractor._collect_new_io_core+   s{   € ð
 ×3Ò3°KÑ@Ô@ˆØˆØ"5ð 	Fð 	FÐØ! _Ð4Ð4Ø×%Ò% oÐ6HÔ&IÑJÔJÐJÐJà×%Ò% d¤jÐ1CÔ&DÑEÔEÐEÐEØÐr   Únamesc                óB   — |                       | j        j        |¦  «        S r   )r;   r   Úinput©r   r<   s     r   Ú_collect_new_inputszExtractor._collect_new_inputs9   s   € Ø×(Ò(¨¬Ô)9¸5ÑAÔAÐAr   c                óB   — |                       | j        j        |¦  «        S r   )r;   r   r-   r?   s     r   Ú_collect_new_outputszExtractor._collect_new_outputs<   s   € Ø×(Ò(¨¬Ô):¸EÑBÔBÐBr   Ú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   ú
<listcomp>z9Extractor._dfs_search_reachable_nodes.<locals>.<listcomp>X   s-   € ð ð ð à&Ø%¨Ò+Ð+ð #à+Ð+Ð+r   N)Úpopr   Úaddr   r,   r>   )r   rC   rE   rG   ÚstackÚcurrent_output_namer/   s          r   Ú_dfs_search_reachable_nodesz%Extractor._dfs_search_reachable_nodes?   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$   r/   r   s     €r   rL   z6Extractor._collect_reachable_nodes.<locals>.<listcomp>h   s"   ø€ ÐFÐFÐF¨5�”
” Ô&ÐFÐFÐFr   )ÚsetrQ   Úsorted)r   rR   rS   Ú_input_namesrG   r#   s   `     r   Ú_collect_reachable_nodesz"Extractor._collect_reachable_nodes^   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_typerM   r7   Úextendr,   )r   r[   Úfunction_mapÚfunctionÚreferred_local_functionsÚqueuer,   s          r   Ú!_collect_referred_local_functionsz+Extractor._collect_referred_local_functionsj   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   rL   z8Extractor._collect_reachable_tensors.<locals>.<listcomp>‰   s*   ø€ ÐQÐQÐQ¨¸!Ð?PÐ:PÐ:P�t”y ”|Ð:PÐ:PÐ:Pr   c                ó2   •— g | ]}|‰v ¯‰j         |         ‘ŒS r"   )r   rk   s     €€r   rL   z8Extractor._collect_reachable_tensors.<locals>.<listcomp>Š   s*   ø€ ÐRÐRÐR¨¸1Ð@QÐ;QÐ;Q�d”j ”mÐ;QÐ;QÐ;Qr   r   zlen_sparse_initializer is z, it must be 0.zlen_quantization_annotation is )rW   Úupdater>   r-   r   r   Úlenr   Úsparse_initializerÚ
ValueErrorÚquantization_annotation)r   r[   r,   r   r   Úlen_sparse_initializerÚlen_quantization_annotationrm   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ØQÐQÐQÐQÐQ¨T¬YÐQÑQÔQˆØRÐRÐRÐRÐR¨T¬ZÐRÑRÔRˆ
Ý!$ 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_namer^   )	r   r#   ÚonnxÚhelperÚ
make_graphr   r}   Úopset_importÚ
make_model)
r   r[   rw   rx   r   r   rz   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@   rB   rZ   rv   rg   r†   )
r   rR   rS   rw   rx   r[   r   r   rz   r   s
             r   Úextract_modelzExtractor.extract_model¬   s“   € ð
 ×)Ò)¨+Ñ6Ô6ˆØ×+Ò+¨LÑ9Ô9ˆØ×-Ò-¨k¸<ÑHÔHˆØ"&×"AÒ"AÀ%Ñ"HÔ"HÑˆ�ZØ×@Ò@ÀÑGÔGˆØ× Ò Ø�6˜7 K°¸_ñ
ô 
ˆð ˆr   N)r   r   r   r   )r   r   )r   r(   )r1   r2   r3   r4   r   r5   )r<   r4   r   r5   )rC   rD   rE   rF   rG   rH   r   r   )rR   r4   rS   r4   r   rT   )r[   rT   r   r\   )r[   rT   r   rh   )r[   rT   rw   r5   rx   r5   r   ry   r   r5   rz   r\   r   r   )rR   r4   rS   r4   r   r   )Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr   r   r;   r@   rB   rQ   rZ   rg   rv   r†   rˆ   r"   r   r   r   r      s(  € € € € € ð:ð :ð :ð :ð ð/ð /ð /ñ „\ð/ð ðð ð ñ „\ððð ð ð ðBð Bð Bð BðCð Cð Cð Cðð ð ð ð>
Gð 
Gð 
Gð 
Gð(ð (ð (ð (ð.'ð 'ð 'ð 'ð,5ð 5ð 5ð 5ð*ð ð ð ð ð r   r   TÚ
input_pathústr | os.PathLikeÚoutput_pathrR   r4   rS   Ú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Úexistsrr   rp   rW   r€   Úcheckerr�   ÚgetsizeÚMAXIMUM_PROTOBUFÚshape_inferenceÚinfer_shapes_pathÚloadr’   ÚdirnameÚload_external_data_for_modelr   rˆ   ÚByteSizeÚbasenameÚsave)r�   r�   rR   rS   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 }| D ]Ý}t           j                             ||j        ¦  «        }t           j                             |¦  «        }t           j                             |¦  «        }|                     |¦  «        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
    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.)
r—   r˜   Újoinr#   ÚabspathÚ
startswithÚRuntimeErrorÚissymÚislnkr7   )r¨   rª   ÚresultÚmemberÚmember_pathÚabs_baseÚ
abs_members          r   Ú_tar_members_filterr¸   ÿ   s   € ð €FØð ð ˆÝ”g—l’l 4¨¬Ñ5Ô5ˆÝ”7—?’? 4Ñ(Ô(ˆÝ”W—_’_ [Ñ1Ô1ˆ
Ø×$Ò$ XÑ.Ô.ð 	ÝðS kð Sð Sð Sñô ð ð �<Š<‰>Œ>ð 	˜VŸ\š\™^œ^ð 	ÝðE kð Eð Eð Eñô ð ð 	�Š�fÑÔÐÐØ€Mr   Ú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Ž   rR   r4   rS   r4   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.checkerr€   Úonnx.helperÚonnx.shape_inferencer   r   r   r	   r
   Ú#google.protobuf.internal.containersr   r   rˆ   r¸   rÅ   r"   r   r   ú<module>rÍ      sB  ðð #Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø €€€Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  à Ð Ð Ð Ø Ð Ð Ð Ø Ð Ð Ð Ø RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ Ràð TØSÐSÐSÐSÐSÐSðeð eð eð eð eñ eô eð eðZ Øð@.ð @.ð @.ð @.ð @.ðFð ð ð ð>ð ð ð ð ð r   