o
    wi                     @   sx   d dl mZ d dlmZmZmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZ d dlmZ G dd de	d	d
ddZdS )    )Path)AnyDictOptionalUnion)CONFIG_NAMEPyTorchModelHubMixin)DataclassInstance)
DictConfig	OmegaConf)cached_filec                       s   e Zd Zeddddedee deeeef  dedee	 d	ee d
edeeedf dedef fddZ
ddddddeeef deee	df  dee dedeeeef  dee f fddZ  ZS )
HFHubMixincpuF)map_locationstrictmodel_idrevision	cache_dirforce_downloadproxiesresume_downloadlocal_files_onlytokenNr   r   c       
            s   t |t|||||||dddd}|du rtdt d|tt||d< d|d d< t jd	|||||||||	|
d
|S )
z
        Load Pytorch pretrained weights and return the loaded model.
        Wrapper over PyTorchModelHubMixin that auto-handles config in **model_kwargs.
        F)
r   r   r   r   r   r   r    _raise_exceptions_for_gated_repo%_raise_exceptions_for_missing_entries'_raise_exceptions_for_connection_errorsNzMissing z file for model_id=cfgpretrained_weights)
r   r   r   r   r   r   r   r   r   r    )r   r   RuntimeErrorr   to_containerloadsuper_from_pretrained)clsr   r   r   r   r   r   r   r   r   r   model_kwargsresolved_config_file	__class__r   d/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/collections/speechlm2/parts/hf_hub.pyr#      s@   zHFHubMixin._from_pretrained)configrepo_idpush_to_hubmodel_card_kwargssave_directoryr*   r	   r+   r,   r-   returnc                   sF   |du rt | d}t|trt| j}t jd|||||d|S )a  
        Save weights in local directory.

        Args:
            save_directory (`str` or `Path`):
                Path to directory in which the model weights and configuration will be saved.
            config (`dict` or `DataclassInstance`, *optional*):
                Model configuration specified as a key/value dictionary or a dataclass instance.
                If not provided, we will automatically serialize attribute ``model.cfg``.
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Huggingface Hub after saving it.
            repo_id (`str`, *optional*):
                ID of your repository on the Hub. Used only if `push_to_hub=True`. Will default to the folder name if
                not provided.
            model_card_kwargs (`Dict[str, Any]`, *optional*):
                Additional arguments passed to the model card template to customize the model card.
            push_to_hub_kwargs:
                Additional key word arguments passed along to the [`~ModelHubMixin.push_to_hub`] method.
        Returns:
            `str` or `None`: url of the commit on the Hub if `push_to_hub=True`, `None` otherwise.
        Nr   )r.   r*   r+   r,   r-   r   )getattr
isinstancer
   r   r    r   r"   save_pretrained)selfr.   r*   r+   r,   r-   push_to_hub_kwargsr'   r   r)   r2   V   s   

zHFHubMixin.save_pretrained)__name__
__module____qualname__classmethodstrr   r   r   booldictr#   r   r   r2   __classcell__r   r   r'   r)   r      sT    	
<
	r   NeMozhttps://github.com/NVIDIA/NeMozDhttps://docs.nvidia.com/nemo-framework/user-guide/latest/nemotoolkit)library_namerepo_urldocs_urlN)pathlibr   typingr   r   r   r   huggingface_hubr   r   huggingface_hub.hub_mixinr	   	omegaconfr
   r   transformers.utilsr   r   r   r   r   r)   <module>   s   

