test_parallel_dygraph_mnist_npu.py 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
# Copyright (c) 2021 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.

from __future__ import print_function

import os
import sys
import unittest
sys.path.append("..")

from test_dist_base import TestDistBase
import paddle.fluid as fluid

flag_name = os.path.splitext(__file__)[0]
rank_table_file = b"""{
    "status": "completed",
    "version": "1.0",
    "server_count": "1",
    "server_list": [
        {
            "server_id": "127.0.0.1",
            "device": [
                {
                    "device_id": "0",
                    "device_ip": "192.1.184.23",
                    "rank_id": "0"
                },
                {
                    "device_id": "1",
                    "device_ip": "192.2.21.93",
                    "rank_id": "1"
                }
            ]
        }
    ]
}"""

need_envs = {
    "ASCEND_AICPU_PATH":
    os.getenv("ASCEND_AICPU_PATH", "/usr/local/Ascend/nnae/latest"),
    "ASCEND_OPP_PATH":
    os.getenv("ASCEND_OPP_PATH", "/usr/local/Ascend/nnae/latest/opp"),
    "HCCL_CONNECT_TIMEOUT": "7200",
    "HCCL_WHITELIST_DISABLE": "1",
    "HCCL_SECURITY_MODE": "1",
    "RANK_TABLE_FILE": "rank_table_file.json",
}


class TestParallelDygraphMnistNPU(TestDistBase):
    def _setup_config(self):
        self._sync_mode = False
        self._hccl_mode = True
        self._dygraph = True
        self._enforce_place = "NPU"

    def test_mnist(self):
        with open("rank_table_file.json", "wb") as f:
            f.write(rank_table_file)
        if fluid.core.is_compiled_with_npu():
            self.check_with_place(
                os.path.abspath('../parallel_dygraph_mnist.py'),
                delta=1e-3,
                check_error_log=True,
                need_envs=need_envs,
                log_name=flag_name)


class TestFleetDygraphMnistNPU(TestParallelDygraphMnistNPU):
    def _setup_config(self):
        self._sync_mode = False
        self._hccl_mode = True
        self._dygraph = True
        self._enforce_place = "NPU"
        self._use_fleet_api = True


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