File size: 11,833 Bytes
c336648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright 2023 SLAPaper
#
# 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.

import sys

import gradio as gr
import torch

import launch
import modules.scripts as scripts
import modules.shared as shared
from modules.processing import Processed, StableDiffusionProcessing


class ToMe:
    """ToMe implementation"""

    def __init__(self):
        self.infotext_fields = []
        self.paste_field_names = []

    def patch_model(self, model: torch.nn.Module, is_hires: bool):
        ratio: float = shared.opts.data.get('tome_merging_ratio', 0.5)
        max_downsample: int = int(
            shared.opts.data.get('tome_maximum_down_sampling', 1))
        sx: int = int(shared.opts.data.get('tome_stride_x', 2))
        sy: int = int(shared.opts.data.get('tome_stride_y', 2))
        use_rand: bool = bool(shared.opts.data.get('tome_random', True))
        merge_attn: bool = bool(
            shared.opts.data.get('tome_merge_attention', True))
        merge_crossattn: bool = bool(
            shared.opts.data.get('tome_merge_cross_attention', False))
        merge_mlp: bool = bool(shared.opts.data.get('tome_merge_mlp', False))

        import tomesd
        tomesd.apply_patch(model,
                           ratio=ratio,
                           max_downsample=max_downsample,
                           sx=sx,
                           sy=sy,
                           use_rand=use_rand,
                           merge_attn=merge_attn,
                           merge_crossattn=merge_crossattn,
                           merge_mlp=merge_mlp)

        target = 'to hires pass' if is_hires else 'globally'

        print(
            f"Applying ToMe patch {target} with ratio[{ratio}], "
            f"max_downsample[{max_downsample}], sx[{sx}], sy[{sy}], "
            f"use_rand[{use_rand}], merge_attn[{merge_attn}], "
            f"merge_crossattn[{merge_crossattn}], merge_mlp[{merge_mlp}]",
            file=sys.stderr)

    def on_ui_settings_callback(self):
        section = ('tome', 'ToMe Settings')
        shared.opts.add_option(
            "tome_merging_ratio",
            shared.OptionInfo(
                0.5,
                "ToMe merging ratio, higher the faster, it should not go over 1-(1/(sx*sy)), which is 0.75 by default",
                gr.Slider, {
                    "minimum": 0,
                    "maximum": 0.99,
                    "step": 0.01
                },
                section=section))
        shared.opts.add_option(
            "tome_min_x",
            shared.OptionInfo(
                768,
                "Only activate tome if image width reach this value",
                gr.Slider, {
                    "minimum": 512,
                    "maximum": 2048,
                    "step": 256
                },
                section=section))
        shared.opts.add_option(
            "tome_min_y",
            shared.OptionInfo(
                768,
                "Only activate tome if image height reach this value",
                gr.Slider, {
                    "minimum": 512,
                    "maximum": 2048,
                    "step": 256
                },
                section=section))
        shared.opts.add_option(
            "tome_random",
            shared.OptionInfo(
                False,
                "Use random perturbations - Disable if you see werid artifacts",
                gr.Checkbox,
                section=section))
        shared.opts.add_option(
            "tome_merge_attention",
            shared.OptionInfo(True,
                              "Merge attention (recommended)",
                              gr.Checkbox,
                              section=section))
        shared.opts.add_option(
            "tome_merge_cross_attention",
            shared.OptionInfo(False,
                              "Merge cross attention (not recommended)",
                              gr.Checkbox,
                              section=section))
        shared.opts.add_option(
            "tome_merge_mlp",
            shared.OptionInfo(False,
                              "Merge mlp (very not recommended)",
                              gr.Checkbox,
                              section=section))
        shared.opts.add_option(
            "tome_maximum_down_sampling",
            shared.OptionInfo("1",
                              "Maximum down sampling",
                              gr.Radio, {"choices": ["1", "2", "4", "8"]},
                              section=section))
        shared.opts.add_option(
            "tome_stride_x",
            shared.OptionInfo(2,
                              "Stride - X",
                              gr.Slider, {
                                  "minimum": 2,
                                  "maximum": 8,
                                  "step": 2
                              },
                              section=section))
        shared.opts.add_option(
            "tome_stride_y",
            shared.OptionInfo(2,
                              "Stride - Y",
                              gr.Slider, {
                                  "minimum": 2,
                                  "maximum": 8,
                                  "step": 2
                              },
                              section=section))
        shared.opts.add_option(
            "tome_force_hires",
            shared.OptionInfo(True,
                              "Enable during hires pass (Experimental)",
                              gr.Checkbox,
                              section=section))


_tome_instance = ToMe()
scripts.script_callbacks.on_ui_settings(_tome_instance.on_ui_settings_callback)


class Script(scripts.Script):
    """Use script interface to inject into image generation loop"""

    def title(self):
        return "ToMe"

    def show(self, is_img2img: bool):
        return scripts.AlwaysVisible

    def ui(self, is_img2img: bool):
        enable_checkbox = gr.Checkbox(
            value=True,
            label="Enable ToMe optimization",
            info=
            "Use tomesd to boost generation speed. Other settings in Settings Tab.",
            interactive=True)

        if not launch.is_installed("tomesd"):
            enable_checkbox.info = "Import tomesd failed, please check your environment before using ToMe extension."
            enable_checkbox.interactive = False

        return [enable_checkbox]

    def process(self, p: StableDiffusionProcessing, *args):
        # patch all, unload when postprocess
        if not launch.is_installed("tomesd"):
            print(
                "Cannot import tomesd, please install it manually following the instructions on https://github.com/dbolya/tomesd",
                file=sys.stderr)
            return

        if args and args[0]:
            tome_min_x = shared.opts.data.get('tome_min_x', 768)
            tome_min_y = shared.opts.data.get('tome_min_y', 768)

            if p.width >= tome_min_x and p.height >= tome_min_y:
                _tome_instance.patch_model(p.sd_model, is_hires=False)

                # add generation info
                ratio: float = shared.opts.data.get('tome_merging_ratio', 0.5)
                max_downsample: int = int(
                    shared.opts.data.get('tome_maximum_down_sampling', 1))
                sx: int = int(shared.opts.data.get('tome_stride_x', 2))
                sy: int = int(shared.opts.data.get('tome_stride_y', 2))
                use_rand: bool = bool(shared.opts.data.get(
                    'tome_random', True))
                merge_attn: bool = bool(
                    shared.opts.data.get('tome_merge_attention', True))
                merge_crossattn: bool = bool(
                    shared.opts.data.get('tome_merge_cross_attention', False))
                merge_mlp: bool = bool(
                    shared.opts.data.get('tome_merge_mlp', False))

                p.extra_generation_params['tome_merging_ratio'] = ratio
                p.extra_generation_params[
                    'tome_maximum_down_sampling'] = max_downsample
                p.extra_generation_params['tome_stride_x'] = sx
                p.extra_generation_params['tome_stride_y'] = sy
                p.extra_generation_params['tome_random'] = use_rand
                p.extra_generation_params['tome_merge_attention'] = merge_attn
                p.extra_generation_params[
                    'tome_merge_cross_attention'] = merge_crossattn
                p.extra_generation_params['tome_merge_mlp'] = merge_mlp

                return

            print(
                f'Image size [{p.width}*{p.height}] < '
                f'Threashold [{tome_min_x}*{tome_min_y}]'
                ', no need to apply ToMe patch',
                file=sys.stderr)

    def postprocess(self, p: StableDiffusionProcessing, processed: Processed,
                    *args):
        if not launch.is_installed("tomesd"):
            return

        # remove patch all
        if args and args[0]:
            import tomesd
            tomesd.remove_patch(p.sd_model)

    def before_hires_pass(self, p, *args, **kwargs):
        if not launch.is_installed("tomesd"):
            return

        tome_force_hires: bool = bool(
            shared.opts.data.get('tome_force_hires', True))
        if tome_force_hires and args and args[0]:
            _tome_instance.patch_model(p.sd_model, is_hires=True)

            # add generation info
            ratio: float = shared.opts.data.get('tome_merging_ratio', 0.5)
            max_downsample: int = int(
                shared.opts.data.get('tome_maximum_down_sampling', 1))
            sx: int = int(shared.opts.data.get('tome_stride_x', 2))
            sy: int = int(shared.opts.data.get('tome_stride_y', 2))
            use_rand: bool = bool(shared.opts.data.get('tome_random', True))
            merge_attn: bool = bool(
                shared.opts.data.get('tome_merge_attention', True))
            merge_crossattn: bool = bool(
                shared.opts.data.get('tome_merge_cross_attention', False))
            merge_mlp: bool = bool(
                shared.opts.data.get('tome_merge_mlp', False))
            tome_force_hires: bool = bool(
                shared.opts.data.get('tome_force_hires', True))

            p.extra_generation_params['tome_merging_ratio'] = ratio
            p.extra_generation_params[
                'tome_maximum_down_sampling'] = max_downsample
            p.extra_generation_params['tome_stride_x'] = sx
            p.extra_generation_params['tome_stride_y'] = sy
            p.extra_generation_params['tome_random'] = use_rand
            p.extra_generation_params['tome_merge_attention'] = merge_attn
            p.extra_generation_params[
                'tome_merge_cross_attention'] = merge_crossattn
            p.extra_generation_params['tome_merge_mlp'] = merge_mlp
            p.extra_generation_params['tome_force_hires'] = tome_force_hires

    def post_hires_pass(self, p, *args, **kwargs):
        if not launch.is_installed("tomesd"):
            return

        tome_force_hires: bool = bool(
            shared.opts.data.get('tome_force_hires', True))
        # remove patch all
        if tome_force_hires and args and args[0]:
            import tomesd
            tomesd.remove_patch(p.sd_model)