Ë
    ëÿæið  ã                   ó  — d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
  ed«      Zeegef   Zeegedz  f   Zd	Zd
ddddœZ e
j"                  e«      Ze G d„ d«      «       Zd„ edœdeeef   dedededeez  defd„Zdedefd„Zy)z.Contains helpers to split tensors into shards.é    )ÚCallable)Ú	dataclassÚfield)ÚAnyÚTypeVaré   )ÚloggingÚTensorTNÚ5GBl    J)£i Êš;i@B iè  )ÚTBÚGBÚMBÚKBc                   ór   — e Zd ZU  ed¬«      Zeed<   eee	f   ed<   eee
e   f   ed<   eeef   ed<   d„ Zy)	ÚStateDictSplitF)ÚinitÚ
is_shardedÚmetadataÚfilename_to_tensorsÚtensor_to_filenamec                 ó>   — t        | j                  «      dkD  | _        y )Né   )Úlenr   r   )Úselfs    úx/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/huggingface_hub/serialization/_base.pyÚ__post_init__zStateDictSplit.__post_init__.   s   € Ü˜d×6Ñ6Ó7¸!Ñ;ˆ�ó    N)Ú__name__Ú
__module__Ú__qualname__r   r   ÚboolÚ__annotations__ÚdictÚstrr   Úlistr   © r   r   r   r   '   sE   … á %Ô(€J�Ó(Ø�3˜�8‰nÓØ˜c 4¨¡9˜nÑ-Ó-Ø˜S #˜X™Ó&ó<r   r   c                  ó   — y )Nr&   )Útensors    r   Ú<lambda>r)   7   s   € °4r   )Úget_storage_idÚmax_shard_sizeÚ
state_dictÚget_storage_sizeÚfilename_patternr*   r+   Úreturnc          	      ó  — i }g }i }d}d}	t        |t        «      rt        |«      }| j                  «       D ]§  \  }
}t        |t        «      rt        j                  d|
«       Œ- ||«      }|�||v r||   j                  |
«       ŒP|
g||<    ||«      }||kD  r|	|z  }	|j                  |
|i«       Œ|||z   |kD  r|j                  |«       i }d}|||
<   ||z  }|	|z  }	Œ© t        |«      dkD  r|j                  |«       t        |«      }|j                  «       D ]$  \  }}|D ]  }|d   |v sŒ|D ]
  }
| |
   ||
<   Œ  Œ$ Œ& |dk(  rX|j                  d¬«      }t        d|	i|t        | j                  «       «      i| j                  «       D �
ci c]  }
|
|“Œ c}
¬«      S i }i }t        |«      D ]J  \  }}|j                  d|dz   d	›d
|d	›�¬«      }|D ]  }
|||
<   Œ	 t        |j                  «       «      ||<   ŒL t        d|	i||¬«      S c c}
w )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].

    > [!WARNING]
    > 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`.

    Args:
        state_dict (`dict[str, Tensor]`):
            The state dictionary to save.
        get_storage_size (`Callable[[Tensor], int]`):
            A function that returns the size of a tensor when saved on disk in bytes.
        get_storage_id (`Callable[[Tensor], Optional[Any]]`, *optional*):
            A function that returns a unique identifier to a tensor storage. Multiple different tensors can share the
            same underlying storage. This identifier is guaranteed to be unique and constant for this tensor's storage
            during its lifetime. Two tensor storages with non-overlapping lifetimes may have the same id.
        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`
        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   z8Skipping tensor %s as it is a string (bnb serialization)r   Ú )ÚsuffixÚ
total_size)r   r   r   Ú-Ú05dz-of-)Ú
isinstancer$   Úparse_size_to_intÚitemsÚloggerÚinfoÚappendr   Úformatr   r%   ÚkeysÚ	enumerate)r,   r-   r.   r*   r+   Ústorage_id_to_tensorsÚ
shard_listÚcurrent_shardÚcurrent_shard_sizer3   Úkeyr(   Ú
storage_idÚtensor_sizeÚ	nb_shardsr=   ÚshardÚfilenameÚtensor_name_to_filenamer   Úidxs                        r   Ú$split_state_dict_into_shards_factoryrK   2   s–  € ðL 35Ðà+-€JØ(*€MØÐØ€Jä�.¤#Ô&Ü*¨>Ó:ˆà!×'Ñ'Ö)‰ˆˆVô �fœcÔ"Ü�K‰KÐRÐTWÔXØñ $ FÓ+ˆ
ØÐ!ØÐ2Ñ2à% jÑ1×8Ñ8¸Ô=Øð 69°EÐ% jÑ1ñ ' vÓ.ˆð ˜Ò'Ø˜+Ñ%ˆJØ×Ñ˜s F˜mÔ,Øð  Ñ+¨nÒ<Ø×Ñ˜mÔ,ØˆMØ!"Ðð $ˆ�cÑØ˜kÑ)ÐØ�kÑ!‰
ðM *ôR ˆ=Ó˜AÒØ×Ñ˜-Ô(Ü�J“€Ið 2×7Ñ7Ö9Ñˆ
�DãˆEØ�A‰w˜%ÒÛ�CØ!+¨C¡�E˜#’Jð  áñ	  ð :ð �A‚~Ø#×*Ñ*°"Ð*Ó5ˆÜØ" JÐ/Ø!)¬4°
·±Ó0AÓ+BÐ CØ9C¿¹Ô9JÓKÑ9J°#  X¡Ð9JÑKô
ð 	
ð !ÐØÐÜ 
Ö+‰
ˆˆUØ#×*Ñ*°A°c¸A±g¸c°]À$ÀyÐQTÀoÐ2VÐ*ÓWˆÛˆCØ+3Ð# CÒ(ð ä(,¨U¯Z©Z«\Ó(:Ð˜HÒ%ð	 ,ô Ø 
Ð+Ø/Ø2ôð ùò  Ls   Æ
H
Úsize_as_strc                 ó*  — | j                  «       } | dd j                  «       }|t        vrt        d|› d| › d�«      ‚t        |   }	 t	        | dd j                  «       «      }t        ||z  «      S # t        $ r}t        d| › d|› �«      |‚d}~ww xY w)aM  
    Parse a size expressed as a string with digits and unit (like `"5MB"`) to an integer (in bytes).

    Supported units are "TB", "GB", "MB", "KB".

    Args:
        size_as_str (`str`): The size to convert. Will be directly returned if an `int`.

    Example:

    ```py
    >>> parse_size_to_int("5MB")
    5000000
    ```
    éþÿÿÿNzUnit 'z:' not supported. Supported units are TB, GB, MB, KB. Got 'z'.z%Could not parse the size value from 'z': )ÚstripÚupperÚ
SIZE_UNITSÚ
ValueErrorÚfloatÚint)rL   ÚunitÚ
multiplierÚvalueÚes        r   r7   r7   ²   s½   € ð  ×#Ñ#Ó%€Kð �r�sÐ×!Ñ!Ó#€DØ”:ÑÜ˜6 $ Ð'aÐbmÐanÐnpÐqÓrÐrÜ˜DÑ!€Jð]Ü�k # 2Ð&×,Ñ,Ó.Ó/ˆô ˆu�zÑ!Ó"Ð"øô ò ]ÜÐ@ÀÀÈSÐQRÐPSÐTÓUÐ[\Ð\ûð]ús   ÁA2 Á2	BÁ;BÂB)Ú__doc__Úcollections.abcr   Údataclassesr   r   Útypingr   r   r1   r	   r
   rT   ÚTensorSizeFn_TÚStorageIDFn_TÚMAX_SHARD_SIZErQ   Ú
get_loggerÚ__file__r9   r   r#   r$   rK   r7   r&   r   r   Ú<module>rb      sû   ðñ 5å $ß (ß å ñ �)Ó
€Ø˜7˜) S˜.Ñ)€Ø˜'˜ C¨$¡JÐ.Ñ/€à€à
Ø
Ø
Ø
ñ	€
ð 
ˆ×	Ñ	˜HÓ	%€ð ÷<ð <ó ð<ñ %8Ø .ò}Ø�S˜'�\Ñ"ð}ð %ð}ð ð	}ð
 "ð}ð ˜#‘Ið}ð ó}ð@# 3ð #¨3ô #r   