File size: 48,571 Bytes
6755a2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
import argparse
import copy
import os
from pathlib import Path
import logging
from collections import OrderedDict
from pprint import pprint
import random

import numpy as np
from omegaconf import OmegaConf, SCMode
import torch
from einops import rearrange, repeat
import cv2
from PIL import Image
#from diffusers.models.autoencoder_kl import AutoencoderKL
from diffusers import AutoencoderKL

from mmcm.utils.load_util import load_pyhon_obj
from mmcm.utils.seed_util import set_all_seed
from mmcm.utils.signature import get_signature_of_string
from mmcm.utils.task_util import fiss_tasks, generate_tasks as generate_tasks_from_table
from mmcm.vision.utils.data_type_util import is_video, is_image, read_image_as_5d
from mmcm.utils.str_util import clean_str_for_save
from mmcm.vision.data.video_dataset import DecordVideoDataset
from musev.auto_prompt.util import generate_prompts


from musev.models.facein_loader import load_facein_extractor_and_proj_by_name
from musev.models.referencenet_loader import load_referencenet_by_name
from musev.models.ip_adapter_loader import (
    load_ip_adapter_vision_clip_encoder_by_name,
    load_vision_clip_encoder_by_name,
    load_ip_adapter_image_proj_by_name,
)
from musev.models.ip_adapter_face_loader import (
    load_ip_adapter_face_extractor_and_proj_by_name,
)
from musev.pipelines.pipeline_controlnet_predictor import (
    DiffusersPipelinePredictor,
)
from musev.models.referencenet import ReferenceNet2D
from musev.models.unet_loader import load_unet_by_name
from musev.utils.util import save_videos_grid_with_opencv
from musev import logger

logger.setLevel("INFO")

file_dir = os.path.dirname(__file__)
PROJECT_DIR = os.path.join(os.path.dirname(__file__))
DATA_DIR = os.path.join(PROJECT_DIR, "data")


# TODO:use group to group arguments
def parse_args():
    parser = argparse.ArgumentParser(description="musev Text to video")
    parser.add_argument(
        "-test_data_path",
        type=str,
        help=(
            "Path to the test data configuration file, now only support yaml ext, "
            "task file simialr to musev/configs/tasks/example.yaml"
        ),
    )
    parser.add_argument(
        "--target_datas",
        type=str,
        default="all",
        help="Names of the test data to run, to select sub tasks, default=`all`",
    )
    parser.add_argument(
        "--sd_model_cfg_path",
        type=str,
        default=os.path.join(PROJECT_DIR, "configs/model/T2I_all_model.py"),
        help="Path to the model configuration file",
    )
    parser.add_argument(
        "--sd_model_name",
        type=str,
        default="all",
        help="Names of the models to run, or path.",
    )
    parser.add_argument(
        "--unet_model_cfg_path",
        type=str,
        default=os.path.join(PROJECT_DIR, "./configs/model/motion_model.py"),
        help="Path to motion_cfg path or motion unet path",
    )
    parser.add_argument(
        "--unet_model_name",
        type=str,
        default="musev_referencenet",
        help=(
            "class Name of the unet model, use load_unet_by_name to init unet,"
            "now only support `musev`, `musev_referencenet`,"
        ),
    )
    parser.add_argument(
        "--lcm_model_cfg_path",
        type=str,
        default=os.path.join(PROJECT_DIR, "./configs/model/lcm_model.py"),
        help="Path to lcm lora path",
    )
    parser.add_argument(
        "--lcm_model_name",
        type=str,
        default=None,
        help="lcm model name, None means do not use lcm_lora default=`None`",
        choices=[
            "lcm",
        ],
    )
    parser.add_argument(
        "--referencenet_model_cfg_path",
        type=str,
        default=os.path.join(PROJECT_DIR, "./configs/model/referencenet.py"),
        help="Path to referencenet model config path",
    )
    parser.add_argument(
        "--referencenet_model_name",
        type=str,
        default=None,
        help="referencenet model name, None means do not use referencenet, default=`None`",
        choices=["musev_referencenet"],
    )
    parser.add_argument(
        "--ip_adapter_model_cfg_path",
        type=str,
        default=os.path.join(PROJECT_DIR, "./configs/model/ip_adapter.py"),
        help="Path to ip_adapter model config path",
    )
    parser.add_argument(
        "--ip_adapter_model_name",
        type=str,
        default=None,
        help="ip_adapter model name, None means do not use ip_adapter, default=`None`",
        choices=["musev_referencenet"],
    )
    parser.add_argument(
        "--vision_clip_model_path",
        type=str,
        default="./checkpoints/ip_adapter/models/image_encoder",
        help="vision_clip_extractor_class_name vision_clip_model_path, default=`./checkpoints/ip_adapter/models/image_encoder`",
    )
    parser.add_argument(
        "--vision_clip_extractor_class_name",
        type=str,
        default=None,
        help="vision_clip_extractor_class_name None means according to ip_adapter_model_name, default=`None`",
        choices=["ImageClipVisionFeatureExtractor"],
    )
    parser.add_argument(
        "--facein_model_cfg_path",
        type=str,
        default=os.path.join(PROJECT_DIR, "./configs/model/facein.py"),
        help="Path to facein model config path",
    )
    parser.add_argument(
        "--facein_model_name",
        type=str,
        default=None,
        help="facein model name,  None means do not use facein, now unsupported default=`None`",
    )
    parser.add_argument(
        "--ip_adapter_face_model_cfg_path",
        type=str,
        default=os.path.join(PROJECT_DIR, "./configs/model/ip_adapter.py"),
        help="Path to facein model config path",
    )
    parser.add_argument(
        "--ip_adapter_face_model_name",
        type=str,
        default=None,
        help="facein model name, None means do not use ip_adapter_face, default=`None`",
    )
    parser.add_argument(
        "--output_dir",
        type=str,
        default=os.path.join(PROJECT_DIR, "results"),
        help="Output directory, default=`musev/results`",
    )
    parser.add_argument(
        "--save_filetype",
        type=str,
        default="mp4",
        help="Type of file to save the video, default=`mp4`",
        choices=["gif", "mp4", "webp", "images"],
    )
    parser.add_argument(
        "--save_images",
        action="store_true",
        default=False,
        help="more than video, whether save generated video into images, default=`False`",
    )
    parser.add_argument(
        "--overwrite",
        action="store_true",
        help="Whether to overwrite existing files, default=`False`",
    )
    parser.add_argument(
        "--seed",
        type=int,
        default=None,
        help="Random seed, default=`None`",
    )
    parser.add_argument(
        "--cross_attention_dim",
        type=int,
        default=768,
        help="Cross attention dimension, default=`768`",
    )
    parser.add_argument(
        "--n_batch",
        type=int,
        default=1,
        help="Maximum number of iterations to run, total_frames=n_batch*time_size, default=`1`",
    )
    parser.add_argument(
        "--fps",
        type=int,
        default=4,
        help="Frames per second for save video,default is same to of training, default=`4`",
    )
    parser.add_argument(
        "--use_condition_image",
        action="store_false",
        help=(
            "Whether to use the first frame of the test dataset as the initial image, default=`True`"
            "now only support image"
        ),
    )
    parser.add_argument(
        "--fix_condition_images",
        action="store_true",
        help=("Whether to fix condition_image for every shot, default=`False`"),
    )
    parser.add_argument(
        "--redraw_condition_image",
        action="store_true",
        help="Whether to use the redrawn first frame as the initial image, default=`False`",
    )
    parser.add_argument(
        "--need_img_based_video_noise",
        action="store_false",
        help="Whether to use noise based on the initial frame when adding noise to the video, default=`True`",
    )
    parser.add_argument(
        "--img_weight",
        type=float,
        default=1e-3,
        help="Weight of the vision_condtion frame to video noise, default=`1e-3`",
    )
    parser.add_argument(
        "--write_info",
        action="store_true",
        help="Whether to write frame index, default=`False`",
    )
    parser.add_argument(
        "--height",
        type=int,
        default=None,
        help="Height of the generated video, if none then use height of condition_image, if all none raise error, default=`None`",
    )
    parser.add_argument(
        "--width",
        type=int,
        default=None,
        help="Width of the generated video, if none then use height of condition_image, if all none raise error, default=`None`",
    )
    parser.add_argument(
        "--img_length_ratio",
        type=float,
        default=1.0,
        help="ratio to resize target width, target height of generated video, default=`1.0`",
    )

    parser.add_argument(
        "--n_cols",
        type=int,
        default=3,
        help="Number of columns in the output video grid, unused, now",
    )
    parser.add_argument(
        "--time_size",
        type=int,
        default=12,
        help="Number of frames to generate per iteration, same as of training, default=`12`",
    )
    parser.add_argument(
        "--noise_type",
        type=str,
        default="video_fusion",
        help="Type of noise to add to the video, default=`video_fusion`",
        choices=["video_fusion", "random"],
    )
    parser.add_argument(
        "--guidance_scale",
        type=float,
        default=7.5,
        help="guidance_scale of first frame, default=`7.5`",
    )
    parser.add_argument(
        "--video_guidance_scale",
        type=float,
        default=3.5,
        help="video_guidance_scale of video, the greater the value, the greater the video change, the more likely video error, default=`3.5`",
    )
    parser.add_argument(
        "--video_guidance_scale_end",
        type=float,
        default=None,
        help="changed video_guidance_scale_end with timesteps, None means unchanged, default=`None`",
    ),
    parser.add_argument(
        "--video_guidance_scale_method",
        type=str,
        default="linear",
        help="generate  changed video_guidance_scale with timesteps, default=`linear`",
        choices=["linear", "two_stage", "three_stage", "fix_two_stage"],
    ),
    parser.add_argument(
        "--num_inference_steps",
        type=int,
        default=30,
        help="inference steps of first frame redraw, default=`30",
    )
    parser.add_argument(
        "--video_num_inference_steps",
        type=int,
        default=10,
        help="inference steps of video, default=`10`",
    )
    parser.add_argument(
        "--strength",
        type=float,
        default=0.8,
        help="Strength of the redrawn first frame, default=`0.8`",
    )
    parser.add_argument(
        "--negprompt_cfg_path",
        type=str,
        default=os.path.join(PROJECT_DIR, "configs/model/negative_prompt.py"),
        help="Path to the negtive prompt configuration file",
    )
    parser.add_argument(
        "--video_negative_prompt",
        type=str,
        default="V2",
        help="video negative prompt",
    ),
    parser.add_argument(
        "--negative_prompt",
        type=str,
        default="V2",
        help="first frame negative prompt",
    ),
    parser.add_argument(
        "--motion_speed",
        type=float,
        default=8.0,
        help="motion speed, sample rate in training stage, default=`8.0`",
    ),
    parser.add_argument(
        "--need_hist_match",
        default=False,
        action="store_true",
        help="wthether hist match video with vis cond, default=`False`",
    ),
    parser.add_argument(
        "--log_level",
        type=str,
        default="INFO",
    )
    parser.add_argument(
        "--add_static_video_prompt",
        action="store_true",
        default=False,
        help="add static_video_prompt in head of prompt",
    )
    parser.add_argument(
        "--n_vision_condition",
        type=int,
        default=1,
        help="num of vision_condition , default=`1`",
    )
    parser.add_argument(
        "--fixed_refer_image",
        action="store_false",
        default=True,
        help="whether fix referencenet image or not, if none and referencenet is not None, use vision condition frame, default=`True`",
    )
    parser.add_argument(
        "--fixed_ip_adapter_image",
        action="store_false",
        default=True,
        help="whether fixed_ip_adapter_image or not , if none and ipadapter is not None, use vision condition frame, default=`True`",
    )
    parser.add_argument(
        "--fixed_refer_face_image",
        action="store_false",
        default=True,
        help="whether fix facein image or not, if not and ipadapterfaceid is not None, use vision condition frame, default=`True`",
    )
    parser.add_argument(
        "--redraw_condition_image_with_referencenet",
        action="store_false",
        default=True,
        help="whether use ip_adapter when redrawing vision condition image default=`True`",
    )
    parser.add_argument(
        "--redraw_condition_image_with_ipdapter",
        action="store_false",
        default=True,
        help="whether use ip_adapter when redrawing vision condition image default=`True`",
    )
    parser.add_argument(
        "--redraw_condition_image_with_facein",
        action="store_false",
        default=True,
        help="whether use face tool when redrawing vision condition image, default=`True`",
    )
    parser.add_argument(
        "--w_ind_noise",
        default=0.5,
        type=float,
        help="independent ration of videofusion noise, the greater the value, the greater the video change, the more likely video error,  default=`0.5`",
    )
    parser.add_argument(
        "--ip_adapter_scale",
        default=1.0,
        type=float,
        help="ipadapter weight, default=`1.0`",
    )
    parser.add_argument(
        "--facein_scale",
        default=1.0,
        type=float,
        help="facein weight, default=`1.0`",
    )
    parser.add_argument(
        "--face_image_path",
        default=None,
        type=str,
        help="face_image_str, default=`None`",
    )
    parser.add_argument(
        "--ipadapter_image_path",
        default=None,
        type=str,
        help="face_image_str, default=`None`",
    )
    parser.add_argument(
        "--referencenet_image_path",
        default=None,
        type=str,
        help="referencenet_image_path, default=`None`",
    )
    parser.add_argument(
        "--vae_model_path",
        default="./checkpoints/vae/sd-vae-ft-mse",
        type=str,
        help="vae path, default=`./checkpoints/vae/sd-vae-ft-mse`",
    )
    parser.add_argument(
        "--redraw_condition_image_with_ip_adapter_face",
        action="store_false",
        default=True,
        help="whether use facein when redrawing vision condition image, default=`True`",
    )
    parser.add_argument(
        "--ip_adapter_face_scale",
        default=1.0,
        type=float,
        help="ip_adapter face default=`1.0`",
    )
    parser.add_argument(
        "--prompt_only_use_image_prompt",
        action="store_true",
        default=False,
        help="prompt_only_use_image_prompt, if true, replace text_prompt_emb with image_prompt_emb in ip_adapter_cross_attn, default=`False`",
    )
    parser.add_argument(
        "--record_mid_video_noises",
        action="store_true",
        default=False,
        help="whether record middle timestep noise of the last frames of last shot, default=`False`",
    )
    parser.add_argument(
        "--record_mid_video_latents",
        action="store_true",
        default=False,
        help="whether record middle timestep latent of the last frames of last shot, default=`False`",
    )
    parser.add_argument(
        "--video_overlap",
        default=1,
        type=int,
        help="overlap when generate long video with end2end method, default=`1`",
    )
    parser.add_argument(
        "--context_schedule",
        default="uniform_v2",
        type=str,
        help="how to generate multi shot index when parallel denoise, default=`uniform_v2`",
        choices=["uniform", "uniform_v2"],
    )
    parser.add_argument(
        "--context_frames",
        default=12,
        type=int,
        help="window size of a subshot in parallel denoise, default=`12`",
    )
    parser.add_argument(
        "--context_stride",
        default=1,
        type=int,
        help="window stride of a subshot in parallel denoise, unvalid paramter, to delete, default=`1`",
    )
    parser.add_argument(
        "--context_overlap",
        default=4,
        type=int,
        help="window overlap of a subshot in parallel denoise,default=`4`",
    )
    parser.add_argument(
        "--context_batch_size",
        default=1,
        type=int,
        help="num of subshot in parallel denoise, change in batch_size, need more gpu memory, default=`1`",
    )
    parser.add_argument(
        "--interpolation_factor",
        default=1,
        type=int,
        help="whether do super resolution to latents, `1` means do nothing, default=`1`",
    )
    parser.add_argument(
        "--n_repeat",
        default=1,
        type=int,
        help="repeat times for every task, default=`1`",
    )
    args = parser.parse_args()
    return args


args = parse_args()
print("args")
pprint(args.__dict__)
print("\n")

logger.setLevel(args.log_level)
overwrite = args.overwrite
cross_attention_dim = args.cross_attention_dim
time_size = args.time_size  # 一次视频生成的帧数
n_batch = args.n_batch  # 按照time_size的尺寸 生成n_batch次,总帧数 = time_size * n_batch
fps = args.fps
fix_condition_images = args.fix_condition_images
use_condition_image = args.use_condition_image  # 当 test_data 中有图像时,作为初始图像
redraw_condition_image = args.redraw_condition_image  # 用于视频生成的首帧是否使用重绘后的
need_img_based_video_noise = (
    args.need_img_based_video_noise
)  # 视频加噪过程中是否使用首帧 condition_images
img_weight = args.img_weight
height = args.height  # 如果测试数据中没有单独指定宽高,则默认这里
width = args.width  # 如果测试数据中没有单独指定宽高,则默认这里
img_length_ratio = args.img_length_ratio  # 如果测试数据中没有单独指定图像宽高比resize比例,则默认这里
n_cols = args.n_cols
noise_type = args.noise_type
strength = args.strength  # 首帧重绘程度参数
video_guidance_scale = args.video_guidance_scale  # 视频 condition与 uncond的权重参数
guidance_scale = args.guidance_scale  # 时序条件帧 condition与uncond的权重参数
video_num_inference_steps = args.video_num_inference_steps  # 视频迭代次数
num_inference_steps = args.num_inference_steps  # 时序条件帧 重绘参数
seed = args.seed
save_filetype = args.save_filetype
save_images = args.save_images
sd_model_cfg_path = args.sd_model_cfg_path
sd_model_name = (
    args.sd_model_name
    if args.sd_model_name in ["all", "None"]
    else args.sd_model_name.split(",")
)
unet_model_cfg_path = args.unet_model_cfg_path
unet_model_name = args.unet_model_name
test_data_path = args.test_data_path
target_datas = (
    args.target_datas if args.target_datas == "all" else args.target_datas.split(",")
)
device = "cuda" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16
negprompt_cfg_path = args.negprompt_cfg_path
video_negative_prompt = args.video_negative_prompt
negative_prompt = args.negative_prompt
motion_speed = args.motion_speed
need_hist_match = args.need_hist_match
video_guidance_scale_end = args.video_guidance_scale_end
video_guidance_scale_method = args.video_guidance_scale_method
add_static_video_prompt = args.add_static_video_prompt
n_vision_condition = args.n_vision_condition
lcm_model_cfg_path = args.lcm_model_cfg_path
lcm_model_name = args.lcm_model_name
referencenet_model_cfg_path = args.referencenet_model_cfg_path
referencenet_model_name = args.referencenet_model_name
ip_adapter_model_cfg_path = args.ip_adapter_model_cfg_path
ip_adapter_model_name = args.ip_adapter_model_name
vision_clip_model_path = args.vision_clip_model_path
vision_clip_extractor_class_name = args.vision_clip_extractor_class_name
facein_model_cfg_path = args.facein_model_cfg_path
facein_model_name = args.facein_model_name
ip_adapter_face_model_cfg_path = args.ip_adapter_face_model_cfg_path
ip_adapter_face_model_name = args.ip_adapter_face_model_name

fixed_refer_image = args.fixed_refer_image
fixed_ip_adapter_image = args.fixed_ip_adapter_image
fixed_refer_face_image = args.fixed_refer_face_image
redraw_condition_image_with_referencenet = args.redraw_condition_image_with_referencenet
redraw_condition_image_with_ipdapter = args.redraw_condition_image_with_ipdapter
redraw_condition_image_with_facein = args.redraw_condition_image_with_facein
redraw_condition_image_with_ip_adapter_face = (
    args.redraw_condition_image_with_ip_adapter_face
)
w_ind_noise = args.w_ind_noise
ip_adapter_scale = args.ip_adapter_scale
facein_scale = args.facein_scale
ip_adapter_face_scale = args.ip_adapter_face_scale
face_image_path = args.face_image_path
ipadapter_image_path = args.ipadapter_image_path
referencenet_image_path = args.referencenet_image_path
vae_model_path = args.vae_model_path
prompt_only_use_image_prompt = args.prompt_only_use_image_prompt
# serial_denoise parameter start
record_mid_video_noises = args.record_mid_video_noises
record_mid_video_latents = args.record_mid_video_latents
video_overlap = args.video_overlap
# serial_denoise parameter end
# parallel_denoise parameter start
context_schedule = args.context_schedule
context_frames = args.context_frames
context_stride = args.context_stride
context_overlap = args.context_overlap
context_batch_size = args.context_batch_size
interpolation_factor = args.interpolation_factor
n_repeat = args.n_repeat

# parallel_denoise parameter end

b = 1
negative_embedding = [
    ["./checkpoints/embedding/badhandv4.pt", "badhandv4"],
    [
        "./checkpoints/embedding/ng_deepnegative_v1_75t.pt",
        "ng_deepnegative_v1_75t",
    ],
    [
        "./checkpoints/embedding/EasyNegativeV2.safetensors",
        "EasyNegativeV2",
    ],
    [
        "./checkpoints/embedding/bad_prompt_version2-neg.pt",
        "bad_prompt_version2-neg",
    ],
]
prefix_prompt = ""
suffix_prompt = ", beautiful, masterpiece, best quality"
suffix_prompt = ""


# sd model parameters
if sd_model_name != "None":
    # use sd_model_path in sd_model_cfg_path
    sd_model_params_dict_src = load_pyhon_obj(sd_model_cfg_path, "MODEL_CFG")
    sd_model_params_dict = {
        k: v
        for k, v in sd_model_params_dict_src.items()
        if sd_model_name == "all" or k in sd_model_name
    }
else:
    # get sd_model_path in sd_model_cfg_path by sd_model_name
    # if set path of sd_model_path in cmd, should set sd_model_name as None,
    sd_model_name = os.path.basename(sd_model_cfg_path).split(".")[0]
    sd_model_params_dict = {sd_model_name: {"sd": sd_model_cfg_path}}
    sd_model_params_dict_src = sd_model_params_dict
if len(sd_model_params_dict) == 0:
    raise ValueError(
        "has not target model, please set one of {}".format(
            " ".join(list(sd_model_params_dict_src.keys()))
        )
    )
print("running model, T2I SD")
pprint(sd_model_params_dict)

# lcm parameters
if lcm_model_name is not None:
    lcm_model_params_dict_src = load_pyhon_obj(lcm_model_cfg_path, "MODEL_CFG")
    print("lcm_model_params_dict_src")
    lcm_lora_dct = lcm_model_params_dict_src[lcm_model_name]
else:
    lcm_lora_dct = None
print("lcm: ", lcm_model_name, lcm_lora_dct)


# motion net parameters
if os.path.isdir(unet_model_cfg_path):
    unet_model_path = unet_model_cfg_path
elif os.path.isfile(unet_model_cfg_path):
    unet_model_params_dict_src = load_pyhon_obj(unet_model_cfg_path, "MODEL_CFG")
    print("unet_model_params_dict_src", unet_model_params_dict_src.keys())
    unet_model_path = unet_model_params_dict_src[unet_model_name]["unet"]
else:
    raise ValueError(f"expect dir or file, but given {unet_model_cfg_path}")
print("unet: ", unet_model_name, unet_model_path)


# referencenet parameters
if referencenet_model_name is not None:
    if os.path.isdir(referencenet_model_cfg_path):
        referencenet_model_path = referencenet_model_cfg_path
    elif os.path.isfile(referencenet_model_cfg_path):
        referencenet_model_params_dict_src = load_pyhon_obj(
            referencenet_model_cfg_path, "MODEL_CFG"
        )
        print(
            "referencenet_model_params_dict_src",
            referencenet_model_params_dict_src.keys(),
        )
        referencenet_model_path = referencenet_model_params_dict_src[
            referencenet_model_name
        ]["net"]
    else:
        raise ValueError(f"expect dir or file, but given {referencenet_model_cfg_path}")
else:
    referencenet_model_path = None
print("referencenet: ", referencenet_model_name, referencenet_model_path)


# ip_adapter parameters
if ip_adapter_model_name is not None:
    ip_adapter_model_params_dict_src = load_pyhon_obj(
        ip_adapter_model_cfg_path, "MODEL_CFG"
    )
    print("ip_adapter_model_params_dict_src", ip_adapter_model_params_dict_src.keys())
    ip_adapter_model_params_dict = ip_adapter_model_params_dict_src[
        ip_adapter_model_name
    ]
else:
    ip_adapter_model_params_dict = None
print("ip_adapter: ", ip_adapter_model_name, ip_adapter_model_params_dict)


# facein parameters
if facein_model_name is not None:
    raise NotImplementedError("unsupported facein by now")
    facein_model_params_dict_src = load_pyhon_obj(facein_model_cfg_path, "MODEL_CFG")
    print("facein_model_params_dict_src", facein_model_params_dict_src.keys())
    facein_model_params_dict = facein_model_params_dict_src[facein_model_name]
else:
    facein_model_params_dict = None
print("facein: ", facein_model_name, facein_model_params_dict)

# ip_adapter_face
if ip_adapter_face_model_name is not None:
    ip_adapter_face_model_params_dict_src = load_pyhon_obj(
        ip_adapter_face_model_cfg_path, "MODEL_CFG"
    )
    print(
        "ip_adapter_face_model_params_dict_src",
        ip_adapter_face_model_params_dict_src.keys(),
    )
    ip_adapter_face_model_params_dict = ip_adapter_face_model_params_dict_src[
        ip_adapter_face_model_name
    ]
else:
    ip_adapter_face_model_params_dict = None
print(
    "ip_adapter_face: ", ip_adapter_face_model_name, ip_adapter_face_model_params_dict
)


# negative_prompt
def get_negative_prompt(negative_prompt, cfg_path=None, n: int = 10):
    name = negative_prompt[:n]
    if cfg_path is not None and cfg_path not in ["None", "none"]:
        dct = load_pyhon_obj(cfg_path, "Negative_Prompt_CFG")
        negative_prompt = dct[negative_prompt]["prompt"]

    return name, negative_prompt


negtive_prompt_length = 10
video_negative_prompt_name, video_negative_prompt = get_negative_prompt(
    video_negative_prompt,
    cfg_path=negprompt_cfg_path,
    n=negtive_prompt_length,
)
negative_prompt_name, negative_prompt = get_negative_prompt(
    negative_prompt,
    cfg_path=negprompt_cfg_path,
    n=negtive_prompt_length,
)
print("video_negprompt", video_negative_prompt_name, video_negative_prompt)
print("negprompt", negative_prompt_name, negative_prompt)

output_dir = args.output_dir
os.makedirs(output_dir, exist_ok=True)


# test_data_parameters
def load_yaml(path):
    tasks = OmegaConf.to_container(
        OmegaConf.load(path), structured_config_mode=SCMode.INSTANTIATE, resolve=True
    )
    return tasks


if test_data_path.endswith(".yaml"):
    test_datas_src = load_yaml(test_data_path)
elif test_data_path.endswith(".csv"):
    test_datas_src = generate_tasks_from_table(test_data_path)
else:
    raise ValueError("expect yaml or csv, but given {}".format(test_data_path))

test_datas = [
    test_data
    for test_data in test_datas_src
    if target_datas == "all" or test_data.get("name", None) in target_datas
]

test_datas = fiss_tasks(test_datas)
test_datas = generate_prompts(test_datas)

n_test_datas = len(test_datas)
if n_test_datas == 0:
    raise ValueError(
        "n_test_datas == 0, set target_datas=None or set atleast one of {}".format(
            " ".join(list(d.get("name", "None") for d in test_datas_src))
        )
    )
print("n_test_datas", n_test_datas)
# pprint(test_datas)


def read_image(path):
    name = os.path.basename(path).split(".")[0]
    image = read_image_as_5d(path)
    return image, name


def read_image_lst(path):
    images_names = [read_image(x) for x in path]
    images, names = zip(*images_names)
    images = np.concatenate(images, axis=2)
    name = "_".join(names)
    return images, name


def read_image_and_name(path):
    if isinstance(path, str):
        path = [path]
    images, name = read_image_lst(path)
    return images, name


# load referencenet
if referencenet_model_name is not None:
    referencenet = load_referencenet_by_name(
        model_name=referencenet_model_name,
        # sd_model=sd_model_path,
        # sd_model="./checkpoints/Moore-AnimateAnyone/AnimateAnyone/reference_unet.pth",
        sd_referencenet_model=referencenet_model_path,
        cross_attention_dim=cross_attention_dim,
    )
else:
    referencenet = None
    referencenet_model_name = "no"

# load vision_clip_extractor
if vision_clip_extractor_class_name is not None:
    vision_clip_extractor = load_vision_clip_encoder_by_name(
        ip_image_encoder=vision_clip_model_path,
        vision_clip_extractor_class_name=vision_clip_extractor_class_name,
    )
    logger.info(
        f"vision_clip_extractor, name={vision_clip_extractor_class_name}, path={vision_clip_model_path}"
    )
else:
    vision_clip_extractor = None
    logger.info(f"vision_clip_extractor, None")

# load ip_adapter_model
if ip_adapter_model_name is not None:
    ip_adapter_image_proj = load_ip_adapter_image_proj_by_name(
        model_name=ip_adapter_model_name,
        ip_image_encoder=ip_adapter_model_params_dict.get(
            "ip_image_encoder", vision_clip_model_path
        ),
        ip_ckpt=ip_adapter_model_params_dict["ip_ckpt"],
        cross_attention_dim=cross_attention_dim,
        clip_embeddings_dim=ip_adapter_model_params_dict["clip_embeddings_dim"],
        clip_extra_context_tokens=ip_adapter_model_params_dict[
            "clip_extra_context_tokens"
        ],
        ip_scale=ip_adapter_model_params_dict["ip_scale"],
        device=device,
    )
else:
    ip_adapter_image_proj = None
    ip_adapter_model_name = "no"

for model_name, sd_model_params in sd_model_params_dict.items():
    lora_dict = sd_model_params.get("lora", None)
    model_sex = sd_model_params.get("sex", None)
    model_style = sd_model_params.get("style", None)
    sd_model_path = sd_model_params["sd"]
    test_model_vae_model_path = sd_model_params.get("vae", vae_model_path)
    # load unet according test_data
    unet = load_unet_by_name(
        model_name=unet_model_name,
        sd_unet_model=unet_model_path,
        sd_model=sd_model_path,
        # sd_model="./checkpoints/Moore-AnimateAnyone/AnimateAnyone/denoising_unet.pth",
        cross_attention_dim=cross_attention_dim,
        need_t2i_facein=facein_model_name is not None,
        # ip_adapter_face_model_name not train in unet, need load individually
        strict=not (ip_adapter_face_model_name is not None),
        need_t2i_ip_adapter_face=ip_adapter_face_model_name is not None,
    )

    # load facein according test_data
    if facein_model_name is not None:
        (
            face_emb_extractor,
            facein_image_proj,
        ) = load_facein_extractor_and_proj_by_name(
            model_name=facein_model_name,
            ip_image_encoder=facein_model_params_dict["ip_image_encoder"],
            ip_ckpt=facein_model_params_dict["ip_ckpt"],
            cross_attention_dim=cross_attention_dim,
            clip_embeddings_dim=facein_model_params_dict["clip_embeddings_dim"],
            clip_extra_context_tokens=facein_model_params_dict[
                "clip_extra_context_tokens"
            ],
            ip_scale=facein_model_params_dict["ip_scale"],
            device=device,
            unet=unet,
        )
    else:
        face_emb_extractor = None
        facein_image_proj = None

    # load ipadapter_face model according test_data
    if ip_adapter_face_model_name is not None:
        (
            ip_adapter_face_emb_extractor,
            ip_adapter_face_image_proj,
        ) = load_ip_adapter_face_extractor_and_proj_by_name(
            model_name=ip_adapter_face_model_name,
            ip_image_encoder=ip_adapter_face_model_params_dict["ip_image_encoder"],
            ip_ckpt=ip_adapter_face_model_params_dict["ip_ckpt"],
            cross_attention_dim=cross_attention_dim,
            clip_embeddings_dim=ip_adapter_face_model_params_dict[
                "clip_embeddings_dim"
            ],
            clip_extra_context_tokens=ip_adapter_face_model_params_dict[
                "clip_extra_context_tokens"
            ],
            ip_scale=ip_adapter_face_model_params_dict["ip_scale"],
            device=device,
            unet=unet,
        )
    else:
        ip_adapter_face_emb_extractor = None
        ip_adapter_face_image_proj = None

    print("test_model_vae_model_path", test_model_vae_model_path)

    # init sd_predictor
    sd_predictor = DiffusersPipelinePredictor(
        sd_model_path=sd_model_path,
        unet=unet,
        lora_dict=lora_dict,
        lcm_lora_dct=lcm_lora_dct,
        device=device,
        dtype=torch_dtype,
        negative_embedding=negative_embedding,
        referencenet=referencenet,
        ip_adapter_image_proj=ip_adapter_image_proj,
        vision_clip_extractor=vision_clip_extractor,
        facein_image_proj=facein_image_proj,
        face_emb_extractor=face_emb_extractor,
        vae_model=test_model_vae_model_path,
        ip_adapter_face_emb_extractor=ip_adapter_face_emb_extractor,
        ip_adapter_face_image_proj=ip_adapter_face_image_proj,
    )
    logger.debug(f"load referencenet"),

    for i_test_data, test_data in enumerate(test_datas):
        batch = []
        texts = []
        print("\n i_test_data", i_test_data, model_name)
        test_data_name = test_data.get("name", i_test_data)
        prompt = test_data["prompt"]
        prompt = prefix_prompt + prompt + suffix_prompt
        prompt_hash = get_signature_of_string(prompt, length=5)
        test_data["prompt_hash"] = prompt_hash
        test_data_height = test_data.get("height", height)
        test_data_width = test_data.get("width", width)
        test_data_condition_images_path = test_data.get("condition_images", None)
        test_data_condition_images_index = test_data.get("condition_images_index", None)
        test_data_redraw_condition_image = test_data.get(
            "redraw_condition_image", redraw_condition_image
        )
        # read condition_image
        if (
            test_data_condition_images_path is not None
            and use_condition_image
            and (
                isinstance(test_data_condition_images_path, list)
                or (
                    isinstance(test_data_condition_images_path, str)
                    and is_image(test_data_condition_images_path)
                )
            )
        ):
            (
                test_data_condition_images,
                test_data_condition_images_name,
            ) = read_image_and_name(test_data_condition_images_path)
            condition_image_height = test_data_condition_images.shape[3]
            condition_image_width = test_data_condition_images.shape[4]
            logger.debug(
                f"test_data_condition_images use {test_data_condition_images_path}"
            )
        else:
            test_data_condition_images = None
            test_data_condition_images_name = "no"
            condition_image_height = None
            condition_image_width = None
            logger.debug(f"test_data_condition_images is None")

        # if test_data_height is not assigned, use height of condition, if still None, use of video
        if test_data_height is None:
            test_data_height = condition_image_height

        if test_data_width is None:
            test_data_width = condition_image_width

        test_data_img_length_ratio = float(
            test_data.get("img_length_ratio", img_length_ratio)
        )

        # to align height of generated video with video2video, use `64`` as basic pixel unit instead of `8``
        # test_data_height = int(test_data_height * test_data_img_length_ratio // 8 * 8)
        # test_data_width = int(test_data_width * test_data_img_length_ratio // 8 * 8)
        test_data_height = int(test_data_height * test_data_img_length_ratio // 64 * 64)
        test_data_width = int(test_data_width * test_data_img_length_ratio // 64 * 64)
        pprint(test_data)
        print(f"test_data_height={test_data_height}")
        print(f"test_data_width={test_data_width}")
        # continue
        test_data_style = test_data.get("style", None)
        test_data_sex = test_data.get("sex", None)
        # if paramters in test_data is str, but float in fact, convert it into float,int.
        test_data_motion_speed = float(test_data.get("motion_speed", motion_speed))
        test_data_w_ind_noise = float(test_data.get("w_ind_noise", w_ind_noise))
        test_data_img_weight = float(test_data.get("img_weight", img_weight))
        logger.debug(
            f"test_data_condition_images_path {test_data_condition_images_path}"
        )
        logger.debug(
            f"test_data_condition_images_index {test_data_condition_images_index}"
        )
        test_data_refer_image_path = test_data.get(
            "refer_image", referencenet_image_path
        )
        test_data_ipadapter_image_path = test_data.get(
            "ipadapter_image", ipadapter_image_path
        )
        test_data_refer_face_image_path = test_data.get("face_image", face_image_path)

        if negprompt_cfg_path is not None:
            if "video_negative_prompt" in test_data:
                (
                    test_data_video_negative_prompt_name,
                    test_data_video_negative_prompt,
                ) = get_negative_prompt(
                    test_data.get(
                        "video_negative_prompt",
                    ),
                    cfg_path=negprompt_cfg_path,
                    n=negtive_prompt_length,
                )
            else:
                test_data_video_negative_prompt_name = video_negative_prompt_name
                test_data_video_negative_prompt = video_negative_prompt
            if "negative_prompt" in test_data:
                (
                    test_data_negative_prompt_name,
                    test_data_negative_prompt,
                ) = get_negative_prompt(
                    test_data.get(
                        "negative_prompt",
                    ),
                    cfg_path=negprompt_cfg_path,
                    n=negtive_prompt_length,
                )
            else:
                test_data_negative_prompt_name = negative_prompt_name
                test_data_negative_prompt = negative_prompt
        else:
            test_data_video_negative_prompt = test_data.get(
                "video_negative_prompt", video_negative_prompt
            )
            test_data_video_negative_prompt_name = test_data_video_negative_prompt[
                :negtive_prompt_length
            ]
            test_data_negative_prompt = test_data.get(
                "negative_prompt", negative_prompt
            )
            test_data_negative_prompt_name = test_data_negative_prompt[
                :negtive_prompt_length
            ]

        # prepare test_data_refer_image
        if referencenet is not None:
            if test_data_refer_image_path is None:
                test_data_refer_image = test_data_condition_images
                test_data_refer_image_name = test_data_condition_images_name
                logger.debug(f"test_data_refer_image use test_data_condition_images")
            else:
                test_data_refer_image, test_data_refer_image_name = read_image_and_name(
                    test_data_refer_image_path
                )
                logger.debug(f"test_data_refer_image use {test_data_refer_image_path}")
        else:
            test_data_refer_image = None
            test_data_refer_image_name = "no"
            logger.debug(f"test_data_refer_image is None")

        # prepare test_data_ipadapter_image
        if vision_clip_extractor is not None:
            if test_data_ipadapter_image_path is None:
                test_data_ipadapter_image = test_data_condition_images
                test_data_ipadapter_image_name = test_data_condition_images_name

                logger.debug(
                    f"test_data_ipadapter_image use test_data_condition_images"
                )
            else:
                (
                    test_data_ipadapter_image,
                    test_data_ipadapter_image_name,
                ) = read_image_and_name(test_data_ipadapter_image_path)
                logger.debug(
                    f"test_data_ipadapter_image use f{test_data_ipadapter_image_path}"
                )
        else:
            test_data_ipadapter_image = None
            test_data_ipadapter_image_name = "no"
            logger.debug(f"test_data_ipadapter_image is None")

        # prepare test_data_refer_face_image

        if facein_image_proj is not None or ip_adapter_face_image_proj is not None:
            if test_data_refer_face_image_path is None:
                test_data_refer_face_image = test_data_condition_images
                test_data_refer_face_image_name = test_data_condition_images_name

                logger.debug(
                    f"test_data_refer_face_image use test_data_condition_images"
                )
            else:
                (
                    test_data_refer_face_image,
                    test_data_refer_face_image_name,
                ) = read_image_and_name(test_data_refer_face_image_path)
                logger.debug(
                    f"test_data_refer_face_image use f{test_data_refer_face_image_path}"
                )
        else:
            test_data_refer_face_image = None
            test_data_refer_face_image_name = "no"
            logger.debug(f"test_data_refer_face_image is None")

        # if sex, style of test_data is not aligned with of model
        # skip this test_data

        if (
            model_sex is not None
            and test_data_sex is not None
            and model_sex != test_data_sex
        ) or (
            model_style is not None
            and test_data_style is not None
            and model_style != test_data_style
        ):
            print("model doesnt match test_data")
            print("model name: ", model_name)
            print("test_data: ", test_data)
            continue
        if add_static_video_prompt:
            test_data_video_negative_prompt = "static video, {}".format(
                test_data_video_negative_prompt
            )
        for i_num in range(n_repeat):
            test_data_seed = random.randint(0, 1e8) if seed is None else seed
            cpu_generator, gpu_generator = set_all_seed(test_data_seed)
            save_file_name = (
                f"m={model_name}_rm={referencenet_model_name}_case={test_data_name}"
                f"_w={test_data_width}_h={test_data_height}_t={time_size}_nb={n_batch}"
                f"_s={test_data_seed}_p={prompt_hash}"
                f"_w={test_data_img_weight}"
                f"_ms={test_data_motion_speed}"
                f"_s={strength}_g={video_guidance_scale}"
                f"_c-i={test_data_condition_images_name[:5]}_r-c={test_data_redraw_condition_image}"
                f"_w={test_data_w_ind_noise}_{test_data_video_negative_prompt_name}"
                f"_r={test_data_refer_image_name[:3]}_ip={test_data_refer_image_name[:3]}_f={test_data_refer_face_image_name[:3]}"
            )

            save_file_name = clean_str_for_save(save_file_name)
            output_path = os.path.join(
                output_dir,
                f"{save_file_name}.{save_filetype}",
            )
            if os.path.exists(output_path) and not overwrite:
                print("existed", output_path)
                continue

            print("output_path", output_path)
            out_videos = sd_predictor.run_pipe_text2video(
                video_length=time_size,
                prompt=prompt,
                width=test_data_width,
                height=test_data_height,
                generator=gpu_generator,
                noise_type=noise_type,
                negative_prompt=test_data_negative_prompt,
                video_negative_prompt=test_data_video_negative_prompt,
                max_batch_num=n_batch,
                strength=strength,
                need_img_based_video_noise=need_img_based_video_noise,
                video_num_inference_steps=video_num_inference_steps,
                condition_images=test_data_condition_images,
                fix_condition_images=fix_condition_images,
                video_guidance_scale=video_guidance_scale,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
                redraw_condition_image=test_data_redraw_condition_image,
                img_weight=test_data_img_weight,
                w_ind_noise=test_data_w_ind_noise,
                n_vision_condition=n_vision_condition,
                motion_speed=test_data_motion_speed,
                need_hist_match=need_hist_match,
                video_guidance_scale_end=video_guidance_scale_end,
                video_guidance_scale_method=video_guidance_scale_method,
                vision_condition_latent_index=test_data_condition_images_index,
                refer_image=test_data_refer_image,
                fixed_refer_image=fixed_refer_image,
                redraw_condition_image_with_referencenet=redraw_condition_image_with_referencenet,
                ip_adapter_image=test_data_ipadapter_image,
                refer_face_image=test_data_refer_face_image,
                fixed_refer_face_image=fixed_refer_face_image,
                facein_scale=facein_scale,
                redraw_condition_image_with_facein=redraw_condition_image_with_facein,
                ip_adapter_face_scale=ip_adapter_face_scale,
                redraw_condition_image_with_ip_adapter_face=redraw_condition_image_with_ip_adapter_face,
                fixed_ip_adapter_image=fixed_ip_adapter_image,
                ip_adapter_scale=ip_adapter_scale,
                redraw_condition_image_with_ipdapter=redraw_condition_image_with_ipdapter,
                prompt_only_use_image_prompt=prompt_only_use_image_prompt,
                # serial_denoise parameter start
                record_mid_video_noises=record_mid_video_noises,
                record_mid_video_latents=record_mid_video_latents,
                video_overlap=video_overlap,
                # serial_denoise parameter end
                # parallel_denoise parameter start
                context_schedule=context_schedule,
                context_frames=context_frames,
                context_stride=context_stride,
                context_overlap=context_overlap,
                context_batch_size=context_batch_size,
                interpolation_factor=interpolation_factor,
                # parallel_denoise parameter end
            )
            out = np.concatenate([out_videos], axis=0)
            texts = ["out"]
            save_videos_grid_with_opencv(
                out,
                output_path,
                texts=texts,
                fps=fps,
                tensor_order="b c t h w",
                n_cols=n_cols,
                write_info=args.write_info,
                save_filetype=save_filetype,
                save_images=save_images,
            )
            print("Save to", output_path)
            print("\n" * 2)