|
| 1 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +import tensorflow as tf |
| 14 | + |
| 15 | +import smdistributed.dataparallel.tensorflow as dist |
| 16 | + |
| 17 | +tf.random.set_seed(42) |
| 18 | + |
| 19 | +dist.init() |
| 20 | + |
| 21 | +gpus = tf.config.experimental.list_physical_devices("GPU") |
| 22 | +for gpu in gpus: |
| 23 | + tf.config.experimental.set_memory_growth(gpu, True) |
| 24 | +if gpus: |
| 25 | + tf.config.experimental.set_visible_devices(gpus[dist.local_rank()], "GPU") |
| 26 | + |
| 27 | +(mnist_images, mnist_labels), _ = tf.keras.datasets.mnist.load_data( |
| 28 | + path="mnist-%d.npz" % dist.rank() |
| 29 | +) |
| 30 | + |
| 31 | +dataset = tf.data.Dataset.from_tensor_slices( |
| 32 | + (tf.cast(mnist_images[..., tf.newaxis] / 255.0, tf.float32), tf.cast(mnist_labels, tf.int64)) |
| 33 | +) |
| 34 | +dataset = dataset.repeat().shuffle(10000).batch(128) |
| 35 | + |
| 36 | +mnist_model = tf.keras.Sequential( |
| 37 | + [ |
| 38 | + tf.keras.layers.Conv2D(32, [3, 3], activation="relu"), |
| 39 | + tf.keras.layers.Conv2D(64, [3, 3], activation="relu"), |
| 40 | + tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), |
| 41 | + tf.keras.layers.Dropout(0.25), |
| 42 | + tf.keras.layers.Flatten(), |
| 43 | + tf.keras.layers.Dense(128, activation="relu"), |
| 44 | + tf.keras.layers.Dropout(0.5), |
| 45 | + tf.keras.layers.Dense(10, activation="softmax"), |
| 46 | + ] |
| 47 | +) |
| 48 | +loss = tf.losses.SparseCategoricalCrossentropy() |
| 49 | +# LR for 8 node run : 0.000125 |
| 50 | +# LR for single node run : 0.001 |
| 51 | +opt = tf.optimizers.Adam(0.000125 * dist.size()) |
| 52 | + |
| 53 | +checkpoint_dir = "./checkpoints" |
| 54 | +checkpoint = tf.train.Checkpoint(model=mnist_model, optimizer=opt) |
| 55 | + |
| 56 | + |
| 57 | +@tf.function |
| 58 | +def training_step(images, labels, first_batch): |
| 59 | + with tf.GradientTape() as tape: |
| 60 | + probs = mnist_model(images, training=True) |
| 61 | + loss_value = loss(labels, probs) |
| 62 | + |
| 63 | + tape = dist.DistributedGradientTape(tape) |
| 64 | + |
| 65 | + grads = tape.gradient(loss_value, mnist_model.trainable_variables) |
| 66 | + opt.apply_gradients(zip(grads, mnist_model.trainable_variables)) |
| 67 | + |
| 68 | + if first_batch: |
| 69 | + dist.broadcast_variables(mnist_model.variables, root_rank=0) |
| 70 | + dist.broadcast_variables(opt.variables(), root_rank=0) |
| 71 | + |
| 72 | + loss_value = dist.oob_allreduce(loss_value) # Average the loss across workers |
| 73 | + return loss_value |
| 74 | + |
| 75 | + |
| 76 | +for batch, (images, labels) in enumerate(dataset.take(10000 // dist.size())): |
| 77 | + loss_value = training_step(images, labels, batch == 0) |
| 78 | + |
| 79 | + if batch % 50 == 0 and dist.rank() == 0: |
| 80 | + print("Step #%d\tLoss: %.6f" % (batch, loss_value)) |
| 81 | + |
| 82 | +if dist.rank() == 0: |
| 83 | + checkpoint.save(checkpoint_dir) |
0 commit comments