CoolFace
Apppublic

declare-lab/tango2

sourceHugging Faceupdated 2y agoView on Hugging Face
92likes
configuration_utils.cpython-39.pyc236 linesDownload Raw Back to __pycache__
1a

2��'d�p�@sdZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl3mZddlm
Z
mZmZmZddlZddlmZddlmZmZmZddlmZd	d4lmZd	dlmZmZm Z m!Z!m"Z"m#Z#m$Z$e$�%e&�Z'e�(d�Z)Gd
d�de	�Z*Gdd�d�Z+dd�Z,dd�Z-dS)z& ConfigMixin base class and utilities.�N)�OrderedDict)�	PosixPath)�Any�Dict�Tuple�Union)�hf_hub_download)�EntryNotFoundError�RepositoryNotFoundError�RevisionNotFoundError)�	HTTPError�)�__version__)�DIFFUSERS_CACHE�HUGGINGFACE_CO_RESOLVE_ENDPOINT�DummyObject�	deprecate�extract_commit_hash�http_user_agent�loggingzconfig\.(.*)\.jsoncsTeZdZ�fdd�Zdd�Zdd�Zdd�Zd	d5�Z�fdd�Z�fd
d�Z	�Z6S)�7FrozenDictcs:t�j|i|��|��D]\}}t|||�qd|_dS)NT)�super�__init__�items�setattr�_FrozenDict__frozen)�self�args�kwargs�key�value��	__class__��R/home/deep/Projects/audio_diffusion/diffusers/src/diffusers/configuration_utils.pyr3szFrozenDict.__init__cOstd|jj�d���dS)Nz$You cannot use ``__delitem__`` on a �8 instance.��	Exceptionr"�__name__�rrrr#r#r$�__delitem__;szFrozenDict.__delitem__cOstd|jj�d���dS)Nz#You cannot use ``setdefault`` on a r%r&r)r#r#r$�9setdefault>szFrozenDict.setdefaultcOstd|jj�d���dS)NzYou cannot use ``pop`` on a r%r&r)r#r#r$�popAszFrozenDict.popcOstd|jj�d���dS)NzYou cannot use ``update`` on a r%r&r)r#r#r$�updateDszFrozenDict.updatecs6t|d�r$|jr$td|jj�d���t��||�dS�NZ__frozenz$You cannot use ``__setattr__`` on a r%)�hasattrrr'r"r(r�__setattr__�r�namer r!r#r$r0GszFrozenDict.__setattr__cs6t|d�r$|jr$td|jj�d���t��||�dSr.)r/rr'r"r(r�__setitem__r1r!r#r$r3LszFrozenDict.__setitem__)r(�10__module__�__qualname__rr*r+r,r-r0r3�
__classcell__r#r#r!r$r2src@s(eZdZdZdZgZdZgZdd�Zd"e	e11ejfe
d�dd�Zed#e	eee12effd	�d13d��Zedd
��Zed$e	e14ejfeee15efee16effd�dd��Zedd��Zedd��Zee	e17ejfd�dd��Zdd�Zeee18efd�dd��Ze19d�dd�Ze	e20ejfd�d d!�ZdS)%�ConfigMixina�21    Base class for all configuration classes. Stores all configuration parameters under `self.config` Also handles all22    methods for loading/downloading/saving classes inheriting from [`ConfigMixin`] with23        - [`~ConfigMixin.from_config`]24        - [`~ConfigMixin.save_config`]25 26    Class attributes:27        - **config_name** (`str`) -- A filename under which the config should stored when calling28          [`~ConfigMixin.save_config`] (should be overridden by parent class).29        - **ignore_for_config** (`List[str]`) -- A list of attributes that should not be saved in the config (should be30          overridden by subclass).31        - **has_compatibles** (`bool`) -- Whether the class has compatible classes (should be overridden by subclass).32        - **_deprecated_kwargs** (`List[str]`) -- Keyword arguments that are deprecated. Note that the init function33          should only have a `kwargs` argument if at least one argument is deprecated (should be overridden by34          subclass).35    NFcKs�|jdurtd|j�d���|�dd�|��D]^\}}zt|||�Wq0ty�}z,t�d|�d|�d|���|�WYd}~q0d}~00q0t	|d�s�|}n.t36|j�}i|j�|�}t�d|�d	|���t
|�|_dS)37NzMake sure that z' has defined a class name `config_name`rz38Can't set z with value z for �_internal_dictzUpdating config from z to )�config_name�NotImplementedErrorr"r,rr�AttributeError�logger�errorr/�dictr8�debugr)rrrr �errZ
internal_dictZ
previous_dictr#r#r$�register_to_configis394041zConfigMixin.register_to_config)�save_directory�push_to_hubcKsXtj�|�rtd|�d���tj|dd�tj�||j�}|�|�t�	d|���dS)a_42        Save a configuration object to the directory `save_directory`, so that it can be re-loaded using the43        [`~ConfigMixin.from_config`] class method.44 45        Args:46            save_directory (`str` or `os.PathLike`):47                Directory where the configuration JSON file will be saved (will be created if it does not exist).48        zProvided path (z#) should be a directory, not a fileT)�exist_okzConfiguration saved in N)49�os�path�isfile�AssertionError�makedirs�joinr9�to_json_filer<�info)rrBrCrZoutput_config_filer#r#r$�save_config�s	50zConfigMixin.save_config)�configc51Ks&d|vr|�d�}|dur"td��t|t�s�d}d|jvrL|d|�d�7}n d|jvrl|d	|�d52|�d�7}tdd
|dd�|jf|dd�|��\}}|j|fi|��\}}}d|vr�|�d�|d<|jD]}||vr�|�|�||<q�|fi|��}	|	j	fi|��i|�|�}|�r|	|fS|	SdS)a{53        Instantiate a Python class from a config dictionary54 55        Parameters:56            config (`Dict[str, Any]`):57                A config dictionary from which the Python class will be instantiated. Make sure to only load58                configuration files of compatible classes.59            return_unused_kwargs (`bool`, *optional*, defaults to `False`):60                Whether kwargs that are not consumed by the Python class should be returned or not.61 62            kwargs (remaining dictionary of keyword arguments, *optional*):63                Can be used to update the configuration object (after it being loaded) and initiate the Python class.64                `**kwargs` will be directly passed to the underlying scheduler/model's `__init__` method and eventually65                overwrite same named arguments of `config`.66 67        Examples:68 69        ```python70        >>> from diffusers import DDPMScheduler, DDIMScheduler, PNDMScheduler71 72        >>> # Download scheduler from huggingface.co and cache.73        >>> scheduler = DDPMScheduler.from_pretrained("google/ddpm-cifar10-32")74 75        >>> # Instantiate DDIM scheduler class with same config as DDPM76        >>> scheduler = DDIMScheduler.from_config(scheduler.config)77 78        >>> # Instantiate PNDM scheduler class with same config as DDPM79        >>> scheduler = PNDMScheduler.from_config(scheduler.config)80        ```81        �pretrained_model_name_or_pathNzFPlease make sure to provide a config as the first positional argument.zJIt is deprecated to pass a pretrained model name or path to `from_config`.Z	Schedulerz3If you were trying to load a scheduler, please use z�.from_pretrained(...) instead. Otherwise, please make sure to pass a configuration dictionary instead. This functionality will be removed in v1.0.0.ZModelz/If you were trying to load a model, please use z.load_config(...) followed by z�.from_config(...) instead. Otherwise, please make sure to pass a configuration dictionary instead. This functionality will be removed in v1.0.0.zconfig-passed-as-path�1.0.0F�Z
standard_warnT)rO�return_unused_kwargs�dtype)82r,�83ValueError�84isinstancer>r(r�load_config�extract_init_dict�_deprecated_kwargsrA)85�clsrNrRr�deprecation_message�	init_dict�
unused_kwargsZhidden_dictZdeprecated_kwarg�modelr#r#r$�from_config�s<"86878889�90��91zConfigMixin.from_configcOs,d|�d�}tdd|dd�|j|i|��S)Nz8 The function get_config_dict is deprecated. Please use zE.load_config instead. This function will be removed in version v1.0.0�get_config_dictrPFrQ)rrV)rYrrrZr#r#r$r_�s92�zConfigMixin.get_config_dict)rO�returnc
Ks�|�dt�}|�dd�}|�dd�}|�dd�}|�dd�}	|�dd�}93|�d	d�}|�d94d�}|�dd�}
|�di�}i|�d
di�}t|�}t|�}|jdur�td��tj�|�r�|}�n�tj�	|��rLtj�tj�95||j��r�tj�96||j�}nR|
du�r0tj�tj�97||
|j���r0tj�98||
|j�}ntd|j�d|�d����n0z&t||j|||||99|	||
|d�}W�nt
�y�t|�d���Yn�t�y�t|�d|�d���Yn�t�y�t|�d|j�d���Yn�t�y}z td|�d|����WYd}~njd}~0t�yJtdt�d|�d|j�d���Yn2t�yztd|�d|�d |j�d!���Yn0z|�|�}t|�}Wn*tjtf�y�td"|�d#���Yn0|�s�|�s�|S|f}|�r�||f7}|�r�||f7}|S)$a
100        Instantiate a Python class from a config dictionary101 102        Parameters:103            pretrained_model_name_or_path (`str` or `os.PathLike`, *optional*):104                Can be either:105 106                    - A string, the *model id* of a model repo on huggingface.co. Valid model ids should have an107                      organization name, like `google/ddpm-celebahq-256`.108                    - A path to a *directory* containing model weights saved using [`~ConfigMixin.save_config`], e.g.,109                      `./my_model_directory/`.110 111            cache_dir (`Union[str, os.PathLike]`, *optional*):112                Path to a directory in which a downloaded pretrained model configuration should be cached if the113                standard cache should not be used.114            force_download (`bool`, *optional*, defaults to `False`):115                Whether or not to force the (re-)download of the model weights and configuration files, overriding the116                cached versions if they exist.117            resume_download (`bool`, *optional*, defaults to `False`):118                Whether or not to delete incompletely received files. Will attempt to resume the download if such a119                file exists.120            proxies (`Dict[str, str]`, *optional*):121                A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',122                'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.123            output_loading_info(`bool`, *optional*, defaults to `False`):124                Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.125            local_files_only(`bool`, *optional*, defaults to `False`):126                Whether or not to only look at local files (i.e., do not try to download the model).127            use_auth_token (`str` or *bool*, *optional*):128                The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated129                when running `transformers-cli login` (stored in `~/.huggingface`).130            revision (`str`, *optional*, defaults to `"main"`):131                The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a132                git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any133                identifier allowed by git.134            subfolder (`str`, *optional*, defaults to `""`):135                In case the relevant files are located inside a subfolder of the model repo (either remote in136                huggingface.co or downloaded locally), you can specify the folder name here.137            return_unused_kwargs (`bool`, *optional*, defaults to `False):138                Whether unused keyword arguments of the config shall be returned.139            return_commit_hash (`bool`, *optional*, defaults to `False):140                Whether the commit_hash of the loaded configuration shall be returned.141 142        <Tip>143 144         It is required to be logged in (`huggingface-cli login`) when you want to use private or [gated145         models](https://huggingface.co/docs/hub/models-gated#gated-models).146 147        </Tip>148 149        <Tip>150 151        Activate the special ["offline-mode"](https://huggingface.co/transformers/installation.html#offline-mode) to152        use this method in a firewalled environment.153 154        </Tip>155        �	cache_dir�force_downloadF�resume_download�proxiesN�use_auth_token�local_files_only�revisionZmirror�	subfolder�156user_agent�	file_typerNz�`self.config_name` is not defined. Note that one should not load a config from `ConfigMixin`. Please make sure to define `config_name` in a class inheriting from `ConfigMixin`zError no file named z found in directory �.)157�filenamerarbrdrcrfrerirhrgz� is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'158If this is a private repository, make sure to pass a token having permission to this repo with `use_auth_token` or log in with `huggingface-cli login`.z� is not a valid git identifier (branch name, tag name or commit id) that exists for this model name. Check the model page at 'https://huggingface.co/z' for available revisions.z& does not appear to have a file named z:There was a specific connection error when trying to load z:159zWe couldn't connect to 'zM' to load this model, couldn't find it in the cached files and it looks like z- is not the path to a directory containing a z� file.160Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'.zCan't load config for 'z�'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'z2' is the correct path to a directory containing a z filez"It looks like the config file at 'z' is not a valid JSON file.)r,rr�strr9rTrErFrG�isdirrJ�EnvironmentErrorrr161rr	rr�_dict_from_json_filer�json�JSONDecodeError�UnicodeDecodeError)rYrOrRZreturn_commit_hashrrarbrcrdrerfrg�_rhri�config_filer@�config_dict�commit_hash�outputsr#r#r$rV�s�A162����
�������������163164165166zConfigMixin.load_configcCsttt�|j�j����S�N)�setr>�inspect�	signaturer�167parameters�keys)rYr#r#r$�_get_init_keys�szConfigMixin._get_init_keysc	s@t|���}|�|�}|�d�d|vr2|�d�t|d�rR|jD]}|�|�qBt|j�dkrn|t|j�}t	�168t�d�d�}|j
r�dd�|��D�}ng}t��|D]}|�|�}	��|	��q��|�|���fdd	�|��D�}|�d169|j�}170|171|jk�r<t||172��r<t||173�}|�|�|��fdd	�|��D�}dd	�|��D�}i�|D]Z}||v�r|||v�r||�|�||<||v�r�|�|��|<n||v�rV|�|��|<�qVt|�dk�r�t�d
|�d|j�d|j�d��t����}
t||
�dk�rt�||
�d��i|�|�}�fdd	�|��D�}�||fS)Nrr�_flax_internal_argsrrkcSsg|]}t|t�s|�qSr#)rUr)�.0�cr#r#r$�174<listcomp>��z1ConfigMixin.extract_init_dict.<locals>.<listcomp>csi|]\}}|�vr||�qSr#r#�r��k�v)�expected_keys_comp_clsr#r$�175<dictcomp>�r�z1ConfigMixin.extract_init_dict.<locals>.<dictcomp>�_class_namecsi|]\}}|�vr||�qSr#r#r�)�unexpected_keys_from_origr#r$r��r�cSs i|]\}}|�d�s||�qS�rt��176startswithr�r#r#r$r��r�zThe config attributes z were passed to z?, but are not expected and will be ignored. Please verify your z configuration file.zG was not found in config. Values will be initialized to default values.csi|]\}}|�vr||�qSr#r#r�)r[r#r$r��r�)r>rr�remover/r��len�ignore_for_configrz�	importlib�
import_moduler(�split�has_compatiblesZ_get_compatibles�unionr,�getattrr<�warningr9r~rL)rYrvrZ
original_dictZ
expected_keys�argZdiffusers_libraryZcompatible_classesr�Zexpected_keys_cZ
orig_cls_nameZorig_clsrZpassed_keysr\Zhidden_config_dictr#)r�r[r�r$rW�s`177178179180181182183184185���zConfigMixin.extract_init_dict)�	json_filecCs@t|ddd��}|��}Wd�n1s,0Yt�|�S)N�r�utf-8��encoding)�open�readrq�loads)rYr��reader�textr#r#r$rp�s&z ConfigMixin._dict_from_json_filecCs|jj�d|����S)N� )r"r(�to_json_string�rr#r#r$�__repr__�szConfigMixin.__repr__)r`cCs|jS)z�186        Returns the config of the class as a frozen dictionary187 188        Returns:189            `Dict[str, Any]`: Config of the class.190        )r8r�r#r#r$rNszConfigMixin.configcsZt|d�r|jni}|jj|d<t|d<dd���fdd�|��D�}tj|dd	d191�dS)z�192        Serializes this instance to a JSON string.193 194        Returns:195            `str`: String containing all the attributes that make up this configuration instance in JSON format.196        r8r�Z_diffusers_versioncSs,t|tj�r|��}nt|t�r(t|�}|Sry)rU�np�ndarray�tolistrrm)r r#r#r$�to_json_saveables197198199z4ConfigMixin.to_json_string.<locals>.to_json_saveablecsi|]\}}|�|��qSr#r#r��r�r#r$r�r�z.ConfigMixin.to_json_string.<locals>.<dictcomp>�T)�indent�	sort_keys�200)r/r8r"r(rrrq�dumps)rrvr#r�r$r�szConfigMixin.to_json_string)�json_file_pathcCs@t|ddd��}|�|���Wd�n1s20YdS)z�201        Save this instance to a JSON file.202 203        Args:204            json_file_path (`str` or `os.PathLike`):205                Path to the JSON file in which this configuration instance's parameters will be saved.206        �wr�r�N)r��writer�)rr��writerr#r#r$rK szConfigMixin.to_json_file)F)NF)FF) r(r4r5�__doc__r9r�r�rXrArrmrE�PathLike�boolrM�classmethodrrrr^r_rrV�staticmethodrrWrpr��propertyrNr�rKr#r#r#r$r7Rs<"R207��0208209S	r7cst����fdd��}|S)a�210    Decorator to apply on the init of classes inheriting from [`ConfigMixin`] so that all the arguments are211    automatically sent to `self.register_for_config`. To ignore a specific argument accepted by the init but that212    shouldn't be registered in the config, use the `ignore_for_config` class variable213 214    Warning: Once decorated, all private arguments (beginning with an underscore) are trashed and not sent to the init!215    cs�dd�|��D��dd�|��D�}t|t�sBtd|jj�d���t|dg��i�t���}�fdd�t	|j216���D�}t||���D]\}}|�|<q���
���fdd�|��D��i|����t|d	�fi����|g|�Ri���dS)217NcSs i|]\}}|�d�s||�qSr�r�r�r#r#r$r�8r�z:register_to_config.<locals>.inner_init.<locals>.<dictcomp>cSs i|]\}}|�d�r||�qSr�r�r�r#r#r$r�9r��&`@register_for_config` was applied to �A init method, but this class does not inherit from `ConfigMixin`.r�cs,i|]$\}\}}|dkr|�vr||j�qS)r)�default)r��ir2�p)�ignorer#r$r�Dscs.i|]&\}}|�vr|�vr|��||��qSr#)�get)r�r�r��r��init_kwargs�218new_kwargsr#r$r�Ls�rA)rrUr7�RuntimeErrorr"r(r�r{r|�	enumerater}�zipr~r-)rrrZconfig_init_kwargsr|r}r�r2��initr�r$�219inner_init5s,220�221222�223��z&register_to_config.<locals>.inner_init)�	functools�wraps)r�r�r#r�r$rA,s	 rAcs&|j�t����fdd��}||_|S)Ncs�t|t�std|jj�d���t|���}t�|�}i}|D]@}|j	|j224vrNq<t|j�tj
krjd||j	<q<t||j	�||j	<q<i|�|�}d|vr�|�d�t|�D]\}}	||j	}225|	||226<q�t|d�fi|���|g|�Ri|��dS)Nr�r�rSrA)rUr7r�r"r(r>r�dataclasses�fieldsr2r��typer��
_MISSING_TYPEr�r,r�)rrrr�r��default_kwargs�fieldr�r�r�r2�Z
original_initr#r$r�\s*227�228229230231z%flax_register_to_config.<locals>.init)rr�r�)rYr�r#r�r$�flax_register_to_configYs232$r�).r�r�r�r�r{rqrE�re�collectionsr�pathlibr�typingrrrr�numpyr�Zhuggingface_hubrZhuggingface_hub.utilsr	r233rZrequestsr�r�utilsrrrrrrr�234get_loggerr(r<�compileZ_re_configuration_filerr7rAr�r#r#r#r$�<module>s2$235236 ]-