KublaiKhan1 commited on
Commit
ea03941
·
verified ·
1 Parent(s): 071f3b7

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. dt0_1_continuous/targets_shortcut.py +118 -0
dt0_1_continuous/targets_shortcut.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import jax
2
+ import jax.numpy as jnp
3
+ import numpy as np
4
+
5
+ def get_targets(FLAGS, key, train_state, images, labels, force_t=-1, force_dt=-1):
6
+ label_key, time_key, noise_key = jax.random.split(key, 3)
7
+ info = {}
8
+
9
+ # 1) =========== Sample dt. ============
10
+ bootstrap_batchsize = FLAGS.batch_size // FLAGS.model['bootstrap_every']
11
+ log2_sections = np.log2(FLAGS.model['denoise_timesteps']).astype(np.int32)
12
+ if FLAGS.model['bootstrap_dt_bias'] == 0:
13
+ dt_base = jnp.repeat(log2_sections - 1 - jnp.arange(log2_sections), bootstrap_batchsize // log2_sections)
14
+ dt_base = jnp.concatenate([dt_base, jnp.zeros(bootstrap_batchsize-dt_base.shape[0],)])
15
+ num_dt_cfg = bootstrap_batchsize // log2_sections
16
+ else:
17
+ dt_base = jnp.repeat(log2_sections - 1 - jnp.arange(log2_sections-2), (bootstrap_batchsize // 2) // log2_sections)
18
+ dt_base = jnp.concatenate([dt_base, jnp.ones(bootstrap_batchsize // 4), jnp.zeros(bootstrap_batchsize // 4)])
19
+ dt_base = jnp.concatenate([dt_base, jnp.zeros(bootstrap_batchsize-dt_base.shape[0],)])
20
+ num_dt_cfg = (bootstrap_batchsize // 2) // log2_sections
21
+ force_dt_vec = jnp.ones(bootstrap_batchsize, dtype=jnp.float32) * force_dt
22
+ dt_base = jnp.where(force_dt_vec != -1, force_dt_vec, dt_base)
23
+ dt = 1 / (2 ** (dt_base)) # [1, 1/2, 1/4, 1/8, 1/16, 1/32]
24
+ dt_base_bootstrap = dt_base + 1
25
+ dt_bootstrap = dt / 2
26
+
27
+ if True:#continuous time
28
+ dt_base = jax.random.uniform(0,1)*6
29
+ # 2) =========== Sample t. ============
30
+ dt_sections = jnp.power(2, dt_base) # [1, 2, 4, 8, 16, 32]
31
+ t = jax.random.randint(time_key, (bootstrap_batchsize,), minval=0, maxval=dt_sections).astype(jnp.float32)
32
+ t = t / dt_sections # Between 0 and 1.
33
+ force_t_vec = jnp.ones(bootstrap_batchsize, dtype=jnp.float32) * force_t
34
+ t = jnp.where(force_t_vec != -1, force_t_vec, t)
35
+ t_full = t[:, None, None, None]
36
+
37
+ # 3) =========== Generate Bootstrap Targets ============
38
+ x_1 = images[:bootstrap_batchsize]
39
+ x_0 = jax.random.normal(noise_key, x_1.shape)
40
+ x_t = (1 - (1 - 1e-5) * t_full) * x_0 + t_full * x_1
41
+ bst_labels = labels[:bootstrap_batchsize]
42
+ call_model_fn = train_state.call_model if FLAGS.model['bootstrap_ema'] == 0 else train_state.call_model_ema
43
+ if not FLAGS.model['bootstrap_cfg']:
44
+ dt_base_bootstrap = dt_base_bootstrap / 7.0
45
+ v_b1 = call_model_fn(x_t, t, dt_base_bootstrap, bst_labels, train=False)
46
+ t2 = t + dt_bootstrap
47
+ x_t2 = x_t + dt_bootstrap[:, None, None, None] * v_b1
48
+ x_t2 = jnp.clip(x_t2, -4, 4)
49
+ v_b2 = call_model_fn(x_t2, t2, dt_base_bootstrap, bst_labels, train=False)
50
+ v_target = (v_b1 + v_b2) / 2
51
+ else:
52
+ x_t_extra = jnp.concatenate([x_t, x_t[:num_dt_cfg]], axis=0)
53
+ t_extra = jnp.concatenate([t, t[:num_dt_cfg]], axis=0)
54
+ dt_base_extra = jnp.concatenate([dt_base_bootstrap, dt_base_bootstrap[:num_dt_cfg]], axis=0)
55
+ dt_base_extra = dt_base_extra / 7.0
56
+ labels_extra = jnp.concatenate([bst_labels, jnp.ones(num_dt_cfg, dtype=jnp.int32) * FLAGS.model['num_classes']], axis=0)
57
+ v_b1_raw = call_model_fn(x_t_extra, t_extra, dt_base_extra, labels_extra, train=False)
58
+ v_b_cond = v_b1_raw[:x_1.shape[0]]
59
+ v_b_uncond = v_b1_raw[x_1.shape[0]:]
60
+ v_cfg = v_b_uncond + FLAGS.model['cfg_scale'] * (v_b_cond[:num_dt_cfg] - v_b_uncond)
61
+ v_b1 = jnp.concatenate([v_cfg, v_b_cond[num_dt_cfg:]], axis=0)
62
+
63
+ t2 = t + dt_bootstrap
64
+ x_t2 = x_t + dt_bootstrap[:, None, None, None] * v_b1
65
+ x_t2 = jnp.clip(x_t2, -4, 4)
66
+ x_t2_extra = jnp.concatenate([x_t2, x_t2[:num_dt_cfg]], axis=0)
67
+ t2_extra = jnp.concatenate([t2, t2[:num_dt_cfg]], axis=0)
68
+ v_b2_raw = call_model_fn(x_t2_extra, t2_extra, dt_base_extra, labels_extra, train=False)
69
+ v_b2_cond = v_b2_raw[:x_1.shape[0]]
70
+ v_b2_uncond = v_b2_raw[x_1.shape[0]:]
71
+ v_b2_cfg = v_b2_uncond + FLAGS.model['cfg_scale'] * (v_b2_cond[:num_dt_cfg] - v_b2_uncond)
72
+ v_b2 = jnp.concatenate([v_b2_cfg, v_b2_cond[num_dt_cfg:]], axis=0)
73
+ v_target = (v_b1 + v_b2) / 2
74
+
75
+ v_target = jnp.clip(v_target, -4, 4)
76
+ bst_v = v_target
77
+ bst_dt = dt_base
78
+ bst_t = t
79
+ bst_xt = x_t
80
+ bst_l = bst_labels
81
+
82
+ # 4) =========== Generate Flow-Matching Targets ============
83
+
84
+ labels_dropout = jax.random.bernoulli(label_key, FLAGS.model['class_dropout_prob'], (labels.shape[0],))
85
+ labels_dropped = jnp.where(labels_dropout, FLAGS.model['num_classes'], labels)
86
+ info['dropped_ratio'] = jnp.mean(labels_dropped == FLAGS.model['num_classes'])
87
+
88
+ # Sample t.
89
+ t = jax.random.randint(time_key, (images.shape[0],), minval=0, maxval=FLAGS.model['denoise_timesteps']).astype(jnp.float32)
90
+ t /= FLAGS.model['denoise_timesteps']
91
+ force_t_vec = jnp.ones(images.shape[0], dtype=jnp.float32) * force_t
92
+ t = jnp.where(force_t_vec != -1, force_t_vec, t) # If force_t is not -1, then use force_t.
93
+ t_full = t[:, None, None, None] # [batch, 1, 1, 1]
94
+
95
+ # Sample flow pairs x_t, v_t.
96
+ x_0 = jax.random.normal(noise_key, images.shape)
97
+ x_1 = images
98
+ x_t = x_t = (1 - (1 - 1e-5) * t_full) * x_0 + t_full * x_1
99
+ v_t = v_t = x_1 - (1 - 1e-5) * x_0
100
+ dt_flow = np.log2(FLAGS.model['denoise_timesteps']).astype(jnp.int32)
101
+ dt_base = jnp.ones(images.shape[0], dtype=jnp.int32) * dt_flow
102
+
103
+ # ==== 5) Merge Flow+Bootstrap ====
104
+ bst_size = FLAGS.batch_size // FLAGS.model['bootstrap_every']
105
+ bst_size_data = FLAGS.batch_size - bst_size
106
+ x_t = jnp.concatenate([bst_xt, x_t[:bst_size_data]], axis=0)
107
+ t = jnp.concatenate([bst_t, t[:bst_size_data]], axis=0)
108
+ dt_base = jnp.concatenate([bst_dt, dt_base[:bst_size_data]], axis=0)
109
+ v_t = jnp.concatenate([bst_v, v_t[:bst_size_data]], axis=0)
110
+ labels_dropped = jnp.concatenate([bst_l, labels_dropped[:bst_size_data]], axis=0)
111
+ info['bootstrap_ratio'] = jnp.mean(dt_base != dt_flow)
112
+
113
+ info['v_magnitude_bootstrap'] = jnp.sqrt(jnp.mean(jnp.square(bst_v)))
114
+ info['v_magnitude_b1'] = jnp.sqrt(jnp.mean(jnp.square(v_b1)))
115
+ info['v_magnitude_b2'] = jnp.sqrt(jnp.mean(jnp.square(v_b2)))
116
+ dt_base = dt_base / 7.0
117
+
118
+ return x_t, v_t, t, dt_base, labels_dropped, info