test_dataset.py 48.9 KB
Newer Older
X
xjqbest 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2018 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.
X
xjqbest 已提交
14
"""
X
xjqbest 已提交
15 16
TestCases for Dataset,
including create, config, run, etc.
X
xjqbest 已提交
17
"""
X
xjqbest 已提交
18 19

import os
20
import tempfile
X
xjqbest 已提交
21 22
import unittest

23 24 25 26
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core

X
xjqbest 已提交
27 28

class TestDataset(unittest.TestCase):
29
    """TestCases for Dataset."""
30

Z
Zeng Jinle 已提交
31 32 33 34 35
    def setUp(self):
        self.use_data_loader = False
        self.epoch_num = 10
        self.drop_last = False

X
xjqbest 已提交
36
    def test_dataset_create(self):
37
        """Testcase for dataset create."""
X
xjqbest 已提交
38
        try:
39
            dataset = paddle.distributed.InMemoryDataset()
X
xjqbest 已提交
40 41 42 43
        except:
            self.assertTrue(False)

        try:
44
            dataset = paddle.distributed.QueueDataset()
X
xjqbest 已提交
45 46 47
        except:
            self.assertTrue(False)

48
        try:
49
            dataset = paddle.distributed.fleet.dataset.FileInstantDataset()
50 51 52
        except:
            self.assertTrue(False)

X
xjqbest 已提交
53
        try:
54
            dataset = paddle.distributed.fleet.dataset.MyOwnDataset()
X
xjqbest 已提交
55 56 57 58
            self.assertTrue(False)
        except:
            self.assertTrue(True)

59 60 61 62 63 64 65
    def test_config(self):
        """
        Testcase for python config.
        """
        dataset = fluid.InMemoryDataset()
        dataset.set_parse_ins_id(True)
        dataset.set_parse_content(True)
66
        dataset._set_trainer_num(1)
67 68
        self.assertTrue(dataset.parse_ins_id)
        self.assertTrue(dataset.parse_content)
69
        self.assertEqual(dataset.trainer_num, 1)
70

71 72 73 74 75 76 77 78
    def test_shuffle_by_uid(self):
        """
        Testcase for shuffle_by_uid.
        """
        dataset = paddle.distributed.InMemoryDataset()
        dataset._set_uid_slot('6048')
        dataset._set_shuffle_by_uid(True)

79 80 81 82
    def test_run_with_dump(self):
        """
        Testcase for InMemoryDataset from create to run.
        """
83 84 85 86 87 88

        temp_dir = tempfile.TemporaryDirectory()
        dump_a_path = os.path.join(temp_dir.name, 'test_run_with_dump_a.txt')
        dump_b_path = os.path.join(temp_dir.name, 'test_run_with_dump_b.txt')

        with open(dump_a_path, "w") as f:
89 90 91 92
            data = "1 a 1 a 1 1 2 3 3 4 5 5 5 5 1 1\n"
            data += "1 b 1 b 1 2 2 3 4 4 6 6 6 6 1 2\n"
            data += "1 c 1 c 1 3 2 3 5 4 7 7 7 7 1 3\n"
            f.write(data)
93
        with open(dump_b_path, "w") as f:
94 95 96 97 98 99 100 101 102
            data = "1 d 1 d 1 4 2 3 3 4 5 5 5 5 1 4\n"
            data += "1 e 1 e 1 5 2 3 4 4 6 6 6 6 1 5\n"
            data += "1 f 1 f 1 6 2 3 5 4 7 7 7 7 1 6\n"
            data += "1 g 1 g 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 已提交
103 104
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="int64", lod_level=1
105
            )
106 107
            slots_vars.append(var)

108
        dataset = paddle.distributed.InMemoryDataset()
109
        dataset.init(
110
            batch_size=32, thread_num=2, pipe_command="cat", use_var=slots_vars
111
        )
112
        dataset.update_settings(pipe_command="cat1")
113 114 115 116 117 118
        dataset._init_distributed_settings(
            parse_ins_id=True,
            parse_content=True,
            fea_eval=True,
            candidate_size=10000,
        )
119
        dataset.set_filelist([dump_a_path, dump_b_path])
120 121 122
        dataset.load_into_memory()
        dataset.local_shuffle()

123 124 125 126 127 128
        paddle.enable_static()

        exe = paddle.static.Executor(paddle.CPUPlace())
        startup_program = paddle.static.Program()
        main_program = paddle.static.Program()
        exe.run(startup_program)
129 130
        for i in range(2):
            try:
131
                exe.train_from_dataset(main_program, dataset)
132 133 134 135 136
            except ImportError as e:
                pass
            except Exception as e:
                self.assertTrue(False)

137
        temp_dir.cleanup()
138

X
xjqbest 已提交
139
    def test_dataset_config(self):
140
        """Testcase for dataset configuration."""
X
xjqbest 已提交
141 142 143 144 145
        dataset = fluid.core.Dataset("MultiSlotDataset")
        dataset.set_thread_num(12)
        dataset.set_filelist(["a.txt", "b.txt", "c.txt"])
        dataset.set_trainer_num(4)
        dataset.set_hdfs_config("my_fs_name", "my_fs_ugi")
146
        dataset.set_download_cmd("./read_from_afs my_fs_name my_fs_ugi")
147
        dataset.set_enable_pv_merge(False)
X
xjqbest 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

        thread_num = dataset.get_thread_num()
        self.assertEqual(thread_num, 12)

        filelist = dataset.get_filelist()
        self.assertEqual(len(filelist), 3)
        self.assertEqual(filelist[0], "a.txt")
        self.assertEqual(filelist[1], "b.txt")
        self.assertEqual(filelist[2], "c.txt")

        trainer_num = dataset.get_trainer_num()
        self.assertEqual(trainer_num, 4)

        name, ugi = dataset.get_hdfs_config()
        self.assertEqual(name, "my_fs_name")
        self.assertEqual(ugi, "my_fs_ugi")

165 166 167 168 169 170 171
        download_cmd = dataset.get_download_cmd()
        self.assertEqual(download_cmd, "./read_from_afs my_fs_name my_fs_ugi")

    def test_set_download_cmd(self):
        """
        Testcase for InMemoryDataset from create to run.
        """
172
        temp_dir = tempfile.TemporaryDirectory()
173 174 175 176 177 178
        filename1 = os.path.join(
            temp_dir.name, "afs:test_in_memory_dataset_run_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "afs:test_in_memory_dataset_run_b.txt"
        )
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        with open(filename1, "w") as f:
            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)
        with open(filename2, "w") as f:
            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 已提交
195 196
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="int64", lod_level=1
197
            )
198 199
            slots_vars.append(var)

200
        dataset = paddle.distributed.InMemoryDataset()
201 202
        dataset.init(
            batch_size=32,
203
            thread_num=2,
204 205 206 207
            pipe_command="cat",
            download_cmd="cat",
            use_var=slots_vars,
        )
208 209
        dataset.set_filelist([filename1, filename2])
        dataset.load_into_memory()
210 211 212 213 214
        paddle.enable_static()

        exe = paddle.static.Executor(paddle.CPUPlace())
        startup_program = paddle.static.Program()
        main_program = paddle.static.Program()
215
        exe = fluid.Executor(fluid.CPUPlace())
216
        exe.run(startup_program)
217
        if self.use_data_loader:
218
            data_loader = fluid.io.DataLoader.from_dataset(
219 220
                dataset, fluid.cpu_places(), self.drop_last
            )
221 222
            for i in range(self.epoch_num):
                for data in data_loader():
223
                    exe.run(main_program, feed=data)
224 225 226
        else:
            for i in range(self.epoch_num):
                try:
227
                    exe.train_from_dataset(main_program, dataset)
228 229 230
                except Exception as e:
                    self.assertTrue(False)

231
        temp_dir.cleanup()
232

X
xjqbest 已提交
233
    def test_in_memory_dataset_run(self):
X
xjqbest 已提交
234
        """
X
xjqbest 已提交
235
        Testcase for InMemoryDataset from create to run.
X
xjqbest 已提交
236
        """
237
        temp_dir = tempfile.TemporaryDirectory()
238 239 240 241 242 243
        filename1 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_run_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_run_b.txt"
        )
244 245

        with open(filename1, "w") as f:
X
xjqbest 已提交
246 247 248 249
            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)
250
        with open(filename2, "w") as f:
X
xjqbest 已提交
251 252 253 254 255 256
            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)

257
        slots = ["slot1", "slot2", "slot3", "slot4"]
X
xjqbest 已提交
258 259
        slots_vars = []
        for slot in slots:
G
GGBond8488 已提交
260 261
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="int64", lod_level=1
262
            )
X
xjqbest 已提交
263 264
            slots_vars.append(var)

265
        dataset = paddle.distributed.InMemoryDataset()
266
        dataset.init(
267
            batch_size=32, thread_num=2, pipe_command="cat", use_var=slots_vars
268
        )
269
        dataset._init_distributed_settings(fea_eval=True, candidate_size=1)
270
        dataset.set_filelist([filename1, filename2])
X
xjqbest 已提交
271
        dataset.load_into_memory()
272
        dataset.slots_shuffle(["slot1"])
X
xjqbest 已提交
273
        dataset.local_shuffle()
274 275
        dataset._set_generate_unique_feasigns(True, 15)
        dataset._generate_local_tables_unlock(0, 11, 1, 25, 15)
X
xjqbest 已提交
276 277
        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(fluid.default_startup_program())
Z
Zeng Jinle 已提交
278
        if self.use_data_loader:
279
            data_loader = fluid.io.DataLoader.from_dataset(
280 281
                dataset, fluid.cpu_places(), self.drop_last
            )
Z
Zeng Jinle 已提交
282 283 284 285 286 287
            for i in range(self.epoch_num):
                for data in data_loader():
                    exe.run(fluid.default_main_program(), feed=data)
        else:
            for i in range(self.epoch_num):
                try:
288 289 290
                    exe.train_from_dataset(
                        fluid.default_main_program(), dataset
                    )
Z
Zeng Jinle 已提交
291 292
                except Exception as e:
                    self.assertTrue(False)
X
xjqbest 已提交
293

294
        temp_dir.cleanup()
X
xjqbest 已提交
295

L
lxsbupt 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    def test_in_memory_dataset_gpugraph_mode(self):
        """
        Testcase for InMemoryDataset in gpugraph mode.
        """
        dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
        dataset.set_feed_type("SlotRecordInMemoryDataFeed")
        graph_config = {
            "walk_len": 24,
            "walk_degree": 10,
            "once_sample_startid_len": 80000,
            "sample_times_one_chunk": 5,
            "window": 3,
            "debug_mode": 0,
            "batch_size": 800,
            "meta_path": "cuid2clk-clk2cuid;cuid2conv-conv2cuid;clk2cuid-cuid2clk;clk2cuid-cuid2conv",
            "gpu_graph_training": 1,
        }
        dataset.set_graph_config(graph_config)
        dataset.set_pass_id(0)
        dataset.get_pass_id()
316
        dataset.get_epoch_finish()
L
lxsbupt 已提交
317

318 319 320 321
    def test_in_memory_dataset_masterpatch(self):
        """
        Testcase for InMemoryDataset from create to run.
        """
322
        temp_dir = tempfile.TemporaryDirectory()
323 324 325 326 327 328
        filename1 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_masterpatch_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_masterpatch_b.txt"
        )
329 330

        with open(filename1, "w") as f:
331 332 333 334 335 336 337 338 339 340
            data = "1 id1 1 1 2 3 3 4 5 5 5 5 1 1\n"
            data += "1 id1 1 2 2 3 4 4 6 6 6 6 1 2\n"
            data += "1 id2 1 1 1 1 1 0 1 0\n"
            data += "1 id3 1 0 1 0 1 1 1 1\n"
            data += "1 id3 1 1 1 1 1 0 1 0\n"
            data += "1 id4 1 0 1 0 1 1 1 1\n"
            data += "1 id4 1 0 1 0 1 1 1 1\n"
            data += "1 id5 1 1 1 1 1 0 1 0\n"
            data += "1 id5 1 1 1 1 1 0 1 0\n"
            f.write(data)
341
        with open(filename2, "w") as f:
342 343 344 345 346 347 348 349 350 351 352 353
            data = "1 id6 1 4 2 3 3 4 5 5 5 5 1 4\n"
            data += "1 id6 1 1 2 3 4 4 6 6 6 6 1 5\n"
            data += "1 id6 1 6 2 3 5 4 7 7 7 7 1 6\n"
            data += "1 id6 1 7 2 3 6 4 8 8 8 8 1 7\n"
            f.write(data)

        slots = ["slot1", "slot2", "slot3", "slot4"]
        slots_vars = []
        train_program = fluid.Program()
        startup_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
            for slot in slots[:2]:
G
GGBond8488 已提交
354 355
                var = paddle.static.data(
                    name=slot, shape=[-1, 1], dtype="int64", lod_level=1
356
                )
357 358
                slots_vars.append(var)
            for slot in slots[2:]:
G
GGBond8488 已提交
359 360
                var = paddle.static.data(
                    name=slot, shape=[-1, 1], dtype="float32", lod_level=1
361
                )
362 363
                slots_vars.append(var)

364
        dataset = paddle.distributed.InMemoryDataset()
365

366
        dataset.init(
367
            batch_size=32, thread_num=2, pipe_command="cat", use_var=slots_vars
368
        )
369
        dataset._init_distributed_settings(parse_ins_id=True)
370 371 372 373 374 375
        dataset.set_filelist(
            [
                "test_in_memory_dataset_masterpatch_a.txt",
                "test_in_memory_dataset_masterpatch_b.txt",
            ]
        )
376 377 378 379 380 381 382 383 384 385 386 387 388 389
        dataset.load_into_memory()
        dataset.local_shuffle()

        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(startup_program)

        for i in range(2):
            try:
                exe.train_from_dataset(train_program, dataset)
            except ImportError as e:
                pass
            except Exception as e:
                self.assertTrue(False)

390
        # dataset._set_merge_by_lineid(2)
391
        dataset.update_settings(merge_size=2)
392
        dataset.dataset.merge_by_lineid()
393
        temp_dir.cleanup()
394

395 396 397 398
    def test_in_memory_dataset_masterpatch1(self):
        """
        Testcase for InMemoryDataset from create to run.
        """
399
        temp_dir = tempfile.TemporaryDirectory()
400 401 402 403 404 405
        filename1 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_masterpatch1_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_masterpatch1_b.txt"
        )
406 407

        with open(filename1, "w") as f:
408 409 410 411 412 413 414 415 416 417
            data = "1 id1 1 1 2 3 3 4 5 5 5 5 1 1\n"
            data += "1 id1 1 2 2 3 4 4 6 6 6 6 1 2\n"
            data += "1 id2 1 1 1 1 1 0 1 0\n"
            data += "1 id3 1 0 1 0 1 1 1 1\n"
            data += "1 id3 1 1 1 1 1 0 1 0\n"
            data += "1 id4 1 0 1 0 1 1 1 1\n"
            data += "1 id4 1 0 1 0 1 1 1 1\n"
            data += "1 id5 1 1 1 1 1 0 1 0\n"
            data += "1 id5 1 1 1 1 1 0 1 0\n"
            f.write(data)
418
        with open(filename2, "w") as f:
419 420 421 422 423 424 425 426 427 428
            data = "1 id6 1 4 2 3 3 4 5 5 5 5 1 4\n"
            data += "1 id6 1 1 2 3 4 4 6 6 6 6 1 5\n"
            data += "1 id6 1 6 2 3 5 4 7 7 7 7 1 6\n"
            data += "1 id6 1 7 2 3 6 4 8 8 8 8 1 7\n"
            f.write(data)

        slots_vars = []
        train_program = fluid.Program()
        startup_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
G
GGBond8488 已提交
429 430
            var1 = paddle.static.data(
                name="slot1", shape=[-1, 1], dtype="int64", lod_level=0
431
            )
G
GGBond8488 已提交
432 433
            var2 = paddle.static.data(
                name="slot2", shape=[-1, 1], dtype="int64", lod_level=0
434
            )
G
GGBond8488 已提交
435 436
            var3 = paddle.static.data(
                name="slot3", shape=[-1, 1], dtype="float32", lod_level=0
437
            )
G
GGBond8488 已提交
438 439
            var4 = paddle.static.data(
                name="slot4", shape=[-1, 1], dtype="float32", lod_level=0
440
            )
441 442
            slots_vars = [var1, var2, var3, var4]

443
        dataset = paddle.distributed.InMemoryDataset()
444
        dataset.init(
445
            batch_size=32, thread_num=2, pipe_command="cat", use_var=slots_vars
446
        )
447
        dataset._init_distributed_settings(parse_ins_id=True)
448 449 450 451 452 453
        dataset.set_filelist(
            [
                "test_in_memory_dataset_masterpatch1_a.txt",
                "test_in_memory_dataset_masterpatch1_b.txt",
            ]
        )
454 455 456 457 458 459 460 461 462 463 464 465 466 467
        dataset.load_into_memory()
        dataset.local_shuffle()

        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(startup_program)

        for i in range(2):
            try:
                exe.train_from_dataset(train_program, dataset)
            except ImportError as e:
                pass
            except Exception as e:
                self.assertTrue(False)

468
        dataset._set_merge_by_lineid(2)
469 470
        dataset.dataset.merge_by_lineid()

471
        temp_dir.cleanup()
472

473 474 475 476 477 478
    def test_in_memory_dataset_run_2(self):
        """
        Testcase for InMemoryDataset from create to run.
        Use CUDAPlace
        Use float type id
        """
479
        temp_dir = tempfile.TemporaryDirectory()
480 481 482 483 484 485
        filename1 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_run_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_run_b.txt"
        )
486 487

        with open(filename1, "w") as f:
488 489 490 491
            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)
492
        with open(filename2, "w") as f:
493 494 495 496 497 498 499 500 501
            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_f", "slot2_f", "slot3_f", "slot4_f"]
        slots_vars = []
        for slot in slots:
G
GGBond8488 已提交
502 503
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="float32", lod_level=1
504
            )
505 506
            slots_vars.append(var)

507
        dataset = paddle.distributed.InMemoryDataset()
508
        dataset.init(
509
            batch_size=32, thread_num=2, pipe_command="cat", use_var=slots_vars
510
        )
511
        dataset.set_filelist([filename1, filename2])
512 513 514
        dataset.load_into_memory()
        dataset.local_shuffle()

515 516 517 518 519
        exe = fluid.Executor(
            fluid.CPUPlace()
            if not core.is_compiled_with_cuda()
            else fluid.CUDAPlace(0)
        )
520
        exe.run(fluid.default_startup_program())
521 522 523 524

        for i in range(2):
            try:
                exe.train_from_dataset(fluid.default_main_program(), dataset)
525 526 527
                # exe.train_from_dataset(
                #     fluid.default_main_program(), dataset, thread=1
                # )
528 529 530
                exe.train_from_dataset(
                    fluid.default_main_program(), dataset, thread=2
                )
531 532 533 534 535 536 537 538 539
                # exe.train_from_dataset(
                #     fluid.default_main_program(), dataset, thread=2
                # )
                # exe.train_from_dataset(
                #     fluid.default_main_program(), dataset, thread=3
                # )
                # exe.train_from_dataset(
                #     fluid.default_main_program(), dataset, thread=4
                # )
540 541 542 543 544
            except ImportError as e:
                pass
            except Exception as e:
                self.assertTrue(False)

Z
Zeng Jinle 已提交
545
        if self.use_data_loader:
546
            data_loader = fluid.io.DataLoader.from_dataset(
547 548
                dataset, fluid.cpu_places(), self.drop_last
            )
Z
Zeng Jinle 已提交
549 550 551 552 553 554
            for i in range(self.epoch_num):
                for data in data_loader():
                    exe.run(fluid.default_main_program(), feed=data)
        else:
            for i in range(self.epoch_num):
                try:
555 556 557
                    exe.train_from_dataset(
                        fluid.default_main_program(), dataset
                    )
Z
Zeng Jinle 已提交
558 559
                except Exception as e:
                    self.assertTrue(False)
560

561 562 563
        dataset._set_merge_by_lineid(2)
        dataset._set_parse_ins_id(False)
        dataset._set_fleet_send_sleep_seconds(2)
564 565 566 567
        dataset.preload_into_memory()
        dataset.wait_preload_done()
        dataset.preload_into_memory(1)
        dataset.wait_preload_done()
568
        dataset.dataset.merge_by_lineid()
569 570
        dataset._set_merge_by_lineid(30)
        dataset._set_parse_ins_id(False)
571 572
        dataset.load_into_memory()
        dataset.dataset.merge_by_lineid()
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
        dataset.update_settings(
            batch_size=1,
            thread_num=2,
            input_type=1,
            pipe_command="cat",
            use_var=[],
            fs_name="",
            fs_ugi="",
            download_cmd="cat",
            merge_size=-1,
            parse_ins_id=False,
            parse_content=False,
            fleet_send_batch_size=2,
            fleet_send_sleep_seconds=2,
            fea_eval=True,
        )
589
        fleet_ptr = fluid.core.Fleet()
590
        fleet_ptr.set_client2client_config(1, 1, 1)
591
        fleet_ptr.get_cache_threshold(0)
592

593
        temp_dir.cleanup()
594

X
xjqbest 已提交
595
    def test_queue_dataset_run(self):
X
xjqbest 已提交
596
        """
X
xjqbest 已提交
597
        Testcase for QueueDataset from create to run.
X
xjqbest 已提交
598
        """
599 600 601 602 603
        temp_dir = tempfile.TemporaryDirectory()
        filename1 = os.path.join(temp_dir.name, "test_queue_dataset_run_a.txt")
        filename2 = os.path.join(temp_dir.name, "test_queue_dataset_run_b.txt")

        with open(filename1, "w") as f:
X
xjqbest 已提交
604 605 606 607
            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)
608
        with open(filename2, "w") as f:
X
xjqbest 已提交
609 610 611 612 613 614
            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)

615
        slots = ["slot1", "slot2", "slot3", "slot4"]
X
xjqbest 已提交
616 617
        slots_vars = []
        for slot in slots:
G
GGBond8488 已提交
618 619
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="int64", lod_level=1
620
            )
X
xjqbest 已提交
621 622
            slots_vars.append(var)

623
        dataset = paddle.distributed.QueueDataset()
624
        dataset.init(
625
            batch_size=32, thread_num=2, pipe_command="cat", use_var=slots_vars
626
        )
627
        dataset.set_filelist([filename1, filename2])
X
xjqbest 已提交
628 629 630

        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(fluid.default_startup_program())
Z
Zeng Jinle 已提交
631
        if self.use_data_loader:
632
            data_loader = fluid.io.DataLoader.from_dataset(
633 634
                dataset, fluid.cpu_places(), self.drop_last
            )
Z
Zeng Jinle 已提交
635 636 637 638 639 640
            for i in range(self.epoch_num):
                for data in data_loader():
                    exe.run(fluid.default_main_program(), feed=data)
        else:
            for i in range(self.epoch_num):
                try:
641 642 643
                    exe.train_from_dataset(
                        fluid.default_main_program(), dataset
                    )
Z
Zeng Jinle 已提交
644 645
                except Exception as e:
                    self.assertTrue(False)
X
xjqbest 已提交
646

647
        dataset2 = paddle.distributed.QueueDataset()
648
        dataset2.init(
649
            batch_size=32, thread_num=2, pipe_command="cat", use_var=slots_vars
650
        )
651
        dataset.set_filelist([])
652 653 654 655 656 657
        # try:
        #    exe.train_from_dataset(fluid.default_main_program(), dataset2)
        # except ImportError as e:
        #    print("warning: we skip trainer_desc_pb2 import problem in windows")
        # except Exception as e:
        #    self.assertTrue(False)
658

659
        temp_dir.cleanup()
X
xjqbest 已提交
660

661 662 663 664 665 666
    def test_queue_dataset_run_2(self):
        """
        Testcase for QueueDataset from create to run.
        Use CUDAPlace
        Use float type id
        """
667 668 669 670 671
        temp_dir = tempfile.TemporaryDirectory()
        filename1 = os.path.join(temp_dir.name, "test_queue_dataset_run_a.txt")
        filename2 = os.path.join(temp_dir.name, "test_queue_dataset_run_b.txt")

        with open(filename1, "w") as f:
672 673 674 675
            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)
676
        with open(filename2, "w") as f:
677 678 679 680 681 682 683 684 685
            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_f", "slot2_f", "slot3_f", "slot4_f"]
        slots_vars = []
        for slot in slots:
G
GGBond8488 已提交
686 687
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="float32", lod_level=1
688
            )
689 690
            slots_vars.append(var)

691
        dataset = paddle.distributed.QueueDataset()
692
        dataset.init(
693
            batch_size=32, thread_num=2, pipe_command="cat", use_var=slots_vars
694
        )
695
        dataset.set_filelist([filename1, filename2])
696

697 698 699 700 701
        exe = fluid.Executor(
            fluid.CPUPlace()
            if not core.is_compiled_with_cuda()
            else fluid.CUDAPlace(0)
        )
702 703
        exe.run(fluid.default_startup_program())
        if self.use_data_loader:
704
            data_loader = fluid.io.DataLoader.from_dataset(
705 706
                dataset, fluid.cpu_places(), self.drop_last
            )
707 708 709 710 711 712
            for i in range(self.epoch_num):
                for data in data_loader():
                    exe.run(fluid.default_main_program(), feed=data)
        else:
            for i in range(self.epoch_num):
                try:
713 714 715
                    exe.train_from_dataset(
                        fluid.default_main_program(), dataset
                    )
716 717 718
                except Exception as e:
                    self.assertTrue(False)

719
        temp_dir.cleanup()
720 721 722 723 724 725 726

    def test_queue_dataset_run_3(self):
        """
        Testcase for QueueDataset from create to run.
        Use CUDAPlace
        Use float type id
        """
727 728 729 730 731
        temp_dir = tempfile.TemporaryDirectory()
        filename1 = os.path.join(temp_dir.name, "test_queue_dataset_run_a.txt")
        filename2 = os.path.join(temp_dir.name, "test_queue_dataset_run_b.txt")

        with open(filename1, "w") as f:
732 733 734 735 736
            data = "2 1 2 2 5 4 2 2 7 2 1 3\n"
            data += "2 6 2 2 1 4 2 2 4 2 2 3\n"
            data += "2 5 2 2 9 9 2 2 7 2 1 3\n"
            data += "2 7 2 2 1 9 2 3 7 2 5 3\n"
            f.write(data)
737
        with open(filename2, "w") as f:
738 739 740 741 742 743 744 745 746
            data = "2 1 2 2 5 4 2 2 7 2 1 3\n"
            data += "2 6 2 2 1 4 2 2 4 2 2 3\n"
            data += "2 5 2 2 9 9 2 2 7 2 1 3\n"
            data += "2 7 2 2 1 9 2 3 7 2 5 3\n"
            f.write(data)

        slots = ["slot1", "slot2", "slot3", "slot4"]
        slots_vars = []
        for slot in slots:
747 748 749
            var = fluid.data(
                name=slot, shape=[None, 1], dtype="int64", lod_level=1
            )
750 751
            slots_vars.append(var)

752
        dataset = paddle.distributed.InMemoryDataset()
753 754 755 756 757 758 759
        dataset.init(
            batch_size=1,
            thread_num=2,
            input_type=1,
            pipe_command="cat",
            use_var=slots_vars,
        )
760
        dataset.set_filelist([filename1, filename2])
761 762
        dataset.load_into_memory()

763 764 765 766 767
        exe = fluid.Executor(
            fluid.CPUPlace()
            if not core.is_compiled_with_cuda()
            else fluid.CUDAPlace(0)
        )
768
        exe.run(fluid.default_startup_program())
Z
Zeng Jinle 已提交
769
        if self.use_data_loader:
770
            data_loader = fluid.io.DataLoader.from_dataset(
771 772
                dataset, fluid.cpu_places(), self.drop_last
            )
Z
Zeng Jinle 已提交
773 774 775 776 777 778
            for i in range(self.epoch_num):
                for data in data_loader():
                    exe.run(fluid.default_main_program(), feed=data)
        else:
            for i in range(self.epoch_num):
                try:
779 780 781
                    exe.train_from_dataset(
                        fluid.default_main_program(), dataset
                    )
Z
Zeng Jinle 已提交
782 783
                except Exception as e:
                    self.assertTrue(False)
784

785
        temp_dir.cleanup()
786

D
danleifeng 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    def test_run_with_inmemory_dataset_train_debug_mode(self):
        """
        Testcase for InMemoryDataset from create to run.
        """

        temp_dir = tempfile.TemporaryDirectory()
        dump_a_path = os.path.join(temp_dir.name, 'test_run_with_dump_a.txt')
        dump_b_path = os.path.join(temp_dir.name, 'test_run_with_dump_b.txt')

        with open(dump_a_path, "w") as f:
            data = "1 a 1 a 1 1 2 3 3 4 5 5 5 5 1 1\n"
            data += "1 b 1 b 1 2 2 3 4 4 6 6 6 6 1 2\n"
            data += "1 c 1 c 1 3 2 3 5 4 7 7 7 7 1 3\n"
            f.write(data)
        with open(dump_b_path, "w") as f:
            data = "1 d 1 d 1 4 2 3 3 4 5 5 5 5 1 4\n"
            data += "1 e 1 e 1 5 2 3 4 4 6 6 6 6 1 5\n"
            data += "1 f 1 f 1 6 2 3 5 4 7 7 7 7 1 6\n"
            data += "1 g 1 g 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 已提交
811 812
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="int64", lod_level=1
813
            )
D
danleifeng 已提交
814 815 816
            slots_vars.append(var)

        dataset = paddle.distributed.InMemoryDataset()
817 818
        dataset.init(
            batch_size=32,
819
            thread_num=2,
820 821 822 823 824 825 826 827 828 829
            pipe_command="cat",
            data_feed_type="SlotRecordInMemoryDataFeed",
            use_var=slots_vars,
        )
        dataset._init_distributed_settings(
            parse_ins_id=True,
            parse_content=True,
            fea_eval=True,
            candidate_size=10000,
        )
D
danleifeng 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
        dataset.set_filelist([dump_a_path, dump_b_path])
        dataset.load_into_memory()

        paddle.enable_static()

        exe = paddle.static.Executor(paddle.CPUPlace())
        startup_program = paddle.static.Program()
        main_program = paddle.static.Program()
        exe.run(startup_program)
        for i in range(2):
            try:
                exe.train_from_dataset(main_program, dataset, debug=True)
            except ImportError as e:
                pass
            except Exception as e:
                self.assertTrue(False)

        temp_dir.cleanup()

L
lxsbupt 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
    def test_cuda_in_memory_dataset_run(self):
        """
        Testcase for cuda inmemory dataset hogwild_worker train to run(barrier).
        """
        temp_dir = tempfile.TemporaryDirectory()
        filename1 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_run_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "test_in_memory_dataset_run_b.txt"
        )

        with open(filename1, "w") as f:
            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)
        with open(filename2, "w") as f:
            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 已提交
876 877
            var = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="int64", lod_level=1
L
lxsbupt 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
            )
            slots_vars.append(var)

        dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
        dataset.set_feed_type("SlotRecordInMemoryDataFeed")
        dataset.set_batch_size(1)
        dataset.set_pipe_command("cat")
        dataset.set_use_var(slots_vars)
        dataset.set_filelist([filename1, filename2])

        graph_config = {
            "walk_len": 24,
            "walk_degree": 10,
            "once_sample_startid_len": 80000,
            "sample_times_one_chunk": 5,
            "window": 3,
            "debug_mode": 0,
            "batch_size": 800,
            "meta_path": "cuid2clk-clk2cuid;cuid2conv-conv2cuid;clk2cuid-cuid2clk;clk2cuid-cuid2conv",
            "gpu_graph_training": 1,
        }
        dataset.set_graph_config(graph_config)
        dataset.set_pass_id(2)
        pass_id = dataset.get_pass_id()

903
        dataset.set_thread(2)
L
lxsbupt 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
        dataset.load_into_memory()

        dataset.get_memory_data_size()

        exe = fluid.Executor(
            fluid.CPUPlace()
            if not core.is_compiled_with_cuda()
            else fluid.CUDAPlace(0)
        )
        exe.run(fluid.default_startup_program())
        for i in range(self.epoch_num):
            try:
                exe.train_from_dataset(fluid.default_main_program(), dataset)
            except Exception as e:
                self.assertTrue(False)
        temp_dir.cleanup()

X
xjqbest 已提交
921

Z
Zeng Jinle 已提交
922
class TestDatasetWithDataLoader(TestDataset):
X
xujiaqi01 已提交
923 924 925 926
    """
    Test Dataset With Data Loader class. TestCases.
    """

Z
Zeng Jinle 已提交
927
    def setUp(self):
X
xujiaqi01 已提交
928 929 930
        """
        Test Dataset With Data Loader, setUp.
        """
Z
Zeng Jinle 已提交
931 932 933 934 935
        self.use_data_loader = True
        self.epoch_num = 10
        self.drop_last = False


936
class TestDatasetWithFetchHandler(unittest.TestCase):
X
xujiaqi01 已提交
937 938 939 940
    """
    Test Dataset With Fetch Handler. TestCases.
    """

941
    def net(self):
X
xujiaqi01 已提交
942 943 944
        """
        Test Dataset With Fetch Handler. TestCases.
        """
945 946 947 948
        slots = ["slot1", "slot2", "slot3", "slot4"]
        slots_vars = []
        poolings = []
        for slot in slots:
G
GGBond8488 已提交
949 950
            data = paddle.static.data(
                name=slot, shape=[-1, 1], dtype="int64", lod_level=1
951
            )
952
            var = paddle.cast(x=data, dtype='float32')
953 954 955
            pool = paddle.static.nn.sequence_lod.sequence_pool(
                input=var, pool_type='AVERAGE'
            )
956 957 958 959

            slots_vars.append(data)
            poolings.append(pool)

960
        concated = paddle.concat(poolings, axis=1)
C
Charles-hit 已提交
961
        fc = paddle.static.nn.fc(x=concated, activation='tanh', size=32)
962 963 964
        return slots_vars, fc

    def get_dataset(self, inputs, files):
X
xujiaqi01 已提交
965 966 967 968 969 970 971
        """
        Test Dataset With Fetch Handler. TestCases.

        Args:
            inputs(list): inputs of get_dataset
            files(list): files of  get_dataset
        """
972
        dataset = paddle.distributed.QueueDataset()
973
        dataset.init(
974
            batch_size=32, thread_num=2, pipe_command="cat", use_var=inputs
975
        )
976 977 978 979
        dataset.set_filelist(files)
        return dataset

    def setUp(self):
X
xujiaqi01 已提交
980 981 982
        """
        Test Dataset With Fetch Handler. TestCases.
        """
983
        self.temp_dir = tempfile.TemporaryDirectory()
984 985 986 987 988 989
        self.filename1 = os.path.join(
            self.temp_dir.name, "test_queue_dataset_run_a.txt"
        )
        self.filename2 = os.path.join(
            self.temp_dir.name, "test_queue_dataset_run_b.txt"
        )
990 991

        with open(self.filename1, "w") as f:
992 993 994 995
            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)
996
        with open(self.filename2, "w") as f:
997 998 999 1000 1001 1002 1003
            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)

    def tearDown(self):
X
xujiaqi01 已提交
1004 1005 1006
        """
        Test Dataset With Fetch Handler. TestCases.
        """
1007
        self.temp_dir.cleanup()
1008 1009

    def test_dataset_none(self):
X
xujiaqi01 已提交
1010 1011 1012
        """
        Test Dataset With Fetch Handler. TestCases.
        """
1013
        slots_vars, out = self.net()
1014
        files = [self.filename1, self.filename2]
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
        dataset = self.get_dataset(slots_vars, files)

        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(fluid.default_startup_program())

        # test dataset->None
        try:
            exe.train_from_dataset(fluid.default_main_program(), None)
        except ImportError as e:
            print("warning: we skip trainer_desc_pb2 import problem in windows")
        except RuntimeError as e:
            error_msg = "dataset is need and should be initialized"
1027
            self.assertEqual(error_msg, str(e))
1028 1029 1030 1031
        except Exception as e:
            self.assertTrue(False)

    def test_infer_from_dataset(self):
X
xujiaqi01 已提交
1032 1033 1034
        """
        Test Dataset With Fetch Handler. TestCases.
        """
1035
        slots_vars, out = self.net()
1036
        files = [self.filename1, self.filename2]
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
        dataset = self.get_dataset(slots_vars, files)

        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(fluid.default_startup_program())

        try:
            exe.infer_from_dataset(fluid.default_main_program(), dataset)
        except ImportError as e:
            print("warning: we skip trainer_desc_pb2 import problem in windows")
        except Exception as e:
            self.assertTrue(False)

1049 1050 1051 1052 1053
    def test_fetch_handler(self):
        """
        Test Dataset With Fetch Handler. TestCases.
        """
        slots_vars, out = self.net()
1054
        files = [self.filename1, self.filename2]
1055 1056 1057 1058 1059 1060 1061 1062 1063
        dataset = self.get_dataset(slots_vars, files)

        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(fluid.default_startup_program())

        fh = fluid.executor.FetchHandler(out.name)
        fh.help()

        try:
1064 1065 1066 1067 1068
            exe.train_from_dataset(
                program=fluid.default_main_program(),
                dataset=dataset,
                fetch_handler=fh,
            )
1069 1070 1071 1072
        except ImportError as e:
            print("warning: we skip trainer_desc_pb2 import problem in windows")
        except RuntimeError as e:
            error_msg = "dataset is need and should be initialized"
1073
            self.assertEqual(error_msg, str(e))
1074 1075 1076
        except Exception as e:
            self.assertTrue(False)

1077

X
xujiaqi01 已提交
1078
class TestDataset2(unittest.TestCase):
1079
    """TestCases for Dataset."""
X
xujiaqi01 已提交
1080 1081

    def setUp(self):
1082
        """TestCases for Dataset."""
X
xujiaqi01 已提交
1083 1084 1085 1086 1087 1088 1089 1090
        self.use_data_loader = False
        self.epoch_num = 10
        self.drop_last = False

    def test_dataset_fleet(self):
        """
        Testcase for InMemoryDataset from create to run.
        """
1091
        temp_dir = tempfile.TemporaryDirectory()
1092 1093 1094 1095 1096 1097
        filename1 = os.path.join(
            temp_dir.name, "test_in_memory_dataset2_run_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "test_in_memory_dataset2_run_b.txt"
        )
1098 1099 1100

        self.skipTest("parameter server will add pslib UT later")

1101
        with open(filename1, "w") as f:
X
xujiaqi01 已提交
1102 1103 1104 1105
            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)
1106
        with open(filename2, "w") as f:
X
xujiaqi01 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115
            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)

        train_program = fluid.Program()
        startup_program = fluid.Program()
        scope = fluid.Scope()
1116
        from paddle.incubate.distributed.fleet.parameter_server.distribute_transpiler import (
1117 1118 1119
            fleet,
        )

X
xujiaqi01 已提交
1120 1121 1122 1123
        with fluid.program_guard(train_program, startup_program):
            slots = ["slot1_ff", "slot2_ff", "slot3_ff", "slot4_ff"]
            slots_vars = []
            for slot in slots:
G
GGBond8488 已提交
1124 1125
                var = paddle.static.data(
                    name=slot, shape=[-1, 1], dtype="float32", lod_level=1
1126
                )
X
xujiaqi01 已提交
1127
                slots_vars.append(var)
1128
            fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1])
1129
            fake_cost = paddle.mean(fake_cost)
X
xujiaqi01 已提交
1130 1131 1132 1133
        with fluid.scope_guard(scope):
            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
            try:
X
xujiaqi01 已提交
1134
                fleet.init()
X
xujiaqi01 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
            except ImportError as e:
                print("warning: no mpi4py")
            adam = fluid.optimizer.Adam(learning_rate=0.000005)
            try:
                adam = fleet.distributed_optimizer(adam)
                adam.minimize([fake_cost], [scope])
            except AttributeError as e:
                print("warning: no mpi")
            except ImportError as e:
                print("warning: no mpi4py")
            exe.run(startup_program)
1146 1147
            dataset = paddle.distributed.InMemoryDataset()

1148 1149
            dataset.init(
                batch_size=32,
1150
                thread_num=2,
1151 1152 1153
                pipe_command="cat",
                use_var=slots_vars,
            )
1154
            dataset.set_filelist([filename1, filename2])
X
xujiaqi01 已提交
1155 1156 1157 1158
            dataset.load_into_memory()
            fleet._opt_info = None
            fleet._fleet_ptr = None

1159
        temp_dir.cleanup()
X
xujiaqi01 已提交
1160 1161 1162 1163 1164

    def test_dataset_fleet2(self):
        """
        Testcase for InMemoryDataset from create to run.
        """
1165
        temp_dir = tempfile.TemporaryDirectory()
1166 1167 1168 1169 1170 1171
        filename1 = os.path.join(
            temp_dir.name, "test_in_memory_dataset2_run2_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "test_in_memory_dataset2_run2_b.txt"
        )
1172 1173

        with open(filename1, "w") as f:
X
xujiaqi01 已提交
1174 1175 1176 1177
            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)
1178
        with open(filename2, "w") as f:
X
xujiaqi01 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187
            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)

        train_program = fluid.Program()
        startup_program = fluid.Program()
        scope = fluid.Scope()
1188 1189 1190
        from paddle.incubate.distributed.fleet.parameter_server.pslib import (
            fleet,
        )
1191

X
xujiaqi01 已提交
1192 1193 1194 1195
        with fluid.program_guard(train_program, startup_program):
            slots = ["slot1_ff", "slot2_ff", "slot3_ff", "slot4_ff"]
            slots_vars = []
            for slot in slots:
G
GGBond8488 已提交
1196 1197
                var = paddle.static.data(
                    name=slot, shape=[-1, 1], dtype="float32", lod_level=1
1198
                )
X
xujiaqi01 已提交
1199
                slots_vars.append(var)
1200
            fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1])
1201
            fake_cost = paddle.mean(fake_cost)
X
xujiaqi01 已提交
1202 1203 1204 1205
        with fluid.scope_guard(scope):
            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
            try:
X
xujiaqi01 已提交
1206
                fleet.init()
X
xujiaqi01 已提交
1207 1208 1209 1210
            except ImportError as e:
                print("warning: no mpi4py")
            adam = fluid.optimizer.Adam(learning_rate=0.000005)
            try:
1211 1212 1213 1214 1215 1216 1217 1218 1219
                adam = fleet.distributed_optimizer(
                    adam,
                    strategy={
                        "fs_uri": "fs_uri_xxx",
                        "fs_user": "fs_user_xxx",
                        "fs_passwd": "fs_passwd_xxx",
                        "fs_hadoop_bin": "fs_hadoop_bin_xxx",
                    },
                )
X
xujiaqi01 已提交
1220 1221 1222 1223 1224 1225
                adam.minimize([fake_cost], [scope])
            except AttributeError as e:
                print("warning: no mpi")
            except ImportError as e:
                print("warning: no mpi4py")
            exe.run(startup_program)
1226
            dataset = paddle.distributed.InMemoryDataset()
1227 1228
            dataset.init(
                batch_size=32,
1229
                thread_num=2,
1230 1231 1232
                pipe_command="cat",
                use_var=slots_vars,
            )
1233
            dataset.set_filelist([filename1, filename2])
X
xujiaqi01 已提交
1234
            dataset.load_into_memory()
X
xujiaqi01 已提交
1235 1236 1237 1238
            try:
                dataset.global_shuffle(fleet)
            except:
                print("warning: catch expected error")
X
xujiaqi01 已提交
1239 1240
            fleet._opt_info = None
            fleet._fleet_ptr = None
1241 1242
            dataset = paddle.distributed.InMemoryDataset()
            dataset.init(fs_name="", fs_ugi="")
1243
            d = paddle.distributed.fleet.DatasetBase()
1244
            try:
1245
                dataset._set_feed_type("MultiSlotInMemoryDataFeed")
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
            except:
                print("warning: catch expected error")
            dataset.thread_num = 0
            try:
                dataset._prepare_to_run()
            except:
                print("warning: catch expected error")
            try:
                dataset.preprocess_instance()
            except:
                print("warning: catch expected error")
            try:
                dataset.set_current_phase(1)
            except:
                print("warning: catch expected error")
            try:
                dataset.postprocess_instance()
            except:
                print("warning: catch expected error")
1265
            dataset._set_fleet_send_batch_size(1024)
1266 1267 1268 1269
            try:
                dataset.global_shuffle()
            except:
                print("warning: catch expected error")
1270
            # dataset.get_pv_data_size()
1271 1272
            dataset.get_memory_data_size()
            dataset.get_shuffle_data_size()
1273
            dataset = paddle.distributed.QueueDataset()
1274 1275 1276 1277 1278 1279 1280 1281
            try:
                dataset.local_shuffle()
            except:
                print("warning: catch expected error")
            try:
                dataset.global_shuffle()
            except:
                print("warning: catch expected error")
1282
            dataset = paddle.distributed.fleet.FileInstantDataset()
1283 1284 1285 1286 1287 1288 1289 1290
            try:
                dataset.local_shuffle()
            except:
                print("warning: catch expected error")
            try:
                dataset.global_shuffle()
            except:
                print("warning: catch expected error")
X
xujiaqi01 已提交
1291

1292
        temp_dir.cleanup()
X
xujiaqi01 已提交
1293

1294 1295 1296 1297
    def test_bosps_dataset_fleet2(self):
        """
        Testcase for InMemoryDataset from create to run.
        """
1298
        temp_dir = tempfile.TemporaryDirectory()
1299 1300 1301 1302 1303 1304
        filename1 = os.path.join(
            temp_dir.name, "test_in_memory_dataset2_run2_a.txt"
        )
        filename2 = os.path.join(
            temp_dir.name, "test_in_memory_dataset2_run2_b.txt"
        )
1305 1306

        with open(filename1, "w") as f:
1307 1308 1309 1310
            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)
1311
        with open(filename2, "w") as f:
1312 1313 1314 1315 1316 1317 1318 1319 1320
            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)

        train_program = fluid.Program()
        startup_program = fluid.Program()
        scope = fluid.Scope()
1321 1322 1323
        from paddle.incubate.distributed.fleet.parameter_server.pslib import (
            fleet,
        )
1324

1325 1326 1327 1328
        with fluid.program_guard(train_program, startup_program):
            slots = ["slot1_ff", "slot2_ff", "slot3_ff", "slot4_ff"]
            slots_vars = []
            for slot in slots:
G
GGBond8488 已提交
1329 1330
                var = paddle.static.data(
                    name=slot, shape=[-1, 1], dtype="float32", lod_level=1
1331
                )
1332
                slots_vars.append(var)
1333
            fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1])
1334
            fake_cost = paddle.mean(fake_cost)
1335 1336 1337 1338 1339 1340 1341 1342 1343
        with fluid.scope_guard(scope):
            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
            try:
                fleet.init()
            except ImportError as e:
                print("warning: no mpi4py")
            adam = fluid.optimizer.Adam(learning_rate=0.000005)
            try:
1344 1345 1346 1347 1348 1349 1350 1351 1352
                adam = fleet.distributed_optimizer(
                    adam,
                    strategy={
                        "fs_uri": "fs_uri_xxx",
                        "fs_user": "fs_user_xxx",
                        "fs_passwd": "fs_passwd_xxx",
                        "fs_hadoop_bin": "fs_hadoop_bin_xxx",
                    },
                )
1353 1354 1355 1356 1357 1358 1359
                adam.minimize([fake_cost], [scope])
            except AttributeError as e:
                print("warning: no mpi")
            except ImportError as e:
                print("warning: no mpi4py")
            exe.run(startup_program)
            dataset = paddle.distributed.fleet.BoxPSDataset()
1360 1361
            dataset.init(
                batch_size=32,
1362
                thread_num=2,
1363 1364 1365
                pipe_command="cat",
                use_var=slots_vars,
            )
1366
            dataset.set_filelist([filename1, filename2])
1367 1368 1369 1370 1371 1372 1373 1374
            dataset.load_into_memory()
            try:
                dataset.global_shuffle(fleet)
            except:
                print("warning: catch expected error")
            fleet._opt_info = None
            fleet._fleet_ptr = None
            dataset = paddle.distributed.fleet.BoxPSDataset()
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
            dataset.init(
                rank_offset="",
                pv_batch_size=1,
                fs_name="",
                fs_ugi="",
                data_feed_type="MultiSlotInMemoryDataFeed",
                parse_logkey=True,
                merge_by_sid=True,
                enable_pv_merge=True,
            )
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
            d = paddle.distributed.fleet.DatasetBase()
            try:
                dataset._set_feed_type("MultiSlotInMemoryDataFeed")
            except:
                print("warning: catch expected error")
            dataset.thread_num = 0
            try:
                dataset._prepare_to_run()
            except:
                print("warning: catch expected error")
            dataset._set_parse_logkey(True)
            dataset._set_merge_by_sid(True)
            dataset._set_enable_pv_merge(True)
            try:
                dataset.preprocess_instance()
            except:
                print("warning: catch expected error")
            try:
                dataset.set_current_phase(1)
            except:
                print("warning: catch expected error")
            try:
                dataset.postprocess_instance()
            except:
                print("warning: catch expected error")
            dataset._set_fleet_send_batch_size(1024)
            try:
                dataset.global_shuffle()
            except:
                print("warning: catch expected error")
1415
            # dataset.get_pv_data_size()
1416 1417
            dataset.get_memory_data_size()
            dataset.get_shuffle_data_size()
1418
        temp_dir.cleanup()
1419

X
xujiaqi01 已提交
1420

X
xjqbest 已提交
1421
if __name__ == '__main__':
X
xjqbest 已提交
1422
    unittest.main()