Serializes the configuration instance to a JSON string. Returns: `str`: String containing all the attributes that make up the configuration instance in JSON format.
(self)
| 617 | return self._internal_dict |
| 618 | |
| 619 | def to_json_string(self) -> str: |
| 620 | """ |
| 621 | Serializes the configuration instance to a JSON string. |
| 622 | |
| 623 | Returns: |
| 624 | `str`: |
| 625 | String containing all the attributes that make up the configuration instance in JSON format. |
| 626 | """ |
| 627 | config_dict = self._internal_dict if hasattr(self, "_internal_dict") else {} |
| 628 | config_dict["_class_name"] = self.__class__.__name__ |
| 629 | config_dict["_diffusers_version"] = __version__ |
| 630 | |
| 631 | def to_json_saveable(value): |
| 632 | if isinstance(value, np.ndarray): |
| 633 | value = value.tolist() |
| 634 | elif isinstance(value, Path): |
| 635 | value = value.as_posix() |
| 636 | elif hasattr(value, "to_dict") and callable(value.to_dict): |
| 637 | value = value.to_dict() |
| 638 | elif isinstance(value, list): |
| 639 | value = [to_json_saveable(v) for v in value] |
| 640 | return value |
| 641 | |
| 642 | if "quantization_config" in config_dict: |
| 643 | config_dict["quantization_config"] = ( |
| 644 | config_dict.quantization_config.to_dict() |
| 645 | if not isinstance(config_dict.quantization_config, dict) |
| 646 | else config_dict.quantization_config |
| 647 | ) |
| 648 | |
| 649 | config_dict = {k: to_json_saveable(v) for k, v in config_dict.items()} |
| 650 | # Don't save "_ignore_files" or "_use_default_values" |
| 651 | config_dict.pop("_ignore_files", None) |
| 652 | config_dict.pop("_use_default_values", None) |
| 653 | # pop the `_pre_quantization_dtype` as torch.dtypes are not serializable. |
| 654 | _ = config_dict.pop("_pre_quantization_dtype", None) |
| 655 | |
| 656 | if getattr(self, "_auto_class", None) is not None: |
| 657 | module = self.__class__.__module__.split(".")[-1] |
| 658 | auto_map = config_dict.get("auto_map", {}) |
| 659 | auto_map[self._auto_class] = f"{module}.{self.__class__.__name__}" |
| 660 | config_dict["auto_map"] = auto_map |
| 661 | |
| 662 | return json.dumps(config_dict, indent=2, sort_keys=True) + "\n" |
| 663 | |
| 664 | def to_json_file(self, json_file_path: str | os.PathLike): |
| 665 | """ |