1717
1818init_ipex ()
1919
20+ import random
21+ import numpy as np
2022from accelerate .utils import set_seed
21- from library import deepspeed_utils , anima_models , anima_train_utils , anima_utils , strategy_base , strategy_anima , sai_model_spec
23+ from library import deepspeed_utils , anima_models , anima_train_utils , anima_utils , strategy_base , strategy_anima , sai_model_spec , model_util
24+ from library .ramtorch_util import apply_ramtorch_to_module
2225
2326import library .train_util as train_util
2427
@@ -42,6 +45,7 @@ def train(args):
4245 train_util .verify_training_args (args )
4346 train_util .prepare_dataset_args (args , True )
4447 deepspeed_utils .prepare_deepspeed_args (args )
48+ train_util .set_torch_cuda_reduced_precision (args )
4549 setup_logging (args , reset = True )
4650
4751 # backward compatibility
@@ -87,7 +91,7 @@ def train(args):
8791 use_dreambooth_method = args .in_json is None
8892
8993 if args .seed is not None :
90- set_seed (args . seed )
94+ train_util . args_set_seed (args )
9195
9296 # prepare caching strategy: must be set before preparing dataset
9397 if args .cache_latents :
@@ -140,6 +144,19 @@ def train(args):
140144 train_dataset_group = train_util .load_arbitrary_dataset (args )
141145 val_dataset_group = None
142146
147+ if args .protected_tags_file :
148+ logger .info (f"Injecting protected_tags_file into datasets: { args .protected_tags_file } " )
149+ for ds in train_dataset_group .datasets :
150+ ds .protected_tags_file = args .protected_tags_file
151+ if args .log_caption_tag_dropout :
152+ logger .info ("Enabling caption tag dropout logging for datasets..." )
153+ for ds in train_dataset_group .datasets :
154+ ds .log_caption_tag_dropout = True
155+ if args .log_caption_dropout :
156+ logger .info ("Enabling caption dropout logging for datasets..." )
157+ for ds in train_dataset_group .datasets :
158+ ds .log_caption_dropout = True
159+
143160 current_epoch = Value ("i" , 0 )
144161 current_step = Value ("i" , 0 )
145162 ds_for_collator = train_dataset_group if args .max_data_loader_n_workers == 0 else None
@@ -283,6 +300,12 @@ def train(args):
283300 logger .info ("Loading Anima VAE..." )
284301 vae , vae_mean , vae_std , vae_scale = anima_utils .load_anima_vae (args .vae_path , dtype = vae_dtype , device = "cpu" )
285302
303+ if args .vae_reflection_padding :
304+ vae = model_util .use_reflection_padding (vae )
305+
306+ if args .use_ramtorch_vae :
307+ vae = apply_ramtorch_to_module (vae , "vae" , accelerator .device , vae_dtype )
308+
286309 if cache_latents :
287310 vae .to (accelerator .device , dtype = weight_dtype )
288311 vae .requires_grad_ (False )
@@ -849,6 +872,33 @@ def setup_parser() -> argparse.ArgumentParser:
849872 anima_train_utils .add_anima_training_arguments (parser )
850873 sai_model_spec .add_model_spec_arguments (parser )
851874
875+ parser .add_argument (
876+ "--protected_tags_file" ,
877+ type = str ,
878+ default = None ,
879+ help = "Path to file containing protected tags that should not be dropped or shuffled" ,
880+ )
881+ parser .add_argument (
882+ "--log_caption_tag_dropout" ,
883+ action = "store_true" ,
884+ help = "Log which tags are dropped during caption tag dropout" ,
885+ )
886+ parser .add_argument (
887+ "--log_caption_dropout" ,
888+ action = "store_true" ,
889+ help = "Log if caption is dropped during caption dropout" ,
890+ )
891+ parser .add_argument (
892+ "--vae_reflection_padding" ,
893+ action = "store_true" ,
894+ help = "Use reflection padding for VAE to reduce artifacts at tile boundaries" ,
895+ )
896+ parser .add_argument (
897+ "--use_ramtorch_vae" ,
898+ action = "store_true" ,
899+ help = "Use RamTorch to offload VAE to CPU/RAM when not in use" ,
900+ )
901+
852902 parser .add_argument (
853903 "--blockwise_fused_optimizers" ,
854904 action = "store_true" ,
0 commit comments