
    Ji"                     J   S SK r S SKrS SKJrJrJrJr  S SKrS SK	r	S SK
JrJrJrJrJr   SS\\\	R                   4   S\\\\4      S\4S jjr SS\\\	R                   4   S\\\ R&                  4   S\\\\4      SS4S	 jjrSS
\S\S\\\	R                   4   4S jjr SS\\\ R&                  4   S\\\	R                   4   4S jjr SS\\\R.                  4   S\S\\\	R                   4   4S jjrS\\\	R                   4   S\\\R2                  4   4S jr\	R6                  S\	R8                  S\	R:                  S\	R<                  S\	R>                  S\	R@                  S\	RB                  S\	RD                  S\	RF                  S\	RH                  S\	RJ                  S\	RL                  S\	RN                  S0r(\	RH                  \	R8                  \	R>                  \	R<                  \	R6                  \	R:                  \	R@                  \	RD                  \	RB                  \	RF                  \	RJ                  \	RL                  S.r)\	R6                  \R6                  \	R8                  \R8                  \	R:                  \R:                  \	R<                  \R>                  \	R>                  \R>                  \	R@                  \R@                  \	RB                  \RB                  \	RD                  \RD                  \	RF                  \#\	RH                  \RH                  \	RJ                  \RB                  \	RL                  \RB                  0r*S\S\	RV                  4S jr,S\\\	R                   4   4S jr-S\	R                   S\S\4S jr.S\\\	R                   4   S\\\\\4   4   4S jr/g)     N)AnyDictOptionalUnion)numpydeserialize	safe_open	serializeserialize_filetensorsmetadatareturnc                 B    [        [        U 5      US9n[        U5      nU$ )a  
Saves a dictionary of tensors into raw bytes in safetensors format.

Args:
    tensors (`Dict[str, paddle.Tensor]`):
        The incoming tensors. Tensors need to be contiguous and dense.
    metadata (`Dict[str, str]`, *optional*, defaults to `None`):
        Optional text only metadata you might want to save in your header.
        For instance it can be useful to specify more about the underlying
        tensors. This is purely informative and does not affect tensor loading.

Returns:
    `bytes`: The raw bytes representing the format

Example:

```python
from safetensors.paddle import save
import paddle

tensors = {"embedding": paddle.zeros((512, 1024)), "attention": paddle.zeros((256, 256))}
byte_data = save(tensors)
```
r   )r
   _flattenbytes)r   r   
serializedresults       T/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/safetensors/paddle.pysaver      s$    6 8G,x@J:FM    filenamec                 *    [        [        U 5      XS9  g)a  
Saves a dictionary of tensors into raw bytes in safetensors format.

Args:
    tensors (`Dict[str, paddle.Tensor]`):
        The incoming tensors. Tensors need to be contiguous and dense.
    filename (`str`, or `os.PathLike`)):
        The filename we're saving into.
    metadata (`Dict[str, str]`, *optional*, defaults to `None`):
        Optional text only metadata you might want to save in your header.
        For instance it can be useful to specify more about the underlying
        tensors. This is purely informative and does not affect tensor loading.

Returns:
    `None`

Example:

```python
from safetensors.paddle import save_file
import paddle

tensors = {"embedding": paddle.zeros((512, 1024)), "attention": paddle.zeros((256, 256))}
save_file(tensors, "model.safetensors")
```
r   N)r   r   )r   r   r   s      r   	save_filer   +   s    > 8G$hBr   datadevicec                     [         R                  S:  a  [        U 5      n[        X!5      $ [        R
                  " U 5      n[        X!5      $ )a  
Loads a safetensors file into paddle format from pure bytes.

Args:
    data (`bytes`):
        The content of a safetensors file

Returns:
    `Dict[str, paddle.Tensor]`: dictionary that contains name as key, value as `paddle.Tensor` on cpu

Example:

```python
from safetensors.paddle import load

file_path = "./my_folder/bert.safetensors"
with open(file_path, "rb") as f:
    data = f.read()

loaded = load(data)
```
3.2.0)paddle__version__r   _view2paddler   load
_np2paddle)r   r   flats      r   r"   r"   M   s@    . W$4 D))zz$$''r   c                    0 n[         R                  S:  a@  [        U SUS9 nUR                  5        H  nUR	                  U5      X$'   M     SSS5        U$ [
        R                  " U 5      n[        XQ5      nU$ ! , (       d  f       U$ = f)a[  
Loads a safetensors file into paddle format.

Args:
    filename (`str`, or `os.PathLike`)):
        The name of the file which contains the tensors
    device (`Union[Dict[str, any], str]`, *optional*, defaults to `cpu`):
        The device where the tensors need to be located after load.
        available options are all regular paddle device locations

Returns:
    `Dict[str, paddle.Tensor]`: dictionary that contains name as key, value as `paddle.Tensor`

Example:

```python
from safetensors.paddle import load_file

file_path = "./my_folder/bert.safetensors"
loaded = load_file(file_path)
```
r   r   )	frameworkr   N)r   r    r	   offset_keys
get_tensorr   	load_filer#   )r   r   r   fkr$   s         r   r)   r)   l   s    2 FW$x8FCq]]_LLO	 % D M x(D)M DC Ms   +A::
B	
numpy_dictc                 d    U R                  5        H  u  p#[        R                  " X1S9X'   M     U $ )Nplace)itemsr   	to_tensor)r,   r   r+   vs       r   r#   r#      s1       "((9
 #r   paddle_dictc                     U R                  5        H3  u  pUR                  5       R                  5       R                  5       X'   M5     U $ N)r0   detachcpur   )r3   r+   r2   s      r   
_paddle2npr8      s:    !!#)//1 $r               )F64F32F16BF16I64I32I16I8U8BOOLF8_E4M3F8_E5M2	dtype_strc                     [         U    $ r5   )_TYPES)rI   s    r   	_getdtyperL      s    )r   c                    0 nU  H  u  p4[        US   5      n[        US   5      S:X  a5  [        S US    5       5      (       d   e[        R                  " US   US9nOV[        R
                  R                  R                  US   U5      R                  US   5      nUS:w  a  UR                  U5      n[        R                  S:X  a0  [        R                  " UR                  5       R                  S	S
9US9nXbU'   M     U$ )Ndtyper   r   c              3   *   #    U  H	  oS :H  v   M     g7f)r   N ).0xs     r   	<genexpr>_view2paddle.<locals>.<genexpr>   s     2z!Avzs   shape)rN   r7   bigFinplacer.   )rL   lenanyr   emptybasecore
frombufferreshapetosys	byteorderr1   r   byteswap)safeviewr   r   r+   r2   rN   arrs          r   r!   r!      s    F!G*%qy>Q2qz22222,,qz7C++""--ai?GG'
SCffVn==E!""399;#7#7#7#FfUCq	  Mr   tensornamec                    U R                  5       (       d  [        SU S35      eU R                  R                  5       (       d  U R	                  5       n SS KnSS Kn[        UR                  " U R                  5      R                  5       5      n[        U R                     nXE-  nU R                  5       nUS:X  a  gUR                  XrR                  UR                   5      5      nUR"                  R%                  X45      n	[&        R(                  S:X  a1  [*        U R                     n
U	R-                  U
5      R/                  SS9n	U	R1                  5       $ )Nz1You are trying to save a non contiguous tensor: `a  ` which is not allowed. It either means you are trying to save tensors which are reference of each other in which case it's recommended to save only the full tensors, and reslice at load time, or simply call `.contiguous()` on your tensor to pack it before saving.r   r   rV   FrW   )is_contiguous
ValueErrorr/   is_cpu_placer7   ctypesr   intprodrU   item_SIZErN   data_ptrcastPOINTERc_ubyte	ctypeslibas_arrayra   rb   NPDTYPESviewrc   tobytes)rf   rg   rl   nplengthbytes_per_itemtotal_bytesptrnewptrr   npdtypes              r   _tobytesr      s   !!?v F& &
 	
 <<$$&& &++-.F6<<(N)K
//
C
ax[[nnV^^<=F<<  8D
}}6<<(yy!**5*9<<>r   c                    [        U [        5      (       d  [        S[        U 5       35      eU R	                  5        H>  u  p[        U[
        R                  5      (       a  M&  [        SU S[        U5       35      e   U R	                  5        VVs0 s HD  u  pU[        UR                  5      R                  S5      S   UR                  [        X!5      S._MF     snn$ s  snnf )Nz5Expected a dict of [str, paddle.Tensor] but received zKey `z2` is invalid, expected paddle.Tensor but received .)rN   rU   r   )
isinstancedictrj   typer0   r   TensorstrrN   splitrU   r   )r   r+   r2   s      r   r   r     s    gt$$CDM?S
 	
 !V]]++sLTRSWIV    MMO $DA 	
\'',R0WWQN
 	

 $  s   AC!r5   )r7   )0osra   typingr   r   r   r   r   rz   r   safetensorsr   r	   r
   r   r   r   r   r   PathLiker   r"   r)   ndarrayr#   arrayr8   int64float32int32bfloat16float16int16uint8int8boolfloat64float8_e4m3fnfloat8_e5m2	complex64rp   rK   rw   rN   rL   r!   r   r   rP   r   r   <module>r      s   	 
 - -   P P MQ#v}}$%19$sCx.1I
F *.C#v}}$%CC$%C tCH~&C 
	CD(u (c (d33E.F (@ /4!C$%!	#v}}
!J 6;S"**_%/2	#v}}
Dfmm!34 c288m9L  LL!
NNA
LL!
OOQ
NNA
LL!
LL!
KK
KK
NNA
!

a	. >>>>>>OO<<<<<<
++
,,KK##!!
  LL"((
NNBJJ
LL"((
OORZZ
NNBJJ
LL"((
LL"((
KK
KK
NNBJJ
"((
$  d3+=&> & V]]  #  %  Fd3-. 4T#s(^8K3L r   