o
    sh                  
   @   s>  d dl Z d dlmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZmZ ddeee
f deeeef  defdd	Z	ddeee
f d
eee jf deeeef  ddfddZdedeee
f fddZd
eee jf deee
f fddZdeeejf deee
f fddZdeee
f deeejf fddZdS )    N)DictOptionalUnion)Array)numpy	safe_opentensorsmetadatareturnc                 C   s   t | }tj||dS )a(  
    Saves a dictionary of tensors into raw bytes in safetensors format.

    Args:
        tensors (`Dict[str, 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.flax import save
    from jax import numpy as jnp

    tensors = {"embedding": jnp.zeros((512, 1024)), "attention": jnp.zeros((256, 256))}
    byte_data = save(tensors)
    ```
    r	   )_jnp2npr   save)r   r	   
np_tensors r   d/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/safetensors/flax.pyr      s   r   filenamec                 C   s   t | }tj|||dS )al  
    Saves a dictionary of tensors into raw bytes in safetensors format.

    Args:
        tensors (`Dict[str, 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.flax import save_file
    from jax import numpy as jnp

    tensors = {"embedding": jnp.zeros((512, 1024)), "attention": jnp.zeros((256, 256))}
    save_file(tensors, "model.safetensors")
    ```
    r   )r   r   	save_file)r   r   r	   r   r   r   r   r   (   s   r   datac                 C   s   t | }t|S )a  
    Loads a safetensors file into flax format from pure bytes.

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

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

    Example:

    ```python
    from safetensors.flax import load

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

    loaded = load(data)
    ```
    )r   load_np2jnp)r   flatr   r   r   r   K   s   
r   c                 C   sR   i }t | dd}| D ]	}||||< qW d   |S 1 s"w   Y  |S )a  
    Loads a safetensors file into flax format.

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

    Returns:
        `Dict[str, Array]`: dictionary that contains name as key, value as `Array`

    Example:

    ```python
    from safetensors.flax import load_file

    file_path = "./my_folder/bert.safetensors"
    loaded = load_file(file_path)
    ```
    flax)	frameworkN)r   keys
get_tensor)r   resultfkr   r   r   	load_filef   s   
r   
numpy_dictc                 C   $   |   D ]\}}t|| |< q| S N)itemsjnparray)r   r   vr   r   r   r         r   jnp_dictc                 C   r    r!   )r"   npasarray)r'   r   r%   r   r   r   r      r&   r   r!   )ostypingr   r   r   r   r(   	jax.numpyr#   jaxr   safetensorsr   strbytesr   PathLiker   r   r   ndarrayr   r$   r   r   r   r   r   <module>   s*    , 

#$$(