
    h-                     J   S SK r S SKrS SKrS SKrS SKJr  S SKJrJrJ	r	  S SK
JrJrJrJr   " S S5      rS\S\S	S4S
 jrS\S\S	S4S jr    S-S\S\S\	\   S\	\   S\	\   S\	\   S	S4S jjr    S.S\S\S\	\   S\S\S	S4S jjrS\S	S4S jrS\S\S	S4S jrS\S	\\   4S jrS\S\\/\\   4   S	\\   4S jrS\S	\\   4S  jrS\S	\\   4S! jrS\S	\\   4S" jrS\S	\\   4S# jrS$\S	\4S% jrS&\S	\4S' jr S\S	\4S( jr!S\S)\S	S4S* jr"S\S+\S	\4S, jr#g)/    N)chain)CallableIterableOptional)AttributeProto
GraphProto
ModelProtoTensorProtoc                   &    \ rS rSrS\SS4S jrSrg)ExternalDataInfo   tensorreturnNc                 ^   SU l         S U l        S U l        S U l        SU l        UR
                   H#  n[        XR                  UR                  5        M%     U R                  (       a  [        U R                  5      U l        U R                  (       a  [        U R                  5      U l        g g )N )
locationoffsetlengthchecksumbasepathexternal_datasetattrkeyvalueint)selfr   entrys      S/var/www/fran/franai/venv/lib/python3.13/site-packages/onnx/external_data_helper.py__init__ExternalDataInfo.__init__   s|    ))ED))U[[1 * ;;dkk*DK;;dkk*DK     )r   r   r   r   r   )__name__
__module____qualname____firstlineno__r
   r   __static_attributes__ r!   r   r   r      s    +{ +t +r!   r   r   base_dirr   c                    [        U 5      n[        UR                  5      n[        R                  R                  X5      n[        US5       nUR                  (       a  UR                  UR                  5        UR                  (       a!  UR                  UR                  5      U l        OUR                  5       U l        SSS5        g! , (       d  f       g= f)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   _sanitize_pathr   ospathjoinopenr   seekr   readraw_data)r   r(   infofile_locationexternal_data_file_path	data_files         r   load_external_data_for_tensorr7       s     F#D"4==1M ggll8C	%t	,	;;NN4;;';;'nnT[[9FO'nn.FO 
-	,	,s   A4C		
Cmodelc                     [        U 5       HD  n[        U5      (       d  M  [        X!5        [        R                  Ul        UR                  SS2	 MF     g)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_datar7   r
   DEFAULTdata_locationr   )r8   r(   r   s      r   load_external_data_for_modelr>   7   sB     #5)f%%)&;#.#6#6F $$Q' *r!   r   r   r   r   r   c                    U R                  S5      (       d  [        SU R                  -   S-   5      eU R                  S S 2	 [        R
                  U l        UUb  [        U5      OS Ub  [        U5      OS UUS.R                  5        H:  u  pgUc  M
  U R                  R                  5       nXhl
        [        U5      Ul        M<     g )Nr2   zTensor zGdoes not have raw_data field. Cannot set external data for this tensor.)r   r   r   r   r   )HasField
ValueErrornamer   r
   EXTERNALr=   r   itemsaddr   strr   )	r   r   r   r   r   r   kvr   s	            r   set_external_datarI   H   s     ??:&&kkWX
 	
 	Q&//F!'!3#f+!'!3#f+ eg =((,,.EIa&EKr!   all_tensors_to_one_filesize_thresholdconvert_attributec                    [        U 5      nU(       a  [        U 5      nU(       a  [        [        R                  " 5       5      nU(       a1  [
        R                  R                  U5      (       a  [        S5      eUnU HL  nUR                  S5      (       d  M  [        R                  " UR                  5      U:  d  MA  [        Xv5        MN     gU H  nUR                  S5      (       d  M  [        R                  " UR                  5      U:  d  MA  UR                  n[        U5      (       d  [        [        R                  " 5       5      n[        Xx5        M     g)a  
Call to set all tensors with raw data as external data. This call should preceed '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.r2   N)_get_initializer_tensorsr:   rF   uuiduuid1r,   r-   isabsrA   r@   sys	getsizeofr2   rI   rB   _is_valid_filename)	r8   rJ   r   rK   rL   tensors	file_namer   tensor_locations	            r   convert_model_to_external_datarX   f   s    . 'u-G"5)

%	ww}}X&& Z  !IF
++MM&//2nD!&4  F
++MM&//2nD"(++)/::&)$**,&7O!&: r!   c                     [        U 5       HZ  n[        U5      (       d  M  UR                  S5      (       d  [        S5      eUR                  SS2	 [
        R                  Ul        M\     g)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.
r2   raw_data field doesn't exist.N)r:   r;   r@   rA   r   r
   r<   r=   )r8   r   s     r    convert_model_from_external_datar[      sU     #5)f%%??:.. !@AA$$Q'#.#6#6F  *r!   	base_pathc                 *   [        U 5      n[        R                  R                  XR                  5      nU R                  S5      (       d  [        S5      e[        R                  R                  U5      (       d  [        US5          SSS5        [        US5       nUR                  SS5        UR                  b\  UR                  5       nUR                  U:  a!  UR                  SUR                  U-
  -  5        UR                  UR                  5        UR                  5       nUR                  U R                  5        [        XR                  XdR                  5       U-
  5        SSS5        g! , (       d  f       N= f! , (       d  f       g= f)	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
r2   rZ   abNzr+br          )r   r,   r-   r.   r   r@   rA   isfiler/   r0   r   tellwriter2   rI   )r   r\   r3   r5   r6   	file_sizer   s          r   save_external_datare      s$    F#D ggll9mmD ??:&&899 77>>122)40 1 
%u	-q!;;"!(I{{Y&y)@ ABNN4;;'!(&--9IF9RS 
.	-	 10 
.	-s   E3CF3
F
Fonnx_model_protoc                 >    [        [        U 5      [        U 5      5      $ )z=Scan an ONNX model for all tensors and return as an iterator.)r   rN   _get_attribute_tensorsrf   s    r   r:   r:      s!     !12/0 r!   	attributefuncc              #     #    U R                   [        R                  :X  a  U" U R                  5       Sh  vN   U R                   [        R                  :X  a$  U R
                   H  nU" U5       Sh  vN   M     gg NG N7f)zICreate an iterator through processing ONNX model attributes with functor.N)typer   GRAPHgGRAPHSgraphs)rj   rk   graphs      r   _recursive_attribute_processorrs      si      ~~---	$$$~~...%%EE{"" & / 	% #s!   4BA><B3B 4B Bonnx_model_proto_graphc              #      #    U R                    Sh  vN   U R                   H.  nUR                   H  n[        U[        5       Sh  vN   M     M0     g NC N7f)z@Create an iterator of initializer tensors from ONNX model graph.N)initializernoderj   rs   #_get_initializer_tensors_from_graphrt   rw   rj   s      r   rx   rx      sV      &1111&++I5>   ( , 2s!   AA5AA	AAc              #   J   #    [        U R                  5       Sh  vN   g N7f)z:Create an iterator of initializer tensors from ONNX model.N)rx   rr   ri   s    r   rN   rN      s     23C3I3IJJJ   #!#c              #      #    U R                    Hf  nUR                   HS  nUR                  S5      (       a  UR                  v   UR                   Sh  vN   [        U[        5       Sh  vN   MU     Mh     g N% N7f)zJCreate an iterator of tensors from node attributes of an ONNX model graph.tN)rw   rj   r@   r}   rU   rs   !_get_attribute_tensors_from_graphry   s      r   r~   r~      sq      '++I!!#&&kk! ((((5<  	 ( , )s$   AA>A:A>,A<-A><A>c              #   J   #    [        U R                  5       Sh  vN   g N7f)zDCreate an iterator of tensors from node attributes of an ONNX model.N)r~   rr   ri   s    r   rh   rh     s     01A1G1GHHHr{   r-   c                 $    U R                  S5      $ )zRemove path components which would allow traversing up a directory tree from a base path.

Note: This method is currently very basic and should be expanded.
z/.)lstrip)r-   s    r   r+   r+     s    
 ;;tr!   filenamec                 f    [         R                  " S5      nUR                  U 5      n[        U5      $ )z8Utility to check whether the provided filename is valid.z^[^<>:;,?"*|/]+$)recompilematchbool)r   expr   s      r   rT   rT     s)    
**'
(CIIhE;r!   c                 l    U R                  S5      =(       a    U R                  [        R                  :H  $ )z?Returns true if the tensor stores data in an external location.r=   )r@   r=   r
   rC   )r   s    r   r;   r;     s.     	( 	9  K$8$88r!   	field_keyc                 ~    [        U R                  5       H$  u  p#UR                  U:X  d  M  U R                  U	 M&     g)z
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     s5     f22399	!$$Q' 4r!   filepathc                     [        U 5       HI  n[        U5      (       d  M  UR                  S5      (       d  M-  [        X!5        UR	                  S5        MK     U $ )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.
r2   )r:   r;   r@   re   
ClearField)r8   r   r   s      r   write_external_data_tensorsr   ,  sK     #5)
 f%%&//**E*Ev0j) * Lr!   )NNNN)TNi   F)$r,   r   rR   rO   	itertoolsr   typingr   r   r   onnx.onnx_pbr   r   r	   r
   r   rF   r7   r>   r   rI   r   rX   r[   re   r:   rs   rx   rN   r~   rh   r+   rT   r;   r   r   r'   r!   r   <module>r      sj   
 	 
   / / L L+ +$/+ / / /.(
 (c (d (( ! ""!!! SM! SM	!
 sm! sm! 
!@ %)"#2;2;!2; sm2; 	2;
 2; 
2;j7J 74 7" T{  Ts  Tt  TFz h{6K ##%-zlH[<Q.Q%R#k#	&	k	Kz Kh{>S K
&kIZ IH[<Q I
    { t ({ (s (t (z S Z r!   