1- from diffusers import UNetUnconditionalModel
1+ from diffusers import UNetUnconditionalModel , DDPMScheduler , DDPMPipeline
22import argparse
33import json
44import torch
@@ -56,7 +56,7 @@ def assign_to_checkpoint(paths, checkpoint, old_checkpoint, attention_paths_to_s
5656
5757 if attention_paths_to_split is not None :
5858 if config is None :
59- raise ValueError (f "Please specify the config if setting 'attention_paths_to_split' to 'True'." )
59+ raise ValueError ("Please specify the config if setting 'attention_paths_to_split' to 'True'." )
6060
6161 for path , path_map in attention_paths_to_split .items ():
6262 old_tensor = old_checkpoint [path ]
@@ -86,7 +86,6 @@ def assign_to_checkpoint(paths, checkpoint, old_checkpoint, attention_paths_to_s
8686 for replacement in additional_replacements :
8787 new_path = new_path .replace (replacement ['old' ], replacement ['new' ])
8888
89-
9089 if 'attentions' in new_path :
9190 checkpoint [new_path ] = old_checkpoint [path ['old' ]].squeeze ()
9291 else :
@@ -97,7 +96,6 @@ def convert_ddpm_checkpoint(checkpoint, config):
9796 """
9897 Takes a state dict and a config, and returns a converted checkpoint.
9998 """
100-
10199 new_checkpoint = {}
102100
103101 new_checkpoint ['time_embedding.linear_1.weight' ] = checkpoint ['temb.dense.0.weight' ]
@@ -121,7 +119,6 @@ def convert_ddpm_checkpoint(checkpoint, config):
121119
122120 for i in range (num_downsample_blocks ):
123121 block_id = (i - 1 ) // (config ['num_res_blocks' ] + 1 )
124- layer_in_block_id = (i - 1 ) % (config ['num_res_blocks' ] + 1 )
125122
126123 if any ('downsample' in layer for layer in downsample_blocks [i ]):
127124 new_checkpoint [f'downsample_blocks.{ i } .downsamplers.0.conv.weight' ] = checkpoint [f'down.{ i } .downsample.conv.weight' ]
@@ -138,7 +135,6 @@ def convert_ddpm_checkpoint(checkpoint, config):
138135 paths = renew_resnet_paths (blocks [j ])
139136 assign_to_checkpoint (paths , new_checkpoint , checkpoint )
140137
141-
142138 if any ('attn' in layer for layer in downsample_blocks [i ]):
143139 num_attn = len ({'.' .join (shave_segments (layer , 2 ).split ('.' )[:2 ]) for layer in downsample_blocks [i ] if 'attn' in layer })
144140 attns = {layer_id : [key for key in downsample_blocks [i ] if f'attn.{ layer_id } ' in key ] for layer_id in range (num_blocks )}
@@ -148,7 +144,6 @@ def convert_ddpm_checkpoint(checkpoint, config):
148144 paths = renew_attention_paths (attns [j ])
149145 assign_to_checkpoint (paths , new_checkpoint , checkpoint , config = config )
150146
151-
152147 mid_block_1_layers = [key for key in checkpoint if "mid.block_1" in key ]
153148 mid_block_2_layers = [key for key in checkpoint if "mid.block_2" in key ]
154149 mid_attn_1_layers = [key for key in checkpoint if "mid.attn_1" in key ]
@@ -186,7 +181,6 @@ def convert_ddpm_checkpoint(checkpoint, config):
186181 paths = renew_resnet_paths (blocks [j ])
187182 assign_to_checkpoint (paths , new_checkpoint , checkpoint , additional_replacements = [replace_indices ])
188183
189-
190184 if any ('attn' in layer for layer in upsample_blocks [i ]):
191185 num_attn = len ({'.' .join (shave_segments (layer , 2 ).split ('.' )[:2 ]) for layer in upsample_blocks [i ] if 'attn' in layer })
192186 attns = {layer_id : [key for key in upsample_blocks [i ] if f'attn.{ layer_id } ' in key ] for layer_id in range (num_blocks )}
@@ -220,12 +214,21 @@ def convert_ddpm_checkpoint(checkpoint, config):
220214 "--dump_path" , default = None , type = str , required = True , help = "Path to the output model."
221215 )
222216
223-
224217 args = parser .parse_args ()
225218 checkpoint = torch .load (args .checkpoint_path )
226219
227220 with open (args .config_file ) as f :
228221 config = json .loads (f .read ())
229222
230- converted_checkpoint = convert_ddpm_checkpoint (args .checkpoint_path , args .config_file )
231- torch .save (converted_checkpoint , args .dump_path )
223+ converted_checkpoint = convert_ddpm_checkpoint (checkpoint , config )
224+
225+ if "ddpm" in config :
226+ del config ["ddpm" ]
227+
228+ model = UNetUnconditionalModel (** config )
229+ model .load_state_dict (converted_checkpoint )
230+
231+ scheduler = DDPMScheduler .from_config ("/" .join (args .checkpoint_path .split ("/" )[:- 1 ]))
232+
233+ pipe = DDPMPipeline (unet = model , scheduler = scheduler )
234+ pipe .save_pretrained (args .dump_path )
0 commit comments