(original_config, image_size: int, attention_type)
| 391 | |
| 392 | |
| 393 | def create_unet_config(original_config, image_size: int, attention_type): |
| 394 | unet_params = original_config["model"]["params"] |
| 395 | vae_params = original_config["autoencoder"]["params"]["ddconfig"] |
| 396 | |
| 397 | block_out_channels = [unet_params["model_channels"] * mult for mult in unet_params["channel_mult"]] |
| 398 | |
| 399 | down_block_types = [] |
| 400 | resolution = 1 |
| 401 | for i in range(len(block_out_channels)): |
| 402 | block_type = "CrossAttnDownBlock2D" if resolution in unet_params["attention_resolutions"] else "DownBlock2D" |
| 403 | down_block_types.append(block_type) |
| 404 | if i != len(block_out_channels) - 1: |
| 405 | resolution *= 2 |
| 406 | |
| 407 | up_block_types = [] |
| 408 | for i in range(len(block_out_channels)): |
| 409 | block_type = "CrossAttnUpBlock2D" if resolution in unet_params["attention_resolutions"] else "UpBlock2D" |
| 410 | up_block_types.append(block_type) |
| 411 | resolution //= 2 |
| 412 | |
| 413 | vae_scale_factor = 2 ** (len(vae_params["ch_mult"]) - 1) |
| 414 | |
| 415 | head_dim = unet_params["num_heads"] if "num_heads" in unet_params else None |
| 416 | use_linear_projection = ( |
| 417 | unet_params["use_linear_in_transformer"] if "use_linear_in_transformer" in unet_params else False |
| 418 | ) |
| 419 | if use_linear_projection: |
| 420 | if head_dim is None: |
| 421 | head_dim = [5, 10, 20, 20] |
| 422 | |
| 423 | config = { |
| 424 | "sample_size": image_size // vae_scale_factor, |
| 425 | "in_channels": unet_params["in_channels"], |
| 426 | "down_block_types": tuple(down_block_types), |
| 427 | "block_out_channels": tuple(block_out_channels), |
| 428 | "layers_per_block": unet_params["num_res_blocks"], |
| 429 | "cross_attention_dim": unet_params["context_dim"], |
| 430 | "attention_head_dim": head_dim, |
| 431 | "use_linear_projection": use_linear_projection, |
| 432 | "attention_type": attention_type, |
| 433 | } |
| 434 | |
| 435 | return config |
| 436 | |
| 437 | |
| 438 | def convert_gligen_to_diffusers( |
no outgoing calls
no test coverage detected
searching dependent graphs…