
    /;ji/                    :   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mc mZ d dlmZmZmZmZmZ e	rd dlmZmZ  G d d          Zd<dZd=dZ	 	 	 	 d>d?dZ	 	 	 	 d@dAd#ZdBd$ZdCd&ZdDd)ZdEd.ZdFd1Z dDd2Z!dFd3Z"dDd4Z#dGd6Z$dHd7Z%dId9Z&dJd;Z'dS )K    )annotationsN)chain)TYPE_CHECKING)AttributeProtoFunctionProto
GraphProto
ModelProtoTensorProto)CallableIterablec                      e Zd ZddZdS )ExternalDataInfotensorr
   returnNonec                   d| _         d | _        d | _        d | _        d| _        |j        D ]}t          | |j        |j                   | j        rt          | j                  | _        | j        rt          | j                  | _        d S d S )N )
locationoffsetlengthchecksumbasepathexternal_datasetattrkeyvalueint)selfr   entrys      S/root/voice-cloning/.venv/lib/python3.11/site-packages/onnx/external_data_helper.py__init__zExternalDataInfo.__init__   s    ) 	2 	2ED%)U[1111; 	+dk**DK; 	+dk**DKKK	+ 	+    N)r   r
   r   r   )__name__
__module____qualname__r!    r"   r    r   r      s(        + + + + + +r"   r   r   r
   base_dirstrr   r   c                x   t          |           }t          j        ||j        | j                  }t          |d          5 }|j        r|                    |j                   |j        r |	                    |j                  | _
        n|	                                | _
        ddd           dS # 1 swxY w Y   dS )zLoads data from an external file for tensor.
    Ideally TensorProto should not hold any raw data but if it does it will be ignored.

    Arguments:
        tensor: a TensorProto object.
        base_dir: directory that contains the external data.
    rbN)r   	c_checker_resolve_external_data_locationr   nameopenr   seekr   readraw_data)r   r'   infoexternal_data_file_path	data_files        r    load_external_data_for_tensorr5   ,   s     F##D'G$-  
%t	,	, /	; 	(NN4;'''; 	/'nnT[99FOO'nn..FO/ / / / / / / / / / / / / / / / / /s    A"B//B36B3modelr	   c                    t          |           D ]<}t          |          r+t          ||           t          j        |_        |j        dd= =dS )zLoads external tensors into model

    Arguments:
        model: ModelProto to load external data to
        base_dir: directory that contains external data
    N)_get_all_tensorsuses_external_datar5   r
   DEFAULTdata_locationr   )r6   r'   r   s      r    load_external_data_for_modelr<   B   s`     #5)) ( (f%% 	()&(;;;#.#6F $QQQ'( (r"   r   r   
int | Noner   r   
str | Noner   c                   |                      d          st          d| j        z   dz             | j        d d = t          j        | _        ||t          |          nd |t          |          nd ||d                                D ];\  }}|4| j        	                                }||_
        t          |          |_        <d S )Nr1   zTensor zGdoes not have raw_data field. Cannot set external data for this tensor.)r   r   r   r   r   )HasField
ValueErrorr-   r   r
   EXTERNALr;   r   itemsaddr   r(   r   )	r   r   r   r   r   r   kvr   s	            r    set_external_datarG   R   s     ??:&& 
kWX
 
 	
 	QQQ&/F!'!3#f+++!'!3#f+++  egg
! 
!1 =(,,..EEIa&&EK
! 
!r"   T   Fall_tensors_to_one_fileboolsize_thresholdr   convert_attributec                   t          |           }|rt          |           }|rt          t          j                              dz   }|rbt
          j                            |          rt          d          t
          j        	                    |          rt          d| d          |}|D ]D}|                    d          r-t          j        |j                  |k    rt          ||           EdS |D ]z}|                    d          rct          j        |j                  |k    rF|j        }t#          |          s t          t          j                              }t          ||           {dS )a  Call to set all tensors with raw data as external data. This call should precede 'save_model'.
    'save_model' saves all the tensors data as external data after calling this function.

    Arguments:
        model (ModelProto): Model to be converted.
        all_tensors_to_one_file (bool): If true, save all tensors to one external file specified by location.
            If false, save each tensor to a file named with the tensor name.
        location: specify the external file relative to the model that all tensors to save to.
            Path is relative to the model path.
            If not specified, will use the model name.
        size_threshold: Threshold for size of data. Only when tensor's data is >= the size_threshold
            it will be converted to external data. To convert every tensor with raw data to external data set size_threshold=0.
        convert_attribute (bool): If true, convert all tensors to external data
                       If false, convert only non-attribute tensors to external data

    Raise:
        ValueError: If location is not a relative path.
        FileExistsError: If a file already exists in location.
    z.datazDlocation must be a relative path that is relative to the model path.zExternal data file exists in .r1   N)_get_initializer_tensorsr8   r(   uuiduuid1ospathisabsrA   existsFileExistsErrorr@   sys	getsizeofr1   rG   r-   _is_valid_filename)	r6   rI   r   rK   rL   tensors	file_namer   tensor_locations	            r    convert_model_to_external_datar]   p   s   4 'u--G *"5)) ;
%%/	 	!w}}X&&  Z   w~~h'' S%&Qh&Q&Q&QRRR I 	5 	5F
++5M&/22nDD!&)444	5 	5  	; 	;F
++;M&/22nDD"(+)/:: 8&)$*,,&7&7O!&/:::	; 	;r"   c                    t          |           D ]P}t          |          r?|                    d          st          d          |j        dd= t
          j        |_        QdS )zCall to set all tensors which use external data as embedded data.
    save_model saves all the tensors data as embedded data after
    calling this function.

    Arguments:
        model (ModelProto): Model to be converted.
    r1   raw_data field doesn't exist.N)r8   r9   r@   rA   r   r
   r:   r;   )r6   r   s     r     convert_model_from_external_datar`      st     #5)) 7 7f%% 	7??:.. B !@AAA$QQQ'#.#6F 7 7r"   	base_pathc                8   t          |           }t          j                            ||j                  }|                     d          st          d          t          j                            |          s)t          |d          5  	 ddd           n# 1 swxY w Y   t          |d          5 }|	                    dd           |j
        Y|                                }|j
        |k    r |                    d|j
        |z
  z             |	                    |j
                   |                                }|                    | j                   t          | |j        ||                                |z
             ddd           dS # 1 swxY w Y   dS )	zWrites tensor data to an external file according to information in the `external_data` field.

    Arguments:
        tensor (TensorProto): Tensor object to be serialized
        base_path: System path of a folder where tensor data is to be stored
    r1   r_   abNzr+br          )r   rR   rS   joinr   r@   rA   isfiler.   r/   r   tellwriter1   rG   )r   ra   r2   r3   r4   	file_sizer   s          r    save_external_datark      s	    F##D gll9dmDD ??:&& :8999 7>>122 )400 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
%u	-	- Tq!;"!((I{Y&&y)@ ABBBNN4;'''!!(((&$-9I9IF9RSSST T T T T T T T T T T T T T T T T Ts%   BBB1CFFFonnx_model_protoIterable[TensorProto]c                V    t          t          |           t          |                     S )z=Scan an ONNX model for all tensors and return as an iterator.)r   rO   _get_attribute_tensors)rl   s    r    r8   r8      s,     !122/00  r"   	attributer   func-Callable[[GraphProto], Iterable[TensorProto]]c              #     K   | j         t          j        k    r || j                  E d{V  | j         t          j        k    r| j        D ]} ||          E d{V  dS dS )zICreate an iterator through processing ONNX model attributes with functor.N)typer   GRAPHgGRAPHSgraphs)rp   rq   graphs      r    _recursive_attribute_processorrz      s       ~---4	$$$$$$$$$~...% 	# 	#EtE{{"""""""" /.	# 	#r"   graph_or_functionGraphProto | FunctionProtoc             #     K   t          | t                    r| j        E d{V  | j        D ]'}|j        D ]}t          |t                    E d{V  (dS )zICreate an iterator of initializer tensors from ONNX model graph/function.N)
isinstancer   initializernoderp   rz   #_get_initializer_tensors_from_graphr{   r   rp   s      r    r   r      s       #Z00 1$00000000!&   	 	I5>         	 r"   c              #  |   K   t          | j                  E d{V  | j        D ]}t          |          E d{V  dS )z:Create an iterator of initializer tensors from ONNX model.N)r   ry   	functions!_get_attribute_tensors_from_graphrl   functions     r    rO   rO      si      23C3IJJJJJJJJJ$. ? ?4X>>>>>>>>>>? ?r"   c             #     K   | j         D ]R}|j        D ]H}|                    d          r	|j        V  |j        E d{V  t          |t                    E d{V  ISdS )zSCreate an iterator of tensors from node attributes of an ONNX model graph/function.tN)r   rp   r@   r   rZ   rz   r   r   s      r    r   r     s       "&   	 	I!!#&& "k!!! ((((((((5<         		 r"   c              #  |   K   t          | j                  E d{V  | j        D ]}t          |          E d{V  dS )zDCreate an iterator of tensors from node attributes of an ONNX model.N)r   ry   r   r   s     r    ro   ro     si      01A1GHHHHHHHHH$. ? ?4X>>>>>>>>>>? ?r"   filenamec                r    t          j        d          }|                    |           }t          |          S )z8Utility to check whether the provided filename is valid.z^[^<>:;,?"*|/]+$)recompilematchrJ   )r   expr   s      r    rY   rY     s/    
*'
(
(CIIhE;;r"   c                V    |                      d          o| j        t          j        k    S )z?Returns true if the tensor stores data in an external location.r;   )r@   r;   r
   rB   )r   s    r    r9   r9     s+     	(( 	9 K$88r"   	field_keyc                `    t          | j                  D ]\  }}|j        |k    r| j        |= dS )a  Removes a field from a Tensor's external_data key-value store.

    Modifies tensor object in place.

    Arguments:
        tensor (TensorProto): Tensor object from which value will be removed
        field_key (string): The key of the field to be removed
    N)	enumerater   r   )r   r   ifields       r    remove_external_data_fieldr   '  sE     f233 ( (59	!!$Q'( (r"   filepathc                    t          |           D ]K}t          |          r:|                    d          r%t          ||           |                    d           L| S )a  Serializes data for all the tensors which have data location set to TensorProto.External.

    Note: This function also strips basepath information from all tensors' external_data fields.

    Arguments:
        model (ModelProto): Model object which is the source of tensors to serialize.
        filepath: System path to the directory which should be treated as base path for external data.

    Returns:
        ModelProto: The modified model object.
    r1   )r8   r9   r@   rk   
ClearField)r6   r   r   s      r    write_external_data_tensorsr   5  sj     #5)) * *
 f%% 	*&//**E*E 	*vx000j)))Lr"   )r   r
   r'   r(   r   r   )r6   r	   r'   r(   r   r   )NNNN)r   r
   r   r(   r   r=   r   r=   r   r>   r   r>   r   r   )TNrH   F)r6   r	   rI   rJ   r   r>   rK   r   rL   rJ   r   r   )r6   r	   r   r   )r   r
   ra   r(   r   r   )rl   r	   r   rm   )rp   r   rq   rr   r   rm   )r{   r|   r   rm   )r   r(   r   rJ   )r   r
   r   rJ   )r   r
   r   r(   r   r   )r6   r	   r   r(   r   r	   )(
__future__r   rR   r   rW   rP   	itertoolsr   typingr   onnx.onnx_cpp2py_export.checkeronnx_cpp2py_exportcheckerr+   onnx.onnx_pbr   r   r   r	   r
   collections.abcr   r   r   r5   r<   rG   r]   r`   rk   r8   rz   r   rO   r   ro   rY   r9   r   r   r&   r"   r    <module>r      s   # " " " " " 				 				 



                    3 3 3 3 3 3 3 3 3               322222222+ + + + + + + +$/ / / /,( ( ( (& ! ! ! ! !@ %)#7; 7; 7; 7; 7;t7 7 7 7 T T T TD   # # # #
 
 
 
? ? ? ?   ? ? ? ?      ( ( ( (     r"   