r""" Instantiate a Python class from a config dictionary. Parameters: config (`dict[str, Any]`): A config dictionary from which the Python class is instantiated. Make sure to only load configuration files of compatible classes.
(
cls, config: FrozenDict | dict[str, Any] = None, return_unused_kwargs=False, **kwargs
)
| 221 | |
| 222 | @classmethod |
| 223 | def from_config( |
| 224 | cls, config: FrozenDict | dict[str, Any] = None, return_unused_kwargs=False, **kwargs |
| 225 | ) -> Self | tuple[Self, dict[str, Any]]: |
| 226 | r""" |
| 227 | Instantiate a Python class from a config dictionary. |
| 228 | |
| 229 | Parameters: |
| 230 | config (`dict[str, Any]`): |
| 231 | A config dictionary from which the Python class is instantiated. Make sure to only load configuration |
| 232 | files of compatible classes. |
| 233 | return_unused_kwargs (`bool`, *optional*, defaults to `False`): |
| 234 | Whether kwargs that are not consumed by the Python class should be returned or not. |
| 235 | kwargs (remaining dictionary of keyword arguments, *optional*): |
| 236 | Can be used to update the configuration object (after it is loaded) and initiate the Python class. |
| 237 | `**kwargs` are passed directly to the underlying scheduler/model's `__init__` method and eventually |
| 238 | overwrite the same named arguments in `config`. |
| 239 | |
| 240 | Returns: |
| 241 | [`ModelMixin`] or [`SchedulerMixin`]: |
| 242 | A model or scheduler object instantiated from a config dictionary. |
| 243 | |
| 244 | Examples: |
| 245 | |
| 246 | ```python |
| 247 | >>> from diffusers import DDPMScheduler, DDIMScheduler, PNDMScheduler |
| 248 | |
| 249 | >>> # Download scheduler from huggingface.co and cache. |
| 250 | >>> scheduler = DDPMScheduler.from_pretrained("google/ddpm-cifar10-32") |
| 251 | |
| 252 | >>> # Instantiate DDIM scheduler class with same config as DDPM |
| 253 | >>> scheduler = DDIMScheduler.from_config(scheduler.config) |
| 254 | |
| 255 | >>> # Instantiate PNDM scheduler class with same config as DDPM |
| 256 | >>> scheduler = PNDMScheduler.from_config(scheduler.config) |
| 257 | ``` |
| 258 | """ |
| 259 | # <===== TO BE REMOVED WITH DEPRECATION |
| 260 | # TODO(Patrick) - make sure to remove the following lines when config=="model_path" is deprecated |
| 261 | if "pretrained_model_name_or_path" in kwargs: |
| 262 | config = kwargs.pop("pretrained_model_name_or_path") |
| 263 | |
| 264 | if config is None: |
| 265 | raise ValueError("Please make sure to provide a config as the first positional argument.") |
| 266 | # ======> |
| 267 | |
| 268 | if not isinstance(config, dict): |
| 269 | deprecation_message = "It is deprecated to pass a pretrained model name or path to `from_config`." |
| 270 | if "Scheduler" in cls.__name__: |
| 271 | deprecation_message += ( |
| 272 | f"If you were trying to load a scheduler, please use {cls}.from_pretrained(...) instead." |
| 273 | " Otherwise, please make sure to pass a configuration dictionary instead. This functionality will" |
| 274 | " be removed in v1.0.0." |
| 275 | ) |
| 276 | elif "Model" in cls.__name__: |
| 277 | deprecation_message += ( |
| 278 | f"If you were trying to load a model, please use {cls}.load_config(...) followed by" |
| 279 | f" {cls}.from_config(...) instead. Otherwise, please make sure to pass a configuration dictionary" |
| 280 | " instead. This functionality will be removed in v1.0.0." |
no test coverage detected