o
    sh2                  
   @   s:  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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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eeejf 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	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, tf.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.tensorflow import save
    import tensorflow as tf

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

    Args:
        tensors (`Dict[str, tf.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.tensorflow import save_file
    import tensorflow as tf

    tensors = {"embedding": tf.zeros((512, 1024)), "attention": tf.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 tensorflow format from pure bytes.

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

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

    Example:

    ```python
    from safetensors.tensorflow import load

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

    loaded = load(data)
    ```
    )r   load_np2tf)r   flatr   r   r   r   J   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 tensorflow format.

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

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

    Example:

    ```python
    from safetensors.tensorflow import load_file

    file_path = "./my_folder/bert.safetensors"
    loaded = load_file(file_path)
    ```
    tf)	frameworkN)r   keys
get_tensor)r   resultfkr   r   r   	load_filee   s   
r   
numpy_dictc                 C   s$   |   D ]\}}t|| |< q| S N)itemsr   convert_to_tensor)r   r   vr   r   r   r      s   r   tf_dictc                 C   s"   |   D ]
\}}| | |< q| S r   )r    r   )r#   r   r"   r   r   r   r      s   r   r   )ostypingr   r   r   r   np
tensorflowr   safetensorsr   strTensorbytesr   PathLiker   r   r   ndarrayr   arrayr   r   r   r   r   <module>   s(    . 
#&&*