Creates a config for the diffusers based on the config of the LDM model.
(original_config, image_size: int, controlnet=False)
| 8 | |
| 9 | |
| 10 | def create_unet_diffusers_config(original_config, image_size: int, controlnet=False): |
| 11 | """ |
| 12 | Creates a config for the diffusers based on the config of the LDM model. |
| 13 | """ |
| 14 | if controlnet: |
| 15 | unet_params = original_config.model.params.control_stage_config.params |
| 16 | else: |
| 17 | if "unet_config" in original_config.model.params and original_config.model.params.unet_config is not None: |
| 18 | unet_params = original_config.model.params.unet_config.params |
| 19 | else: |
| 20 | unet_params = original_config.model.params.network_config.params |
| 21 | |
| 22 | vae_params = original_config.model.params.first_stage_config.params.encoder_config.params |
| 23 | |
| 24 | block_out_channels = [unet_params.model_channels * mult for mult in unet_params.channel_mult] |
| 25 | |
| 26 | down_block_types = [] |
| 27 | resolution = 1 |
| 28 | for i in range(len(block_out_channels)): |
| 29 | block_type = ( |
| 30 | "CrossAttnDownBlockSpatioTemporal" |
| 31 | if resolution in unet_params.attention_resolutions |
| 32 | else "DownBlockSpatioTemporal" |
| 33 | ) |
| 34 | down_block_types.append(block_type) |
| 35 | if i != len(block_out_channels) - 1: |
| 36 | resolution *= 2 |
| 37 | |
| 38 | up_block_types = [] |
| 39 | for i in range(len(block_out_channels)): |
| 40 | block_type = ( |
| 41 | "CrossAttnUpBlockSpatioTemporal" |
| 42 | if resolution in unet_params.attention_resolutions |
| 43 | else "UpBlockSpatioTemporal" |
| 44 | ) |
| 45 | up_block_types.append(block_type) |
| 46 | resolution //= 2 |
| 47 | |
| 48 | if unet_params.transformer_depth is not None: |
| 49 | transformer_layers_per_block = ( |
| 50 | unet_params.transformer_depth |
| 51 | if isinstance(unet_params.transformer_depth, int) |
| 52 | else list(unet_params.transformer_depth) |
| 53 | ) |
| 54 | else: |
| 55 | transformer_layers_per_block = 1 |
| 56 | |
| 57 | vae_scale_factor = 2 ** (len(vae_params.ch_mult) - 1) |
| 58 | |
| 59 | head_dim = unet_params.num_heads if "num_heads" in unet_params else None |
| 60 | use_linear_projection = ( |
| 61 | unet_params.use_linear_in_transformer if "use_linear_in_transformer" in unet_params else False |
| 62 | ) |
| 63 | if use_linear_projection: |
| 64 | # stable diffusion 2-base-512 and 2-768 |
| 65 | if head_dim is None: |
| 66 | head_dim_mult = unet_params.model_channels // unet_params.num_head_channels |
| 67 | head_dim = [head_dim_mult * c for c in list(unet_params.channel_mult)] |
nothing calls this directly
no outgoing calls
no test coverage detected