
    g)              	           d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
mZmZ erddlZej                  e
dd	eed
f   dedeeef   defdZdd
defdZdefdZy)z%Contains tensorflow-specific helpers.    N)TYPE_CHECKINGDictUnion   )	constants   )MAX_SHARD_SIZEStateDictSplit$split_state_dict_into_shards_factory)filename_patternmax_shard_size
state_dictz	tf.Tensorr   r   returnc                (    t        | ||t              S )a=  
    Split a model state dictionary in shards so that each shard is smaller than a given size.

    The shards are determined by iterating through the `state_dict` in the order of its keys. There is no optimization
    made to make each shard as close as possible to the maximum size passed. For example, if the limit is 10GB and we
    have tensors of sizes [6GB, 6GB, 2GB, 6GB, 2GB, 2GB] they will get sharded as [6GB], [6+2GB], [6+2+2GB] and not
    [6+2+2GB], [6+2GB], [6GB].

    <Tip warning={true}>

    If one of the model's tensor is bigger than `max_shard_size`, it will end up in its own shard which will have a
    size greater than `max_shard_size`.

    </Tip>

    Args:
        state_dict (`Dict[str, Tensor]`):
            The state dictionary to save.
        filename_pattern (`str`, *optional*):
            The pattern to generate the files names in which the model will be saved. Pattern must be a string that
            can be formatted with `filename_pattern.format(suffix=...)` and must contain the keyword `suffix`
            Defaults to `"tf_model{suffix}.h5"`.
        max_shard_size (`int` or `str`, *optional*):
            The maximum size of each shard, in bytes. Defaults to 5GB.

    Returns:
        [`StateDictSplit`]: A `StateDictSplit` object containing the shards and the index to retrieve them.
    )r   r   get_storage_size)r   get_tf_storage_size)r   r   r   s      ^/var/www/openai/venv/lib/python3.12/site-packages/huggingface_hub/serialization/_tensorflow.pysplit_tf_state_dict_into_shardsr      s    D 0%),	     tensorc                     t        j                  | j                         j                  t	        | j
                        z        S )N)mathceilnumpysize_dtype_byte_size_tfdtype)r   s    r   r   r   F   s.     99V\\^((+>v||+LLMMr   c                     ddl }| |j                  k(  ryt        j                  d| j                        }|t        d|  d      t        |j                         d         }|dz  S )aZ  
    Returns the size (in bytes) occupied by one parameter of type `dtype`.
    Taken from https://github.com/huggingface/transformers/blob/74d9d0cebb0263a3f8ab9c280569170cc74651d0/src/transformers/modeling_tf_utils.py#L608.
    NOTE: why not `tensor.numpy().nbytes`?
    Example:
    ```py
    >>> _dtype_byte_size(tf.float32)
    4
    ```
    r   Ng      ?z[^\d](\d+)$z`dtype` is not a valid dtype: .   )
tensorflowboolresearchname
ValueErrorintgroups)r   tf
bit_searchbit_sizes       r   r   r   L   sg     >5::6J9%BCC:$$&q)*Hq=r   )__doc__r   r#   typingr   r   r    r   _baser	   r
   r   r!   r)   TF2_WEIGHTS_FILE_PATTERNstrr'   r   r   floatr    r   r   <module>r4      s    ,  	 - -  W W  &>>&4	'S+%&' ' #s(O	'
 'TN N N% r   