
    g                     b   d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZ dd	lmZmZ d
dlmZmZmZ  ej@                  e!      Z"erddl#Z#ddeddddddddee$ef   dee$   de%dee&e$f   deee$e$f      de%de%deee$      fdZ'ddeddddddee$df   dee$ef   dee$   de%dee&e$f   deee$e$f      de%de%deee$      ddfdZ(ejR                  eddee$df   de$dee&e$f   defdZ*ddddddd ddd!ee$ejV                  f   d"e%d#e%d$e%d%eee$d&f      d'e%dee$   defd(Z,ddejR                  d)dddejV                  d"e%d$e%de$defd*Z-	 	 	 dHd+ee$ejV                  f   d%eee$d&f      d$e%d'e%deee$df   ef   f
d,Z.ddd-ee$   ddfd.Z/d/ddee&eed0f   f   fd1Z0d/ddeed&ee&eed0f   f   e&f      fd2Z1d/dde&fd3Z2 e	       dId4       Z3d/ddee&eed0f   f   fd5Z4	 	 dJdee$df   dee$e$f   de%deee$      fd6Z5d/dde&fd7Z6d8eee$      dee$df   deee$      fd9Z7dee$df   deee$      fd:Z8d/dde%fd;Z9ddd<dee$df   d=eee$      d>eee$      dee$ee$   f   fd?Z: e	       d@dAde&fdB       Z; G dC dD edEdFdGg            Z<y)Kz"Contains pytorch-specific helpers.    N)defaultdict
namedtuple)	lru_cache)Path)
TYPE_CHECKINGAnyDictIterableList
NamedTupleOptionalSetTupleUnion)version   )	constantslogging   )MAX_SHARD_SIZEStateDictSplit$split_state_dict_into_shards_factoryT)filename_patternforce_contiguousmax_shard_sizemetadatasafe_serializationis_main_processshared_tensors_to_discardmodelztorch.nn.Modulesave_directoryr   r   r   r   r   r   r   c                H    t        | j                         ||||||||	       y)a	  
    Saves a given torch model to disk, handling sharding and shared tensors issues.

    See also [`save_torch_state_dict`] to save a state dict with more flexibility.

    For more information about tensor sharing, check out [this guide](https://huggingface.co/docs/safetensors/torch_shared_tensors).

    The model state dictionary is split into shards so that each shard is smaller than a given size. The shards are
    saved in the `save_directory` with the given `filename_pattern`. If the model is too big to fit in a single shard,
    an index file is saved in the `save_directory` to indicate where each tensor is saved. This helper uses
    [`split_torch_state_dict_into_shards`] under the hood. If `safe_serialization` is `True`, the shards are saved as
    safetensors (the default). Otherwise, the shards are saved as pickle.

    Before saving the model, the `save_directory` is cleaned from any previous shard files.

    <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>

    <Tip warning={true}>

    If your model is a `transformers.PreTrainedModel`, you should pass `model._tied_weights_keys` as `shared_tensors_to_discard` to properly handle shared tensors saving. This ensures the correct duplicate tensors are discarded during saving.

    </Tip>

    Args:
        model (`torch.nn.Module`):
            The model to save on disk.
        save_directory (`str` or `Path`):
            The directory in which the model will be saved.
        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 `"model{suffix}.safetensors"` or `pytorch_model{suffix}.bin` depending on `safe_serialization`
            parameter.
        force_contiguous (`boolean`, *optional*):
            Forcing the state_dict to be saved as contiguous tensors. This has no effect on the correctness of the
            model, but it could potentially change performance if the layout of the tensor was chosen specifically for
            that reason. Defaults to `True`.
        max_shard_size (`int` or `str`, *optional*):
            The maximum size of each shard, in bytes. Defaults to 5GB.
        metadata (`Dict[str, str]`, *optional*):
            Extra information to save along with the model. Some metadata will be added for each dropped tensors.
            This information will not be enough to recover the entire shared structure but might help understanding
            things.
        safe_serialization (`bool`, *optional*):
            Whether to save as safetensors, which is the default behavior. If `False`, the shards are saved as pickle.
            Safe serialization is recommended for security reasons. Saving as pickle is deprecated and will be removed
            in a future version.
        is_main_process (`bool`, *optional*):
            Whether the process calling this is the main process or not. Useful when in distributed training like
            TPUs and need to call this function from all processes. In this case, set `is_main_process=True` only on
            the main process to avoid race conditions. Defaults to True.
        shared_tensors_to_discard (`List[str]`, *optional*):
            List of tensor names to drop when saving shared tensors. If not provided and shared tensors are
            detected, it will drop the first name alphabetically.

    Example:

    ```py
    >>> from huggingface_hub import save_torch_model
    >>> model = ... # A PyTorch model

    # Save state dict to "path/to/folder". The model will be split into shards of 5GB each and saved as safetensors.
    >>> save_torch_model(model, "path/to/folder")

    # Load model back
    >>> from huggingface_hub import load_torch_model  # TODO
    >>> load_torch_model(model, "path/to/folder")
    >>>
    ```
    )	
state_dictr   r   r   r   r   r!   r   r   N)save_torch_state_dictr#   )	r    r!   r   r   r   r   r   r   r   s	            Y/var/www/openai/venv/lib/python3.12/site-packages/huggingface_hub/serialization/_torch.pysave_torch_modelr&   '   s1    n ##%))%-%'";
    r#   ztorch.Tensorreturnc          	         t        |      }|"|rt        j                  nt        j                  }|i }|r	 ddlm}	 t        | |||      } nddlm	}	 t        j                  d       t        | ||      }|rt        j                  |j                  d	
      dz         }t!        j"                  |      D ]a  }|j%                  |      s	 t        j'                  d| d       t!        j(                  t         j*                  j-                  ||             c ddi}|j0                  s|j3                  |       |rd|ini }|j4                  j7                         D ]X  \  }}|D ci c]  }|| |   
 }} |	|t         j*                  j-                  ||      fi | t        j'                  d|        Z |j0                  r|j                  d
      dz   }i |j8                  ||j:                  d}t=        t         j*                  j-                  ||      d      5 }t?        j@                  ||d       ddd       t        jC                  d| dtE        |j4                         d| d       t        jC                  d| d        y# t        $ r}
t        d      |
d}
~
ww xY w# t.        $ r'}
t        j                  d| d|
 d       Y d}
~
d}
~
ww xY wc c}w # 1 sw Y   xY w)!a  
    Save a model state dictionary to the disk, handling sharding and shared tensors issues.

    See also [`save_torch_model`] to directly save a PyTorch model.

    For more information about tensor sharing, check out [this guide](https://huggingface.co/docs/safetensors/torch_shared_tensors).

    The model state dictionary is split into shards so that each shard is smaller than a given size. The shards are
    saved in the `save_directory` with the given `filename_pattern`. If the model is too big to fit in a single shard,
    an index file is saved in the `save_directory` to indicate where each tensor is saved. This helper uses
    [`split_torch_state_dict_into_shards`] under the hood. If `safe_serialization` is `True`, the shards are saved as
    safetensors (the default). Otherwise, the shards are saved as pickle.

    Before saving the model, the `save_directory` is cleaned from any previous shard files.

    <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>

    <Tip warning={true}>

    If your model is a `transformers.PreTrainedModel`, you should pass `model._tied_weights_keys` as `shared_tensors_to_discard` to properly handle shared tensors saving. This ensures the correct duplicate tensors are discarded during saving.

    </Tip>

    Args:
        state_dict (`Dict[str, torch.Tensor]`):
            The state dictionary to save.
        save_directory (`str` or `Path`):
            The directory in which the model will be saved.
        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 `"model{suffix}.safetensors"` or `pytorch_model{suffix}.bin` depending on `safe_serialization`
            parameter.
        force_contiguous (`boolean`, *optional*):
            Forcing the state_dict to be saved as contiguous tensors. This has no effect on the correctness of the
            model, but it could potentially change performance if the layout of the tensor was chosen specifically for
            that reason. Defaults to `True`.
        max_shard_size (`int` or `str`, *optional*):
            The maximum size of each shard, in bytes. Defaults to 5GB.
        metadata (`Dict[str, str]`, *optional*):
            Extra information to save along with the model. Some metadata will be added for each dropped tensors.
            This information will not be enough to recover the entire shared structure but might help understanding
            things.
        safe_serialization (`bool`, *optional*):
            Whether to save as safetensors, which is the default behavior. If `False`, the shards are saved as pickle.
            Safe serialization is recommended for security reasons. Saving as pickle is deprecated and will be removed
            in a future version.
        is_main_process (`bool`, *optional*):
            Whether the process calling this is the main process or not. Useful when in distributed training like
            TPUs and need to call this function from all processes. In this case, set `is_main_process=True` only on
            the main process to avoid race conditions. Defaults to True.
        shared_tensors_to_discard (`List[str]`, *optional*):
            List of tensor names to drop when saving shared tensors. If not provided and shared tensors are
            detected, it will drop the first name alphabetically.

    Example:

    ```py
    >>> from huggingface_hub import save_torch_state_dict
    >>> model = ... # A PyTorch model

    # Save state dict to "path/to/folder". The model will be split into shards of 5GB each and saved as safetensors.
    >>> state_dict = model_to_save.state_dict()
    >>> save_torch_state_dict(state_dict, "path/to/folder")
    ```
    Nr   )	save_filezjPlease install `safetensors` to use safe serialization. You can install it with `pip install safetensors`.)r   r   )savea  You are using unsafe serialization. Due to security reasons, it is recommended not to load pickled models from untrusted sources. If you intend to share your model, we strongly recommend using safe serialization by installing `safetensors` with `pip install safetensors`.r   r   z(-\d{5}-of-\d{5})?suffixz(\.index\.json)?zRemoving existing file 'z' from folder.z&Error when trying to remove existing 'z' from folder: z. Continuing...formatptr   zShard saved to  .index.json)r   
weight_mapwr   )indentz:The model is bigger than the maximum size per checkpoint (z#). Model weighs have been saved in z^ checkpoint shards. You can find where each parameters has been saved in the index located at .z$Model weights successfully saved to !)#strr    SAFETENSORS_WEIGHTS_FILE_PATTERNPYTORCH_WEIGHTS_FILE_PATTERNsafetensors.torchr*   ImportError!_clean_state_dict_for_safetensorstorchr+   loggerwarning"split_torch_state_dict_into_shardsrecompiler/   oslistdirmatchdebugremovepathjoin	Exception
is_shardedupdatefilename_to_tensorsitemsr   tensor_to_filenameopenjsondumpinfolen)r#   r!   r   r   r   r   r   r   r   save_file_fnestate_dict_splitexisting_files_regexfilenameper_file_metadatasafe_file_kwargstensorstensorshard
index_pathindexfs                         r%   r$   r$      s   f (N " 6677 	 	C 7-&?	

 	/c	
 :%5n
 !zz*:*A*AI^*A*_bu*uv

>2H#))(3LL#;H:^!TUIIbggll>8DE	 3 "4(&&  *:L
$56RT-AAGGI':AB'F++'BUBGGLLBWFVWxj12 J ""%,,B,7-G
A+44AA*==
 "'',,~z:C@AIIeQq) AHHX Y//23C3W3W/X.Y ZYYcXddeg	
 KK6~6FaHI  	E 	B ! NN@
/Z[Y\\kl  C A@sB   J AJ)&KK!	J&J!!J&)	K2KK!K*r,   c                2    t        | ||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>

    To save a model state dictionary to the disk, see [`save_torch_state_dict`]. This helper uses
    `split_torch_state_dict_into_shards` under the hood.

    </Tip>

    <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, torch.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 `"model{suffix}.safetensors"`.
        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.

    Example:
    ```py
    >>> import json
    >>> import os
    >>> from safetensors.torch import save_file as safe_save_file
    >>> from huggingface_hub import split_torch_state_dict_into_shards

    >>> def save_state_dict(state_dict: Dict[str, torch.Tensor], save_directory: str):
    ...     state_dict_split = split_torch_state_dict_into_shards(state_dict)
    ...     for filename, tensors in state_dict_split.filename_to_tensors.items():
    ...         shard = {tensor: state_dict[tensor] for tensor in tensors}
    ...         safe_save_file(
    ...             shard,
    ...             os.path.join(save_directory, filename),
    ...             metadata={"format": "pt"},
    ...         )
    ...     if state_dict_split.is_sharded:
    ...         index = {
    ...             "metadata": state_dict_split.metadata,
    ...             "weight_map": state_dict_split.tensor_to_filename,
    ...         }
    ...         with open(os.path.join(save_directory, "model.safetensors.index.json"), "w") as f:
    ...             f.write(json.dumps(index, indent=2))
    ```
    )r   r   get_storage_sizeget_storage_id)r   get_torch_storage_sizeget_torch_storage_id)r#   r   r   s      r%   rA   rA   .  s!    F 0%)/+ r'   F)strictsafeweights_onlymap_locationmmapr   checkpoint_pathrh   ri   rj   rk   ztorch.devicerl   c                   t        |      }|j                         st        d| d      |j                         r!t	        |||      }| j                  ||      S |Jt        j                  }||j                  d      dz   z  }	|	j                         s|st        j                  }||j                  d      dz   z  }	|	j                         rt        | ||||      S t        |j                  |rd	nd
            }
t        |
      dk(  r%t	        |
d   |||      }| j                  ||      S t        d| d      )a  
    Load a checkpoint into a model, handling both sharded and non-sharded checkpoints.

    Args:
        model (`torch.nn.Module`):
            The model in which to load the checkpoint.
        checkpoint_path (`str` or `os.PathLike`):
            Path to either the checkpoint file or directory containing the checkpoint(s).
        strict (`bool`, *optional*, defaults to `False`):
            Whether to strictly enforce that the keys in the model state dict match the keys in the checkpoint.
        safe (`bool`, *optional*, defaults to `True`):
            If `safe` is True, the safetensors files will be loaded. If `safe` is False, the function
            will first attempt to load safetensors files if they are available, otherwise it will fall back to loading
            pickle files. `filename_pattern` parameter takes precedence over `safe` parameter.
        weights_only (`bool`, *optional*, defaults to `False`):
            If True, only loads the model weights without optimizer states and other metadata.
            Only supported in PyTorch >= 1.13.
        map_location (`str` or `torch.device`, *optional*):
            A `torch.device` object, string or a dict specifying how to remap storage locations. It
            indicates the location where all tensors should be loaded.
        mmap (`bool`, *optional*, defaults to `False`):
            Whether to use memory-mapped file loading. Memory mapping can improve loading performance
            for large models in PyTorch >= 2.1.0 with zipfile-based checkpoints.
        filename_pattern (`str`, *optional*):
            The pattern to look for the index file. Pattern must be a string that
            can be formatted with `filename_pattern.format(suffix=...)` and must contain the keyword `suffix`
            Defaults to `"model{suffix}.safetensors"`.
    Returns:
        `NamedTuple`: A named tuple with `missing_keys` and `unexpected_keys` fields.
            - `missing_keys` is a list of str containing the missing keys, i.e. keys that are in the model but not in the checkpoint.
            - `unexpected_keys` is a list of str containing the unexpected keys, i.e. keys that are in the checkpoint but not in the model.

    Raises:
        [`FileNotFoundError`](https://docs.python.org/3/library/exceptions.html#FileNotFoundError)
            If the checkpoint file or directory does not exist.
        [`ImportError`](https://docs.python.org/3/library/exceptions.html#ImportError)
            If safetensors or torch is not installed when trying to load a .safetensors file or a PyTorch checkpoint respectively.
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
           If the checkpoint path is invalid or if the checkpoint format cannot be determined.

    Example:
    ```python
    >>> from huggingface_hub import load_torch_model
    >>> model = ... # A PyTorch model
    >>> load_torch_model(model, "path/to/checkpoint")
    ```
    zCheckpoint path z does not exist)checkpoint_filerk   rj   rh   r1   r-   r2   )r    r!   rh   rj   r   z*.safetensorsz*.binr   r   )ro   rk   rj   rl   zDirectory 'zw' does not contain a valid checkpoint. Expected either a sharded checkpoint with an index file, or a single model file.)r   exists
ValueErroris_fileload_state_dict_from_fileload_state_dictr   r9   r/   r:   _load_sharded_checkpointlistglobrU   )r    rm   rh   ri   rj   rk   rl   r   r#   r`   model_filess              r%   load_torch_modelrz   }  su   t ?+O!!#+O+<OLMM .+%%


 $$Z$?? $EE$(8(?(?r(?(J](Z[
!!#D(EE $4$;$;2$;$F$VWJ'*%-
 	
 ++tOQRK
;1.'N%%	

 $$Z$??

o& '[ 	[ r'   )rh   rj   r   c                   |j                  d      dz   }t        j                  j                  ||      }t	        |dd      5 }t        j                  |      }ddd       |rt        | d   j                                t        t        d   j                                     }	|	D ]D  }
t        j                  j                  ||
      }t        |d	|
      }| j                  ||       ~F t        |d   j                               }t        | j                         j                               }t        t        ||z
        t        ||z
              S # 1 sw Y   xY w)a  
    Loads a sharded checkpoint into a model. This is the same as
    [`torch.nn.Module.load_state_dict`](https://pytorch.org/docs/stable/generated/torch.nn.Module.html?highlight=load_state_dict#torch.nn.Module.load_state_dict)
    but for a sharded checkpoint. Each shard is loaded one by one and removed from memory after being loaded into the model.

    Args:
        model (`torch.nn.Module`):
            The model in which to load the checkpoint.
        save_directory (`str` or `os.PathLike`):
            A path to a folder containing the sharded checkpoint.
        strict (`bool`, *optional*, defaults to `False`):
            Whether to strictly enforce that the keys in the model state dict match the keys in the sharded checkpoint.
        weights_only (`bool`, *optional*, defaults to `False`):
            If True, only loads the model weights without optimizer states and other metadata.
            Only supported in PyTorch >= 1.13.
        filename_pattern (`str`, *optional*, defaults to `"model{suffix}.safetensors"`):
            The pattern to look for the index file. Pattern must be a string that
            can be formatted with `filename_pattern.format(suffix=...)` and must contain the keyword `suffix`
            Defaults to `"model{suffix}.safetensors"`.

    Returns:
        `NamedTuple`: A named tuple with `missing_keys` and `unexpected_keys` fields,
            - `missing_keys` is a list of str containing the missing keys
            - `unexpected_keys` is a list of str containing the unexpected keys
    r1   r-   r2   rzutf-8)encodingNr3   cpu)rk   rj   rp   )missing_keysunexpected_keys)r/   rD   rI   rJ   rQ   rR   load!_validate_keys_for_strict_loadingkeysrw   setvaluesrt   ru   r#   _IncompatibleKeys)r    r!   rh   rj   r   r`   
index_filerb   ra   shard_files
shard_file
shard_pathr#   loaded_keys
model_keyss                  r%   rv   rv     s<   H "(((3mCJnj9J	j#	0A		! 
1
 )%|1D1I1I1KL s5.55789K!
WW\\.*=
.%

 	j8 " eL)..01KU%%',,./J*{23T+XbJbEc 7 
1	0s   EE ro   c                 X   t        |       }|j                         st        d| d      |j                  dk(  r	 ddlm} ddlm}  || d	
      5 }|j                         }	ddd       	"|	j                  d      dvrt        d|  d      |!t        |d      rt        |j                        n|}
|
dk(  rt        j!                  d       d}
 || |
      S 	 ddl}ddlm} i }t'        j(                  |j*                        t'        j(                  d      k\  r||d<   t'        j(                  |j*                        t'        j(                  d      k\  r||d<    || fd|i|S # t        $ r}t        d      |d}~ww xY w# 1 sw Y   "xY w# t        $ r}t        d      |d}~ww xY w)aW
  
    Loads a checkpoint file, handling both safetensors and pickle checkpoint formats.

    Args:
        checkpoint_file (`str` or `os.PathLike`):
            Path to the checkpoint file to load. Can be either a safetensors or pickle (`.bin`) checkpoint.
        map_location (`str` or `torch.device`, *optional*):
            A `torch.device` object, string or a dict specifying how to remap storage locations. It
            indicates the location where all tensors should be loaded.
        weights_only (`bool`, *optional*, defaults to `False`):
            If True, only loads the model weights without optimizer states and other metadata.
            Only supported for pickle (`.bin`) checkpoints with PyTorch >= 1.13. Has no effect when
            loading safetensors files.
        mmap (`bool`, *optional*, defaults to `False`):
            Whether to use memory-mapped file loading. Memory mapping can improve loading performance
            for large models in PyTorch >= 2.1.0 with zipfile-based checkpoints. Has no effect when
            loading safetensors files, as the `safetensors` library uses memory mapping by default.

    Returns:
        `Union[Dict[str, "torch.Tensor"], Any]`: The loaded checkpoint.
            - For safetensors files: always returns a dictionary mapping parameter names to tensors.
            - For pickle files: returns any Python object that was pickled (commonly a state dict, but could be
              an entire model, optimizer state, or any other Python object).

    Raises:
        [`FileNotFoundError`](https://docs.python.org/3/library/exceptions.html#FileNotFoundError)
            If the checkpoint file does not exist.
        [`ImportError`](https://docs.python.org/3/library/exceptions.html#ImportError)
            If safetensors or torch is not installed when trying to load a .safetensors file or a PyTorch checkpoint respectively.
        [`OSError`](https://docs.python.org/3/library/exceptions.html#OSError)
            If the checkpoint file format is invalid or if git-lfs files are not properly downloaded.
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If the checkpoint file path is empty or invalid.

    Example:
    ```python
    >>> from huggingface_hub import load_state_dict_from_file

    # Load a PyTorch checkpoint
    >>> state_dict = load_state_dict_from_file("path/to/model.bin", map_location="cpu")
    >>> model.load_state_dict(state_dict)

    # Load a safetensors checkpoint
    >>> state_dict = load_state_dict_from_file("path/to/model.safetensors")
    >>> model.load_state_dict(state_dict)
    ```
    zNo checkpoint file found at 'zO'. Please verify the path is correct and the file has been properly downloaded.z.safetensorsr   )	safe_open)	load_filezoPlease install `safetensors` to load safetensors checkpoint. You can install it with `pip install safetensors`.Nr0   )	frameworkr/   )r0   mlxz"The safetensors archive passed at zg does not contain the valid metadata. Make sure you save your model with the `save_torch_model` method.typemetazJMeta device is not supported with safetensors. Falling back to CPU device.r~   )device)r   zZPlease install `torch` to load torch tensors. You can install it with `pip install torch`.z2.1.0rl   z1.13.0rj   rk   )r   rs   FileNotFoundErrorr.   safetensorsr   r;   r   r<   r   getOSErrorhasattrr8   r   r?   r@   r>   r   r   parse__version__)ro   rk   rj   rl   rm   r   r   rW   rb   r   r   r>   r   additional_kwargss                 r%   rt   rt   .  s   j ?+O ""$+O+< =5 5
 	
 /	-3 $71zz|H 8 HLL$:-$O4_4E FJ J  ,8+CP\^dHe\&&'kwVNNghF88 }}U&&'7==+AA$(&! }}U&&'7==+BB,8.)!  M  	E 	 87$  k
	s;   E% F
F %	E?.E::E?F	F)F$$F)r   c                    t        |      }t        | j                         j                               }||z
  }||z
  }|s|rd| j                  j                   }|r5dj                  t        |      D cg c]  }d| d
 c}      }|d| dz  }|r5dj                  t        |      D cg c]  }d| d
 c}      }	|d|	 dz  }t        |      yc c}w c c}w )a  
    Validate that model keys match loaded keys when strict loading is enabled.

    Args:
        model: The PyTorch model being loaded
        loaded_keys: The keys present in the checkpoint

    Raises:
        RuntimeError: If there are missing or unexpected keys in strict mode
    z#Error(s) in loading state_dict for ,"z
Missing key(s): r6   z
Unexpected key(s): N)r   r#   r   	__class____name__rJ   sortedRuntimeError)
r    r   loaded_keys_setr   r   r   error_messagekstr_missing_keysstr_unexpected_keyss
             r%   r   r     s    +&OU%%',,./J/L%
2O=eoo>V>V=WX"xx6,;O(P;Oa1QCq;O(PQ12B1C1EEM"%((f_>U+V>Uas!H>U+V"W45H4IKKM=)) ' )Q ,Ws   4C+Cr^   .c                 2    	 ddl m}  |       r' j                         \  }}t         fd|D              S 	  j
                  j                  dk(  r+t               r!ddl}|j                  j                         }|S t               }|S # t        $ r Y \w xY w)zReturns a unique id for plain tensor
    or a (potentially nested) Tuple of unique id for the flattened Tensor
    if the input is a wrapper tensor subclass Tensor
    r   is_traceable_wrapper_subclassc              3   H   K   | ]  }t        t        |              y wN)_get_unique_idgetattr.0attrr^   s     r%   	<genexpr>z!_get_unique_id.<locals>.<genexpr>  s     Q54(=>5   "xlaN)torch.utils._python_dispatchr   __tensor_flatten__tupler<   r   r   is_torch_tpu_available	torch_xla_XLAC_xla_get_tensor_idstorage_ptr)r^   r   attrs_r   	unique_ids   `     r%   r   r     s    
N(0002HE1Q5QQQ 1 }}U"'='?
 	OO66v>	   '	  s   4B
 
	BBc                 x    | j                   j                  dk(  ry| j                   t        |       t        |       fS )a,  
    Return 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.
    In the case of meta tensors, we return None since we can't tell if they share the same storage.

    Taken from https://github.com/huggingface/transformers/blob/1ecf5f7c982d761b4daaa96719d162c324187c64/src/transformers/pytorch_utils.py#L278.
    r   N)r   r   r   rf   )r^   s    r%   rg   rg     s5     }}V#}}nV46LV6TTTr'   c                     	 ddl m}  |       r' j                         \  }}t         fd|D              S 	 	  j                         j                         S # t        $ r Y *w xY w# t        $ ro 	  j                         j                         t         j                        z  cY S # t        $ r+  j                         t         j                        z  cY cY S w xY ww xY w)z
    Taken from https://github.com/huggingface/safetensors/blob/08db34094e9e59e2f9218f2df133b7b4aaff5a99/bindings/python/py_src/safetensors/torch.py#L31C1-L41C59
    r   r   c              3   H   K   | ]  }t        t        |              y wr   )rf   r   r   s     r%   r   z)get_torch_storage_size.<locals>.<genexpr>  s!     WQV-gfd.CDQVr   )r   r   r   sumr<   untyped_storagenbytesAttributeErrorstoragesize_get_dtype_sizedtypeNotImplementedErrornelementr^   r   r   r   s   `   r%   rf   rf     s    	N(0002HE1WQVWWW 1	E%%'..00    E	E>>#((*_V\\-JJJ" 	E ??$v||'DDD	E	Es@   4A A' 	A$#A$'	C13B'$C'/CCCCc                     t         j                  j                  d      | r	 ddlmc m} |j                         }yyy# t        $ r Y yw xY w)z
    Checks if `torch_xla` is installed and potentially if a TPU is in the environment

    Taken from https://github.com/huggingface/transformers/blob/1ecf5f7c982d761b4daaa96719d162c324187c64/src/transformers/utils/import_utils.py#L463.
    r   Nr   TF)	importlibutil	find_spectorch_xla.core.xla_modelcore	xla_model
xla_devicer   )check_devicexmr   s      r%   r   r     sQ     ~~,855MMO    s   ? 	A
Ac                    	 ddl m}  ||       rt        |       S 	 	 | j	                         j                         S # t        $ r Y *w xY w# t        $ r2 	 | j                         j                         cY S # t        $ r Y Y yw xY ww xY w)z
    Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L11.
    r   r   )	r   r   r   r<   r   data_ptrrK   r   r   )r^   r   s     r%   r   r      s    N(0!&)) 1%%'0022    	>>#,,.." 			s>   ; A
 	AA
	BA41B4	B=B BBc                     t        | |      }|j                         D ]  \  }}|D ]  }|i }||vr|||<   | |=   |r1| j                         D 	ci c]  \  }}	||	j                          } }}	| S c c}	}w )a6  Remove shared tensors from state_dict and update metadata accordingly (for reloading).

    Warning: `state_dict` and `metadata` are mutated in-place!

    Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L155.
    )discard_names)_remove_duplicate_namesrO   
contiguous)
r#   r   r   r   
to_removes	kept_nameto_remove_group	to_remover   vs
             r%   r=   r=   9  s     )C\]J&0&6&6&8"	?(I(&/#9% ) '9 4>4D4D4FG4FDAqa'4F
G Hs   A2c                     | j                         r:| j                  d      d   j                         t        | j                        z   }|S | j                         }|S )z
    Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L23.
    )r   viewr   r   r   )r^   stops     r%   _end_ptrr   T  sQ     {{2r"++-0MM K  Kr'   r]   c                    g }| D ]  }t        |      dk  r|j                  |       #g }|D ]2  }||   }|j                  |j                         t        |      |f       4 |j	                          |d   \  }}}	|j                  |	h       |dd D ]4  \  }
}}|
|k\  r|j                  |h       n|d   j                  |       |}6  |S )z
    Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L44
    r   r   r   Nr   )rU   appendr   r   sortadd)r]   r#   filtered_tensorssharedareasnamer^   r   	last_stop	last_namestartr   s               r%   _filter_shared_not_sharedr   _  s     v;?##F+D%FLL&//+Xf-=tDE  	

"'(9i,!&qrE4	! ''/ $((.I "+ ( r'   c                    ddl }t        t              }| j                         D ]v  \  }}|j                   |j                  d      k7  s&t        |      dk7  s5t        |      dk7  sD||j                  t        |      t        |      f   j                  |       x t        t        |j                                     }t        ||       }|S )z
    Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L69.
    r   Nr   )r>   r   r   rO   r   r   rf   r   rw   r   r   r   )r#   r>   tensors_dictr   r   r]   s         r%   _find_shared_tensorsr   {  s     s#L  "188|u||F++A!0CH^_`HaefHf!((KN4J14MNOSSTUV # 6,--/01G'<GNr'   c                 0    	 ddl m}  |       r' j                         \  }}t         fd|D              S 	  j                         t               k(  xr2  j                         t         j                        z  t               k(  S # t        $ r Y [w xY w)
    Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L80
    r   r   c              3   H   K   | ]  }t        t        |              y wr   )_is_completer   r   s     r%   r   z_is_complete.<locals>.<genexpr>  s     Mut|GFD$9:ur   )r   r   r   allr<   r   r   r   r   r   rf   r   s   `   r%   r   r     s    	N(0002HE1MuMMM 1 ??F 33 (8IOM 9		'9( (	  s   4B	 		BB)preferred_namesr   r   r   c          
      .   |g }t        |      }|g }t        |      }t        |       }t        t              }|D ]  }t        |D cg c]  }t	        | |         s| c}      }	|	st        d| d      t        t        |	            d   }
|	j                  |      }|rt        t        |            d   }
|r*|j                  |	      }|rt        t        |            d   }
t        |      D ]  }||
k7  s	||
   j                  |         |S c c}w )r   zvError while trying to find names to remove to save state dict, but found no suitable name to keep for saving amongst: z. None is covering the entire storage. Refusing to save/load the model since you could be storing much more memory than needed. Please refer to https://huggingface.co/docs/safetensors/torch_shared_tensors for more information. Or open an issue.r   )
r   r   r   rw   r   r   r   
differenceintersectionr   )r#   r   r   unique_preferred_namesunique_discard_namessharedsr   r   r   complete_names	keep_name	preferreds               r%   r   r     s7     1}-":.GD!IvXvtjQUFV9WdvXY((.x 0  4/03	 #--.BC	tI/2I!.;;NKI"4	?3A6	6NDy )$++D1 #3 8 7 Ys   D
D
r   ztorch.dtypec                 D   ddl }t        |dd      }t        |dd      }|j                  d|j                  d|j                  d|j
                  d|j                  d|j                  d|j                  d|j                  d|j                  d|j                  d|d|di}||    S )	z
    Taken from https://github.com/huggingface/safetensors/blob/08db34094e9e59e2f9218f2df133b7b4aaff5a99/bindings/python/py_src/safetensors/torch.py#L344
    r   Nfloat8_e4m3fnfloat8_e5m2      r   r   )r>   r   int64float32int32bfloat16float16int16uint8int8boolfloat64)r   r>   _float8_e4m3fn_float8_e5m2_SIZEs        r%   r   r     s    
  UOT:N5-6LQqQqQQ

A

AqaE <r'   c                   ,     e Zd ZdZdef fdZeZ xZS )r   z
    This is used to report missing and unexpected keys in the state dict.
    Taken from https://github.com/pytorch/pytorch/blob/main/torch/nn/modules/module.py#L52.

    r(   c                 R    | j                   s| j                  syt        |          S )Nz<All keys matched successfully>)r   r   super__repr__)selfr   s    r%   r  z_IncompatibleKeys.__repr__  s%      )=)=4w!!r'   )r   
__module____qualname____doc__r8   r  __str____classcell__)r   s   @r%   r   r     s    "# "
 Gr'   r   IncompatibleKeysr   r   )NFF)T)TN)=r   r   rR   rD   rB   collectionsr   r   	functoolsr   pathlibr   typingr   r   r	   r
   r   r   r   r   r   r   	packagingr   r1   r   r   _baser   r   r   
get_logger__file__r?   r>   r8   r  intr&   r$   r9   rA   PathLikerz   rv   rt   r   r   rg   rf   r   r   r=   r   r   r   r   r   r   r    r'   r%   <module>r/     s   )   	 	 /   d d d  ! W W 
		H	% '+!&4)-# 59aa#t)$a sm	a
 a #s(Oa tCH~&a a a  (S	2aP '+!&4)-# 59`JS.()`J#t)$`J sm	`J
 `J #s(O`J tCH~&`J `J `J  (S	2`J 
`JL &FF&4	IS.()I I #s(O	I
 If 9=&*hh3+,h 	h
 h h 5n!456h h smh h^ %FFCCKKC 	C
 C C CP :>	m3+,m5n!456m m 	m
 4^#$c)*mf**#* 
*<> eCsCx4H.I @U UHU>SXY\^cdgildl^mYmSnpsCs=t4u U"E> Ec E6  ( 5eCHo1E+F 8 "59	S.()38n   (S	2	6^  tCH~ 4^H[C\ aefijmfnao 8T#~*=%> 4C>  ( (D (. ,0)-	.S.(). d3i(. DI&	.
 
#tCy..b = S  4
#5HY7Z[ r'   