-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
344 lines (275 loc) · 11 KB
/
train.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
import time
import functools
from typing import Any
from absl import logging
from clu import metric_writers
from clu import periodic_actions
import jax
import jax.numpy as jnp
from jax import lax
from jax import random
import flax
from flax import jax_utils
from flax import optim
import optax
from flax.training import checkpoints
from flax.training import common_utils
from flax.training import train_state
import tensorflow as tf
import tensorflow_datasets as tfds
import ml_collections
import input_pipeline
import models
NUM_CLASSES = 1000
def create_model(*, model_cls, half_precision, **kwargs):
platform = jax.local_devices()[0].platform
if half_precision:
if platform == 'tpu':
model_dtype = jnp.bfloat16
else:
model_dtype = jnp.float16
else:
model_dtype = jnp.float32
return model_cls(num_classes=NUM_CLASSES, dtype=model_dtype, **kwargs)
def initialized(key, image_size, model):
input_shape = (1, image_size, image_size, 3)
@jax.jit
def init(*args):
return model.init(*args)
variables = init({'params': key}, jnp.ones(input_shape, model.dtype))
return variables['params'], variables['batch_stats']
def cross_entropy_loss(logits, labels):
one_hot_labels = jax.nn.one_hot(labels, num_classes=NUM_CLASSES)
cross_entropy = -jnp.sum(one_hot_labels * jax.nn.log_softmax(logits, axis=-1), axis=-1)
return jnp.mean(cross_entropy)
def compute_metrics(logits, labels):
loss = cross_entropy_loss(logits, labels)
accuracy = jnp.mean(jnp.argmax(logits, -1) == labels)
metrics = {
'loss': loss,
'accuracy': accuracy,
}
metrics = lax.pmean(metrics, axis_name='batch')
return metrics
def create_learning_rate_fn(config: ml_collections.ConfigDict,
base_learning_rate: float,
steps_per_epoch: int):
warmup_steps = config.warmup_epochs * steps_per_epoch
warmup_fn = optax.linear_schedule(
init_value=0, end_value=base_learning_rate,
transition_steps=warmup_steps)
cosine_epochs = max(config.num_epochs - config.warmup_epochs, 1)
cosine_fn = optax.cosine_decay_schedule(
init_value=base_learning_rate,
decay_steps=cosine_epochs * steps_per_epoch)
schedule_fn = optax.join_schedules(
schedules=[warmup_fn, cosine_fn],
boundaries=[warmup_steps])
return schedule_fn
def train_step(state, batch, learning_rate_fn):
def loss_fn(params):
logits, new_model_state = state.apply_fn(
{'params': params, 'batch_stats': state.batch_stats},
batch['image'],
mutable=['batch_stats'])
loss = cross_entropy_loss(logits, batch['label'])
weight_penalty_params = jax.tree_leaves(params)
weight_decay = 1e-4
weight_l2 = sum([jnp.sum(x ** 2) for x in weight_penalty_params if x.ndim > 1])
weight_penalty = weight_decay * 0.5 * weight_l2
loss = loss + weight_penalty
return loss, (new_model_state, logits)
step = state.step
dynamic_scale = state.dynamic_scale
lr = learning_rate_fn(step)
if dynamic_scale:
grad_fn = dynamic_scale.value_and_grad(
loss_fn, has_aux=True, axis_name='batch')
dynamic_scale, is_fin, aux, grads = grad_fn(state.params)
else:
grad_fn = jax.value_and_grad(loss_fn, has_aux=True)
aux, grads = grad_fn(state.params)
grads = lax.pmean(grads, axis_name='batch') # Re-use same axis_name in `pmap(..)`
new_model_state, logits = aux[1]
metrics = compute_metrics(logits, batch['label'])
metrics['learning_rate'] = lr
new_state = state.apply_gradients(
grads=grads, batch_stats=new_model_state['batch_stats'])
if dynamic_scale:
# if is_fin == False the gradients contain Inf/NaNs and optimizer state and
# params should be restored (= skip this step).
new_state = new_state.replace(
opt_state=jax.tree_multimap(
functools.partial(jnp.where, is_fin),
new_state.opt_state,
state.opt_state),
params=jax.tree_multimap(
functools.partial(jnp.where, is_fin),
new_state.params,
state.params))
metrics['scale'] = dynamic_scale.scale
return new_state, metrics
def eval_step(state, batch):
variables = {'params': state.params, 'batch_stats': state.batch_stats}
logits = state.apply_fn(
variables, batch['image'], train=False, mutable=False)
return compute_metrics(logits, batch['label'])
def prepare_tf_data(xs):
local_device_count = jax.local_device_count()
def _prepare(x):
x = x._numpy() # for zero-copy conversion between TF and Numpy
# Reshape data to shard to multiple devices.
# [N, ...] -> [C, N // C, ...]
return x.reshape((local_device_count, -1) + x.shape[1:])
return jax.tree_map(_prepare, xs)
def create_input_iter(dataset_builder,
batch_size,
image_size,
dtype,
train,
seed,
cache):
ds = input_pipeline.create_split(
dataset_builder, batch_size, image_size=image_size, dtype=dtype,
train=train, seed=seed, cache=cache)
it = map(prepare_tf_data, ds)
it = jax_utils.prefetch_to_device(it, 2)
return it
class TrainState(train_state.TrainState):
batch_stats: Any
dynamic_scale: flax.optim.DynamicScale
def restore_checkpoint(state, workdir):
return checkpoints.restore_checkpoint(workdir, state)
def save_checkpoint(state, workdir):
# Get train state from the first replica
if jax.process_index() == 0:
state = jax.device_get(jax.tree_map(lambda x: x[0], state))
step = int(state.step)
checkpoints.save_checkpoint(workdir, state, step, keep=3)
# pmean only works inside pmap because it needs an axis name
# This `cross_replica_mean` function will average the inputs across all devices
cross_replica_mean = jax.pmap(lambda x: lax.pmean(x, 'x'), 'x')
def sync_batch_stats(state):
"""Sync the batch statistics across replicas."""
return state.replace(batch_stats=cross_replica_mean(state.batch_stats))
def create_train_state(rng,
config: ml_collections.ConfigDict,
model,
image_size,
learning_rate_fn):
dynamic_scale = None
platform = jax.local_devices()[0].platform
if config.half_precision and platform == 'gpu':
dynamic_scale = optim.DynamicScale()
else:
dynamic_scale = None
params, batch_stats = initialized(rng, image_size, model)
tx = optax.sgd(
learning_rate=learning_rate_fn,
momentum=config.momentum,
nesterov=True)
state = TrainState.create(
apply_fn=model.apply,
params=params,
tx=tx,
batch_stats=batch_stats,
dynamic_scale=dynamic_scale)
return state
def train_and_evaluate(config: ml_collections.ConfigDict,
workdir: str, data_dir: str, seed: int) -> TrainState:
writer = metric_writers.create_default_writer(
logdir=workdir, just_logging=bool(jax.process_index())) # only host write metrics.
rng = random.PRNGKey(seed)
image_size = 224
if config.batch_size % jax.device_count() > 0:
raise ValueError('Batch size must be divisible by the number of devices')
local_batch_size = config.batch_size // jax.process_count()
platform = jax.local_devices()[0].platform
if config.half_precision:
if platform == 'tpu':
input_dtype = tf.bfloat16
else:
input_dtype = tf.float16
else:
input_dtype = tf.float32
dataset_builder = tfds.builder(config.dataset, data_dir=data_dir)
train_iter = create_input_iter(
dataset_builder, local_batch_size, image_size, input_dtype,
train=True, seed=seed, cache=config.cache)
eval_iter = create_input_iter(
dataset_builder, local_batch_size, image_size, input_dtype,
train=False, seed=seed, cache=config.cache)
steps_per_epoch = (
dataset_builder.info.splits['train'].num_examples // config.batch_size)
if config.num_train_steps == -1:
num_steps = int(steps_per_epoch * config.num_epochs)
else:
num_steps = config.num_train_steps
if config.steps_per_eval == -1:
num_validation_examples = dataset_builder.info.splits[
'validation'].num_examples
steps_per_eval = num_validation_examples // config.batch_size
else:
steps_per_eval = config.steps_per_epoch * 10
steps_per_checkpoint = steps_per_epoch * 10
base_learning_rate = config.learning_rate * config.batch_size / 256.
model_cls = getattr(models, config.model)
model = create_model(
model_cls=model_cls, half_precision=config.half_precision)
learning_rate_fn = create_learning_rate_fn(
config, base_learning_rate, steps_per_epoch)
state = create_train_state(rng, config, model, image_size, learning_rate_fn)
state = restore_checkpoint(state, workdir)
step_offset = int(state.step)
state = jax_utils.replicate(state) # replicates arrays to multiple devices.
# `state` pytree -> `replicated states` pytree
p_train_step = jax.pmap(
functools.partial(train_step, learning_rate_fn=learning_rate_fn),
axis_name='batch')
p_eval_step = jax.pmap(eval_step, axis_name='batch')
train_metrics = []
hooks = []
if jax.process_index() == 0: # only for host machine
hooks += [periodic_actions.Profile(num_profile_steps=5, logdir=workdir)]
train_metrics_last_t = time.time()
logging.info('Initial compilation, this might take some minutes...')
for step, batch in zip(range(step_offset, num_steps), train_iter):
state, metrics = p_train_step(state, batch)
for h in hooks:
h(step)
if step == step_offset:
logging.info('Initial compilation completed.')
if config.get('log_every_steps'):
train_metrics.append(metrics)
if (step + 1) % config.log_every_steps == 0:
train_metrics = common_utils.get_metrics(train_metrics)
summary = {
f'train_{k}': v
for k, v in jax.tree_map(lambda x: x.mean(), train_metrics).items()
}
summary['steps_per_second'] = config.log_every_steps / (
time.time() - train_metrics_last_t)
writer.write_scalars(step + 1, summary)
train_metrics = []
train_metrics_last_t = time.time()
if (step + 1) % steps_per_epoch == 0:
epoch = step // steps_per_epoch
eval_metrics = []
state = sync_batch_stats(state)
for _ in range(steps_per_eval):
eval_batch = next(eval_iter)
metrics = p_eval_step(state, eval_batch)
eval_metrics.append(metrics)
eval_metrics = common_utils.get_metrics(eval_metrics)
summary = jax.tree_map(lambda x: x.mean(), eval_metrics)
logging.info('eval epoch: %d, loss: %.4f, accuracy: %.2f',
epoch, summary['loss'], summary['accuracy'] * 100)
writer.write_scalars(
step + 1, {f'eval_{key}': val for key, val in summary.items()})
writer.flush()
if (step + 1) % steps_per_checkpoint == 0 or step + 1 == num_steps:
state = sync_batch_stats(state)
save_checkpoint(state, workdir)
# Wait until computations are done before exiting
jax.random.normal(jax.random.PRNGKey(0), ()).block_until_ready()
return state