|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2022 The HuggingFace Inc. team. |
| 3 | +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | +from typing import Optional, Union |
| 19 | +import importlib |
| 20 | + |
| 21 | +from .configuration_utils import Config |
| 22 | + |
| 23 | +# CHANGE to diffusers.utils |
| 24 | +from transformers.utils import logging |
| 25 | + |
| 26 | + |
| 27 | +INDEX_FILE = "diffusion_model.pt" |
| 28 | + |
| 29 | + |
| 30 | +logger = logging.get_logger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +LOADABLE_CLASSES = { |
| 34 | + "diffusers": { |
| 35 | + "PreTrainedModel": ["save_pretrained", "from_pretrained"], |
| 36 | + "GaussianDiffusion": ["save_config", "from_config"], |
| 37 | + }, |
| 38 | + "transformers": { |
| 39 | + "PreTrainedModel": ["save_pretrained", "from_pretrained"], |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +class DiffusionPipeline(Config): |
| 45 | + |
| 46 | + config_name = "model_index.json" |
| 47 | + |
| 48 | + def __init__(self, **kwargs): |
| 49 | + for name, module in kwargs.items(): |
| 50 | + # retrive library |
| 51 | + library = module.__module__.split(".")[0] |
| 52 | + # retrive class_name |
| 53 | + class_name = module.__class__.__name__ |
| 54 | + |
| 55 | + # save model index config |
| 56 | + self.register(**{name: (library, class_name)}) |
| 57 | + |
| 58 | + # set models |
| 59 | + setattr(self, name, module) |
| 60 | + |
| 61 | + def save_pretrained(self, save_directory: Union[str, os.PathLike]): |
| 62 | + self.save_config(save_directory) |
| 63 | + |
| 64 | + model_index_dict = self._dict_to_save |
| 65 | + model_index_dict.pop("_class_name") |
| 66 | + |
| 67 | + for name, (library_name, class_name) in self._dict_to_save.items(): |
| 68 | + importable_classes = LOADABLE_CLASSES[library_name] |
| 69 | + |
| 70 | + library = importlib.import_module(library_name) |
| 71 | + class_obj = getattr(library, class_name) |
| 72 | + class_candidates = {c: getattr(library, c) for c in importable_classes.keys()} |
| 73 | + |
| 74 | + save_method_name = None |
| 75 | + for class_name, class_candidate in class_candidates.items(): |
| 76 | + if issubclass(class_obj, class_candidate): |
| 77 | + save_method_name = importable_classes[class_name][0] |
| 78 | + |
| 79 | + save_method = getattr(getattr(self, name), save_method_name) |
| 80 | + save_method(os.path.join(save_directory, name)) |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.PathLike]], **kwargs): |
| 84 | + # use snapshot download here to get it working from from_pretrained |
| 85 | + config_dict, _ = cls.get_config_dict(pretrained_model_name_or_path) |
| 86 | + |
| 87 | + init_kwargs = {} |
| 88 | + |
| 89 | + for name, (library_name, class_name) in config_dict.items(): |
| 90 | + importable_classes = LOADABLE_CLASSES[library_name] |
| 91 | + |
| 92 | + library = importlib.import_module(library_name) |
| 93 | + class_obj = getattr(library, class_name) |
| 94 | + class_candidates = {c: getattr(library, c) for c in importable_classes.keys()} |
| 95 | + |
| 96 | + load_method_name = None |
| 97 | + for class_name, class_candidate in class_candidates.items(): |
| 98 | + if issubclass(class_obj, class_candidate): |
| 99 | + load_method_name = importable_classes[class_name][1] |
| 100 | + |
| 101 | + load_method = getattr(class_obj, load_method_name) |
| 102 | + |
| 103 | + loaded_sub_model = load_method(os.path.join(pretrained_model_name_or_path, name)) |
| 104 | + |
| 105 | + init_kwargs[name] = loaded_sub_model |
| 106 | + |
| 107 | + model = cls(**init_kwargs) |
| 108 | + return model |
0 commit comments