(original_unet_config, class_embed_type=None)
| 208 | |
| 209 | |
| 210 | def create_unet_diffusers_config(original_unet_config, class_embed_type=None): |
| 211 | attention_resolutions = parse_list(original_unet_config["attention_resolutions"]) |
| 212 | attention_resolutions = [original_unet_config["image_size"] // int(res) for res in attention_resolutions] |
| 213 | |
| 214 | channel_mult = parse_list(original_unet_config["channel_mult"]) |
| 215 | block_out_channels = [original_unet_config["model_channels"] * mult for mult in channel_mult] |
| 216 | |
| 217 | down_block_types = [] |
| 218 | resolution = 1 |
| 219 | |
| 220 | for i in range(len(block_out_channels)): |
| 221 | if resolution in attention_resolutions: |
| 222 | block_type = "SimpleCrossAttnDownBlock2D" |
| 223 | elif original_unet_config["resblock_updown"]: |
| 224 | block_type = "ResnetDownsampleBlock2D" |
| 225 | else: |
| 226 | block_type = "DownBlock2D" |
| 227 | |
| 228 | down_block_types.append(block_type) |
| 229 | |
| 230 | if i != len(block_out_channels) - 1: |
| 231 | resolution *= 2 |
| 232 | |
| 233 | up_block_types = [] |
| 234 | for i in range(len(block_out_channels)): |
| 235 | if resolution in attention_resolutions: |
| 236 | block_type = "SimpleCrossAttnUpBlock2D" |
| 237 | elif original_unet_config["resblock_updown"]: |
| 238 | block_type = "ResnetUpsampleBlock2D" |
| 239 | else: |
| 240 | block_type = "UpBlock2D" |
| 241 | up_block_types.append(block_type) |
| 242 | resolution //= 2 |
| 243 | |
| 244 | head_dim = original_unet_config["num_head_channels"] |
| 245 | |
| 246 | use_linear_projection = ( |
| 247 | original_unet_config["use_linear_in_transformer"] |
| 248 | if "use_linear_in_transformer" in original_unet_config |
| 249 | else False |
| 250 | ) |
| 251 | if use_linear_projection: |
| 252 | # stable diffusion 2-base-512 and 2-768 |
| 253 | if head_dim is None: |
| 254 | head_dim = [5, 10, 20, 20] |
| 255 | |
| 256 | projection_class_embeddings_input_dim = None |
| 257 | |
| 258 | if class_embed_type is None: |
| 259 | if "num_classes" in original_unet_config: |
| 260 | if original_unet_config["num_classes"] == "sequential": |
| 261 | class_embed_type = "projection" |
| 262 | assert "adm_in_channels" in original_unet_config |
| 263 | projection_class_embeddings_input_dim = original_unet_config["adm_in_channels"] |
| 264 | else: |
| 265 | raise NotImplementedError( |
| 266 | f"Unknown conditional unet num_classes config: {original_unet_config['num_classes']}" |
| 267 | ) |
no test coverage detected
searching dependent graphs…