-
Notifications
You must be signed in to change notification settings - Fork 643
/
train.py
421 lines (351 loc) · 12.1 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
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
# Copyright 2024 The Flax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""ImageNet example.
This script trains a ResNet-50 on the ImageNet dataset.
The data is loaded using tensorflow_datasets.
"""
import functools
import time
from typing import Any
from absl import logging
from clu import metric_writers
from clu import periodic_actions
from flax import jax_utils
from flax.training import checkpoints
from flax.training import common_utils
from flax.training import dynamic_scale as dynamic_scale_lib
from flax.training import train_state
import jax
from jax import lax
import jax.numpy as jnp
from jax import random
import ml_collections
import optax
import tensorflow as tf
import tensorflow_datasets as tfds
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 = common_utils.onehot(labels, num_classes=NUM_CLASSES)
xentropy = optax.softmax_cross_entropy(logits=logits, labels=one_hot_labels)
return jnp.mean(xentropy)
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,
):
"""Create learning rate schedule."""
warmup_fn = optax.linear_schedule(
init_value=0.0,
end_value=base_learning_rate,
transition_steps=config.warmup_epochs * steps_per_epoch,
)
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=[config.warmup_epochs * steps_per_epoch],
)
return schedule_fn
def train_step(state, batch, learning_rate_fn):
"""Perform a single training step."""
def loss_fn(params):
"""loss function used for training."""
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_util.tree_leaves(params)
weight_decay = 0.0001
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)
# dynamic loss takes care of averaging gradients across replicas
else:
grad_fn = jax.value_and_grad(loss_fn, has_aux=True)
aux, grads = grad_fn(state.params)
# Re-use same axis_name as in the call to `pmap(...train_step...)` below.
grads = lax.pmean(grads, axis_name='batch')
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=lax.pmean(new_model_state['batch_stats'], 'batch'),
)
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_util.tree_map(
functools.partial(jnp.where, is_fin),
new_state.opt_state,
state.opt_state,
),
params=jax.tree_util.tree_map(
functools.partial(jnp.where, is_fin), new_state.params, state.params
),
dynamic_scale=dynamic_scale,
)
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):
"""Convert a input batch from tf Tensors to numpy arrays."""
local_device_count = jax.local_device_count()
def _prepare(x):
# Use _numpy() for zero-copy conversion between TF and NumPy.
x = x._numpy() # pylint: disable=protected-access
# reshape (host_batch_size, height, width, 3) to
# (local_devices, device_batch_size, height, width, 3)
return x.reshape((local_device_count, -1) + x.shape[1:])
return jax.tree_util.tree_map(_prepare, xs)
def create_input_iter(
dataset_builder,
batch_size,
image_size,
dtype,
train,
cache,
shuffle_buffer_size,
prefetch,
):
ds = input_pipeline.create_split(
dataset_builder,
batch_size,
image_size=image_size,
dtype=dtype,
train=train,
cache=cache,
shuffle_buffer_size=shuffle_buffer_size,
prefetch=prefetch,
)
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: dynamic_scale_lib.DynamicScale
def restore_checkpoint(state, workdir):
return checkpoints.restore_checkpoint(workdir, state)
def save_checkpoint(state, workdir):
step = int(state.step)
logging.info('Saving checkpoint step %d.', step)
checkpoints.save_checkpoint_multiprocess(workdir, state, step, keep=3)
def create_train_state(
rng, config: ml_collections.ConfigDict, model, image_size, learning_rate_fn
):
"""Create initial training state."""
dynamic_scale = None
platform = jax.local_devices()[0].platform
if config.half_precision and platform == 'gpu':
dynamic_scale = dynamic_scale_lib.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
) -> TrainState:
"""Execute model training and evaluation loop.
Args:
config: Hyperparameter configuration for training and evaluation.
workdir: Directory where the tensorboard summaries are written to.
Returns:
Final TrainState.
"""
writer = metric_writers.create_default_writer(
logdir=workdir, just_logging=jax.process_index() != 0
)
rng = random.key(0)
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)
train_iter = create_input_iter(
dataset_builder,
local_batch_size,
image_size,
input_dtype,
train=True,
cache=config.cache,
shuffle_buffer_size=config.shuffle_buffer_size,
prefetch=config.prefetch,
)
eval_iter = create_input_iter(
dataset_builder,
local_batch_size,
image_size,
input_dtype,
train=False,
cache=config.cache,
shuffle_buffer_size=None,
prefetch=config.prefetch,
)
steps_per_epoch = (
dataset_builder.info.splits['train'].num_examples // config.batch_size
)
if config.num_train_steps <= 0:
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_eval
steps_per_checkpoint = steps_per_epoch * 10
base_learning_rate = config.learning_rate * config.batch_size / 256.0
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 > 0 if restarting from checkpoint
step_offset = int(state.step)
p_train_step = jax.pmap(
functools.partial(train_step, learning_rate_fn=learning_rate_fn),
in_axes=(None, 0),
out_axes=(None, 0),
axis_name='batch',
)
p_eval_step = jax.pmap(eval_step, in_axes=(None, 0), axis_name='batch')
train_metrics = []
hooks = []
if jax.process_index() == 0 and config.profile:
hooks += [
periodic_actions.Profile(
num_profile_steps=3, profile_duration_ms=None, 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_util.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 = []
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_util.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:
save_checkpoint(state, workdir)
# Wait until computations are done before exiting
jax.random.normal(jax.random.key(0), ()).block_until_ready()
return state