test_mix_precision_all_reduce_fuse.py 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import unittest

L
LoneRanger 已提交
17
import nets
18
import numpy as np
19 20 21
from parallel_executor_test_base import DeviceType, TestParallelExecutorBase
from simple_nets import init_data

22
import paddle
23 24
from paddle import fluid
from paddle.fluid import core
25 26 27 28 29 30

batch_size = 12
img_shape = [1, 28, 28]


def loss_net(hidden, label):
C
Charles-hit 已提交
31
    prediction = paddle.static.nn.fc(x=hidden, size=10, activation='softmax')
32 33 34
    loss = paddle.nn.functional.cross_entropy(
        input=prediction, label=label, reduction='none', use_softmax=False
    )
35
    avg_loss = paddle.mean(loss)
36 37 38 39
    return avg_loss


def conv_net(use_feed):
G
GGBond8488 已提交
40 41 42 43
    img = paddle.static.data(
        name='image', shape=[-1] + img_shape, dtype='float16'
    )
    label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
44

L
LoneRanger 已提交
45
    conv_pool_1 = nets.simple_img_conv_pool(
46 47 48 49 50 51 52
        input=img,
        filter_size=5,
        num_filters=20,
        pool_size=2,
        pool_stride=2,
        act="relu",
    )
53
    conv_pool_1 = paddle.static.nn.batch_norm(conv_pool_1)
54

55
    conv_pool_1 = paddle.cast(conv_pool_1, np.float32)
L
LoneRanger 已提交
56
    conv_pool_2 = nets.simple_img_conv_pool(
57 58 59 60 61 62 63
        input=conv_pool_1,
        filter_size=5,
        num_filters=50,
        pool_size=2,
        pool_stride=2,
        act="relu",
    )
64
    hidden = paddle.cast(conv_pool_2, np.float32)
65 66 67 68 69 70 71 72 73
    return loss_net(hidden, label)


def _optimizer(learning_rate=1e-6):
    optimizer = fluid.optimizer.SGD(learning_rate=learning_rate)
    return optimizer


class TestResnet(TestParallelExecutorBase):
74
    def check_model(self, use_device):
75 76 77
        img, label = init_data(
            batch_size=batch_size, img_shape=img_shape, label_range=9
        )
78
        img = np.float16(img)
79 80 81 82 83 84
        feed_dict = {"image": img, "label": label}

        TestParallelExecutorBase.check_network_convergence(
            conv_net,
            feed_dict=feed_dict,
            iter=10,
85
            use_device=use_device,
86
            fuse_all_reduce_ops=True,
87 88
            optimizer=_optimizer,
        )
89 90 91

    def test_model(self):
        if core.is_compiled_with_cuda():
92
            self.check_model(DeviceType.CUDA)
93 94 95 96


if __name__ == '__main__':
    unittest.main()