
    Si<2                    
   S r SSKJr  SSKrSSKrSSKJrJr  SSKr	SSK
r
SSKJr  SSKr
SSKJs  Jr  \(       a  SSKJr      S             SS jjrS r          SS jr " S	 S
5      r S       SS jjrg)zXImplements function make_large_model to easily create and save models
bigger than 2 Gb.
    )annotationsN)TYPE_CHECKINGAny)Iterablec                   U R                   S S 2	 [        R                  R                  U l        UUUUUS.R                  5        H:  u  pgUc  M
  U R                   R                  5       nXhl        [        U5      Ul	        M<     g )N)locationoffsetlengthchecksumbasepath)
external_dataonnxTensorProtoEXTERNALdata_locationitemsaddkeystrvalue)	tensorr   r	   r
   r   r   kventrys	            S/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx/model_container.py_set_external_datar      s|     	Q++44F eg =((,,.EIa&EK    c              #     #    U R                    HT  nUR                   HA  nUR                  (       d  M  UR                  v   [        UR                  5       S h  vN   MC     MV     g  N7fN)node	attributeg_enumerate_subgraphs)graphr    atts      r   r#   r#   0   sJ     

>>Cuuuee/666 "  7s   .A*&A*A(A*c                    U n[         R                  " 5       nXl        [        XT5        X%l        UR
                  R                  U5        U$ )zCreate an external tensor.

Arguments:
    location: unique identifier (not necessary a path)
    tensor_name: tensor name in the graph
    tensor_type: onnx type
    shape: shape the of the initializer

Returns:
    the created tensor
)r   r   namer   	data_typedimsextend)r   tensor_nametensor_typeshapetensor_locationr   s         r   make_large_tensor_protor/   8   sA     OFKv/"
KKuMr   c                      \ rS rSrSrSS jrS rSS jr\SS j5       r	\	R                  SS j5       r	SS jrSS	 jrSS
 jrSS jr      SS jr S     SS jjrSSS jjrS rSrg)ModelContainerO   a  Implements an API to store large tensors outside the main ModelProto,
it avoids copying large initializers when defining the model and these initializers
are never serialized through protobuf.
No tensor is stored on disk until the user explicitly saves the model.
c                     S U l         0 U l        g r   )model_proto_large_initializersselfs    r   __init__ModelContainer.__init__V   s    489;r   c                r    U R                   b*  [        R                  R                  U R                   5        g g r   )model_protor   checkercheck_modelr6   s    r   r=   ModelContainer.check_modelZ   s+    'LL$$T%5%56 (r   c                    XR                   ;  a&  [        SU< S[        U R                   5       S35      eU R                   U   $ )z*Returns an external tensor given its name.zUnable to find large tensor z among .)r5   
ValueErrorsortedr7   r'   s     r   __getitem__ModelContainer.__getitem__^   sM    ....thgfTE\E\>]=^^_`  &&t,,r   c                J    U R                   c  [        S5      eU R                   $ )NzModelContainer is empty.)r4   RuntimeErrorr6   s    r   r;   ModelContainer.model_protof   s&    $9::   r   c                L    Xl         [        U R                  5       5      U l        g r   )r4   listenumerate_graph_protosgraphs_)r7   r;   s     r   r;   rH   l   s    'D779:r   c              #     #    U R                   R                  v   [        U R                   R                  5       Sh  vN   g N7f)z&Enumerates all GraphProtos in a model.N)r;   r$   r#   r6   s    r   rK   %ModelContainer.enumerate_graph_protosq   s1     $$$'(8(8(>(>???s   ;AAAc                $    UR                  S5      $ )zuTells if an initializer name is an external initializer stored in memory.
The name must start with '#' in that case.
#)
startswithrC   s     r   !is_in_memory_external_initializer0ModelContainer.is_in_memory_external_initializerv   s     s##r   c                n    U H)  nU R                  U5      (       a  M  [        SU< S35      e   Xl        g)z1Adds all large tensors (not stored in the model).zThe location z2 must start with '#' to be ignored by check model.N)rR   rA   r5   )r7   r5   r   s      r   set_large_initializers%ModelContainer.set_large_initializers|   s?    #A99!<< #A5(Z[  $
 #5r   c                   [         R                  " U R                  5       H  n[         R                  " U5      (       d  M   S nUR                   H  nUR
                  S:X  d  M  UnM     Uc  [        SUR                  < S35      eUR                  U R                  ;  d  M  [        SUR                  < SUR                  < S[        U R                  5       S35      e   g )Nr   "No location found for tensor name r@   "Unable to find large tensor named  with location  in )ext_data_get_all_tensorsr;   uses_external_datar   r   rG   r'   r   r5   rB   )r7   r   propexts       r   check_large_initializers'ModelContainer.check_large_initializers   s    //0@0@AF..v667;D++77j(D , |"8qI  zz!8!88"8 H%%)ZZN$d5567q:  Br   c                   SS jn0 n[         R                  R                  U5      n[         R                  R                  U5      (       d  [	        SU< S35      eU R
                  R                  5       n[        R                  " 5       nUR                  U5        [         R                  R                  [         R                  R                  U5      S   5      S   nU(       aB  [         R                  R                  U5      S    S3n	U S3n
Sn[        U
S5       n S	S	S	5        [        R                  " U5       GH  n[        R                  " U5      (       d  M!  S	nUR                    H  nUR"                  S
:X  d  M  UnM     Uc  [%        SUR&                  < S35      eUR(                  U R*                  ;  a>  [%        SUR&                  < SUR(                  < S[-        U R*                  5       S35      eU R*                  UR(                     n[        R.                  R1                  U5      nU(       aL  [3        UW	W[5        U5      S9  U[5        U5      -  n[        W
S5       nUR7                  U5        S	S	S	5        GMJ  U" XR(                  U5       S3n[3        UUS9  [         R                  R9                  UU5      nUUl        [        US5       nUR7                  U5        S	S	S	5        GM     [        US5       nUR7                  UR                  5       5        S	S	S	5        U$ ! , (       d  f       GN= f! , (       d  f       GM  = f! , (       d  f       GM'  = f! , (       d  f       U$ = f)a  Save the large model into a main onnx file and one file
per tensor. Follows the same format as :func:`write_external_data_tensors
<onnx.external_data_helper.write_external_data_tensors>`.
The main model needs to be modified to update the file location,
the function returns this modified copy.

Arguments:
    file_path: model file
    all_tensors_to_one_file: all tensors in one file

Returns:
    modified main model proto
c                    U (       a  U  SU 3nS H  nUR                  US5      nM     UnX;   a  X!   S-   nXRU'   U SU 3$ SX!'   U$ )N-z:/\;,!    _)replace)prefixr'   unique_namesc	base_nameis         r   _clean_name2ModelContainer._save_external.<locals>._clean_name   sm     4&)||Ar* I# &*%&T"#AaS))!"LKr   zFolder z does not exist.r   rg   z.weightwbNr   rX   r@   rY   rZ   r[   )r   r	   r
   abr   )rj   r   r'   r   rk   zdict[str, int]returnr   )ospathdirnameexistsFileNotFoundErrorr;   SerializeToStringr   
ModelProtoParseFromStringsplitextsplitopenr\   r]   r^   r   r   rG   r'   r   r5   rB   numpy_helpertobytes_little_endianr   lenwritejoin)r7   	file_pathall_tensors_to_one_filero   rk   folderprotocopyrj   file_weightfull_file_weightr	   fr   r_   r`   	np_tensortensor_bytesr'   	full_names                       r   _save_externalModelContainer._save_external   s   "	 (*+ww~~f%%#gfZ7G$HII  224 U#!!"''--	":2">?B"WW]]95a89AK"+G4F&- . //5F..v667;D++77j(D , |"8qI  zz!8!88"8 H%%)ZZN$d5567q: 
 //

;I,,BB9ML&"(!|,	 #l++*D1QGGL) 21 &fjj,GHP"6D9GGLL6	!
)T*aGGL) +*I 6N )T"aGGD**,- #Y .-@ 21 +* #"s0   L.)M M M&.
L= 
M	
M#	&
M5c                     U R                  XS9$ )a  Save the large model.
The function returns a ModelProto,
the current one if the model did not need any modification,
a modified copy of it if it required changes such as giving file names
to every external tensor.

Arguments:
    file_path: model file
    all_tensors_to_one_file: saves all large tensors in one file or
        one file per lerge tensor

Returns:
    the saved ModelProto
)r   )r   )r7   r   r   s      r   saveModelContainer.save   s    & "" # 
 	
r   c                j    [         R                  " USS9U l        U(       a  U R                  U5        gg)a>  Load the large model.

Arguments:
    file_path: model file
    load_large_initializers: loads the large initializers,
        if not done, the model is incomplete but it can be used to
        look into the model without executing it and method
        :meth:`_load_large_initializers` can be used to load them later
F)load_external_dataN)r   
load_modelr4   _load_large_initializers)r7   r   load_large_initializerss      r   loadModelContainer.load  s-     !OOI%P")))4 #r   c                n   U R                   c  [        S5      e0 U l        [        R                  R                  U5      n[        [        R                  " U R                   5      5       GH  u  p4[        R                  " U5      (       d  M#  [        R                  " U5      n[        R                  " X%R                  UR                  5      nSU 3n[        XGS9  [        R                   n[#        [        S5      (       a  U[        R$                  -  n[        R&                  " Xh5      n	[        R(                  " U	S5       n
[        R*                  " XUR                  5      n[,        R.                  R1                  UR2                  5      n[5        UR6                  5      n[8        R:                  S:X  a2  [<        R>                  " XS9RA                  5       RC                  U5      nO#[<        R>                  " XS9RC                  U5      nXR                  U'   SSS5        GM     g! , (       d  f       GM  = f)	zLoads large initializers.

Arguments:
    file_path: model file, the weight are expected to be in the same folder as this file
Nz2A model must be loaded before loading the weights.z#trt   
O_NOFOLLOWrbbig)dtype)"r4   rG   r5   rv   rw   rx   	enumerater\   r]   r^   ExternalDataInfo	c_checker_resolve_external_data_locationr   r'   r   O_RDONLYhasattrr   r   fdopen#_validate_external_data_file_boundsr   helpertensor_dtype_to_np_dtyper(   tupler)   sys	byteordernp
frombufferbyteswapreshape)r7   r   base_dirrn   r   infoexternal_data_file_pathr   
open_flagsfd	data_fileraw_datar   r-   r   s                  r   r   'ModelContainer._load_large_initializers  s    $STT"$77??9-"8#<#<T=N=N#OPIA..v66,,V4D&/&O&O--'#
 qc(Cv4 Jr<((bmm+
0=B2t$	#GGV[[ <<V=M=MNfkk*==E)h<EEGOOPUV  !#h D L LU SI/8'', %$% Q$ %$s   ?CH$$
H4	)rL   r5   r4   N)ru   None)r'   r   ru   z
np.ndarray)ru   onnx.ModelProto)r;   r   )ru   zIterable[onnx.GraphProto])r'   r   ru   bool)r5   zdict[str, np.ndarray])r   r   r   r   ru   r   )F)T)r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r8   r=   rD   propertyr;   setterrK   rR   rU   ra   r   r   r   r   __static_attributes__ r   r   r1   r1   O   s    <7- ! !
 ; ;@
$5&WW7;W	Wx ).

 "&
 
	
.5+9r   r1   c                    [         R                  R                  " U 40 UD6n[        5       nX4l        U(       a!  UR                  U5        UR                  5         U$ )a3  Construct a ModelContainer

C API and Python API of protobuf do not operate without serializing
the protos. This function uses the Python API of ModelContainer.

Arguments:
    graph: *make_graph* returns
    large_initializers: dictionary `name: large tensor`,
        large tensor is any python object supporting the DLPack protocol,
        the ownership the tensor is transferred to the ModelContainer,
        the tensor must define method `tobytes` like numpy tensors
    **kwargs: any attribute to add to the returned instance

Returns:
    ModelContainer
)r   r   
make_modelr1   r;   rU   ra   )r$   r5   kwargsmodellarge_models        r   make_large_modelr   D  sO    * KK""53F3E "K#**+=>,,.r   )NNNN)r   onnx.TensorProtor   r   r	   
int | Noner
   r   r   
str | Noner   r   ru   r   )
r   r   r+   r   r,   intr-   ztuple[int, ...]ru   r   r   )r$   zonnx.GraphProtor5   zdict[str, np.ndarray] | Noner   r   ru   r1   )r   
__future__r   rv   r   typingr   r   numpyr   r   onnx.external_data_helperexternal_data_helperr\   onnx.helperonnx.onnx_cpp2py_export.checkeronnx_cpp2py_exportr<   r   collections.abcr   r   r#   r/   r1   r   r   r   r   <module>r      s    # 	 
 %   ,  3 3( !!! ! 	!
 ! ! 
!.7 #25>M.r9 r9n 8<4  	r   