wL i dZddlmZmZddlmZmZmZmZm Z m Z m Z ddl m Z e dZeegefZeege efZdZdd d d d Ze j(eZeGd dZdeddeeefdededede eefdef dZdedefdZy)z.Contains helpers to split tensors into shards.) dataclassfield)AnyCallableDictListOptionalTypeVarUnion)loggingTensorT5GBlJ)iʚ;i@Bi)TBGBMBKBcreZdZUedZeed<eee fed<eee efed<eeefed<dZ y) StateDictSplitF)init is_shardedmetadatafilename_to_tensorstensor_to_filenamec>t|jdkD|_y)N)lenrr)selfs i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/serialization/_base.py __post_init__zStateDictSplit.__post_init__-sd667!;N) __name__ __module__ __qualname__rrbool__annotations__rstrrrr r!rrr&sE%(J(38nc49n--S#X&r+6sr!)get_storage_idmax_shard_size state_dictget_storage_sizefilename_patternr,r-returnc i}g}i}d}d} t|tr t|}|jD]\} } t| trtj d| -|| } | | |vr|| j | P| g|| <|| } | |kDr| | z } |j | | i||| z|kDr|j |i}d}| || <|| z }| | z } t|dkDr|j |t|}|jD]$\} }|D]}|d|vs |D] } || || < $&|dk(rX|jd}td| i|t|ji|jD cic]} | |c} Si}i}t|D]J\}}|jd|dzd d |d }|D]} ||| < t|j||<Ltd| i||Scc} 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. rz8Skipping tensor %s as it is a string (bnb serialization)r)suffix total_size)rrr-05dz-of-) isinstancer'parse_size_to_intitemsloggerinfoappendrformatrlistkeys enumerate)r.r/r0r,r-storage_id_to_tensors shard_list current_shardcurrent_shard_sizer5keyr* storage_id tensor_size nb_shardsr@shardfilenametensor_name_to_filenameridxs r$split_state_dict_into_shards_factoryrN1sL35+-J(*MJ.#&*>:!'')&" V fc " KKRTW X $F+  !22%j188=69E%j1'v.   ' + %J   sFm ,   +n <   m ,M!" $ ck)k! M&"R =A-(JI2779 D EAw%1C!+CE#J1  A~#**"*5"J/!)4 0A+B C9C9JK#X K  ! +; U#**AcAgc]$yQTo2V*W 4C+3 #C ( 4(,UZZ\(:H% ;  +/2  Ls H size_as_strc*|j}|ddj}|tvrtd|d|dt|} t |ddj}t ||zS#t$r}td|d||d}~wwxYw)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 ``` NzUnit 'z:' not supported. Supported units are TB, GB, MB, KB. Got 'z'.z%Could not parse the size value from 'z': )stripupper SIZE_UNITS ValueErrorfloatint)rOunit multipliervaluees rr9r9s ##%K rs  ! ! #D :6$'abmannpqrrD!J]k#2&,,./ uz! "" ]@ SQRPSTU[\\]sA22 B;B  BN)__doc__ dataclassesrrtypingrrrrr r r r3r rrWTensorSizeFn_T StorageIDFn_TMAX_SHARD_SIZErT get_logger__file__r;rr'rNr9r(r!rrds5(FFF ) 7)S.)'HSM12          H % << <%8&4 }S'\"}%} } " } #s(O }}@#3#3#r!