"examples/pytorch/argo/ogb_example_ARGO.py" did not exist on "97bb85d505880e13bb70879cac7c5effe05af4b1"
pipeline_utils.py 5.05 KB
Newer Older
Patrick von Platen's avatar
Patrick von Platen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# coding=utf-8
# Copyright 2022 The HuggingFace Inc. team.
# Copyright (c) 2022, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Patrick von Platen's avatar
improve  
Patrick von Platen committed
17
import importlib
Patrick von Platen's avatar
Patrick von Platen committed
18
19
import os
from typing import Optional, Union
Patrick von Platen's avatar
up  
Patrick von Platen committed
20
from huggingface_hub import snapshot_download
Patrick von Platen's avatar
Patrick von Platen committed
21
22
23
24

# CHANGE to diffusers.utils
from transformers.utils import logging

Patrick von Platen's avatar
Patrick von Platen committed
25
from .configuration_utils import ConfigMixin
patil-suraj's avatar
patil-suraj committed
26
from .dynamic_modules_utils import get_class_from_dynamic_module
Patrick von Platen's avatar
improve  
Patrick von Platen committed
27

Patrick von Platen's avatar
Patrick von Platen committed
28
29
30
31
32
33
34
35
36

INDEX_FILE = "diffusion_model.pt"


logger = logging.get_logger(__name__)


LOADABLE_CLASSES = {
    "diffusers": {
Patrick von Platen's avatar
Patrick von Platen committed
37
        "ModelMixin": ["save_pretrained", "from_pretrained"],
Patrick von Platen's avatar
improve  
Patrick von Platen committed
38
        "GaussianDDPMScheduler": ["save_config", "from_config"],
Patrick von Platen's avatar
Patrick von Platen committed
39
40
    },
    "transformers": {
Patrick von Platen's avatar
Patrick von Platen committed
41
        "ModelMixin": ["save_pretrained", "from_pretrained"],
Patrick von Platen's avatar
Patrick von Platen committed
42
43
44
45
    },
}


Patrick von Platen's avatar
Patrick von Platen committed
46
class DiffusionPipeline(ConfigMixin):
Patrick von Platen's avatar
Patrick von Platen committed
47
48
49

    config_name = "model_index.json"

Patrick von Platen's avatar
up  
Patrick von Platen committed
50
    def register_modules(self, **kwargs):
Patrick von Platen's avatar
Patrick von Platen committed
51
52
53
54
55
56
        for name, module in kwargs.items():
            # retrive library
            library = module.__module__.split(".")[0]
            # retrive class_name
            class_name = module.__class__.__name__

57
58
            register_dict = {name: (library, class_name)}

Patrick von Platen's avatar
Patrick von Platen committed
59
            # save model index config
60
            self.register(**register_dict)
Patrick von Platen's avatar
Patrick von Platen committed
61
62
63

            # set models
            setattr(self, name, module)
64

65
66
        register_dict = {"_module" : self.__module__.split(".")[-1] + ".py"}
        self.register(**register_dict)
Patrick von Platen's avatar
Patrick von Platen committed
67
68
69
70
71
72

    def save_pretrained(self, save_directory: Union[str, os.PathLike]):
        self.save_config(save_directory)

        model_index_dict = self._dict_to_save
        model_index_dict.pop("_class_name")
73
        model_index_dict.pop("_module")
Patrick von Platen's avatar
Patrick von Platen committed
74
75
76
77

        for name, (library_name, class_name) in self._dict_to_save.items():
            importable_classes = LOADABLE_CLASSES[library_name]

78
79
80
81
            # TODO: Suraj
            if library_name == self.__module__:
                library_name = self

Patrick von Platen's avatar
Patrick von Platen committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            library = importlib.import_module(library_name)
            class_obj = getattr(library, class_name)
            class_candidates = {c: getattr(library, c) for c in importable_classes.keys()}

            save_method_name = None
            for class_name, class_candidate in class_candidates.items():
                if issubclass(class_obj, class_candidate):
                    save_method_name = importable_classes[class_name][0]

            save_method = getattr(getattr(self, name), save_method_name)
            save_method(os.path.join(save_directory, name))

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.PathLike]], **kwargs):
        # use snapshot download here to get it working from from_pretrained
Patrick von Platen's avatar
Patrick von Platen committed
97
98
99
100
        if not os.path.isdir(pretrained_model_name_or_path):
            cached_folder = snapshot_download(pretrained_model_name_or_path)
        else:
            cached_folder = pretrained_model_name_or_path
101

patil-suraj's avatar
patil-suraj committed
102
        config_dict = cls.get_config_dict(cached_folder)
103
104
105

        # if we load from explicit class, let's use it
        if cls != DiffusionPipeline:
106
107
            pipeline_class = cls
        else:
108
109
110
            # else we need to load the correct module from the Hub
            class_name_ = config_dict["_class_name"]
            module = config_dict["_module"]
111
            pipeline_class = get_class_from_dynamic_module(cached_folder, module, class_name_, cached_folder)
112

113
        init_dict, _ = pipeline_class.extract_init_dict(config_dict, **kwargs)
Patrick von Platen's avatar
Patrick von Platen committed
114
115
116

        init_kwargs = {}

patil-suraj's avatar
patil-suraj committed
117
        for name, (library_name, class_name) in init_dict.items():
Patrick von Platen's avatar
Patrick von Platen committed
118
119
            importable_classes = LOADABLE_CLASSES[library_name]

120
121
            if library_name == module:
                # TODO(Suraj)
122
                # for vq
123
124
                pass

Patrick von Platen's avatar
Patrick von Platen committed
125
126
127
128
129
130
131
132
133
134
135
            library = importlib.import_module(library_name)
            class_obj = getattr(library, class_name)
            class_candidates = {c: getattr(library, c) for c in importable_classes.keys()}

            load_method_name = None
            for class_name, class_candidate in class_candidates.items():
                if issubclass(class_obj, class_candidate):
                    load_method_name = importable_classes[class_name][1]

            load_method = getattr(class_obj, load_method_name)

Patrick von Platen's avatar
Patrick von Platen committed
136
            if os.path.isdir(os.path.join(cached_folder, name)):
137
138
139
                loaded_sub_model = load_method(os.path.join(cached_folder, name))
            else:
                loaded_sub_model = load_method(cached_folder)
Patrick von Platen's avatar
Patrick von Platen committed
140

141
            init_kwargs[name] = loaded_sub_model  # UNet(...), # DiffusionSchedule(...)
Patrick von Platen's avatar
Patrick von Platen committed
142

143
        model = pipeline_class(**init_kwargs)
Patrick von Platen's avatar
Patrick von Platen committed
144
        return model