o
    éôjR-  ã                   @   s&  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mZm	Z	 d dl
m  mZ d dlmZmZmZmZ G dd„ dƒZdeded	dfd
d„Zdeded	dfdd„Z				d>dedede	e de	e de	e de	e d	dfdd„Z				d?dedede	e deded	dfdd„Zded	dfdd„Zded ed	dfd!d"„Zd#ed	ee fd$d%„Zd&ed'eegee f d	ee fd(d)„Zd*ed	ee fd+d,„Zd#ed	ee fd-d.„Z d*ed	ee fd/d0„Z!d#ed	ee fd1d2„Z"d3ed	efd4d5„Z#ded	efd6d7„Z$ded8ed	dfd9d:„Z%ded;ed	efd<d=„Z&dS )@é    N)Úchain)ÚCallableÚIterableÚOptional)ÚAttributeProtoÚ
GraphProtoÚ
ModelProtoÚTensorProtoc                   @   s   e Zd Zdeddfdd„ZdS )ÚExternalDataInfoÚtensorÚreturnNc                 C   sf   d| _ d | _d | _d | _d| _|jD ]
}t| |j|jƒ q| jr&t	| jƒ| _| jr1t	| jƒ| _d S d S )NÚ )
ÚlocationÚoffsetÚlengthÚchecksumÚbasepathÚexternal_dataÚsetattrÚkeyÚvalueÚint)Úselfr   Úentry© r   úl/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/onnx/external_data_helper.pyÚ__init__   s   
ÿzExternalDataInfo.__init__)Ú__name__Ú
__module__Ú__qualname__r	   r   r   r   r   r   r
      s    r
   r   Úbase_dirr   c                 C   sŒ   t | ƒ}t ||j| j¡}t|dƒ�*}|jr| |j¡ |jr'| 	|j¡| _
n| 	¡ | _
W d  ƒ dS W d  ƒ dS 1 s?w   Y  dS )züLoads 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_filer   r   r   Úload_external_data_for_tensor!   s   
ÿù"ûr,   Úmodelc                 C   s8   t | ƒD ]}t|ƒrt||ƒ tj|_|jdd…= qdS )z¡Loads 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_datar,   r	   ÚDEFAULTÚdata_locationr   )r-   r    r   r   r   r   Úload_external_data_for_model7   s   
€úr2   r   r   r   r   r   c           	      C   s”   |   d¡std| j d ƒ‚| jd d …= tj| _||d ur!t|ƒnd |d ur*t|ƒnd ||dœ ¡ D ]\}}|d urG| j 	¡ }||_
t|ƒ|_q2d S )Nr(   zTensor zGdoes not have raw_data field. Cannot set external data for this tensor.)r   r   r   r   r   )ÚHasFieldÚ
ValueErrorr$   r   r	   ÚEXTERNALr1   r   ÚitemsÚaddr   Ústrr   )	r   r   r   r   r   r   ÚkÚvr   r   r   r   Úset_external_dataG   s0   
ÿþÿû
ú

€ör;   Té   FÚall_tensors_to_one_fileÚsize_thresholdÚconvert_attributec           	      C   s¾   t | ƒ}|r
t| ƒ}|r9tt ¡ ƒ}|r tj |¡rtdƒ‚|}|D ]}| 	d¡r6t
 |j¡|kr6t||ƒ q"dS |D ]!}| 	d¡r\t
 |j¡|kr\|j}t|ƒsWtt ¡ ƒ}t||ƒ q;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
    zDlocation must be a relative path that is relative to the model path.r(   N)Ú_get_initializer_tensorsr.   r8   ÚuuidÚuuid1ÚosÚpathÚisabsr4   r3   ÚsysÚ	getsizeofr(   r;   r$   Ú_is_valid_filename)	r-   r=   r   r>   r?   ÚtensorsÚ	file_namer   Ztensor_locationr   r   r   Úconvert_model_to_external_datae   s8   ÿÿ
€ûÿ
€ørK   c                 C   s@   t | ƒD ]}t|ƒr| d¡stdƒ‚|jdd…= tj|_qdS )zåCall 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.
    r(   úraw_data field doesn't exist.N)r.   r/   r3   r4   r   r	   r0   r1   )r-   r   r   r   r   Ú convert_model_from_external_data™   s   
€ûrM   Ú	base_pathc                 C   s  t | ƒ}tj ||j¡}|  d¡stdƒ‚tj |¡s0t|dƒ� W d  ƒ n1 s+w   Y  t|dƒ�C}| 	dd¡ |j
durZ| ¡ }|j
|krT| d|j
|  ¡ | 	|j
¡ | ¡ }| | j¡ t| |j|| ¡ | ƒ W d  ƒ dS 1 s{w   Y  dS )	züWrites 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
    r(   rL   ÚabNzr+br   é   ó    )r
   rC   rD   Újoinr   r3   r4   Úisfiler%   r&   r   ÚtellÚwriter(   r;   )r   rN   r)   r*   r+   Ú	file_sizer   r   r   r   Úsave_external_data©   s&   
ÿ

"õrW   Úonnx_model_protoc                 C   s   t t| ƒt| ƒƒS )z=Scan an ONNX model for all tensors and return as an iterator.)r   r@   Ú_get_attribute_tensors©rX   r   r   r   r.   Ë   s   þr.   Ú	attributeÚfuncc                 c   sL   � | j tjkr|| jƒE dH  | j tjkr"| jD ]}||ƒE dH  qdS dS )zICreate an iterator through processing ONNX model attributes with functor.N)Útyper   ÚGRAPHÚgÚGRAPHSÚgraphs)r[   r\   Úgraphr   r   r   Ú_recursive_attribute_processorÓ   s   €
þrc   Úonnx_model_proto_graphc                 c   s:   � | j E dH  | jD ]}|jD ]
}t|tƒE dH  qq
dS )z@Create an iterator of initializer tensors from ONNX model graph.N)ÚinitializerÚnoder[   rc   Ú#_get_initializer_tensors_from_graph©rd   rf   r[   r   r   r   rg   Þ   s   €

ÿÿÿrg   c                 c   ó   � t | jƒE dH  dS )z:Create an iterator of initializer tensors from ONNX model.N)rg   rb   rZ   r   r   r   r@   ê   ó   €r@   c                 c   sL   � | j D ]}|jD ]}| d¡r|jV  |jE dH  t|tƒE dH  q	qdS )zJCreate an iterator of tensors from node attributes of an ONNX model graph.ÚtN)rf   r[   r3   rk   rI   rc   Ú!_get_attribute_tensors_from_graphrh   r   r   r   rl   ï   s   €


ÿüÿrl   c                 c   ri   )zDCreate an iterator of tensors from node attributes of an ONNX model.N)rl   rb   rZ   r   r   r   rY   ý   rj   rY   Úfilenamec                 C   s   t  d¡}| | ¡}t|ƒS )z8Utility to check whether the provided filename is valid.z^[^<>:;,?"*|/]+$)ÚreÚcompileÚmatchÚbool)rm   Úexprp   r   r   r   rH     s   

rH   c                 C   s   |   d¡o
| jtjkS )z?Returns true if the tensor stores data in an external location.r1   )r3   r1   r	   r5   )r   r   r   r   r/   	  s   

þr/   Ú	field_keyc                 C   s*   t | jƒD ]\}}|j|kr| j|= q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   rs   ÚiÚfieldr   r   r   Úremove_external_data_field  s
   	
€þrw   Úfilepathc                 C   s8   t | ƒD ]}t|ƒr| d¡rt||ƒ | d¡ q| 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.
    r(   )r.   r/   r3   rW   Ú
ClearField)r-   rx   r   r   r   r   Úwrite_external_data_tensors  s   

€rz   )NNNN)TNr<   F)'rC   rn   rF   rA   Ú	itertoolsr   Útypingr   r   r   Zonnx.onnx_cpp2py_export.checkerÚonnx_cpp2py_exportÚcheckerr"   Úonnx.onnx_pbr   r   r   r	   r
   r8   r,   r2   r   r;   rq   rK   rM   rW   r.   rc   rg   r@   rl   rY   rH   r/   rw   rz   r   r   r   r   Ú<module>   s’   úÿþýüûú
ù ûÿþýüû
ú4"ÿÿ
þÿ
þÿ
þ