Creates a config for the diffusers based on the config of the LDM model.
(original_config, image_size: int, controlnet=False)
| 35 | |
| 36 | |
| 37 | def create_unet_diffusers_config(original_config, image_size: int, controlnet=False): |
| 38 | """ |
| 39 | Creates a config for the diffusers based on the config of the LDM model. |
| 40 | """ |
| 41 | if controlnet: |
| 42 | unet_params = original_config["model"]["params"]["control_stage_config"]["params"] |
| 43 | else: |
| 44 | if ( |
| 45 | "unet_config" in original_config["model"]["params"] |
| 46 | and original_config["model"]["params"]["unet_config"] is not None |
| 47 | ): |
| 48 | unet_params = original_config["model"]["params"]["unet_config"]["params"] |
| 49 | else: |
| 50 | unet_params = original_config["model"]["params"]["network_config"]["params"] |
| 51 | |
| 52 | vae_params = original_config["model"]["params"]["first_stage_config"]["params"]["ddconfig"] |
| 53 | |
| 54 | block_out_channels = [unet_params["model_channels"] * mult for mult in unet_params["channel_mult"]] |
| 55 | |
| 56 | down_block_types = [] |
| 57 | resolution = 1 |
| 58 | for i in range(len(block_out_channels)): |
| 59 | block_type = "CrossAttnDownBlock2D" if resolution in unet_params["attention_resolutions"] else "DownBlock2D" |
| 60 | down_block_types.append(block_type) |
| 61 | if i != len(block_out_channels) - 1: |
| 62 | resolution *= 2 |
| 63 | |
| 64 | up_block_types = [] |
| 65 | for i in range(len(block_out_channels)): |
| 66 | block_type = "CrossAttnUpBlock2D" if resolution in unet_params["attention_resolutions"] else "UpBlock2D" |
| 67 | up_block_types.append(block_type) |
| 68 | resolution //= 2 |
| 69 | |
| 70 | if unet_params["transformer_depth"] is not None: |
| 71 | transformer_layers_per_block = ( |
| 72 | unet_params["transformer_depth"] |
| 73 | if isinstance(unet_params["transformer_depth"], int) |
| 74 | else list(unet_params["transformer_depth"]) |
| 75 | ) |
| 76 | else: |
| 77 | transformer_layers_per_block = 1 |
| 78 | |
| 79 | vae_scale_factor = 2 ** (len(vae_params["ch_mult"]) - 1) |
| 80 | |
| 81 | head_dim = unet_params["num_heads"] if "num_heads" in unet_params else None |
| 82 | use_linear_projection = ( |
| 83 | unet_params["use_linear_in_transformer"] if "use_linear_in_transformer" in unet_params else False |
| 84 | ) |
| 85 | if use_linear_projection: |
| 86 | # stable diffusion 2-base-512 and 2-768 |
| 87 | if head_dim is None: |
| 88 | head_dim_mult = unet_params["model_channels"] // unet_params["num_head_channels"] |
| 89 | head_dim = [head_dim_mult * c for c in list(unet_params["channel_mult"])] |
| 90 | |
| 91 | class_embed_type = None |
| 92 | addition_embed_type = None |
| 93 | addition_time_embed_dim = None |
| 94 | projection_class_embeddings_input_dim = None |
no outgoing calls
no test coverage detected