shenzhebei commited on
Commit
6d613c2
·
verified ·
1 Parent(s): 8dc5bee

Upload folder using huggingface_hub

Browse files
config/__pycache__/base.cpython-310.pyc ADDED
Binary file (1.46 kB). View file
 
config/__pycache__/qwen_image_edit_nft.cpython-310.pyc ADDED
Binary file (3.12 kB). View file
 
config/base.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ml_collections
2
+
3
+
4
+ def get_config():
5
+ config = ml_collections.ConfigDict()
6
+
7
+ ###### General ######
8
+ # run name for wandb logging and checkpoint saving -- if not provided, will be auto-generated based on the datetime.
9
+ config.run_name = ""
10
+ config.debug = False
11
+
12
+ # random seed for reproducibility.
13
+ config.seed = 42
14
+ # top-level logging directory for checkpoint saving.
15
+ config.logdir = "logs"
16
+ # number of epochs to train for. each epoch is one round of sampling from the model followed by training on those
17
+ # samples.
18
+ config.num_epochs = 100000
19
+ # number of epochs between saving model checkpoints.
20
+ config.save_freq = 10
21
+ config.eval_freq = 10
22
+ # mixed precision training. options are "fp16", "bf16", and "no". half-precision speeds up training significantly.
23
+ config.mixed_precision = "bf16"
24
+ # allow tf32 on Ampere GPUs, which can speed up training.
25
+ config.allow_tf32 = True
26
+ # resume training from a checkpoint. either an exact checkpoint directory (e.g. checkpoint_50), or a directory
27
+ # containing checkpoints, in which case the latest one will be used. `config.use_lora` must be set to the same value
28
+ # as the run that generated the saved checkpoint.
29
+ config.resume_from = ""
30
+ # whether or not to use LoRA.
31
+ config.use_lora = True
32
+ config.dataset = ""
33
+ config.resolution = 768
34
+
35
+ ###### Pretrained Model ######
36
+ config.pretrained = pretrained = ml_collections.ConfigDict()
37
+ # base model to load. either a path to a local directory, or a model name from the HuggingFace model hub.
38
+ pretrained.model = ""
39
+ # revision of the model to load.
40
+ pretrained.revision = ""
41
+
42
+ ###### Sampling ######
43
+ config.sample = sample = ml_collections.ConfigDict()
44
+ # number of sampler inference steps.
45
+ sample.num_steps = 40
46
+ sample.eval_num_steps = 40
47
+ # classifier-free guidance weight. 1.0 is no guidance.
48
+ sample.guidance_scale = 4.5
49
+ # batch size (per GPU!) to use for sampling.
50
+ sample.train_batch_size = 1
51
+ sample.num_image_per_prompt = 1
52
+ sample.test_batch_size = 1
53
+ # number of batches to sample per epoch. the total number of samples per epoch is `num_batches_per_epoch *
54
+ # batch_size * num_gpus`.
55
+ sample.num_batches_per_epoch = 2
56
+ # Whether use all samples in a batch to compute std
57
+ sample.global_std = True
58
+ # noise level
59
+ sample.noise_level = 1.0
60
+
61
+ ###### Training ######
62
+ config.train = train = ml_collections.ConfigDict()
63
+ # batch size (per GPU!) to use for training.
64
+ train.batch_size = 1
65
+ # learning rate.
66
+ train.learning_rate = 3e-4
67
+ # Adam beta1.
68
+ train.adam_beta1 = 0.9
69
+ # Adam beta2.
70
+ train.adam_beta2 = 0.999
71
+ # Adam weight decay.
72
+ train.adam_weight_decay = 1e-4
73
+ # Adam epsilon.
74
+ train.adam_epsilon = 1e-8
75
+ # number of gradient accumulation steps. the effective batch size is `batch_size * num_gpus *
76
+ # gradient_accumulation_steps`.
77
+ train.gradient_accumulation_steps = 1
78
+ # maximum gradient norm for gradient clipping.
79
+ train.max_grad_norm = 1.0
80
+ # number of inner epochs per outer epoch. each inner epoch is one iteration through the data collected during one
81
+ # outer epoch's round of sampling.
82
+ train.num_inner_epochs = 1
83
+ # clip advantages to the range [-adv_clip_max, adv_clip_max].
84
+ train.adv_clip_max = 5
85
+ # the fraction of timesteps to train on. if set to less than 1.0, the model will be trained on a subset of the
86
+ # timesteps for each sample. this will speed up training but reduce the accuracy of policy gradient estimates.
87
+ train.timestep_fraction = 0.99
88
+ # kl ratio
89
+ train.beta = 0.0001
90
+ # pretrained lora path
91
+ train.lora_path = None
92
+ train.ema = True
93
+
94
+ ###### Prompt Function ######
95
+ # prompt function to use. see `prompts.py` for available prompt functions.
96
+ config.prompt_fn = ""
97
+ # kwargs to pass to the prompt function.
98
+ config.prompt_fn_kwargs = {}
99
+
100
+ ###### Reward Function ######
101
+ # reward function to use. see `rewards.py` for available reward functions.
102
+ config.reward_fn = ml_collections.ConfigDict()
103
+ config.save_dir = ""
104
+ # whether to save all generated images during periodic eval.
105
+ config.save_eval_images = True
106
+ # optional custom directory for eval images; empty means {save_dir}/eval_images.
107
+ config.eval_image_dir = ""
108
+
109
+ ###### Per-Prompt Stat Tracking ######
110
+ config.per_prompt_stat_tracking = True
111
+
112
+ return config
config/kontext_nft.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import imp
2
+ import os
3
+
4
+ base = imp.load_source("base", os.path.join(os.path.dirname(__file__), "base.py"))
5
+
6
+
7
+ def get_config(name):
8
+ return globals()[name]()
9
+
10
+ def _get_config(base_model="kontext", n_gpus=1, gradient_step_per_epoch=1, reward_fn={}, name=""):
11
+ config = base.get_config()
12
+
13
+ config.base_model = base_model
14
+ config.dataset = "../edit-r1-dataset"
15
+
16
+ config.pretrained.model = "black-forest-labs/FLUX.1-Kontext-dev"
17
+ config.sample.num_steps = 6
18
+ config.sample.eval_num_steps = 15
19
+ config.sample.guidance_scale = 2.5
20
+ config.resolution = 512
21
+ config.train.beta = 0.0001
22
+ config.sample.noise_level = 0.7
23
+ bsz = 3
24
+
25
+ config.sample.num_image_per_prompt = 12
26
+
27
+ config.sample.ban_std_thres = 0.05
28
+ config.sample.ban_mean_thres = 0.9
29
+ config.sample.ban_prompt = False
30
+
31
+ num_groups = 24
32
+
33
+ while True:
34
+ if bsz < 1:
35
+ assert False, "Cannot find a proper batch size."
36
+ if (
37
+ num_groups * config.sample.num_image_per_prompt % (n_gpus * bsz) == 0
38
+ and bsz * n_gpus % config.sample.num_image_per_prompt == 0
39
+ ):
40
+ n_batch_per_epoch = num_groups * config.sample.num_image_per_prompt // (n_gpus * bsz)
41
+ if n_batch_per_epoch % gradient_step_per_epoch == 0:
42
+ config.sample.train_batch_size = bsz
43
+ config.sample.num_batches_per_epoch = n_batch_per_epoch
44
+ config.train.batch_size = config.sample.train_batch_size
45
+ config.train.gradient_accumulation_steps = (
46
+ config.sample.num_batches_per_epoch // gradient_step_per_epoch
47
+ )
48
+ break
49
+ bsz -= 1
50
+
51
+ # special design, the test set has a total of 1018/2212/2048 for ocr/geneval/pickscore, to make gpu_num*bs*n as close as possible to it, because when the number of samples cannot be divided evenly by the number of cards, multi-card will fill the last batch to ensure each card has the same number of samples, affecting gradient synchronization.
52
+ config.sample.test_batch_size = bsz
53
+ if n_gpus > 32:
54
+ config.sample.test_batch_size = config.sample.test_batch_size // 2
55
+
56
+ config.prompt_fn = "geneval"
57
+
58
+ config.run_name = f"nft_{base_model}_{name}"
59
+ config.save_dir = f"logs/nft/{base_model}/{name}"
60
+ config.reward_fn = reward_fn
61
+
62
+ config.decay_type = 1
63
+ config.beta = 1.0
64
+ config.train.adv_mode = "all"
65
+
66
+ # config.sample.guidance_scale = 1.0
67
+ config.sample.deterministic = True
68
+ config.sample.solver = "dpm2"
69
+ return config
70
+
71
+ def kontext_mllm_reward():
72
+ reward_fn = {
73
+ "mllm_score_continue": 1.0,
74
+ }
75
+ config = _get_config(
76
+ base_model="kontext",
77
+ n_gpus=24,
78
+ gradient_step_per_epoch=1,
79
+ reward_fn=reward_fn,
80
+ name="mllm_score_continue",
81
+ )
82
+ return config
83
+
84
+ def kontext_mllm_reward_ban_prompt():
85
+ reward_fn = {
86
+ "mllm_score_continue": 1.0,
87
+ }
88
+ config = _get_config(
89
+ base_model="kontext",
90
+ n_gpus=24,
91
+ gradient_step_per_epoch=1,
92
+ reward_fn=reward_fn,
93
+ name="mllm_score_continue_ban_prompt",
94
+ )
95
+ config.sample.ban_prompt = True
96
+ config.sample.ban_std_thres = 0.05
97
+ return config
config/qwen_image_edit_nft.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import imp
2
+ import os
3
+
4
+ base = imp.load_source("base", os.path.join(os.path.dirname(__file__), "base.py"))
5
+
6
+
7
+ def get_config(name):
8
+ return globals()[name]()
9
+
10
+ def _get_config(
11
+ base_model="qwen_image_edit",
12
+ n_gpus=1,
13
+ gradient_step_per_epoch=1,
14
+ reward_fn={},
15
+ name="",
16
+ sample_batch_size_start=3,
17
+ num_groups=24,
18
+ num_image_per_prompt=12,
19
+ ):
20
+ config = base.get_config()
21
+
22
+ config.base_model = base_model
23
+ config.transformer_path = None
24
+ config.dataset = "../edit-r1-dataset"
25
+
26
+ config.pretrained.model = "Qwen/Qwen-Image-Edit-2509"
27
+ config.sample.num_steps = 6
28
+ config.sample.eval_num_steps = 15
29
+ config.sample.guidance_scale = 1.0
30
+ config.resolution = 512
31
+ config.train.beta = 0.0001
32
+ config.sample.noise_level = 0.7
33
+ bsz = sample_batch_size_start
34
+ config.sample.num_image_per_prompt = num_image_per_prompt
35
+
36
+ config.sample.ban_std_thres = 0.05
37
+ config.sample.ban_mean_thres = 0.9
38
+ config.sample.ban_prompt = False
39
+ while True:
40
+ if bsz < 1:
41
+ assert False, "Cannot find a proper batch size."
42
+ if (
43
+ num_groups * config.sample.num_image_per_prompt % (n_gpus * bsz) == 0
44
+ and bsz * n_gpus % config.sample.num_image_per_prompt == 0
45
+ ):
46
+ n_batch_per_epoch = num_groups * config.sample.num_image_per_prompt // (n_gpus * bsz)
47
+ if n_batch_per_epoch % gradient_step_per_epoch == 0:
48
+ config.sample.train_batch_size = bsz
49
+ config.sample.num_batches_per_epoch = n_batch_per_epoch
50
+ config.train.batch_size = config.sample.train_batch_size
51
+ config.train.gradient_accumulation_steps = (
52
+ config.sample.num_batches_per_epoch // gradient_step_per_epoch
53
+ )
54
+ break
55
+ bsz -= 1
56
+
57
+ # special design, the test set has a total of 1018/2212/2048 for ocr/geneval/pickscore, to make gpu_num*bs*n as close as possible to it, because when the number of samples cannot be divided evenly by the number of cards, multi-card will fill the last batch to ensure each card has the same number of samples, affecting gradient synchronization.
58
+ config.sample.test_batch_size = bsz
59
+ if n_gpus > 32:
60
+ config.sample.test_batch_size = config.sample.test_batch_size // 2
61
+
62
+ config.prompt_fn = "geneval"
63
+
64
+ config.run_name = f"nft_{base_model}_{name}"
65
+ config.save_dir = f"logs/nft/{base_model}/{name}"
66
+ config.reward_fn = reward_fn
67
+
68
+ config.decay_type = 1
69
+ config.beta = 1.0
70
+ config.train.adv_mode = "all"
71
+
72
+ # config.sample.guidance_scale = 1.0
73
+ config.sample.deterministic = True
74
+ config.sample.solver = "dpm2"
75
+ return config
76
+
77
+ def qwen_mllm_reward():
78
+ reward_fn = {
79
+ "mllm_score_continue": 1.0,
80
+ }
81
+ config = _get_config(
82
+ base_model="qwen_image_edit",
83
+ n_gpus=48,
84
+ gradient_step_per_epoch=1,
85
+ reward_fn=reward_fn,
86
+ name="mllm_score_continue",
87
+ )
88
+ config.sample.ban_prompt = True
89
+ config.sample.ban_std_thres = 0.05
90
+ return config
91
+
92
+
93
+ def qwen_mllm_reward_h200_single():
94
+ reward_fn = {
95
+ "mllm_score_continue": 1.0,
96
+ }
97
+ config = _get_config(
98
+ base_model="qwen_image_edit",
99
+ n_gpus=1,
100
+ gradient_step_per_epoch=4,
101
+ reward_fn=reward_fn,
102
+ name="mllm_score_continue_h200_single",
103
+ sample_batch_size_start=12,
104
+ num_groups=24,
105
+ num_image_per_prompt=12,
106
+ )
107
+ config.sample.ban_prompt = True
108
+ config.sample.ban_std_thres = 0.05
109
+ config.pretrained.model = "/cpfs01/VideoGen/user_data/shenzhebei/Qwen-Image-Edit-2511"
110
+ config.sample.test_batch_size = 4
111
+ return config
112
+
113
+
114
+ def qwen_mllm_reward_h200_dual():
115
+ reward_fn = {
116
+ "mllm_score_continue": 1.0,
117
+ }
118
+ config = _get_config(
119
+ base_model="qwen_image_edit",
120
+ n_gpus=2,
121
+ gradient_step_per_epoch=4,
122
+ reward_fn=reward_fn,
123
+ name="mllm_score_continue_h200_dual",
124
+ sample_batch_size_start=6,
125
+ num_groups=24,
126
+ num_image_per_prompt=12,
127
+ )
128
+ config.sample.ban_prompt = True
129
+ config.sample.ban_std_thres = 0.05
130
+ config.pretrained.model = "/cpfs01/VideoGen/user_data/shenzhebei/Qwen-Image-Edit-2511"
131
+ config.sample.test_batch_size = 4
132
+ return config
133
+
134
+
135
+ def qwen_mllm_reward_h200_six():
136
+ reward_fn = {
137
+ "mllm_score_continue": 1.0,
138
+ }
139
+ config = _get_config(
140
+ base_model="qwen_image_edit",
141
+ n_gpus=6,
142
+ gradient_step_per_epoch=4,
143
+ reward_fn=reward_fn,
144
+ name="mllm_score_continue_h200_six",
145
+ sample_batch_size_start=12,
146
+ num_groups=24,
147
+ num_image_per_prompt=12,
148
+ )
149
+ config.sample.ban_prompt = True
150
+ config.sample.ban_std_thres = 0.05
151
+ config.pretrained.model = "/cpfs01/VideoGen/user_data/shenzhebei/Qwen-Image-Edit-2511"
152
+ config.sample.test_batch_size = 4
153
+ return config
154
+
155
+
156
+ def qwen_mllm_reward_h200_three():
157
+ reward_fn = {
158
+ "mllm_score_continue": 1.0,
159
+ }
160
+ config = _get_config(
161
+ base_model="qwen_image_edit",
162
+ n_gpus=3,
163
+ gradient_step_per_epoch=4,
164
+ reward_fn=reward_fn,
165
+ name="mllm_score_continue_h200_three",
166
+ sample_batch_size_start=8,
167
+ num_groups=24,
168
+ num_image_per_prompt=12,
169
+ )
170
+ config.sample.ban_prompt = True
171
+ config.sample.ban_std_thres = 0.05
172
+ config.pretrained.model = "/cpfs01/VideoGen/user_data/shenzhebei/Qwen-Image-Edit-2511"
173
+ config.sample.test_batch_size = 4
174
+ return config
175
+