-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtraining_sharded_1-5.py
465 lines (383 loc) · 14.9 KB
/
training_sharded_1-5.py
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
import json
import ast
import gc
import jax
import jaxlib
import jax.numpy as jnp
import numpy as np
from PIL import Image
from typing import Union
from PIL import Image
import optax
from flax.training import train_state
from diffusers import (
FlaxAutoencoderKL,
FlaxDDPMScheduler,
FlaxStableDiffusionPipeline,
# FlaxUNet2DConditionModel,
)
from models import FlaxUNet2DConditionModel
from transformers import CLIPFeatureExtractor, CLIPTokenizer, FlaxCLIPTextModel
import diffusers.schedulers.scheduling_ddim_flax
from jax.experimental import mesh_utils
from jax.sharding import PositionalSharding
from jax.sharding import Mesh
from jax.sharding import PartitionSpec
from jax.sharding import NamedSharding
from jax.experimental import mesh_utils
P = PartitionSpec
# adjust this sharding mesh to create appropriate sharding rule
# assume we have 8 device
# (1,8) = model parallel
# (8,1) = data parallel
# (4,2)/(2,4) = model data parallel
devices = mesh_utils.create_device_mesh((1,8))
mesh = Mesh(devices, axis_names=('dp', 'mp'))
# global var
adam_to_lion_scale_factor = 7
u_net_learning_rate = 1e-5
text_encoder_learning_rate = 1e-5
# typehint definition
transformed_params = dict
params = dict
rng = jax.random.PRNGKey
noise_scheduler_state = diffusers.schedulers.scheduling_ddim_flax.DDIMSchedulerState
sharding = PositionalSharding(mesh_utils.create_device_mesh((jax.device_count(),)))
use_offset_noise = False
strip_bos_eos_token = True
def read_json_file(file_path):
try:
with open(file_path, 'r') as json_file:
data_dict = json.load(json_file)
return data_dict
except FileNotFoundError:
print(f"File not found: {file_path}")
return None
except json.JSONDecodeError:
print(f"Error decoding JSON in file: {file_path}")
return None
def save_model_tree_as_json(file_name: str, params: dict) -> None:
# Save the dictionary as JSON
with open(file_name, "w") as json_file:
json.dump(params, json_file)
def shard_weight_column(model_params: jnp.array):
device_count = jax.device_count()
# get parallel device count to slice weight column wise
sharding = PositionalSharding(mesh_utils.create_device_mesh((device_count,)))
# check if model params is divisible by shard if it's not then just replicate for now
if model_params.shape[-1] % device_count == 0:
# replicate on last axis because sd last axis is shardable
param_dim_count = len(model_params.shape)
if param_dim_count > 1:
# just putting 1 as placeholder
# example [1,1,1,8] which replicate
sharding_rule = sharding.reshape([1] * (param_dim_count - 1) + [device_count])
model_params = jax.device_put(model_params, sharding_rule)
else:
model_params = jax.device_put(model_params, sharding.replicate())
pass
else:
# just replicate everything on all devices
model_params = jax.device_put(model_params, sharding.replicate())
return model_params
# convert it as actual sharding
def predefined_sharding(layouts:dict) -> dict:
# convert list as sharding and none as replicate
def _convert(param):
if param != None:
param = sharding.reshape(param)
else:
param = sharding.replicate()
return param
layouts = jax.tree_map(lambda x: _convert(ast.literal_eval(x)), layouts)
return layouts
# convert it as actual sharding
def predefined_mesh_sharding(layouts:dict) -> dict:
# convert string like (None, 'mp') to actual tuple and turn it to sharding rule
def _convert(param):
param = NamedSharding(mesh, P(*param))
return param
layouts = jax.tree_map(lambda x: _convert(ast.literal_eval(x)), layouts)
return layouts
def shard_remainder_state_param(param_leaf):
# if it already sharded then ignore it
if hasattr(param_leaf, "sharding"):
# if it's not sharded then shard it
if type(param_leaf.sharding) == jaxlib.xla_extension.SingleDeviceSharding:
shard_rule = NamedSharding(mesh, P())
else:
shard_rule = param_leaf.sharding
# shard / replicate pesky remainder params
else:
shard_rule = NamedSharding(mesh, P())
return shard_rule
def all_same_bool_values(d):
values = set()
for v in d.values():
if isinstance(v, bool):
values.add(v)
elif isinstance(v, dict):
values.update(all_same_bool_values(v))
return values
model_dir = "/home/teor/secondary_storage/tpu8/model/fluffyrock-576-704-832-960-1088-lion-e130"
# load the model params and model object
tokenizer = CLIPTokenizer.from_pretrained(model_dir, subfolder="tokenizer")
unet, unet_params = FlaxUNet2DConditionModel.from_pretrained(
model_dir, subfolder="unet", dtype=jnp.bfloat16, use_memory_efficient=True
)
text_encoder, text_encoder_params = FlaxCLIPTextModel.from_pretrained(
model_dir, subfolder="text_encoder", dtype=jnp.bfloat16, _do_init=False
)
vae, vae_params = FlaxAutoencoderKL.from_pretrained(
model_dir,
dtype=jnp.bfloat16,
subfolder="vae",
)
noise_scheduler = FlaxDDPMScheduler(
beta_start=0.00085,
beta_end=0.012,
beta_schedule="scaled_linear",
num_train_timesteps=1000,
prediction_type="v_prediction",
)
noise_scheduler_state = noise_scheduler.create_state()
# shard the weights across device
# unet_params = jax.tree_map(shard_weight_column, unet_params)
# text_encoder_params = jax.tree_map(shard_weight_column, text_encoder_params)
# vae_params = jax.tree_map(shard_weight_column, vae_params)
# grab tree sharding layout for each layer
unet_params_shard_layout = read_json_file("unet_sharding_layout.json")
text_encoder_shard_layout = read_json_file("clip_sharding_layout.json")
vae_params_shard_layout = read_json_file("vae_sharding_layout.json")
unet_params_shard_layout = predefined_mesh_sharding(unet_params_shard_layout)
text_encoder_shard_layout = predefined_mesh_sharding(text_encoder_shard_layout)
vae_params_shard_layout = predefined_mesh_sharding(vae_params_shard_layout)
# jax.profiler.start_trace("./tensorboard")
# NOTE: there's must be a way to define shard without materializing this to TPU!
# init the state in cpu first then manually shard the state perhaps hmmm
unet_params = jax.tree_map(lambda params, layout: jax.device_put(params, device=layout), unet_params, unet_params_shard_layout)
text_encoder_params = jax.tree_map(lambda params, layout: jax.device_put(params, device=layout), text_encoder_params, text_encoder_shard_layout)
vae_params = jax.tree_map(lambda params, layout: jax.device_put(params, device=layout), vae_params, vae_params_shard_layout)
u_net_constant_scheduler = optax.constant_schedule(
u_net_learning_rate / adam_to_lion_scale_factor
)
text_encoder_constant_scheduler = optax.constant_schedule(
text_encoder_learning_rate / adam_to_lion_scale_factor
)
# optimizer for U-Net
u_net_lion = optax.lion(
learning_rate=u_net_constant_scheduler,
b1=0.9,
b2=0.99,
weight_decay=1e-2 * adam_to_lion_scale_factor,
)
u_net_optimizer = optax.chain(
optax.clip_by_global_norm(1), # prevent explosion
u_net_lion,
)
# optimizer for CLIP text encoder
text_encoder_lion = optax.lion(
learning_rate=text_encoder_constant_scheduler,
b1=0.9,
b2=0.99,
weight_decay=1e-2 * adam_to_lion_scale_factor,
)
text_encoder_optimizer = optax.chain(
optax.clip_by_global_norm(1), # prevent explosion
text_encoder_lion,
)
unet_state = train_state.TrainState.create(
apply_fn=unet.__call__,
params=unet_params,
tx=u_net_optimizer
)
text_encoder_state = train_state.TrainState.create(
apply_fn=text_encoder.__call__,
params=text_encoder_params,
tx=text_encoder_optimizer
)
# delete previous params because state creates a copy of it and occupy a memory
del unet_params
del text_encoder_params
gc.collect()
def train_step(unet_state, text_encoder_state, vae_params, batch, train_rng:jax.random.PRNGKey):
# generate rng and return new_train_rng to be used for the next iteration step
# rng is comunicated though device aparently
dropout_rng, sample_rng, new_train_rng = jax.random.split(
train_rng, num=3)
# trainable params is passed as an argument while
# non trainable params are implicitly referenced in loss calculation
params = {
"text_encoder": text_encoder_state.params,
"unet": unet_state.params
}
def compute_loss(params):
# Convert images to latent space
vae_outputs = vae.apply(
{"params": vae_params},
batch["pixel_values"],
deterministic=True,
method=vae.encode
)
# get sample distribution from VAE latent
latents = vae_outputs.latent_dist.sample(sample_rng)
# (NHWC) -> (NCHW)
latents = jnp.transpose(latents, (0, 3, 1, 2))
# weird scaling don't touch it's a lazy normalization
latents = latents * 0.18215
# Sample noise that we'll add to the latents
# I think I should combine this with the first noise seed generator
noise_offset_rng, noise_rng, timestep_rng = jax.random.split(
sample_rng, num=3)
noise = jax.random.normal(noise_rng, latents.shape)
if use_offset_noise:
# mean offset noise, why add offset?
# here https://www.crosslabs.org//blog/diffusion-with-offset-noise
noise_offset = jax.random.normal(
noise_offset_rng,
(latents.shape[0], latents.shape[1], 1, 1)
) * 0.1
noise = noise + noise_offset
# Sample a random timestep for each image
bsz = latents.shape[0]
timesteps = jax.random.randint(
timestep_rng,
(bsz,),
0,
noise_scheduler.config.num_train_timesteps,
)
# Add noise to the latents according to the noise magnitude at each timestep
# (this is the forward diffusion process)
noisy_latents = noise_scheduler.add_noise(
noise_scheduler_state,
latents,
noise,
timesteps
)
print(batch["input_ids"].shape)
encoder_hidden_states = text_encoder_state.apply_fn(
batch["input_ids"],
params=params["text_encoder"],
dropout_rng=dropout_rng,
train=True
)[0]
print(encoder_hidden_states.shape)
# reshape encoder_hidden_states to shape (batch, token_append, token, hidden_states)
encoder_hidden_states = jnp.reshape(
encoder_hidden_states,
(latents.shape[0], -1, 77, encoder_hidden_states.shape[-1]),
)
print(encoder_hidden_states.shape)
if strip_bos_eos_token:
encoder_hidden_states = jnp.concatenate(
[
# first encoder hidden states without eos token
encoder_hidden_states[:, 0, :-1, :],
# the rest of encoder hidden states without both bos and eos token
jnp.reshape(
encoder_hidden_states[:, 1:-1, 1:-1, :],
(
encoder_hidden_states.shape[0],
-1,
encoder_hidden_states.shape[-1]
)
),
# last encoder hidden states without bos token
encoder_hidden_states[:, -1, 1:, :]
],
axis=1
)
else:
# reshape encoder_hidden_states to shape (batch, token_append & token, hidden_states)
encoder_hidden_states = jnp.reshape(
encoder_hidden_states,
(encoder_hidden_states.shape[0], -
1, encoder_hidden_states.shape[-1])
)
print(encoder_hidden_states.shape)
# Predict the noise residual because predicting image is hard :P
# essentially try to undo the noise process
model_pred = unet.apply(
{"params": params["unet"]},
noisy_latents,
timesteps,
encoder_hidden_states,
train=True
).sample
# Get the target for loss depending on the prediction type
# sd1.x use epsilon aka noise residual but sd2.1 use velocity prediction
if noise_scheduler.config.prediction_type == "epsilon":
target = noise
elif noise_scheduler.config.prediction_type == "v_prediction":
target = noise_scheduler.get_velocity(
noise_scheduler_state,
latents,
noise,
timesteps
)
else:
# panic!!
raise ValueError(
f"Unknown prediction type {noise_scheduler.config.prediction_type}")
# MSE loss
loss = (target - model_pred) ** 2
loss = loss.mean()
return loss
# perform autograd
grad_fn = jax.value_and_grad(compute_loss)
loss, grad = grad_fn(params)
# update weight and bias value
new_unet_state = unet_state.apply_gradients(grads=grad["unet"])
new_text_encoder_state = text_encoder_state.apply_gradients(
grads=grad["text_encoder"])
# calculate loss
metrics = {"loss": loss}
return new_unet_state, new_text_encoder_state, metrics, new_train_rng #
# ===============[compile to device]=============== #
# jax.profiler.start_trace("./tensorboard")
train_rngs = rng(2)
# dummy batch input
current_batch = {
'attention_mask': jnp.arange(2 * 1 * 3 * 77).reshape(2 * 1, 3, 77),
'input_ids': jnp.arange(2 * 3 * 77).reshape(2 * 3, 77),
'pixel_values': jax.random.uniform(train_rngs, shape=(2 * 1, 3, 512, 512))
}
# current_batch_shard_layout = {
# 'attention_mask': sharding.replicate(),
# 'input_ids': sharding.replicate(),
# 'pixel_values': sharding.replicate()
# }
current_batch_shard_layout = {
'attention_mask': NamedSharding(mesh, P()),
'input_ids': NamedSharding(mesh, P()),
'pixel_values': NamedSharding(mesh, P()),
}
p_train_step = jax.jit(
train_step ,
donate_argnums=(0, 1),
in_shardings=(
jax.tree_map(lambda x: shard_remainder_state_param(x), unet_state),
jax.tree_map(lambda x: shard_remainder_state_param(x), text_encoder_state),
jax.tree_map(lambda x: shard_remainder_state_param(x), vae_params),
current_batch_shard_layout,
NamedSharding(mesh, P()),# sharding.replicate()
),
out_shardings=(
jax.tree_map(lambda x: shard_remainder_state_param(x), unet_state),
jax.tree_map(lambda x: shard_remainder_state_param(x), text_encoder_state),
{"loss": NamedSharding(mesh, P())},
NamedSharding(mesh, P()), # sharding.replicate() # not sure about this
)
)
batch = jax.tree_map(
lambda x: jax.device_put(x, device=sharding.replicate()), current_batch
)
unet_state, text_encoder_state, metrics, train_rngs = p_train_step(
unet_state,
text_encoder_state,
vae_params,
batch,
train_rngs
)
# jax.profiler.stop_trace()
print()