U
    ”Ê¹jj  ã                   @   s’   d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZ eZeZe
d ZeG dd„ dƒƒZeG dd	„ d	ƒƒZeG d
d„ dƒƒZdS )é    N)Údefaultdict)Ú	dataclassÚfield)ÚDictÚListÚLiteralÚOptionalÚTuple)
ZF64ZF32ZF16ZBF16ZI64ZI32ZI16ZI8ZU8ZBOOLc                   @   sV   e Zd ZU dZeed< ee ed< eeef ed< e	dd�Z
eed< dd	œd
d„ZdS )Ú
TensorInfoa<  Information about a tensor.

    For more details regarding the safetensors format, check out https://huggingface.co/docs/safetensors/index#format.

    Attributes:
        dtype (`str`):
            The data type of the tensor ("F64", "F32", "F16", "BF16", "I64", "I32", "I16", "I8", "U8", "BOOL").
        shape (`List[int]`):
            The shape of the tensor.
        data_offsets (`Tuple[int, int]`):
            The offsets of the data in the file as a tuple `[BEGIN, END]`.
        parameter_count (`int`):
            The number of parameters in the tensor.
    ÚdtypeÚshapeZdata_offsetsF©ÚinitÚparameter_countN©Úreturnc                 C   s6   zt  tj| j¡| _W n tk
r0   d| _Y nX d S )Né   )Ú	functoolsÚreduceÚoperatorÚmulr   r   Ú	TypeError)Úself© r   úa/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/utils/_safetensors.pyÚ__post_init__#   s    zTensorInfo.__post_init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDTYPE_TÚ__annotations__r   Úintr	   r   r   r   r   r   r   r   r
      s   
r
   c                   @   sZ   e Zd ZU dZeeef ed< eeef ed< e	dd�Z
eeef ed< ddœd	d
„ZdS )ÚSafetensorsFileMetadataaÙ  Metadata for a Safetensors file hosted on the Hub.

    This class is returned by [`parse_safetensors_file_metadata`].

    For more details regarding the safetensors format, check out https://huggingface.co/docs/safetensors/index#format.

    Attributes:
        metadata (`Dict`):
            The metadata contained in the file.
        tensors (`Dict[str, TensorInfo]`):
            A map of all tensors. Keys are tensor names and values are information about the corresponding tensor, as a
            [`TensorInfo`] object.
        parameter_count (`Dict[str, int]`):
            A map of the number of parameters per data type. Keys are data types and values are the number of parameters
            of that data type.
    ÚmetadataÚtensorsFr   r   Nr   c                 C   s:   t tƒ}| j ¡ D ]}||j  |j7  < qt|ƒ| _d S ©N)r   r"   r%   Úvaluesr   r   Údict)r   r   Ztensorr   r   r   r   B   s    z%SafetensorsFileMetadata.__post_init__)r   r   r   r   r   Ústrr!   ÚTENSOR_NAME_Tr
   r   r   r    r"   r   r   r   r   r   r#   +   s
   
r#   c                   @   sn   e Zd ZU dZee ed< eed< eee	f ed< ee	e
f ed< edd�Zeeef ed< d	d
œdd„Zd	S )ÚSafetensorsRepoMetadataa  Metadata for a Safetensors repo.

    A repo is considered to be a Safetensors repo if it contains either a 'model.safetensors' weight file (non-shared
    model) or a 'model.safetensors.index.json' index file (sharded model) at its root.

    This class is returned by [`get_safetensors_metadata`].

    For more details regarding the safetensors format, check out https://huggingface.co/docs/safetensors/index#format.

    Attributes:
        metadata (`Dict`, *optional*):
            The metadata contained in the 'model.safetensors.index.json' file, if it exists. Only populated for sharded
            models.
        sharded (`bool`):
            Whether the repo contains a sharded model or not.
        weight_map (`Dict[str, str]`):
            A map of all weights. Keys are tensor names and values are filenames of the files containing the tensors.
        files_metadata (`Dict[str, SafetensorsFileMetadata]`):
            A map of all files metadata. Keys are filenames and values are the metadata of the corresponding file, as
            a [`SafetensorsFileMetadata`] object.
        parameter_count (`Dict[str, int]`):
            A map of the number of parameters per data type. Keys are data types and values are the number of parameters
            of that data type.
    r$   ZshardedZ
weight_mapÚfiles_metadataFr   r   Nr   c                 C   sJ   t tƒ}| j ¡ D ](}|j ¡ D ]\}}||  |7  < q qt|ƒ| _d S r&   )r   r"   r,   r'   r   Úitemsr(   )r   r   Zfile_metadatar   Znb_parameters_r   r   r   r   j   s
    z%SafetensorsRepoMetadata.__post_init__)r   r   r   r   r   r   r!   Úboolr*   Ú
FILENAME_Tr#   r   r   r    r"   r   r   r   r   r   r+   I   s   
r+   )r   r   Úcollectionsr   Údataclassesr   r   Útypingr   r   r   r   r	   r)   r/   r*   r    r
   r#   r+   r   r   r   r   Ú<module>   s   