
    h                         S SK Jr  S SKrS SKrS SKrS SKrS SKJrJrJ	r	J
r
Jr   " S S5      r S           SS jjrg)	    )annotationsN)FunctionProto
ModelProto	NodeProtoTensorProtoValueInfoProtoc                      \ rS rSrSS jr\S 5       rS rSS jrSS jr	        SS jr
      SS jrS	 r    SS
 jr              SS jr      SS jrSrg)	Extractor   c                *   [         R                  R                  U5      U l        U R                  R                  U l        U R                  U R                  R                  5      U l        U R                  U R                  R                  5      U l	        g N)
onnxshape_inferenceinfer_shapesmodelgraph_build_name2obj_dictinitializerwmap
value_infovimap)selfr   s     D/var/www/fran/franai/venv/lib/python3.13/site-packages/onnx/utils.py__init__Extractor.__init__   sc    ))66u=
ZZ%%
--djj.D.DE	..tzz/D/DE
    c                F    U  Vs0 s H  oR                   U_M     sn$ s  snf r   )name)objsobjs     r   r   Extractor._build_name2obj_dict   s     )-.##...s   c                @   U R                  U5      n[        U5      n[        U5      nXT-  nXT-
  n/ nU H  n	UR                  X9   5        M     U H!  n	UR                  U R                  U	   5        M#     U R                  U5      n
U V	s/ s H  oU	   PM	     sn	$ s  sn	f r   )r   setappendr   )r   original_ioio_names_to_extractoriginal_io_maporiginal_io_namess_io_names_to_extractio_names_to_keepnew_io_names_to_addnew_io_tensorsr   new_io_tensors_maps              r   _collect_new_io_coreExtractor._collect_new_io_core   s    33K@0 #$7 80D3G$D!!/"78 %'D!!$**T"23 (
 "66~F5HI5HT4(5HIIIs   
Bc                N    U R                  U R                  R                  U5      $ r   )r.   r   inputr   namess     r   _collect_new_inputsExtractor._collect_new_inputs+   s    (()9)95AAr   c                N    U R                  U R                  R                  U5      $ r   )r.   r   outputr2   s     r   _collect_new_outputsExtractor._collect_new_outputs.   s    (():):EBBr   c                    X;   a  g U R                   R                   HQ  nXR                  ;  a  M  XC;   a  M  UR                  U5        UR                   H  nU R                  XRU5        M     MS     g r   )r   noder7   r$   r1   _dfs_search_reachable_nodes)r   node_output_namegraph_input_namesreachable_nodesr;   r   s         r   r<   %Extractor._dfs_search_reachable_nodes1   sd     0JJOOD{{2&""4(

00_ # $r   c                    / nU H  nU R                  XAU5        M     U R                  R                   Vs/ s H  oUU;   d  M
  UPM     nnU$ s  snf r   )r<   r   r;   )r   input_namesoutput_namesr?   r   nnodess          r   _collect_reachable_nodes"Extractor._collect_reachable_nodesE   sQ    
  D,,TP ! !JJOODOqO/COD Es   	AAc                X   ^  U 4S jn/ nU" X5      nU(       a  U" XC5      nU(       a  M  U$ )Nc                   >^ / nU  Hi  m[        U4S jTR                  R                   5       S 5      nU(       d  M6  X1;  d  M=  UR                  U5        UR	                  UR
                  5        Mk     U$ )Nc              3     >#    U  H?  nUR                   TR                  :X  d  M  UR                  TR                  :X  d  M;  Uv   MA     g 7fr   )r   op_typedomain).0fr;   s     r   	<genexpr>[Extractor._collect_referred_local_functions.<locals>.find_referred_funcs.<locals>.<genexpr>^   s?      !5A66T\\1 67hh$++6M !5s   A
A
	A
)nextr   	functionsr$   extendr;   )rE   referred_local_functions	new_nodesmatch_functionr;   r   s       @r   find_referred_funcsHExtractor._collect_referred_local_functions.<locals>.find_referred_funcsY   sn    I!%!%!5!5
 " ">n&T,33NC$$^%8%89  r    )r   rE   rW   rT   rU   s   `    r   !_collect_referred_local_functions+Extractor._collect_referred_local_functionsQ   s6    	$ $& 'H	+IPI i ('r   c                >   [        5       nU H9  nUR                  UR                  5        UR                  UR                  5        M;     U R                   Vs/ s H  oDU;   d  M
  U R                  U   PM     nnU R
                   Vs/ s H  oDU;   d  M
  U R
                  U   PM     nn[        U R                  R                  5      nUS:w  a  [        SU S35      e[        U R                  R                  5      nUS:w  a  [        SU S35      eXV4$ s  snf s  snf )Nr   zlen_sparse_initializer is z, it must be 0.zlen_quantization_annotation is )r#   updater1   r7   r   r   lenr   sparse_initializer
ValueErrorquantization_annotation)	r   rE   all_tensors_namesr;   tr   r   len_sparse_initializerlen_quantization_annotations	            r   _collect_reachable_tensors$Extractor._collect_reachable_tensorsr   s    '*eD$$TZZ0$$T[[1  .2YYQY?P:P|tyy|YQ-1ZZRZ@Q;QmdjjmZ
R!$TZZ%B%B!C!Q&,-C,DOT  '*$***L*L&M#&!+12M1No^  && RRs   	D%D	DDc           	        SU R                   R                  -   S-   n[        R                  R	                  XX#XES9nU R
                  R                  U R
                  R                  SUS.n	[        R                  R                  " U40 U	D6$ )NzExtracted from {})r   r   zonnx.utils.extract_model)
ir_versionopset_importsproducer_namerR   )	r   r   r   helper
make_graphr   rj   opset_import
make_model)
r   rE   inputsoutputsr   r   local_functionsr   r   metas
             r   _make_modelExtractor._make_model   s     "DJJOO3c9&&k ' 

 **//!ZZ447(	
 {{%%e4t44r   c                    U R                  U5      nU R                  U5      nU R                  X5      nU R                  U5      u  pgU R	                  U5      nU R                  XSXFXx5      n	U	$ r   )r4   r8   rF   rf   rZ   ru   )
r   rB   rC   rq   rr   rE   r   r   rs   r   s
             r   extract_modelExtractor.extract_model   su    
 ))+6++L9--kH"&"A"A%"H@@G  7
 r   )r   r   r   r   N)r   r   returnNone)r3   	list[str]rz   list[ValueInfoProto])r=   strr>   r|   r?   list[NodeProto]rz   r{   )rB   r|   rC   r|   rz   r   )rE   r   rz   z.tuple[list[TensorProto], list[ValueInfoProto]])rE   r   rq   r}   rr   r}   r   zlist[TensorProto]r   r}   rs   zlist[FunctionProto]rz   r   )rB   r|   rC   r|   rz   r   )__name__
__module____qualname____firstlineno__r   staticmethodr   r.   r4   r8   r<   rF   rZ   rf   ru   rx   __static_attributes__rY   r   r   r
   r
      s   F / /J$BC % )	
 
(

  
 
	
(B'' 
8'055 %5 &	5
 '5 )5 -5 
5,   
	r   r
   c                   [         R                  R                  U 5      (       d  [        SU  35      eU(       d  [        S5      eU(       d  [        S5      e[        R
                  R                  U 5        [        R                  " U 5      n[        U5      nUR                  X#5      n[        R                  " Xq5        U(       a   [        R
                  R                  U5        gg)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.

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existsr`   r   checkercheck_modelloadr
   rx   save)
input_pathoutput_pathrB   rC   r   r   e	extracteds           r   rx   rx      s    , 77>>*%%5j\BCC@AABCCLLZ(IIj!E%A:IIIi%  - r   )T)r   str | os.PathLiker   r   rB   r|   rC   r|   r   boolrz   r{   )
__future__r   r   onnx.checkerr   onnx.helperonnx.shape_inferencer   r   r   r   r   r
   rx   rY   r   r   <module>r      sk    # 	    R R` `P %.!%."%. %. 	%.
 %. 
%.r   