Takes a state dict and a config, and returns a converted checkpoint.
(unet_state_dict, config, path=None)
| 296 | |
| 297 | |
| 298 | def convert_ldm_unet_checkpoint(unet_state_dict, config, path=None): |
| 299 | """ |
| 300 | Takes a state dict and a config, and returns a converted checkpoint. |
| 301 | """ |
| 302 | new_checkpoint = {} |
| 303 | |
| 304 | new_checkpoint["time_embedding.linear_1.weight"] = unet_state_dict["time_embed.0.weight"] |
| 305 | new_checkpoint["time_embedding.linear_1.bias"] = unet_state_dict["time_embed.0.bias"] |
| 306 | new_checkpoint["time_embedding.linear_2.weight"] = unet_state_dict["time_embed.2.weight"] |
| 307 | new_checkpoint["time_embedding.linear_2.bias"] = unet_state_dict["time_embed.2.bias"] |
| 308 | |
| 309 | if config["class_embed_type"] in [None, "identity"]: |
| 310 | # No parameters to port |
| 311 | ... |
| 312 | elif config["class_embed_type"] == "timestep" or config["class_embed_type"] == "projection": |
| 313 | new_checkpoint["class_embedding.linear_1.weight"] = unet_state_dict["label_emb.0.0.weight"] |
| 314 | new_checkpoint["class_embedding.linear_1.bias"] = unet_state_dict["label_emb.0.0.bias"] |
| 315 | new_checkpoint["class_embedding.linear_2.weight"] = unet_state_dict["label_emb.0.2.weight"] |
| 316 | new_checkpoint["class_embedding.linear_2.bias"] = unet_state_dict["label_emb.0.2.bias"] |
| 317 | else: |
| 318 | raise NotImplementedError(f"Not implemented `class_embed_type`: {config['class_embed_type']}") |
| 319 | |
| 320 | new_checkpoint["conv_in.weight"] = unet_state_dict["input_blocks.0.0.weight"] |
| 321 | new_checkpoint["conv_in.bias"] = unet_state_dict["input_blocks.0.0.bias"] |
| 322 | |
| 323 | new_checkpoint["conv_norm_out.weight"] = unet_state_dict["out.0.weight"] |
| 324 | new_checkpoint["conv_norm_out.bias"] = unet_state_dict["out.0.bias"] |
| 325 | new_checkpoint["conv_out.weight"] = unet_state_dict["out.2.weight"] |
| 326 | new_checkpoint["conv_out.bias"] = unet_state_dict["out.2.bias"] |
| 327 | |
| 328 | # Retrieves the keys for the input blocks only |
| 329 | num_input_blocks = len({".".join(layer.split(".")[:2]) for layer in unet_state_dict if "input_blocks" in layer}) |
| 330 | input_blocks = { |
| 331 | layer_id: [key for key in unet_state_dict if f"input_blocks.{layer_id}." in key] |
| 332 | for layer_id in range(num_input_blocks) |
| 333 | } |
| 334 | |
| 335 | # Retrieves the keys for the middle blocks only |
| 336 | num_middle_blocks = len({".".join(layer.split(".")[:2]) for layer in unet_state_dict if "middle_block" in layer}) |
| 337 | middle_blocks = { |
| 338 | layer_id: [key for key in unet_state_dict if f"middle_block.{layer_id}" in key] |
| 339 | for layer_id in range(num_middle_blocks) |
| 340 | } |
| 341 | |
| 342 | # Retrieves the keys for the output blocks only |
| 343 | num_output_blocks = len({".".join(layer.split(".")[:2]) for layer in unet_state_dict if "output_blocks" in layer}) |
| 344 | output_blocks = { |
| 345 | layer_id: [key for key in unet_state_dict if f"output_blocks.{layer_id}." in key] |
| 346 | for layer_id in range(num_output_blocks) |
| 347 | } |
| 348 | |
| 349 | for i in range(1, num_input_blocks): |
| 350 | block_id = (i - 1) // (config["layers_per_block"] + 1) |
| 351 | layer_in_block_id = (i - 1) % (config["layers_per_block"] + 1) |
| 352 | |
| 353 | resnets = [ |
| 354 | key for key in input_blocks[i] if f"input_blocks.{i}.0" in key and f"input_blocks.{i}.0.op" not in key |
| 355 | ] |
no test coverage detected