
    Vh#                        d 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 ddl	Z	ddl
Z	ddlZ	ddlmZ ddlmZmZ er
ddlZddlmZ 	 	 	 	 	 	 	 	 ddZdd	Zdd
Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy)zPUtilities for manipulating the onnx and onnx-script dependencies and ONNX proto.    )annotationsN)AnyTYPE_CHECKING)errors)	jit_utilsregistration)Mappingc                   	 ddl }t        j                  j	                  |d|z         }t        j
                  |d       t        | t        j                  j	                  |d      i        t        j                  j	                  |d      }t        j                  j                  |      rt        j                  |       t        j
                  |       |j                  |       }	t        t        |	j                  j                  |            D ]7  \  }
\  }}t        ||t        j                  j	                  |d	|
 d
             9 t        t        |	j                  j                   |            D ]7  \  }
\  }}t        ||t        j                  j	                  |d|
 d
             9 |S # t        $ r}t        d      |d}~ww xY w)u  Export an ONNX model as a self contained ONNX test case.

    The test case contains the model and the inputs/outputs data. The directory structure
    is as follows:

    dir
    ├── test_<name>
    │   ├── model.onnx
    │   └── test_data_set_0
    │       ├── input_0.pb
    │       ├── input_1.pb
    │       ├── output_0.pb
    │       └── output_1.pb

    Args:
        model_bytes: The ONNX model in bytes.
        inputs_data: The inputs data, nested data structure of numpy.ndarray.
        outputs_data: The outputs data, nested data structure of numpy.ndarray.

    Returns:
        The path to the test case directory.
    r   Nz<Export test case to ONNX format failed: Please install ONNX.test_T)exist_ok
model.onnxtest_data_set_0input_z.pboutput_)onnxImportErrorospathjoinmakedirs_export_fileexistsshutilrmtreeload_model_from_string	enumeratezipgraphinputexport_dataoutput)model_bytesinputs_dataoutputs_datanamedirr   exctest_case_dirdata_set_dirprotoiinput_protor   output_protor!   s                  U/home/dcms/DCMS/lib/python3.12/site-packages/torch/onnx/_internal/onnx_proto_utils.pyexport_as_test_caser/      sx   2 GGLLgn5MKK-
]L1

 77<</@AL	ww~~l#l#KK''4E#,S1B1BK-P#Q UKE;\VA3c?(STU%.s5;;3E3E|/T%U X!!L&FL"'',,|wqcQTEU*VWX 1  J
	s   F- -	G6GGc                   	 ddl }ddl m} t        t        j
                  j                  | d      d      5 }|j                         }ddd       t        j
                  j                  | d      }i }t        j                  t        j
                  j                  |d            }|D ]1  }	|j                  |	      }
|j                  |
      ||
j                  <   3 i }t        j                  t        j
                  j                  |d	            }|D ]1  }|j                  |      }
|j                  |
      ||
j                  <   3 ||fS # t        $ r}t        d      |d}~ww xY w# 1 sw Y   "xY w)
u  Load a self contained ONNX test case from a directory.

    The test case must contain the model and the inputs/outputs data. The directory structure
    should be as follows:

    dir
    ├── test_<name>
    │   ├── model.onnx
    │   └── test_data_set_0
    │       ├── input_0.pb
    │       ├── input_1.pb
    │       ├── output_0.pb
    │       └── output_1.pb

    Args:
        dir: The directory containing the test case.

    Returns:
        model_bytes: The ONNX model in bytes.
        inputs: the inputs data, mapping from input name to numpy.ndarray.
        outputs: the outputs data, mapping from output name to numpy.ndarray.
    r   Nnumpy_helperz<Load test case from ONNX format failed: Please install ONNX.r   rbr   z
input_*.pbzoutput_*.pb)r   r2   r   openr   r   r   readglobload_tensorto_arrayr%   )r&   r   r2   r'   fr"   test_data_dirinputsinput_files
input_filetensoroutputsoutput_filesoutput_files                 r.   load_test_caserB   M   sK   .% 
bggll3-t	4 ffh GGLL&78MF))BGGLLEFK! <
!!*-*33F;v{{< G99RWW\\-GHL# =!!+.+44V<= ''+  J
	
 s"   
E
 E'
	E$EE$'E1c                :   	 ddl m} t        |d      5 }|j                  j                  d      r;|j                  |j                  | |j                        j                                n |j                  j                  d      r:|j                  |j                  | |j                        j                                n|j                  j                  d      r:|j                  |j                  | |j                        j                                nV|j                  j                  d	      sJ |j                  |j                  | |j                        j                                ddd       y# t        $ r}t        d      |d}~ww xY w# 1 sw Y   yxY w)
a0  Export data to ONNX protobuf format.

    Args:
        data: The data to export, nested data structure of numpy.ndarray.
        value_info_proto: The ValueInfoProto of the data. The type of the ValueInfoProto
            determines how the data is stored.
        f: The file to write the data to.
    r   r1   z7Export data to ONNX format failed: Please install ONNX.Nwbmap_typesequence_typeoptional_typetensor_type)r   r2   r   r4   typeHasFieldwrite	from_dictr%   SerializeToString	from_listfrom_optional
from_array)datavalue_info_protor9   r2   r'   opened_files         r.   r    r       sm   % 
a +  ))*5&&t-=-B-BCUUW ""++O<&&t-=-B-BCUUW ""++O<***//##% $((11-@@@''.>.C.CDVVX#   E
	
 s#   E4 EF4	F=F		FFc                    t        |      dk(  sJ t        j                  j                  |d      5 }|j	                  |        ddd       y# 1 sw Y   yxY w)z4export/write model bytes into directory/protobuf/zipr   rD   N)lentorchserialization_open_file_likerK   )r"   r9   
export_maprS   s       r.   r   r      sQ     z?a				,	,Q	5 '+&' ' 's   AAc                "   	 ddl }|j	                  |       }g }t               }t        |j                  |||       |r+|j                  j                  |       |j                         } | S # t        $ r}t        j                  d      |d}~ww xY w)zAInsert model-included custom onnx-script function into ModelProtor   NzModule onnx is not installed!)r   r   r   OnnxExporterErrorr   set_find_onnxscript_opr   	functionsextendrM   )r"   custom_opsetsr   emodel_protoonnx_function_listincluded_node_funcs          r.   _add_onnxscript_fnre      s    
O --k:K #&5-}>P $$%78!335-  O&&'FGQNOs   A* *	B3B		Bc                0   | j                   D ]  }|j                  dz   |j                  z   }|j                  D ]'  }|j                  t        |j                  |||       ) t        j                  j                  |      }|j                  st        j                  |j                        rt        j                  |j                        rt        j                  |j                        r|||vs|j                  |j                  d      }|j                  |      }	|	@t        |	d      r2|	j                         }
|j!                  |
       |j#                  |       Yt%        j&                  |||r|j)                               d       ||fS )zYRecursively iterate ModelProto to find ONNXFunction op as it may contain control flow Op.z::N   to_function_proto)nodedomainop_type	attributegr]   r   registryget_function_groupr   is_atenis_primis_onnxgethasattrrh   appendaddr   UnsupportedOperatorErrorget_min_supported)graph_protord   r`   rc   ri   	node_kindattronnx_function_groupspecified_versiononnx_fnonnx_function_protos              r.   r]   r]      s{       "KK$&5	NN 	Dvv!#FF.?Q	 +33FFyQ KK%%dkk2%%dkk2%%dkk2#/!33 - 1 1$++q A)--.?@G"7$78*1*C*C*E'&--.AB&**9511!& $557 
  9"F 111    )r"   bytesr%   strr&   r   returnr   )r&   r   r   ztuple[bytes, Any, Any])r9   r   r   None)r"   r   r9   zio.BytesIO | strrY   zMapping[str, bytes]r   r   )r"   r   r`   Mapping[str, int]r   r   )rd   zset[str]r`   r   rc   list)__doc__
__future__r   r6   r   r   typingr   r   rV   torch.jit._tracetorch.serialization
torch.onnxr   torch.onnx._internalr   r   iocollections.abcr	   r/   rB   r    r   re   r]    r   r.   <module>r      s    V "  	  %     8 '339<3CF33l/(d#L''' $' 
	'$ @*2 *2 %*2 	*2r   