hcpdiff 0.9.0__py3-none-any.whl → 2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hcpdiff/__init__.py +4 -4
- hcpdiff/ckpt_manager/__init__.py +4 -5
- hcpdiff/ckpt_manager/ckpt.py +24 -0
- hcpdiff/ckpt_manager/format/__init__.py +4 -0
- hcpdiff/ckpt_manager/format/diffusers.py +59 -0
- hcpdiff/ckpt_manager/format/emb.py +21 -0
- hcpdiff/ckpt_manager/format/lora_webui.py +244 -0
- hcpdiff/ckpt_manager/format/sd_single.py +41 -0
- hcpdiff/ckpt_manager/loader.py +64 -0
- hcpdiff/data/__init__.py +4 -28
- hcpdiff/data/cache/__init__.py +1 -0
- hcpdiff/data/cache/vae.py +102 -0
- hcpdiff/data/dataset.py +20 -0
- hcpdiff/data/handler/__init__.py +3 -0
- hcpdiff/data/handler/controlnet.py +18 -0
- hcpdiff/data/handler/diffusion.py +80 -0
- hcpdiff/data/handler/text.py +111 -0
- hcpdiff/data/source/__init__.py +1 -2
- hcpdiff/data/source/folder_class.py +12 -29
- hcpdiff/data/source/text2img.py +36 -74
- hcpdiff/data/source/text2img_cond.py +9 -15
- hcpdiff/diffusion/__init__.py +0 -0
- hcpdiff/diffusion/noise/__init__.py +2 -0
- hcpdiff/diffusion/noise/pyramid_noise.py +42 -0
- hcpdiff/diffusion/noise/zero_terminal.py +39 -0
- hcpdiff/diffusion/sampler/__init__.py +5 -0
- hcpdiff/diffusion/sampler/base.py +72 -0
- hcpdiff/diffusion/sampler/ddpm.py +20 -0
- hcpdiff/diffusion/sampler/diffusers.py +66 -0
- hcpdiff/diffusion/sampler/edm.py +22 -0
- hcpdiff/diffusion/sampler/sigma_scheduler/__init__.py +3 -0
- hcpdiff/diffusion/sampler/sigma_scheduler/base.py +14 -0
- hcpdiff/diffusion/sampler/sigma_scheduler/ddpm.py +197 -0
- hcpdiff/diffusion/sampler/sigma_scheduler/edm.py +48 -0
- hcpdiff/easy/__init__.py +2 -0
- hcpdiff/easy/cfg/__init__.py +3 -0
- hcpdiff/easy/cfg/sd15_train.py +201 -0
- hcpdiff/easy/cfg/sdxl_train.py +140 -0
- hcpdiff/easy/cfg/t2i.py +177 -0
- hcpdiff/easy/model/__init__.py +2 -0
- hcpdiff/easy/model/cnet.py +31 -0
- hcpdiff/easy/model/loader.py +79 -0
- hcpdiff/easy/sampler.py +46 -0
- hcpdiff/evaluate/__init__.py +1 -0
- hcpdiff/evaluate/previewer.py +60 -0
- hcpdiff/loss/__init__.py +4 -1
- hcpdiff/loss/base.py +41 -0
- hcpdiff/loss/gw.py +35 -0
- hcpdiff/loss/ssim.py +37 -0
- hcpdiff/loss/vlb.py +79 -0
- hcpdiff/loss/weighting.py +66 -0
- hcpdiff/models/__init__.py +2 -2
- hcpdiff/models/cfg_context.py +17 -14
- hcpdiff/models/compose/compose_hook.py +44 -23
- hcpdiff/models/compose/compose_tokenizer.py +21 -8
- hcpdiff/models/compose/sdxl_composer.py +4 -4
- hcpdiff/models/container.py +1 -1
- hcpdiff/models/controlnet.py +16 -16
- hcpdiff/models/lora_base_patch.py +14 -25
- hcpdiff/models/lora_layers.py +3 -9
- hcpdiff/models/lora_layers_patch.py +14 -24
- hcpdiff/models/text_emb_ex.py +84 -6
- hcpdiff/models/textencoder_ex.py +54 -18
- hcpdiff/models/wrapper/__init__.py +3 -0
- hcpdiff/models/wrapper/pixart.py +19 -0
- hcpdiff/models/wrapper/sd.py +218 -0
- hcpdiff/models/wrapper/utils.py +20 -0
- hcpdiff/parser/__init__.py +1 -0
- hcpdiff/parser/embpt.py +32 -0
- hcpdiff/tools/convert_caption_txt2json.py +1 -1
- hcpdiff/tools/dataset_generator.py +94 -0
- hcpdiff/tools/download_hf_model.py +24 -0
- hcpdiff/tools/embedding_convert.py +6 -2
- hcpdiff/tools/init_proj.py +3 -21
- hcpdiff/tools/lora_convert.py +19 -15
- hcpdiff/tools/save_model.py +12 -0
- hcpdiff/tools/sd2diffusers.py +1 -1
- hcpdiff/train_colo.py +1 -1
- hcpdiff/train_deepspeed.py +1 -1
- hcpdiff/trainer_ac.py +79 -0
- hcpdiff/trainer_ac_single.py +31 -0
- hcpdiff/utils/__init__.py +0 -2
- hcpdiff/utils/inpaint_pipe.py +790 -0
- hcpdiff/utils/net_utils.py +29 -6
- hcpdiff/utils/pipe_hook.py +46 -33
- hcpdiff/utils/utils.py +21 -4
- hcpdiff/workflow/__init__.py +15 -10
- hcpdiff/workflow/daam/__init__.py +1 -0
- hcpdiff/workflow/daam/act.py +66 -0
- hcpdiff/workflow/daam/hook.py +109 -0
- hcpdiff/workflow/diffusion.py +128 -136
- hcpdiff/workflow/fast.py +31 -0
- hcpdiff/workflow/flow.py +67 -0
- hcpdiff/workflow/io.py +36 -68
- hcpdiff/workflow/model.py +46 -43
- hcpdiff/workflow/text.py +84 -52
- hcpdiff/workflow/utils.py +32 -12
- hcpdiff/workflow/vae.py +37 -38
- hcpdiff-2.1.dist-info/METADATA +285 -0
- hcpdiff-2.1.dist-info/RECORD +114 -0
- {hcpdiff-0.9.0.dist-info → hcpdiff-2.1.dist-info}/WHEEL +1 -1
- hcpdiff-2.1.dist-info/entry_points.txt +5 -0
- hcpdiff/ckpt_manager/base.py +0 -16
- hcpdiff/ckpt_manager/ckpt_diffusers.py +0 -45
- hcpdiff/ckpt_manager/ckpt_pkl.py +0 -138
- hcpdiff/ckpt_manager/ckpt_safetensor.py +0 -60
- hcpdiff/ckpt_manager/ckpt_webui.py +0 -54
- hcpdiff/data/bucket.py +0 -358
- hcpdiff/data/caption_loader.py +0 -80
- hcpdiff/data/cond_dataset.py +0 -40
- hcpdiff/data/crop_info_dataset.py +0 -40
- hcpdiff/data/data_processor.py +0 -33
- hcpdiff/data/pair_dataset.py +0 -146
- hcpdiff/data/sampler.py +0 -54
- hcpdiff/data/source/base.py +0 -30
- hcpdiff/data/utils.py +0 -80
- hcpdiff/infer_workflow.py +0 -57
- hcpdiff/loggers/__init__.py +0 -13
- hcpdiff/loggers/base_logger.py +0 -76
- hcpdiff/loggers/cli_logger.py +0 -40
- hcpdiff/loggers/preview/__init__.py +0 -1
- hcpdiff/loggers/preview/image_previewer.py +0 -149
- hcpdiff/loggers/tensorboard_logger.py +0 -30
- hcpdiff/loggers/wandb_logger.py +0 -31
- hcpdiff/loggers/webui_logger.py +0 -9
- hcpdiff/loss/min_snr_loss.py +0 -52
- hcpdiff/models/layers.py +0 -81
- hcpdiff/models/plugin.py +0 -348
- hcpdiff/models/wrapper.py +0 -75
- hcpdiff/noise/__init__.py +0 -3
- hcpdiff/noise/noise_base.py +0 -16
- hcpdiff/noise/pyramid_noise.py +0 -50
- hcpdiff/noise/zero_terminal.py +0 -44
- hcpdiff/train_ac.py +0 -565
- hcpdiff/train_ac_single.py +0 -39
- hcpdiff/utils/caption_tools.py +0 -105
- hcpdiff/utils/cfg_net_tools.py +0 -321
- hcpdiff/utils/cfg_resolvers.py +0 -16
- hcpdiff/utils/ema.py +0 -52
- hcpdiff/utils/img_size_tool.py +0 -248
- hcpdiff/vis/__init__.py +0 -3
- hcpdiff/vis/base_interface.py +0 -12
- hcpdiff/vis/disk_interface.py +0 -48
- hcpdiff/vis/webui_interface.py +0 -17
- hcpdiff/visualizer.py +0 -258
- hcpdiff/visualizer_reloadable.py +0 -237
- hcpdiff/workflow/base.py +0 -59
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/anime/text2img_anime.yaml +0 -21
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/anime/text2img_anime_lora.yaml +0 -58
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/change_vae.yaml +0 -6
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/euler_a.yaml +0 -8
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/img2img.yaml +0 -10
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/img2img_controlnet.yaml +0 -19
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/inpaint.yaml +0 -11
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/load_lora.yaml +0 -26
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/load_unet_part.yaml +0 -18
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/offload_2GB.yaml +0 -6
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/save_model.yaml +0 -44
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/text2img.yaml +0 -53
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/text2img_DA++.yaml +0 -34
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/text2img_sdxl.yaml +0 -9
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/plugins/plugin_controlnet.yaml +0 -17
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/te_struct.txt +0 -193
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/dataset/base_dataset.yaml +0 -29
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/dataset/regularization_dataset.yaml +0 -31
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/CustomDiffusion.yaml +0 -74
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/DreamArtist++.yaml +0 -135
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/DreamArtist.yaml +0 -45
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/DreamBooth.yaml +0 -62
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/FT_sdxl.yaml +0 -33
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/Lion_optimizer.yaml +0 -17
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/TextualInversion.yaml +0 -41
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/add_logger_tensorboard_wandb.yaml +0 -15
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/controlnet.yaml +0 -53
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/ema.yaml +0 -10
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/fine-tuning.yaml +0 -53
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/locon.yaml +0 -24
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/lora_anime_character.yaml +0 -77
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/lora_conventional.yaml +0 -56
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/lora_sdxl.yaml +0 -41
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/min_snr.yaml +0 -7
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/preview_in_training.yaml +0 -6
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples_noob/DreamBooth.yaml +0 -70
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples_noob/TextualInversion.yaml +0 -45
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples_noob/fine-tuning.yaml +0 -45
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples_noob/lora.yaml +0 -63
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/train_base.yaml +0 -81
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/tuning_base.yaml +0 -42
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/unet_struct.txt +0 -932
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/workflow/highres_fix_latent.yaml +0 -86
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/workflow/highres_fix_pixel.yaml +0 -99
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/workflow/text2img.yaml +0 -57
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/workflow/text2img_lora.yaml +0 -70
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/zero2.json +0 -32
- hcpdiff-0.9.0.data/data/hcpdiff/cfgs/zero3.json +0 -39
- hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/caption.txt +0 -1
- hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/name.txt +0 -1
- hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/name_2pt_caption.txt +0 -1
- hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/name_caption.txt +0 -1
- hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/object.txt +0 -27
- hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/object_caption.txt +0 -27
- hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/style.txt +0 -19
- hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/style_caption.txt +0 -19
- hcpdiff-0.9.0.dist-info/METADATA +0 -199
- hcpdiff-0.9.0.dist-info/RECORD +0 -155
- hcpdiff-0.9.0.dist-info/entry_points.txt +0 -2
- {hcpdiff-0.9.0.dist-info → hcpdiff-2.1.dist-info/licenses}/LICENSE +0 -0
- {hcpdiff-0.9.0.dist-info → hcpdiff-2.1.dist-info}/top_level.txt +0 -0
hcpdiff/visualizer_reloadable.py
DELETED
@@ -1,237 +0,0 @@
|
|
1
|
-
from hcpdiff.visualizer import Visualizer
|
2
|
-
import accelerate.hooks
|
3
|
-
from omegaconf import OmegaConf
|
4
|
-
from hcpdiff.models import EmbeddingPTHook
|
5
|
-
import hydra
|
6
|
-
from diffusers import AutoencoderKL, PNDMScheduler
|
7
|
-
import torch
|
8
|
-
from hcpdiff.utils.cfg_net_tools import HCPModelLoader, make_plugin
|
9
|
-
from hcpdiff.utils import load_config, hash_str
|
10
|
-
from copy import deepcopy
|
11
|
-
|
12
|
-
class VisualizerReloadable(Visualizer):
|
13
|
-
def __init__(self, cfgs):
|
14
|
-
self.lora_dict = {}
|
15
|
-
self.part_plugin_cfg_set = set()
|
16
|
-
super().__init__(cfgs)
|
17
|
-
|
18
|
-
def _merge_model(self, cfg_merge):
|
19
|
-
if 'plugin_cfg' in cfg_merge: # Build plugins
|
20
|
-
plugin_cfg = hydra.utils.instantiate(load_config(cfg_merge.plugin_cfg))
|
21
|
-
make_plugin(self.pipe.unet, plugin_cfg.plugin_unet)
|
22
|
-
make_plugin(self.pipe.text_encoder, plugin_cfg.plugin_TE)
|
23
|
-
|
24
|
-
for cfg_group in cfg_merge.values():
|
25
|
-
if hasattr(cfg_group, 'type'):
|
26
|
-
if cfg_group.type == 'unet':
|
27
|
-
lora_group = HCPModelLoader(self.pipe.unet).load_all(cfg_group)
|
28
|
-
elif cfg_group.type == 'TE':
|
29
|
-
lora_group = HCPModelLoader(self.pipe.text_encoder).load_all(cfg_group)
|
30
|
-
else:
|
31
|
-
raise ValueError(f'no host model type named {cfg_group.type}')
|
32
|
-
|
33
|
-
# record all lora plugin with its config hash
|
34
|
-
if not lora_group.empty():
|
35
|
-
for cfg_lora, lora_plugin in zip(cfg_group.lora, lora_group.plugin_dict.values()):
|
36
|
-
self.lora_dict[hash_str(OmegaConf.to_yaml(cfg_lora, resolve=True))] = lora_plugin
|
37
|
-
|
38
|
-
# record all part and plugin config hash
|
39
|
-
for cfg_part in getattr(cfg_group, "part", None) or []:
|
40
|
-
self.part_plugin_cfg_set.add(hash_str(OmegaConf.to_yaml(cfg_part, resolve=True)))
|
41
|
-
for cfg_plugin in getattr(cfg_group, "plugin", None) or []:
|
42
|
-
self.part_plugin_cfg_set.add(hash_str(OmegaConf.to_yaml(cfg_plugin, resolve=True)))
|
43
|
-
|
44
|
-
def merge_model(self):
|
45
|
-
self.part_plugin_cfg_set.clear()
|
46
|
-
self.lora_dict.clear()
|
47
|
-
self._merge_model(self.cfg_merge)
|
48
|
-
|
49
|
-
def part_plugin_changed(self):
|
50
|
-
if not self.cfg_merge:
|
51
|
-
return not self.cfg_same(self.cfg_merge, self.cfgs_old.merge)
|
52
|
-
part_plugin_cfg_set_new = set()
|
53
|
-
for cfg_group in self.cfg_merge.values():
|
54
|
-
for cfg_part in getattr(cfg_group, "part", None) or []:
|
55
|
-
part_plugin_cfg_set_new.add(hash_str(OmegaConf.to_yaml(cfg_part, resolve=True)))
|
56
|
-
for cfg_plugin in getattr(cfg_group, "plugin", None) or []:
|
57
|
-
part_plugin_cfg_set_new.add(hash_str(OmegaConf.to_yaml(cfg_plugin, resolve=True)))
|
58
|
-
return part_plugin_cfg_set_new != self.part_plugin_cfg_set
|
59
|
-
|
60
|
-
@staticmethod
|
61
|
-
def cfg_same(cfg1, cfg2):
|
62
|
-
if cfg1 is None:
|
63
|
-
return cfg2 is None
|
64
|
-
elif cfg2 is None:
|
65
|
-
return cfg1 is None
|
66
|
-
else:
|
67
|
-
return OmegaConf.to_yaml(cfg1) == OmegaConf.to_yaml(cfg2)
|
68
|
-
|
69
|
-
def reload_offload(self) -> bool:
|
70
|
-
if not self.cfg_same(self.cfgs_raw.offload, self.cfgs_raw_old.offload):
|
71
|
-
if self.offload_old:
|
72
|
-
# remove offload hooks
|
73
|
-
accelerate.hooks.remove_hook_from_module(self.pipe.unet, recurse=True)
|
74
|
-
accelerate.hooks.remove_hook_from_module(self.pipe.vae, recurse=True)
|
75
|
-
else:
|
76
|
-
return False
|
77
|
-
|
78
|
-
if self.offload:
|
79
|
-
self.pipe.unet.to('cpu')
|
80
|
-
self.pipe.vae.to('cpu')
|
81
|
-
torch.cuda.empty_cache()
|
82
|
-
torch.cuda.synchronize()
|
83
|
-
self.build_offload(self.cfgs.offload)
|
84
|
-
else:
|
85
|
-
self.pipe.unet.to('cuda')
|
86
|
-
return True
|
87
|
-
|
88
|
-
def reload_emb_hook(self) -> bool:
|
89
|
-
if self.cfgs.emb_dir!=self.cfgs_old.emb_dir or self.cfgs.N_repeats!=self.cfgs_old.N_repeats:
|
90
|
-
self.emb_hook.remove()
|
91
|
-
self.emb_hook, _ = EmbeddingPTHook.hook_from_dir(self.cfgs.emb_dir, self.pipe.tokenizer, self.pipe.text_encoder,
|
92
|
-
N_repeats=self.cfgs.N_repeats)
|
93
|
-
return True
|
94
|
-
return False
|
95
|
-
|
96
|
-
def reload_te_hook(self) -> bool:
|
97
|
-
if self.cfgs.clip_skip != self.cfgs_old.clip_skip or self.cfgs.N_repeats != self.cfgs_old.N_repeats:
|
98
|
-
self.te_hook.N_repeats = self.cfgs.N_repeats
|
99
|
-
self.te_hook.clip_skip = self.cfgs.clip_skip
|
100
|
-
return True
|
101
|
-
return False
|
102
|
-
|
103
|
-
def reload_model(self) -> bool:
|
104
|
-
pipeline = self.get_pipeline()
|
105
|
-
if self.cfgs.pretrained_model!=self.cfgs_old.pretrained_model or self.part_plugin_changed():
|
106
|
-
comp = pipeline.from_pretrained(self.cfgs.pretrained_model, safety_checker=None, requires_safety_checker=False,
|
107
|
-
torch_dtype=self.dtype).components
|
108
|
-
if 'vae' in self.cfgs.new_components:
|
109
|
-
self.cfgs.new_components.vae = hydra.utils.instantiate(self.cfgs.new_components.vae)
|
110
|
-
comp.update(self.cfgs.new_components)
|
111
|
-
self.pipe = pipeline(**comp)
|
112
|
-
if self.cfg_merge:
|
113
|
-
self.merge_model()
|
114
|
-
self.pipe = self.pipe.to(torch_dtype=self.dtype)
|
115
|
-
self.build_optimize()
|
116
|
-
return True
|
117
|
-
return False
|
118
|
-
|
119
|
-
def reload_pipe(self) -> bool:
|
120
|
-
pipeline = self.get_pipeline()
|
121
|
-
if type(self.pipe)!=pipeline:
|
122
|
-
self.pipe = pipeline(**self.pipe.components)
|
123
|
-
return True
|
124
|
-
return False
|
125
|
-
|
126
|
-
|
127
|
-
def reload_scheduler(self) -> bool:
|
128
|
-
if 'scheduler' in self.cfgs_raw_old.new_components and 'scheduler' not in self.cfgs_raw.new_components:
|
129
|
-
# load default scheduler
|
130
|
-
self.pipe.scheduler = PNDMScheduler.from_pretrained(self.cfgs.pretrained_model, subfolder='scheduler', torch_dtype=self.dtype)
|
131
|
-
return True
|
132
|
-
elif not self.cfg_same(getattr(self.cfgs_raw_old.new_components, 'scheduler', {}), getattr(self.cfgs_raw.new_components, 'scheduler', {})):
|
133
|
-
self.pipe.scheduler = self.cfgs.new_components.scheduler
|
134
|
-
return True
|
135
|
-
return False
|
136
|
-
|
137
|
-
def reload_vae(self) -> bool:
|
138
|
-
if 'vae' in self.cfgs_raw_old.new_components and 'vae' not in self.cfgs_raw.new_components:
|
139
|
-
# load default VAE
|
140
|
-
self.cfgs.new_components.vae = AutoencoderKL.from_pretrained(self.cfgs.pretrained_model, subfolder='vae', torch_dtype=self.dtype)
|
141
|
-
return True
|
142
|
-
elif not self.cfg_same(getattr(self.cfgs_raw_old.new_components, 'vae', {}), getattr(self.cfgs_raw.new_components, 'vae', {})):
|
143
|
-
# VAE config changed, need reload
|
144
|
-
if 'vae' in self.cfgs_old.new_components:
|
145
|
-
del self.cfgs_old.new_components.vae
|
146
|
-
torch.cuda.empty_cache()
|
147
|
-
self.cfgs.new_components.vae = hydra.utils.instantiate(self.cfgs.new_components.vae)
|
148
|
-
self.pipe.vae = self.cfgs.new_components.vae
|
149
|
-
return True
|
150
|
-
return False
|
151
|
-
|
152
|
-
def reload_lora(self):
|
153
|
-
if self.cfg_merge is None:
|
154
|
-
if self.cfgs_old.merge is None:
|
155
|
-
return False
|
156
|
-
else:
|
157
|
-
for lora in self.lora_dict.values():
|
158
|
-
lora.remove()
|
159
|
-
self.lora_dict.clear()
|
160
|
-
return True
|
161
|
-
|
162
|
-
cfg_merge = deepcopy(self.cfg_merge)
|
163
|
-
all_lora_hash = set()
|
164
|
-
for k, cfg_group in self.cfg_merge.items():
|
165
|
-
if 'part' in cfg_merge[k]:
|
166
|
-
del cfg_merge[k].part
|
167
|
-
if 'plugin' in cfg_merge[k]:
|
168
|
-
del cfg_merge[k].plugin
|
169
|
-
|
170
|
-
lora_add = []
|
171
|
-
for cfg_lora in getattr(cfg_group, "lora", None) or []:
|
172
|
-
cfg_hash = hash_str(OmegaConf.to_yaml(cfg_lora, resolve=True))
|
173
|
-
if cfg_hash not in self.lora_dict:
|
174
|
-
lora_add.append(cfg_lora)
|
175
|
-
all_lora_hash.add(cfg_hash)
|
176
|
-
cfg_merge[k].lora = OmegaConf.create(lora_add)
|
177
|
-
|
178
|
-
lora_rm_set = set(self.lora_dict.keys())-all_lora_hash
|
179
|
-
for cfg_hash in lora_rm_set:
|
180
|
-
self.lora_dict[cfg_hash].remove()
|
181
|
-
for cfg_hash in lora_rm_set:
|
182
|
-
del self.lora_dict[cfg_hash]
|
183
|
-
|
184
|
-
self._merge_model(cfg_merge)
|
185
|
-
|
186
|
-
def check_reload(self, cfgs):
|
187
|
-
'''
|
188
|
-
Reload and modify each module based on the changes of configuration file.
|
189
|
-
'''
|
190
|
-
self.cfgs_raw_old = self.cfgs_raw
|
191
|
-
self.cfgs_old = self.cfgs
|
192
|
-
self.offload_old = self.offload
|
193
|
-
|
194
|
-
self.cfgs_raw = cfgs
|
195
|
-
|
196
|
-
# Reload vae only when vae config changes
|
197
|
-
if 'vae' in self.cfgs_raw.new_components:
|
198
|
-
vae_cfg = self.cfgs_raw.new_components.vae
|
199
|
-
self.cfgs_raw.new_components.vae = None
|
200
|
-
self.cfgs = hydra.utils.instantiate(self.cfgs_raw)
|
201
|
-
self.cfgs_raw.new_components.vae = vae_cfg
|
202
|
-
self.cfgs.new_components.vae = vae_cfg
|
203
|
-
else:
|
204
|
-
self.cfgs = hydra.utils.instantiate(self.cfgs_raw)
|
205
|
-
|
206
|
-
self.cfg_merge = self.cfgs.merge
|
207
|
-
self.offload = 'offload' in self.cfgs and self.cfgs.offload is not None
|
208
|
-
self.dtype = self.dtype_dict[self.cfgs.dtype]
|
209
|
-
|
210
|
-
self.need_inter_imgs = any(item.need_inter_imgs for item in self.cfgs.interface)
|
211
|
-
|
212
|
-
is_model_reload = self.reload_model()
|
213
|
-
if not is_model_reload:
|
214
|
-
is_vae_reload = self.reload_vae()
|
215
|
-
if is_vae_reload:
|
216
|
-
self.build_vae_offload()
|
217
|
-
self.reload_lora()
|
218
|
-
self.reload_scheduler()
|
219
|
-
self.reload_offload()
|
220
|
-
self.reload_emb_hook()
|
221
|
-
self.reload_te_hook()
|
222
|
-
self.reload_pipe()
|
223
|
-
|
224
|
-
if getattr(self.cfgs, 'vae_optimize', None) is not None:
|
225
|
-
if self.cfgs.vae_optimize.tiling:
|
226
|
-
self.pipe.vae.enable_tiling()
|
227
|
-
else:
|
228
|
-
self.pipe.vae.disable_tiling()
|
229
|
-
|
230
|
-
if self.cfgs.vae_optimize.slicing:
|
231
|
-
self.pipe.vae.enable_slicing()
|
232
|
-
else:
|
233
|
-
self.pipe.vae.disable_slicing()
|
234
|
-
|
235
|
-
del self.cfgs_raw_old
|
236
|
-
del self.cfgs_old
|
237
|
-
|
hcpdiff/workflow/base.py
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
from typing import List, Dict
|
2
|
-
from tqdm.auto import tqdm
|
3
|
-
|
4
|
-
class from_memory:
|
5
|
-
#TODO: add memory for all from_memory in cfg
|
6
|
-
def __init__(self, memory, mem_name):
|
7
|
-
self.mem_name = mem_name
|
8
|
-
self.memory = memory
|
9
|
-
|
10
|
-
def __call__(self):
|
11
|
-
memory = self.memory # use in eval
|
12
|
-
return eval(f'memory.{self.mem_name}')
|
13
|
-
|
14
|
-
def from_memory_context(fun):
|
15
|
-
def f(*args, **kwargs):
|
16
|
-
filter_kwargs = {k: (v() if isinstance(v, from_memory) else v) for k,v in kwargs.items()}
|
17
|
-
return fun(*args, **filter_kwargs)
|
18
|
-
return f
|
19
|
-
|
20
|
-
class BasicAction:
|
21
|
-
def __init__(self):
|
22
|
-
pass
|
23
|
-
|
24
|
-
def __call__(self, *args, **kwargs):
|
25
|
-
return self.forward(*args, **kwargs)
|
26
|
-
|
27
|
-
def forward(self, *args, **kwargs):
|
28
|
-
raise NotImplementedError()
|
29
|
-
|
30
|
-
class MemoryMixin:
|
31
|
-
pass
|
32
|
-
|
33
|
-
class ExecAction(BasicAction, MemoryMixin):
|
34
|
-
def __init__(self, prog:str):
|
35
|
-
self.prog = prog
|
36
|
-
|
37
|
-
def forward(self, memory, **states):
|
38
|
-
exec(self.prog)
|
39
|
-
return states
|
40
|
-
|
41
|
-
class LoopAction(BasicAction, MemoryMixin):
|
42
|
-
def __init__(self, loop_value:Dict[str, str], actions:List[BasicAction]):
|
43
|
-
self.loop_value = loop_value
|
44
|
-
self.actions = actions
|
45
|
-
|
46
|
-
def forward(self, memory, **states):
|
47
|
-
loop_data = [states.pop(k) for k in self.loop_value.keys()]
|
48
|
-
pbar = tqdm(zip(*loop_data), total=len(loop_data[0]))
|
49
|
-
N_steps = len(self.actions)
|
50
|
-
for data in pbar:
|
51
|
-
feed_data = {k:v for k,v in zip(self.loop_value.values(), data)}
|
52
|
-
states.update(feed_data)
|
53
|
-
for step, act in enumerate(self.actions):
|
54
|
-
pbar.set_description(f'[{step+1}/{N_steps}] action: {type(act).__name__}')
|
55
|
-
if isinstance(act, MemoryMixin):
|
56
|
-
states = act(memory=memory, **states)
|
57
|
-
else:
|
58
|
-
states = act(**states)
|
59
|
-
return states
|
@@ -1,21 +0,0 @@
|
|
1
|
-
_base_: [cfgs/infer/text2img.yaml]
|
2
|
-
|
3
|
-
pretrained_model: 'deepghs/animefull-latest' # animefull-latest model
|
4
|
-
prompt: 'masterpiece, best quality, 1girl, solo, tohsaka rin' # image of 远坂凛(tohsaka rin)
|
5
|
-
neg_prompt: 'lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry'
|
6
|
-
|
7
|
-
clip_skip: 1 #动漫模型通常会跳过一个CLIP层
|
8
|
-
|
9
|
-
infer_args:
|
10
|
-
width: 512
|
11
|
-
height: 768 # image size
|
12
|
-
guidance_scale: 7.5 # scale, when higher, the images will tend to be more similar
|
13
|
-
num_inference_steps: 30 # how many steps
|
14
|
-
|
15
|
-
new_components:
|
16
|
-
scheduler:
|
17
|
-
_target_: diffusers.EulerAncestralDiscreteScheduler # change Sampler
|
18
|
-
beta_start: 0.00085
|
19
|
-
beta_end: 0.012
|
20
|
-
beta_schedule: 'scaled_linear'
|
21
|
-
|
@@ -1,58 +0,0 @@
|
|
1
|
-
_base_:
|
2
|
-
- cfgs/infer/anime/text2img_anime.yaml
|
3
|
-
|
4
|
-
pretrained_model: 'stablediffusionapi/anything-v5' # better generic anime model
|
5
|
-
|
6
|
-
# safe prompt
|
7
|
-
prompt: 'masterpiece, best quality, highres, game cg, 1girl, solo, {night}, {starry sky}, beach, beautiful detailed sky, {extremely detailed background:1.2}, mature, {surtr_arknights-${model_steps}:1.2}, red_hair, horns, long_hair, purple_eyes, bangs, looking_at_viewer, bare_shoulders, hair_between_eyes, cleavage, {standing}, looking at viewer, {bikini:1.3}, light smile'
|
8
|
-
|
9
|
-
# r18 prompt
|
10
|
-
# prompt: 'nsfw, masterpiece, best quality, highres, 1girl, solo, {lyging on bed}, {extremely detailed background:1.2}, {nude:1.4}, {spread legs}, {arms up}, mature, {surtr_arknights-1000:1.2}, red_hair, horns, long_hair, purple_eyes, bangs, looking_at_viewer, bare_shoulders, hair_between_eyes, cleavage, nipples, {pussy:1.15}, {pussy juice:1.3}, looking at viewer, {embarrassed}, endured face, feet out of frame'
|
11
|
-
|
12
|
-
# negative prompt
|
13
|
-
neg_prompt: 'lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, white border'
|
14
|
-
N_repeats: 2 # if prompt or neg_prompt is too long, increase this number
|
15
|
-
|
16
|
-
bs: 1
|
17
|
-
num: 1
|
18
|
-
|
19
|
-
# when seed is not set, random seed will be used
|
20
|
-
# seed: 758691538 # seed for safe
|
21
|
-
# seed: 465191133 # seed for r18
|
22
|
-
|
23
|
-
infer_args:
|
24
|
-
width: 512
|
25
|
-
height: 768 # image size
|
26
|
-
guidance_scale: 7.5 # scale, when higher, the images will tend to be more similar
|
27
|
-
num_inference_steps: 30 # how many steps
|
28
|
-
|
29
|
-
exp_dir: 'exps/2023-07-26-01-05-35' # experiment directory
|
30
|
-
model_steps: 1000 # steps of selected model
|
31
|
-
emb_dir: '${exp_dir}/ckpts/'
|
32
|
-
output_dir: 'output/'
|
33
|
-
|
34
|
-
merge:
|
35
|
-
alpha: 0.85 # lora权重, default: 0.85
|
36
|
-
|
37
|
-
group1:
|
38
|
-
type: 'unet'
|
39
|
-
base_model_alpha: 1.0 # base model weight to merge with lora or part
|
40
|
-
lora:
|
41
|
-
- path: '${.....exp_dir}/ckpts/unet-${.....model_steps}.safetensors'
|
42
|
-
alpha: ${....alpha}
|
43
|
-
layers: 'all'
|
44
|
-
part: null
|
45
|
-
|
46
|
-
group2:
|
47
|
-
type: 'TE'
|
48
|
-
base_model_alpha: 1.0 # base model weight to merge with lora or part
|
49
|
-
lora:
|
50
|
-
- path: '${.....exp_dir}/ckpts/text_encoder-${.....model_steps}.safetensors'
|
51
|
-
alpha: ${....alpha}
|
52
|
-
layers: 'all'
|
53
|
-
part: null
|
54
|
-
|
55
|
-
interface:
|
56
|
-
- _target_: hcpdiff.vis.DiskInterface
|
57
|
-
show_steps: 0
|
58
|
-
save_root: '${output_dir}'
|
@@ -1,19 +0,0 @@
|
|
1
|
-
_base_: [cfgs/infer/text2img.yaml]
|
2
|
-
|
3
|
-
ex_input:
|
4
|
-
cond:
|
5
|
-
_target_: hcpdiff.data.data_processor.ControlNetProcessor
|
6
|
-
image: 'cond_img.png'
|
7
|
-
|
8
|
-
merge:
|
9
|
-
plugin_cfg: cfgs/plugins/plugin_controlnet.yaml
|
10
|
-
|
11
|
-
group1:
|
12
|
-
type: 'unet'
|
13
|
-
base_model_alpha: 1.0 # base model weight to merge with lora or part
|
14
|
-
lora: null
|
15
|
-
part: null
|
16
|
-
plugin:
|
17
|
-
controlnet1:
|
18
|
-
path: 'ckpts/controlnet.ckpt'
|
19
|
-
layers: 'all'
|
@@ -1,26 +0,0 @@
|
|
1
|
-
_base_: [cfgs/infer/text2img.yaml]
|
2
|
-
|
3
|
-
merge:
|
4
|
-
group1:
|
5
|
-
type: 'unet'
|
6
|
-
base_model_alpha: 1.0 # base model weight to merge with lora or part
|
7
|
-
lora:
|
8
|
-
- path: 'lora1-unet.safetensors'
|
9
|
-
alpha: 0.8
|
10
|
-
layers: 'all'
|
11
|
-
- path: 'lora2-unet.safetensors'
|
12
|
-
alpha: 0.65
|
13
|
-
layers: 'all'
|
14
|
-
part: null
|
15
|
-
|
16
|
-
group2:
|
17
|
-
type: 'TE'
|
18
|
-
base_model_alpha: 1.0 # base model weight to infer with lora or part
|
19
|
-
lora:
|
20
|
-
- path: 'lora1-te.safetensors'
|
21
|
-
alpha: 0.8
|
22
|
-
layers: 'all'
|
23
|
-
- path: 'lora2-te.safetensors'
|
24
|
-
alpha: 0.65
|
25
|
-
layers: 'all'
|
26
|
-
part: null
|
@@ -1,18 +0,0 @@
|
|
1
|
-
_base_: [cfgs/infer/text2img.yaml]
|
2
|
-
|
3
|
-
merge:
|
4
|
-
group1:
|
5
|
-
type: 'unet'
|
6
|
-
base_model_alpha: 0.0 # 基础模型权重0.0,part部分新模型权重1.0,表示用part模型覆盖原有模型
|
7
|
-
part:
|
8
|
-
- path: 'unet-100.safetensors的路径'
|
9
|
-
alpha: 1.0
|
10
|
-
layers: 'all'
|
11
|
-
|
12
|
-
group2: # 如果没有练text_encoder,这部分可以不加
|
13
|
-
type: 'TE'
|
14
|
-
base_model_alpha: 0.0
|
15
|
-
lora:
|
16
|
-
- path: 'text_encoder-100.safetensors的路径'
|
17
|
-
alpha: 1.0
|
18
|
-
layers: 'all'
|
@@ -1,44 +0,0 @@
|
|
1
|
-
_base_: [cfgs/infer/text2img.yaml]
|
2
|
-
|
3
|
-
save_model:
|
4
|
-
path: ckpts/model
|
5
|
-
to_safetensors: true
|
6
|
-
|
7
|
-
merge:
|
8
|
-
|
9
|
-
group_lora_unet:
|
10
|
-
type: 'unet'
|
11
|
-
base_model_alpha: 1.0 # base model weight to merge with lora or part
|
12
|
-
lora:
|
13
|
-
- path: 'exps/lora1/ckpts/unet-600.safetensors'
|
14
|
-
alpha: 0.8
|
15
|
-
layers: 'all'
|
16
|
-
- path: 'exps/lora2/ckpts/unet-800.safetensors'
|
17
|
-
alpha: 0.7
|
18
|
-
layers:
|
19
|
-
- 're:.*\.to_k$'
|
20
|
-
- 're:.*\.to_v$'
|
21
|
-
|
22
|
-
group_lora_TE:
|
23
|
-
type: 'TE'
|
24
|
-
base_model_alpha: 1.0 # base model weight to infer with lora or part
|
25
|
-
lora:
|
26
|
-
- path: 'exps/lora1/ckpts/text_encoder-600.safetensors'
|
27
|
-
alpha: 0.8
|
28
|
-
layers: 'all'
|
29
|
-
|
30
|
-
group_part_unet:
|
31
|
-
type: 'unet'
|
32
|
-
base_model_alpha: 0.4 # base model weight to merge with lora or part
|
33
|
-
part:
|
34
|
-
- path: 'exps/part1/ckpts/unet-500.safetensors'
|
35
|
-
alpha: 0.6
|
36
|
-
layers: 'all'
|
37
|
-
|
38
|
-
group_part_TE:
|
39
|
-
type: 'TE'
|
40
|
-
base_model_alpha: 0.4 # base model weight to merge with lora or part
|
41
|
-
part:
|
42
|
-
- path: 'exps/part1/ckpts/text_encoder-500.safetensors'
|
43
|
-
alpha: 0.6
|
44
|
-
layers: 'all'
|
@@ -1,53 +0,0 @@
|
|
1
|
-
# base_state*base_model_alpha + (lora_state[i]*lora_scale[i]*lora_alpha[i]) + (part_state[k]*part_alpha[k])
|
2
|
-
|
3
|
-
pretrained_model: ''
|
4
|
-
prompt: ''
|
5
|
-
neg_prompt: 'lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry'
|
6
|
-
emb_dir: 'embs/'
|
7
|
-
N_repeats: 1
|
8
|
-
clip_skip: 0
|
9
|
-
clip_final_norm: True
|
10
|
-
encoder_attention_mask: True
|
11
|
-
bs: 4
|
12
|
-
num: 1
|
13
|
-
seed: null
|
14
|
-
dtype: 'fp16'
|
15
|
-
amp: True
|
16
|
-
|
17
|
-
condition: null
|
18
|
-
|
19
|
-
ex_input: {}
|
20
|
-
|
21
|
-
# Syntactic sugar for interface
|
22
|
-
save:
|
23
|
-
out_dir: 'output/'
|
24
|
-
save_cfg: True
|
25
|
-
image_type: png
|
26
|
-
quality: 95
|
27
|
-
# image_type: webp
|
28
|
-
# quality: 75
|
29
|
-
|
30
|
-
offload: null
|
31
|
-
|
32
|
-
#vae_optimize: null
|
33
|
-
vae_optimize:
|
34
|
-
tiling: False
|
35
|
-
slicing: False
|
36
|
-
|
37
|
-
interface:
|
38
|
-
- _target_: hcpdiff.vis.DiskInterface
|
39
|
-
show_steps: 0
|
40
|
-
save_root: ${save.out_dir}
|
41
|
-
save_cfg: ${save.save_cfg}
|
42
|
-
image_type: ${save.image_type}
|
43
|
-
quality: ${save.quality}
|
44
|
-
|
45
|
-
infer_args:
|
46
|
-
width: 512
|
47
|
-
height: 512
|
48
|
-
guidance_scale: 7.5
|
49
|
-
num_inference_steps: 50
|
50
|
-
|
51
|
-
new_components: {}
|
52
|
-
|
53
|
-
merge: null
|
@@ -1,34 +0,0 @@
|
|
1
|
-
_base_: [cfgs/infer/text2img.yaml]
|
2
|
-
|
3
|
-
merge:
|
4
|
-
exp_dir: '2023-04-03-10-10-36'
|
5
|
-
alpha: 0.8
|
6
|
-
alpha_neg: 0.65
|
7
|
-
|
8
|
-
group1:
|
9
|
-
type: 'unet'
|
10
|
-
base_model_alpha: 1.0 # base model weight to merge with lora or part
|
11
|
-
lora:
|
12
|
-
- path: 'exps/${merge.exp_dir}/ckpts/unet-600.safetensors'
|
13
|
-
alpha: ${merge.alpha}
|
14
|
-
layers: 'all'
|
15
|
-
mask: [ 0.5, 1 ] #
|
16
|
-
- path: 'exps/${merge.exp_dir}/ckpts/unet-neg-600.safetensors'
|
17
|
-
alpha: ${merge.alpha_neg}
|
18
|
-
layers: 'all'
|
19
|
-
mask: [ 0, 0.5 ]
|
20
|
-
part: null
|
21
|
-
|
22
|
-
group2:
|
23
|
-
type: 'TE'
|
24
|
-
base_model_alpha: 1.0 # base model weight to infer with lora or part
|
25
|
-
lora:
|
26
|
-
- path: 'exps/${merge.exp_dir}/ckpts/text_encoder-600.safetensors'
|
27
|
-
alpha: ${merge.alpha}
|
28
|
-
layers: 'all'
|
29
|
-
mask: [ 0.5, 1 ]
|
30
|
-
- path: 'exps/${merge.exp_dir}/ckpts/text_encoder-neg-600.safetensors'
|
31
|
-
alpha: ${merge.alpha_neg}
|
32
|
-
layers: 'all'
|
33
|
-
mask: [ 0, 0.5 ]
|
34
|
-
part: null
|
@@ -1,17 +0,0 @@
|
|
1
|
-
_base_: [cfgs/train/tuning_base.yaml]
|
2
|
-
|
3
|
-
plugin_unet:
|
4
|
-
controlnet1:
|
5
|
-
_target_: hcpdiff.models.controlnet.ControlNetPlugin
|
6
|
-
_partial_: True
|
7
|
-
lr: 1e-4
|
8
|
-
from_layers:
|
9
|
-
- 'pre_hook:'
|
10
|
-
- 'pre_hook:conv_in' # to make forward inside autocast
|
11
|
-
to_layers:
|
12
|
-
- 'down_blocks.0'
|
13
|
-
- 'down_blocks.1'
|
14
|
-
- 'down_blocks.2'
|
15
|
-
- 'down_blocks.3'
|
16
|
-
- 'mid_block'
|
17
|
-
- 'pre_hook:up_blocks.3.resnets.2'
|