test_fleet_metric.py 4.4 KB
Newer Older
X
xujiaqi01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#   Copyright (c) 2020 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.
"""Test fleet metric."""

from __future__ import print_function
import numpy as np
import paddle
import paddle.fluid as fluid
import os
import unittest
T
tangwei12 已提交
22
import numpy as np
23
import paddle.distributed.fleet.metrics.metric as metric
T
tangwei12 已提交
24 25 26 27
import paddle.distributed.fleet as fleet
from paddle.distributed.fleet.base.util_factory import UtilBase

paddle.enable_static()
X
xujiaqi01 已提交
28 29 30 31 32 33 34 35


class TestFleetMetric(unittest.TestCase):
    """Test cases for fleet metric."""

    def setUp(self):
        """Set up, set envs."""

T
tangwei12 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        class FakeUtil(UtilBase):
            def __init__(self, fake_fleet):
                super(UtilBase, self).__init__()
                self.fleet = fake_fleet

            def all_reduce(self, input, mode="sum", comm_world="worker"):
                input = np.array(input)
                input_shape = input.shape
                input_list = input.reshape(-1).tolist()

                self.fleet._barrier(comm_world)

                ans = self.fleet._all_reduce(input_list, mode)

                output = np.array(ans).reshape(input_shape)
                return output

X
xujiaqi01 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65
        class FakeFleet:
            """Fake fleet only for test."""

            def __init__(self):
                """Init."""
                self.gloo = fluid.core.Gloo()
                self.gloo.set_rank(0)
                self.gloo.set_size(1)
                self.gloo.set_prefix("123")
                self.gloo.set_iface("lo")
                self.gloo.set_hdfs_store("./tmp_test_metric", "", "")
                self.gloo.init()

T
tangwei12 已提交
66
            def _all_reduce(self, input, mode="sum"):
X
xujiaqi01 已提交
67
                """All reduce using gloo."""
T
tangwei12 已提交
68 69
                ans = self.gloo.all_reduce(input, mode)
                return ans
X
xujiaqi01 已提交
70

T
tangwei12 已提交
71 72
            def _barrier(self, comm_world="worker"):
                """Fake barrier, do nothing."""
X
xujiaqi01 已提交
73 74
                pass

T
tangwei12 已提交
75
        self.util = FakeUtil(FakeFleet())
76
        fleet.util = self.util
X
xujiaqi01 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    def test_metric_1(self):
        """Test cases for metrics."""
        train = fluid.Program()
        startup = fluid.Program()
        with fluid.program_guard(train, startup):
            t = fluid.layers.create_global_var(
                shape=[1, 1],
                value=1,
                dtype='int64',
                persistable=True,
                force_cpu=True)
            t1 = fluid.layers.create_global_var(
                shape=[1, 1],
                value=1,
                dtype='int64',
                persistable=True,
                force_cpu=True)
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        scope = fluid.Scope()
        with fluid.scope_guard(scope):
            exe.run(startup)
T
tangwei12 已提交
100 101 102 103
            metric.sum(t, scope, self.util)
            metric.max(t, scope, self.util)
            metric.min(t, scope, self.util)
            metric.auc(t, t1, scope, self.util)
104 105 106
            metric.mae(t, t1, scope, self.util)
            metric.rmse(t, t1, scope, self.util)
            metric.mse(t, t1, scope, self.util)
T
tangwei12 已提交
107
            metric.acc(t, t1, scope, self.util)
108 109 110 111
            metric.sum(str(t.name))
            metric.max(str(t.name))
            metric.min(str(t.name))
            metric.auc(str(t1.name), str(t.name))
112 113 114
            metric.mae(str(t1.name), str(t.name))
            metric.rmse(str(t1.name), str(t.name))
            metric.mse(str(t1.name), str(t.name))
115
            metric.acc(str(t.name), str(t1.name))
X
xujiaqi01 已提交
116
        arr = np.array([1, 2, 3, 4])
T
tangwei12 已提交
117 118 119
        metric.sum(arr, util=self.util)
        metric.max(arr, util=self.util)
        metric.min(arr, util=self.util)
X
xujiaqi01 已提交
120 121 122
        arr1 = np.array([[1, 2, 3, 4]])
        arr2 = np.array([[1, 2, 3, 4]])
        arr3 = np.array([1, 2, 3, 4])
T
tangwei12 已提交
123
        metric.auc(arr1, arr2, util=self.util)
124 125 126
        metric.mae(arr, arr3, util=self.util)
        metric.rmse(arr, arr3, util=self.util)
        metric.mse(arr, arr3, util=self.util)
T
tangwei12 已提交
127
        metric.acc(arr, arr3, util=self.util)
X
xujiaqi01 已提交
128 129 130 131


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