File size: 4,352 Bytes
aded561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (C) 2022  Lopho <contact@lopho.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


def prune(
        checkpoint,
        fp16 = False,
        ema = False,
        clip = True,
        vae = True,
        depth = True,
        unet = True,
):
    sd = checkpoint
    nested_sd = False
    if 'state_dict' in sd:
        sd = sd['state_dict']
        nested_sd = True
    sd_pruned = dict()
    for k in sd:
        cp = unet and k.startswith('model.diffusion_model.')
        cp = cp or (depth and k.startswith('depth_model.'))
        cp = cp or (vae and k.startswith('first_stage_model.'))
        cp = cp or (clip and k.startswith('cond_stage_model.'))
        if cp:
            k_in = k
            if ema:
                k_ema = 'model_ema.' + k[6:].replace('.', '')
                if k_ema in sd:
                    k_in = k_ema
            sd_pruned[k] = sd[k_in].half() if fp16 else sd[k_in]
    if nested_sd:
        return { 'state_dict': sd_pruned }
    else:
        return sd_pruned

def main(args):
    import os
    from argparse import ArgumentParser
    from functools import partial
    parser = ArgumentParser(
            description = "Prune a stable diffusion checkpoint",
            epilog = "Copyright (C) 2022  Lopho <contact@lopho.org> | \
                    Licensed under the AGPLv3 <https://www.gnu.org/licenses/>"
    )
    parser.add_argument(
            'input',
            type = str,
            help = "input checkpoint"
    )
    parser.add_argument(
            'output',
            type = str,
            help = "output checkpoint"
    )
    parser.add_argument(
            '-p', '--fp16',
            action = 'store_true',
            help = "convert to float16"
    )
    parser.add_argument(
            '-e', '--ema',
            action = 'store_true',
            help = "use EMA for weights"
    )
    parser.add_argument(
            '-c', '--no-clip',
            action = 'store_true',
            help = "strip CLIP weights"
    )
    parser.add_argument(
            '-a', '--no-vae',
            action = 'store_true',
            help = "strip VAE weights"
    )
    parser.add_argument(
            '-d', '--no-depth',
            action = 'store_true',
            help = "strip depth model weights"
    )
    parser.add_argument(
            '-u', '--no-unet',
            action = 'store_true',
            help = "strip UNet weights"
    )
    def error(self, message):
        import sys
        sys.stderr.write(f"error: {message}\n")
        self.print_help()
        self.exit()
    parser.error = partial(error, parser) # type: ignore
    args = parser.parse_args(args)
    is_safetensors = os.path.splitext(args.input)[1].lower() == '.safetensors'
    if is_safetensors:
        from safetensors.torch import load_file, save_file
        input_sd = load_file(args.input)
    else:
        from torch import load, save
        import pickle as python_pickle
        class torch_pickle:
            class Unpickler(python_pickle.Unpickler):
                def find_class(self, module, name):
                    try:
                        return super().find_class(module, name)
                    except:
                        return None
        input_sd = load(args.input, pickle_module = torch_pickle) # type: ignore
    pruned = prune(
            input_sd,
            fp16 = args.fp16,
            ema = args.ema,
            clip = not args.no_clip,
            vae = not args.no_vae,
            depth = not args.no_depth,
            unet = not args.no_unet
    )
    if is_safetensors:
        save_file(pruned, args.output)
    else:
        save(pruned, args.output)


if __name__ == '__main__':
    import sys
    main(sys.argv[1:])