o
    shO                  
   @   sD  d dl Z d dlmZmZmZ d dlZd dlZd dlmZ ddee	ej
f deee	e	f  defddZ	ddee	ej
f d	ee	e jf deee	e	f  ddfd
dZddede	dee	ej
f fddZdd	ee	e jf dee	ej
f fddZddee	ejf de	dee	ej
f fddZdee	ej
f dee	ejf fddZdS )    N)DictOptionalUnion)numpy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, 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   )
_paddle2npr   save)r   r   
np_tensors r   f/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/safetensors/paddle.pyr   
   s   r   filenamec                 C   s   t | }tj|||dS )am  
    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	   )r
   r   	save_file)r   r   r   r   r   r   r   r   '   s   r   cpudatadevicec                 C   s   t | }t||S )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)
    ```
    )r   load
_np2paddle)r   r   flatr   r   r   r   J   s   

r   c                 C   s   t | }t||}|S )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   	load_filer   )r   r   r   outputr   r   r   r   e   s   

r   
numpy_dictc                 C   s(   |   D ]\}}tj||d| |< q| S )N)place)itemspaddle	to_tensor)r   r   kvr   r   r   r      s   r   paddle_dictc                 C   s*   |   D ]\}}|   | |< q| S N)r   detachr   r   )r    r   r   r   r   r   r
      s   r
   r!   )r   )ostypingr   r   r   r   npr   safetensorsstrTensorbytesr   PathLiker   r   r   ndarrayr   arrayr
   r   r   r   r   <module>   s(    . 
"#(,*