test_ir_inplace_pass.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 os
import unittest
17

18
import numpy as np
19 20
from parallel_executor_test_base import DeviceType, TestParallelExecutorBase

21
import paddle
22
import paddle.fluid as fluid
23
import paddle.fluid.core as core
24 25 26 27 28 29 30 31 32 33 34 35


def fc_with_batchnorm(use_feed):
    img = fluid.layers.data(name='image', shape=[784], dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')

    hidden = img
    for _ in range(3):
        hidden = fluid.layers.fc(
            hidden,
            size=200,
            act='tanh',
36 37 38 39
            bias_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Constant(value=1.0)
            ),
        )
40

41
        hidden = paddle.static.nn.batch_norm(input=hidden)
42
    prediction = fluid.layers.fc(hidden, size=10, act='softmax')
43 44 45
    loss = paddle.nn.functional.cross_entropy(
        input=prediction, label=label, reduction='none', use_softmax=False
    )
46
    loss = paddle.mean(loss)
47 48 49 50 51 52 53 54
    return loss


class TestIrInplace(TestParallelExecutorBase):
    @classmethod
    def setUpClass(cls):
        os.environ['CPU_NUM'] = str(4)

55
    def _fc_with_batchnorm(self, ir_memory_optimize, enable_inplace):
D
dzhwinter 已提交
56 57 58

        if not core.is_compiled_with_cuda():
            return
59 60 61 62 63
        np.random.seed(5)
        img = np.random.random(size=[32, 784]).astype(np.float32)
        label = np.ones(shape=[32, 1], dtype='int64')
        self.check_network_convergence(
            fc_with_batchnorm,
64
            feed_dict={"image": img, "label": label},
65
            use_device=DeviceType.CUDA,
66
            use_ir_memory_optimize=ir_memory_optimize,
67 68
            enable_inplace=enable_inplace,
        )
69 70 71 72 73 74 75 76 77

    def test_fc_with_batchnorm(self, delta=1e-3):
        loss00 = self._fc_with_batchnorm(False, False)
        loss10 = self._fc_with_batchnorm(True, False)
        loss01 = self._fc_with_batchnorm(False, True)
        loss11 = self._fc_with_batchnorm(True, True)
        self.assertAlmostEqual(loss00, loss10, delta=delta)
        self.assertAlmostEqual(loss00, loss01, delta=delta)
        self.assertAlmostEqual(loss00, loss11, delta=delta)
78 79 80 81


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