
    %h              
          S SK r S SKJrJrJr  S SKrS SKJr	  S SK
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\\\	R                  4   4S jrS\\\ R                   4   S\\\	R                  4   4S jrS\\\R(                  4   S\\\	R                  4   4S jrS\\\	R                  4   S\\\R                  4   4S jrg)    N)DictOptionalUnion)numpy	safe_opentensorsmetadatareturnc                 @    [        U 5      n[        R                  " X!S9$ )a  
Saves a dictionary of tensors into raw bytes in safetensors format.

Args:
    tensors (`Dict[str, mx.array]`):
        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.mlx import save
import mlx.core as mx

tensors = {"embedding": mx.zeros((512, 1024)), "attention": mx.zeros((256, 256))}
byte_data = save(tensors)
```
r	   )_mx2npr   save)r   r	   
np_tensorss      G/var/www/auris/envauris/lib/python3.13/site-packages/safetensors/mlx.pyr   r   
   s    2 J::j44    filenamec                 B    [        U 5      n[        R                  " X1US9$ )a  
Saves a dictionary of tensors into raw bytes in safetensors format.

Args:
    tensors (`Dict[str, mx.array]`):
        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.mlx import save_file
import mlx.core as mx

tensors = {"embedding": mx.zeros((512, 1024)), "attention": mx.zeros((256, 256))}
save_file(tensors, "model.safetensors")
```
r   )r   r   	save_file)r   r   r	   r   s       r   r   r   '   s    > J??:(CCr   datac                 D    [         R                  " U 5      n[        U5      $ )a  
Loads a safetensors file into MLX format from pure bytes.

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

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

Example:

```python
from safetensors.mlx import load

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

loaded = load(data)
```
)r   load_np2mx)r   flats     r   r   r   J   s    . ::dD$<r   c                     0 n[        U SS9 nUR                  5        H  nUR                  U5      X'   M     SSS5        U$ ! , (       d  f       U$ = f)a|  
Loads a safetensors file into MLX format.

Args:
    filename (`str`, or `os.PathLike`)):
        The name of the file which contains the tensors

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

Example:

```python
from safetensors.flax import load_file

file_path = "./my_folder/bert.safetensors"
loaded = load_file(file_path)
```
mlx)	frameworkN)r   keys
get_tensor)r   resultfks       r   	load_filer"   e   sP    ( F	8u	-AQFI  
. M 
.	- Ms   +A
A
numpy_dictc                 h    U R                  5        H  u  p[        R                  " U5      X'   M     U $ N)itemsmxarray)r#   r!   vs      r   r   r      s,      "
 #r   mx_dictc                 l    0 nU R                  5        H  u  p#[        R                  " U5      X'   M     U$ r%   )r&   npasarray)r*   new_dictr!   r)   s       r   r   r      s-    Hjjm  Or   r%   )ostypingr   r   r   r   r,   mlx.corecorer'   safetensorsr   strr(   bytesr   PathLiker   r   r"   ndarrayr   r    r   r   <module>r9      sQ   	 ( (   (5$sBHH}% 5$sCx.1I 5UZ 5@ *. D#rxx-  DC$% D tCH~& D 
	 DFu c288m, 6c2;;./ Dbhh4G 6tCO, c288m1D Dbhh' Dbhh,? r   