MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / ConfigMixin

Class ConfigMixin

diffusers/src/diffusers/configuration_utils.py:87–605  ·  view source on GitHub ↗

r""" Base class for all configuration classes. All configuration parameters are stored under `self.config`. Also provides the [`~ConfigMixin.from_config`] and [`~ConfigMixin.save_config`] methods for loading, downloading, and saving classes that inherit from [`ConfigMixin`]. Class a

Source from the content-addressed store, hash-verified

85
86
87class ConfigMixin:
88 r"""
89 Base class for all configuration classes. All configuration parameters are stored under `self.config`. Also
90 provides the [`~ConfigMixin.from_config`] and [`~ConfigMixin.save_config`] methods for loading, downloading, and
91 saving classes that inherit from [`ConfigMixin`].
92
93 Class attributes:
94 - **config_name** (`str`) -- A filename under which the config should stored when calling
95 [`~ConfigMixin.save_config`] (should be overridden by parent class).
96 - **ignore_for_config** (`List[str]`) -- A list of attributes that should not be saved in the config (should be
97 overridden by subclass).
98 - **has_compatibles** (`bool`) -- Whether the class has compatible classes (should be overridden by subclass).
99 - **_deprecated_kwargs** (`List[str]`) -- Keyword arguments that are deprecated. Note that the `init` function
100 should only have a `kwargs` argument if at least one argument is deprecated (should be overridden by
101 subclass).
102 """
103
104 config_name = None
105 ignore_for_config = []
106 has_compatibles = False
107
108 _deprecated_kwargs = []
109
110 def register_to_config(self, **kwargs):
111 if self.config_name is None:
112 raise NotImplementedError(f"Make sure that {self.__class__} has defined a class name `config_name`")
113 # Special case for `kwargs` used in deprecation warning added to schedulers
114 # TODO: remove this when we remove the deprecation warning, and the `kwargs` argument,
115 # or solve in a more general way.
116 kwargs.pop("kwargs", None)
117
118 if not hasattr(self, "_internal_dict"):
119 internal_dict = kwargs
120 else:
121 previous_dict = dict(self._internal_dict)
122 internal_dict = {**self._internal_dict, **kwargs}
123 logger.debug(f"Updating config from {previous_dict} to {internal_dict}")
124
125 self._internal_dict = FrozenDict(internal_dict)
126
127 def __getattr__(self, name: str) -> Any:
128 """The only reason we overwrite `getattr` here is to gracefully deprecate accessing
129 config attributes directly. See https://github.com/huggingface/diffusers/pull/3129
130
131 This function is mostly copied from PyTorch's __getattr__ overwrite:
132 https://pytorch.org/docs/stable/_modules/torch/nn/modules/module.html#Module
133 """
134
135 is_in_config = "_internal_dict" in self.__dict__ and hasattr(self.__dict__["_internal_dict"], name)
136 is_attribute = name in self.__dict__
137
138 if is_in_config and not is_attribute:
139 deprecation_message = f"Accessing config attribute `{name}` directly via '{type(self).__name__}' object attribute is deprecated. Please access '{name}' over '{type(self).__name__}'s config object instead, e.g. 'scheduler.config.{name}'."
140 deprecate("direct config name access", "1.0.0", deprecation_message, standard_warn=False)
141 return self._internal_dict[name]
142
143 raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
144

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected