sd_models_config.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import os
  2. import torch
  3. from modules import shared, paths, sd_disable_initialization
  4. sd_configs_path = shared.sd_configs_path
  5. sd_repo_configs_path = os.path.join(paths.paths['Stable Diffusion'], "configs", "stable-diffusion")
  6. sd_xl_repo_configs_path = os.path.join(paths.paths['Stable Diffusion XL'], "configs", "inference")
  7. config_default = shared.sd_default_config
  8. config_sd2 = os.path.join(sd_repo_configs_path, "v2-inference.yaml")
  9. config_sd2v = os.path.join(sd_repo_configs_path, "v2-inference-v.yaml")
  10. config_sd2_inpainting = os.path.join(sd_repo_configs_path, "v2-inpainting-inference.yaml")
  11. config_sdxl = os.path.join(sd_xl_repo_configs_path, "sd_xl_base.yaml")
  12. config_sdxl_refiner = os.path.join(sd_xl_repo_configs_path, "sd_xl_refiner.yaml")
  13. config_depth_model = os.path.join(sd_repo_configs_path, "v2-midas-inference.yaml")
  14. config_unclip = os.path.join(sd_repo_configs_path, "v2-1-stable-unclip-l-inference.yaml")
  15. config_unopenclip = os.path.join(sd_repo_configs_path, "v2-1-stable-unclip-h-inference.yaml")
  16. config_inpainting = os.path.join(sd_configs_path, "v1-inpainting-inference.yaml")
  17. config_instruct_pix2pix = os.path.join(sd_configs_path, "instruct-pix2pix.yaml")
  18. config_alt_diffusion = os.path.join(sd_configs_path, "alt-diffusion-inference.yaml")
  19. def is_using_v_parameterization_for_sd2(state_dict):
  20. """
  21. Detects whether unet in state_dict is using v-parameterization. Returns True if it is. You're welcome.
  22. """
  23. import ldm.modules.diffusionmodules.openaimodel
  24. from modules import devices
  25. device = devices.cpu
  26. with sd_disable_initialization.DisableInitialization():
  27. unet = ldm.modules.diffusionmodules.openaimodel.UNetModel(
  28. use_checkpoint=True,
  29. use_fp16=False,
  30. image_size=32,
  31. in_channels=4,
  32. out_channels=4,
  33. model_channels=320,
  34. attention_resolutions=[4, 2, 1],
  35. num_res_blocks=2,
  36. channel_mult=[1, 2, 4, 4],
  37. num_head_channels=64,
  38. use_spatial_transformer=True,
  39. use_linear_in_transformer=True,
  40. transformer_depth=1,
  41. context_dim=1024,
  42. legacy=False
  43. )
  44. unet.eval()
  45. with torch.no_grad():
  46. unet_sd = {k.replace("model.diffusion_model.", ""): v for k, v in state_dict.items() if "model.diffusion_model." in k}
  47. unet.load_state_dict(unet_sd, strict=True)
  48. unet.to(device=device, dtype=torch.float)
  49. test_cond = torch.ones((1, 2, 1024), device=device) * 0.5
  50. x_test = torch.ones((1, 4, 8, 8), device=device) * 0.5
  51. out = (unet(x_test, torch.asarray([999], device=device), context=test_cond) - x_test).mean().item()
  52. return out < -1
  53. def guess_model_config_from_state_dict(sd, filename):
  54. sd2_cond_proj_weight = sd.get('cond_stage_model.model.transformer.resblocks.0.attn.in_proj_weight', None)
  55. diffusion_model_input = sd.get('model.diffusion_model.input_blocks.0.0.weight', None)
  56. sd2_variations_weight = sd.get('embedder.model.ln_final.weight', None)
  57. if sd.get('conditioner.embedders.1.model.ln_final.weight', None) is not None:
  58. return config_sdxl
  59. if sd.get('conditioner.embedders.0.model.ln_final.weight', None) is not None:
  60. return config_sdxl_refiner
  61. elif sd.get('depth_model.model.pretrained.act_postprocess3.0.project.0.bias', None) is not None:
  62. return config_depth_model
  63. elif sd2_variations_weight is not None and sd2_variations_weight.shape[0] == 768:
  64. return config_unclip
  65. elif sd2_variations_weight is not None and sd2_variations_weight.shape[0] == 1024:
  66. return config_unopenclip
  67. if sd2_cond_proj_weight is not None and sd2_cond_proj_weight.shape[1] == 1024:
  68. if diffusion_model_input.shape[1] == 9:
  69. return config_sd2_inpainting
  70. elif is_using_v_parameterization_for_sd2(sd):
  71. return config_sd2v
  72. else:
  73. return config_sd2
  74. if diffusion_model_input is not None:
  75. if diffusion_model_input.shape[1] == 9:
  76. return config_inpainting
  77. if diffusion_model_input.shape[1] == 8:
  78. return config_instruct_pix2pix
  79. if sd.get('cond_stage_model.roberta.embeddings.word_embeddings.weight', None) is not None:
  80. return config_alt_diffusion
  81. return config_default
  82. def find_checkpoint_config(state_dict, info):
  83. if info is None:
  84. return guess_model_config_from_state_dict(state_dict, "")
  85. config = find_checkpoint_config_near_filename(info)
  86. if config is not None:
  87. return config
  88. return guess_model_config_from_state_dict(state_dict, info.filename)
  89. def find_checkpoint_config_near_filename(info):
  90. if info is None:
  91. return None
  92. config = f"{os.path.splitext(info.filename)[0]}.yaml"
  93. if os.path.exists(config):
  94. return config
  95. return None