|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import division |
| 16 | +from __future__ import print_function |
| 17 | + |
| 18 | +import paddle |
| 19 | +import numpy as np |
| 20 | +from hybrid_parallel_mp_model import TestDistMPTraning |
| 21 | +import paddle.distributed.fleet as fleet |
| 22 | +import unittest |
| 23 | + |
| 24 | + |
| 25 | +class TestMPFP16(TestDistMPTraning): |
| 26 | + def build_optimizer(self, model): |
| 27 | + grad_clip = paddle.nn.ClipGradByGlobalNorm(1.0) |
| 28 | + scheduler = paddle.optimizer.lr.ExponentialDecay( |
| 29 | + learning_rate=0.001, gamma=0.999, verbose=True) |
| 30 | + optimizer = paddle.optimizer.SGD(scheduler, |
| 31 | + grad_clip=grad_clip, |
| 32 | + parameters=model.parameters()) |
| 33 | + |
| 34 | + model, optimizer = paddle.amp.decorate( |
| 35 | + models=model, |
| 36 | + optimizers=optimizer, |
| 37 | + level='O2', |
| 38 | + save_dtype='float32') |
| 39 | + |
| 40 | + return optimizer |
| 41 | + |
| 42 | + def train_batch(self, batch, model, optimizer, is_mp): |
| 43 | + scaler = paddle.amp.GradScaler(init_loss_scaling=5160) |
| 44 | + if is_mp: |
| 45 | + scaler = fleet.distributed_scaler(scaler) |
| 46 | + with paddle.amp.auto_cast(enable=True, level="O2"): |
| 47 | + output = model(batch) |
| 48 | + loss = output.mean() |
| 49 | + |
| 50 | + scaled = scaler.scale(loss) |
| 51 | + scaled.backward() |
| 52 | + scaler.step(optimizer) |
| 53 | + scaler.update() |
| 54 | + optimizer.clear_grad() |
| 55 | + return scaled |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + unittest.main() |
0 commit comments