test_monitor.py 3.5 KB
Newer Older
H
hutuxian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#   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.
"""
TestCases for Monitor
"""

18
import paddle
19

T
tangwei12 已提交
20 21
paddle.enable_static()

H
hutuxian 已提交
22
import os
23
import tempfile
24 25 26 27
import unittest

import paddle.fluid as fluid
import paddle.fluid.core as core
H
hutuxian 已提交
28 29 30


class TestDatasetWithStat(unittest.TestCase):
31
    """TestCases for Dataset."""
H
hutuxian 已提交
32 33 34 35 36 37 38

    def setUp(self):
        self.use_data_loader = False
        self.epoch_num = 10
        self.drop_last = False

    def test_dataset_run_with_stat(self):
39 40 41 42
        temp_dir = tempfile.TemporaryDirectory()
        path_a = os.path.join(temp_dir.name, "test_in_memory_dataset_run_a.txt")
        path_b = os.path.join(temp_dir.name, "test_in_memory_dataset_run_b.txt")
        with open(path_a, "w") as f:
H
hutuxian 已提交
43 44 45 46
            data = "1 1 2 3 3 4 5 5 5 5 1 1\n"
            data += "1 2 2 3 4 4 6 6 6 6 1 2\n"
            data += "1 3 2 3 5 4 7 7 7 7 1 3\n"
            f.write(data)
47
        with open(path_b, "w") as f:
H
hutuxian 已提交
48 49 50 51 52 53 54 55 56
            data = "1 4 2 3 3 4 5 5 5 5 1 4\n"
            data += "1 5 2 3 4 4 6 6 6 6 1 5\n"
            data += "1 6 2 3 5 4 7 7 7 7 1 6\n"
            data += "1 7 2 3 6 4 8 8 8 8 1 7\n"
            f.write(data)

        slots = ["slot1", "slot2", "slot3", "slot4"]
        slots_vars = []
        for slot in slots:
G
GGBond8488 已提交
57 58
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="int64", lod_level=1
59
            )
H
hutuxian 已提交
60 61
            slots_vars.append(var)

T
tangwei12 已提交
62 63 64 65 66
        embs = []
        for x in slots_vars:
            emb = fluid.layers.embedding(x, is_sparse=True, size=[100001, 4])
            embs.append(emb)

67 68 69
        dataset = paddle.distributed.InMemoryDataset()
        dataset._set_batch_size(32)
        dataset._set_thread(3)
70
        dataset.set_filelist([path_a, path_b])
71 72
        dataset._set_pipe_command("cat")
        dataset._set_use_var(slots_vars)
H
hutuxian 已提交
73
        dataset.load_into_memory()
74
        dataset._set_fea_eval(1, True)
H
hutuxian 已提交
75 76 77 78 79
        dataset.slots_shuffle(["slot1"])

        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(fluid.default_startup_program())
        if self.use_data_loader:
80
            data_loader = fluid.io.DataLoader.from_dataset(
81 82
                dataset, fluid.cpu_places(), self.drop_last
            )
H
hutuxian 已提交
83 84 85
            for i in range(self.epoch_num):
                for data in data_loader():
                    exe.run(fluid.default_main_program(), feed=data)
T
tangwei12 已提交
86

H
hutuxian 已提交
87 88 89
        else:
            for i in range(self.epoch_num):
                try:
90 91 92 93 94 95 96
                    exe.train_from_dataset(
                        fluid.default_main_program(),
                        dataset,
                        fetch_list=[embs[0], embs[1]],
                        fetch_info=["emb0", "emb1"],
                        print_period=1,
                    )
T
tangwei12 已提交
97

H
hutuxian 已提交
98 99 100 101 102 103 104
                except Exception as e:
                    self.assertTrue(False)

        int_stat = core.get_int_stats()
        # total 56 keys
        print(int_stat["STAT_total_feasign_num_in_mem"])

105
        temp_dir.cleanup()
H
hutuxian 已提交
106 107 108 109


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