未验证 提交 8c7c53b3 编写于 作者: Z zhang wenhui 提交者: GitHub

【NPU】Merge ascend GE&distributed code by 0208 from ascendrc (#31957)

* Ascend rc (#30483)

* Fix compilcation on CANN20.1 and older (#30494)

Fix compilcation on CANN20.1 and older

* Add distribution supported (#30578)

Add distribution supported

* Build praser for Hcom* operators (#30627)

Build praser for Hcom* operators

* Pass device_ids info from launch to trainer. (#30632)

Pass device_ids info from launch to trainer

* Add Hccl program group (#30642)

Add Hccl program group

* Add startup bash files of test_ascend_group. (#30645)

Add startup bash files of test_ascend_group

* cleanup (#30646)

cleanup test_ascend_group.py

* [Feature] Build parser to support distributed training (#30658)

[Feature] Build parser to support distributed training

* fix compilation on ascend-20.1 (#30722)

fix compilation on ascend-20.1

* Dev/fix ascend string (#30749)

Dev/fix ascend string

* code style (#30781)

code style

* Merge ascend_optimizer and ascend_parser. (#30776)

Merge ascend_optimizer and ascend_parser.

* Ascendrc add converted op : [range/equal/range/uniform_random/expand/squeeze], fix cast op bug  (#30797)

Ascendrc add converted op : [range/equal/range/uniform_random/expand/squeeze], fix cast op bug

* Add paddle ascend distribution training supported (#30796)

Add paddle ascend distribution training supported

* pass cxx_flags to gloo cmake (#30857)

* Destroy session first. (#30954)

Destroy session first.

* merge

* fix, test=develop

* fix, test=develop

* fix style, test=develop

* fix, test=develop

* fix

* fix log fatal, test=develop

* fix enforce style, test=develop

* fix, test=develop

* fix, test=develop

* fix rccl, test=develop

* fix test, test=develop

* fix, test=develop

* fix, test=develop

* fix, test=develop

* fix node_num, test=develop

* fix ids str, test=develop

* fix ids str, test=develop

* fix ids str, test=develop

* fix, test=develop

* fix, test=develop

* fix, test=develop

* fix, test=develop

* fix, test=develop

* fix, test=develop

* fix, test=develop

* fix, test=develop

* fix style code, test=develop

* fix style code, test=develop

* fix style code, test=develop

* fix style code, test=develop
Co-authored-by: Nhutuxian <hutuxian2011@sina.cn>
Co-authored-by: Ngongweibao <weibao.gong@gmail.com>
Co-authored-by: NVoid Main <voidmain1313113@gmail.com>
Co-authored-by: NLeo Chen <chenqiuliang@baidu.com>
Co-authored-by: Ndingsiyu <18369187719@163.com>
Co-authored-by: NOleNet <olenet@126.com>
上级 1e60a0c4
...@@ -33,6 +33,7 @@ option(WITH_TENSORRT "Compile PaddlePaddle with NVIDIA TensorRT" OFF) ...@@ -33,6 +33,7 @@ option(WITH_TENSORRT "Compile PaddlePaddle with NVIDIA TensorRT" OFF)
option(WITH_XPU "Compile PaddlePaddle with BAIDU KUNLUN XPU" OFF) option(WITH_XPU "Compile PaddlePaddle with BAIDU KUNLUN XPU" OFF)
option(WITH_WIN_DUMP_DBG "Compile with windows core dump debug mode" OFF) option(WITH_WIN_DUMP_DBG "Compile with windows core dump debug mode" OFF)
option(WITH_ASCEND "Compile PaddlePaddle with ASCEND" OFF) option(WITH_ASCEND "Compile PaddlePaddle with ASCEND" OFF)
option(WITH_ASCEND_CXX11 "Compile PaddlePaddle with ASCEND and CXX11 ABI" OFF)
if (WITH_GPU AND WITH_XPU) if (WITH_GPU AND WITH_XPU)
message(FATAL_ERROR "Error when compile GPU and XPU at the same time") message(FATAL_ERROR "Error when compile GPU and XPU at the same time")
endif() endif()
...@@ -57,6 +58,9 @@ if(WITH_MUSL) ...@@ -57,6 +58,9 @@ if(WITH_MUSL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=deprecated-declarations -Wno-deprecated-declarations -Wno-error=pessimizing-move -Wno-error=deprecated-copy") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=deprecated-declarations -Wno-deprecated-declarations -Wno-error=pessimizing-move -Wno-error=deprecated-copy")
endif() endif()
if(WITH_ASCEND AND NOT WITH_ASCEND_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
endif()
if(WIN32) if(WIN32)
option(MSVC_STATIC_CRT "use static C Runtime library by default" ON) option(MSVC_STATIC_CRT "use static C Runtime library by default" ON)
......
...@@ -12,50 +12,47 @@ ...@@ -12,50 +12,47 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
INCLUDE(ExternalProject)
#NOTE: Logic is from
SET(ASCEND_PROJECT "extern_ascend") # https://github.com/mindspore-ai/graphengine/blob/master/CMakeLists.txt
IF((NOT DEFINED ASCEND_VER) OR (NOT DEFINED ASCEND_URL)) if(DEFINED ENV{ASCEND_CUSTOM_PATH})
MESSAGE(STATUS "use pre defined download url") set(ASCEND_DIR $ENV{ASCEND_CUSTOM_PATH})
SET(ASCEND_VER "0.1.1" CACHE STRING "" FORCE) else()
SET(ASCEND_NAME "ascend" CACHE STRING "" FORCE) set(ASCEND_DIR /usr/local/Ascend)
SET(ASCEND_URL "http://paddle-ascend.bj.bcebos.com/ascend.tar.gz" CACHE STRING "" FORCE) endif()
ENDIF()
MESSAGE(STATUS "ASCEND_NAME: ${ASCEND_NAME}, ASCEND_URL: ${ASCEND_URL}") set(ASCEND_DRIVER_DIR ${ASCEND_DIR}/driver/lib64)
SET(ASCEND_SOURCE_DIR "${THIRD_PARTY_PATH}/ascend") set(ASCEND_DRIVER_COMMON_DIR ${ASCEND_DIR}/driver/lib64/common)
SET(ASCEND_DOWNLOAD_DIR "${ASCEND_SOURCE_DIR}/src/${ASCEND_PROJECT}") set(ASCEND_DRIVER_SHARE_DIR ${ASCEND_DIR}/driver/lib64/share)
SET(ASCEND_DST_DIR "ascend") set(ASCEND_RUNTIME_DIR ${ASCEND_DIR}/fwkacllib/lib64)
SET(ASCEND_INSTALL_ROOT "${THIRD_PARTY_PATH}/install") set(ASCEND_ATC_DIR ${ASCEND_DIR}/atc/lib64)
SET(ASCEND_INSTALL_DIR ${ASCEND_INSTALL_ROOT}/${ASCEND_DST_DIR}) set(ASCEND_ACL_DIR ${ASCEND_DIR}/acllib/lib64)
SET(ASCEND_ROOT ${ASCEND_INSTALL_DIR}) set(STATIC_ACL_LIB ${ASCEND_ACL_DIR})
SET(ASCEND_INC_DIR ${ASCEND_ROOT}/include)
SET(ASCEND_LIB_DIR ${ASCEND_ROOT}/lib) set(ASCEND_MS_RUNTIME_PATH ${ASCEND_RUNTIME_DIR} ${ASCEND_ACL_DIR} ${ASCEND_ATC_DIR})
SET(ASCEND_LIB ${ASCEND_LIB_DIR}/libge_runner.so) set(ASCEND_MS_DRIVER_PATH ${ASCEND_DRIVER_DIR} ${ASCEND_DRIVER_COMMON_DIR})
SET(ASCEND_GRAPH_LIB ${ASCEND_LIB_DIR}/libgraph.so) set(ATLAS_RUNTIME_DIR ${ASCEND_DIR}/ascend-toolkit/latest/fwkacllib/lib64)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${ASCEND_ROOT}/lib") set(ATLAS_RUNTIME_INC_DIR ${ASCEND_DIR}/ascend-toolkit/latest/fwkacllib/include)
set(ATLAS_ACL_DIR ${ASCEND_DIR}/ascend-toolkit/latest/acllib/lib64)
INCLUDE_DIRECTORIES(${ASCEND_INC_DIR}) set(ATLAS_ATC_DIR ${ASCEND_DIR}/ascend-toolkit/latest/atc/lib64)
FILE(WRITE ${ASCEND_DOWNLOAD_DIR}/CMakeLists.txt set(ATLAS_MS_RUNTIME_PATH ${ATLAS_RUNTIME_DIR} ${ATLAS_ACL_DIR} ${ATLAS_ATC_DIR})
"PROJECT(ASCEND)\n"
"cmake_minimum_required(VERSION 3.0)\n" set(atlas_graph_lib ${ATLAS_RUNTIME_DIR}/libgraph.so)
"install(DIRECTORY ${ASCEND_NAME}/include ${ASCEND_NAME}/lib \n" set(atlas_ge_runner_lib ${ATLAS_RUNTIME_DIR}/libge_runner.so)
" DESTINATION ${ASCEND_DST_DIR})\n") set(atlas_acl_lib ${ATLAS_RUNTIME_DIR}/libascendcl.so)
ExternalProject_Add( INCLUDE_DIRECTORIES(${ATLAS_RUNTIME_INC_DIR})
${ASCEND_PROJECT}
${EXTERNAL_PROJECT_LOG_ARGS} if(EXISTS ${ATLAS_RUNTIME_INC_DIR}/graph/ascend_string.h)
PREFIX ${ASCEND_SOURCE_DIR} add_definitions(-DPADDLE_WITH_ASCEND_STRING)
DOWNLOAD_DIR ${ASCEND_DOWNLOAD_DIR} endif()
DOWNLOAD_COMMAND wget --no-check-certificate ${ASCEND_URL} -c -q -O ${ASCEND_NAME}.tar.gz
&& tar zxvf ${ASCEND_NAME}.tar.gz ADD_LIBRARY(ascend_ge SHARED IMPORTED GLOBAL)
DOWNLOAD_NO_PROGRESS 1 SET_PROPERTY(TARGET ascend_ge PROPERTY IMPORTED_LOCATION ${atlas_ge_runner_lib})
UPDATE_COMMAND ""
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${ASCEND_INSTALL_ROOT}
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${ASCEND_INSTALL_ROOT}
)
ADD_LIBRARY(ascend SHARED IMPORTED GLOBAL)
SET_PROPERTY(TARGET ascend PROPERTY IMPORTED_LOCATION ${ASCEND_LIB})
ADD_LIBRARY(ascend_graph SHARED IMPORTED GLOBAL) ADD_LIBRARY(ascend_graph SHARED IMPORTED GLOBAL)
SET_PROPERTY(TARGET ascend_graph PROPERTY IMPORTED_LOCATION ${ASCEND_GRAPH_LIB}) SET_PROPERTY(TARGET ascend_graph PROPERTY IMPORTED_LOCATION ${atlas_graph_lib})
ADD_DEPENDENCIES(ascend ascend_graph ${ASCEND_PROJECT})
ADD_LIBRARY(atlas_acl SHARED IMPORTED GLOBAL)
SET_PROPERTY(TARGET atlas_acl PROPERTY IMPORTED_LOCATION ${atlas_acl_lib})
add_custom_target(extern_ascend DEPENDS ascend_ge ascend_graph atlas_acl)
...@@ -32,7 +32,24 @@ cache_third_party(extern_gloo ...@@ -32,7 +32,24 @@ cache_third_party(extern_gloo
TAG ${GLOO_TAG} TAG ${GLOO_TAG}
DIR GLOO_SOURCE_DIR) DIR GLOO_SOURCE_DIR)
ExternalProject_Add( if(WITH_ASCEND)
ExternalProject_Add(
extern_gloo
${EXTERNAL_PROJECT_LOG_ARGS}
${SHALLOW_CLONE}
"${GLOO_DOWNLOAD_CMD}"
PREFIX "${GLOO_PREFIX_DIR}"
SOURCE_DIR "${GLOO_SOURCE_DIR}"
UPDATE_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND mkdir -p ${GLOO_SOURCE_DIR}/build
&& cd ${GLOO_SOURCE_DIR}/build && cmake .. -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} && make
&& mkdir -p ${GLOO_LIBRARY_DIR} ${GLOO_INCLUDE_DIR}/gloo
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${GLOO_SOURCE_DIR}/build/gloo/libgloo.a ${GLOO_LIBRARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory "${GLOO_SOURCE_DIR}/gloo/" "${GLOO_INCLUDE_DIR}/gloo"
)
else()
ExternalProject_Add(
extern_gloo extern_gloo
${EXTERNAL_PROJECT_LOG_ARGS} ${EXTERNAL_PROJECT_LOG_ARGS}
${SHALLOW_CLONE} ${SHALLOW_CLONE}
...@@ -46,7 +63,8 @@ ExternalProject_Add( ...@@ -46,7 +63,8 @@ ExternalProject_Add(
&& mkdir -p ${GLOO_LIBRARY_DIR} ${GLOO_INCLUDE_DIR}/gloo && mkdir -p ${GLOO_LIBRARY_DIR} ${GLOO_INCLUDE_DIR}/gloo
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${GLOO_SOURCE_DIR}/build/gloo/libgloo.a ${GLOO_LIBRARY_DIR} INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${GLOO_SOURCE_DIR}/build/gloo/libgloo.a ${GLOO_LIBRARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory "${GLOO_SOURCE_DIR}/gloo/" "${GLOO_INCLUDE_DIR}/gloo" COMMAND ${CMAKE_COMMAND} -E copy_directory "${GLOO_SOURCE_DIR}/gloo/" "${GLOO_INCLUDE_DIR}/gloo"
) )
endif()
ADD_LIBRARY(gloo STATIC IMPORTED GLOBAL) ADD_LIBRARY(gloo STATIC IMPORTED GLOBAL)
......
...@@ -198,8 +198,13 @@ FUNCTION(build_protobuf TARGET_NAME BUILD_FOR_HOST) ...@@ -198,8 +198,13 @@ FUNCTION(build_protobuf TARGET_NAME BUILD_FOR_HOST)
"-Dprotobuf_MSVC_STATIC_RUNTIME=${MSVC_STATIC_CRT}") "-Dprotobuf_MSVC_STATIC_RUNTIME=${MSVC_STATIC_CRT}")
ENDIF() ENDIF()
if(WITH_ASCEND AND NOT WITH_ASCEND_CXX11)
SET(PROTOBUF_REPOSITORY https://gitee.com/tianjianhe/protobuf.git)
SET(PROTOBUF_TAG v3.8.0)
else()
SET(PROTOBUF_REPOSITORY ${GIT_URL}/protocolbuffers/protobuf.git) SET(PROTOBUF_REPOSITORY ${GIT_URL}/protocolbuffers/protobuf.git)
SET(PROTOBUF_TAG 9f75c5aa851cd877fb0d93ccc31b8567a6706546) SET(PROTOBUF_TAG 9f75c5aa851cd877fb0d93ccc31b8567a6706546)
endif()
cache_third_party(${TARGET_NAME} cache_third_party(${TARGET_NAME}
REPOSITORY ${PROTOBUF_REPOSITORY} REPOSITORY ${PROTOBUF_REPOSITORY}
...@@ -234,7 +239,11 @@ FUNCTION(build_protobuf TARGET_NAME BUILD_FOR_HOST) ...@@ -234,7 +239,11 @@ FUNCTION(build_protobuf TARGET_NAME BUILD_FOR_HOST)
) )
ENDFUNCTION() ENDFUNCTION()
SET(PROTOBUF_VERSION 3.1.0) if(WITH_ASCEND)
SET(PROTOBUF_VERSION 3.8.0)
else()
SET(PROTOBUF_VERSION 3.1.0)
endif()
IF(NOT PROTOBUF_FOUND) IF(NOT PROTOBUF_FOUND)
build_protobuf(extern_protobuf FALSE) build_protobuf(extern_protobuf FALSE)
......
...@@ -16,7 +16,11 @@ INCLUDE(ExternalProject) ...@@ -16,7 +16,11 @@ INCLUDE(ExternalProject)
SET(THREADPOOL_PREFIX_DIR ${THIRD_PARTY_PATH}/threadpool) SET(THREADPOOL_PREFIX_DIR ${THIRD_PARTY_PATH}/threadpool)
SET(THREADPOOL_SOURCE_DIR ${THIRD_PARTY_PATH}/threadpool/src/extern_threadpool) SET(THREADPOOL_SOURCE_DIR ${THIRD_PARTY_PATH}/threadpool/src/extern_threadpool)
SET(THREADPOOL_REPOSITORY ${GIT_URL}/progschj/ThreadPool.git) if(WITH_ASCEND)
SET(THREADPOOL_REPOSITORY https://gitee.com/tianjianhe/ThreadPool.git)
else()
SET(THREADPOOL_REPOSITORY ${GIT_URL}/progschj/ThreadPool.git)
endif()
SET(THREADPOOL_TAG 9a42ec1329f259a5f4881a291db1dcb8f2ad9040) SET(THREADPOOL_TAG 9a42ec1329f259a5f4881a291db1dcb8f2ad9040)
cache_third_party(extern_threadpool cache_third_party(extern_threadpool
......
...@@ -21,6 +21,8 @@ ENDIF() ...@@ -21,6 +21,8 @@ ENDIF()
SET(WARPCTC_PREFIX_DIR ${THIRD_PARTY_PATH}/warpctc) SET(WARPCTC_PREFIX_DIR ${THIRD_PARTY_PATH}/warpctc)
SET(WARPCTC_SOURCE_DIR ${THIRD_PARTY_PATH}/warpctc/src/extern_warpctc) SET(WARPCTC_SOURCE_DIR ${THIRD_PARTY_PATH}/warpctc/src/extern_warpctc)
SET(WARPCTC_INSTALL_DIR ${THIRD_PARTY_PATH}/install/warpctc) SET(WARPCTC_INSTALL_DIR ${THIRD_PARTY_PATH}/install/warpctc)
# in case of low internet speed
#set(WARPCTC_REPOSITORY https://gitee.com/tianjianhe/warp-ctc.git)
set(WARPCTC_REPOSITORY ${GIT_URL}/baidu-research/warp-ctc.git) set(WARPCTC_REPOSITORY ${GIT_URL}/baidu-research/warp-ctc.git)
set(WARPCTC_TAG c690fc5755abbdbdc98ef78d51ec10a6748a8cd1) set(WARPCTC_TAG c690fc5755abbdbdc98ef78d51ec10a6748a8cd1)
...@@ -41,7 +43,42 @@ cache_third_party(extern_warpctc ...@@ -41,7 +43,42 @@ cache_third_party(extern_warpctc
TAG ${WARPCTC_TAG} TAG ${WARPCTC_TAG}
DIR WARPCTC_SOURCE_DIR) DIR WARPCTC_SOURCE_DIR)
ExternalProject_Add( if(WITH_ASCEND)
ExternalProject_Add(
extern_warpctc
${EXTERNAL_PROJECT_LOG_ARGS}
${SHALLOW_CLONE}
"${WARPCTC_DOWNLOAD_CMD}"
PREFIX ${WARPCTC_PREFIX_DIR}
SOURCE_DIR ${WARPCTC_SOURCE_DIR}
#UPDATE_COMMAND ""
PATCH_COMMAND ""
BUILD_ALWAYS 1
CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
-DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}
"-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
-DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
-DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
-DCMAKE_INSTALL_PREFIX=${WARPCTC_INSTALL_DIR}
-DWITH_GPU=${WITH_GPU}
-DWITH_ROCM=${WITH_ROCM}
-DWITH_OMP=${USE_OMP}
-DWITH_TORCH=OFF
-DCMAKE_DISABLE_FIND_PACKAGE_Torch=ON
-DBUILD_SHARED=ON
-DBUILD_TESTS=OFF
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
${EXTERNAL_OPTIONAL_ARGS}
CMAKE_CACHE_ARGS -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
-DCMAKE_INSTALL_PREFIX:PATH=${WARPCTC_INSTALL_DIR}
)
else()
ExternalProject_Add(
extern_warpctc extern_warpctc
${EXTERNAL_PROJECT_LOG_ARGS} ${EXTERNAL_PROJECT_LOG_ARGS}
${SHALLOW_CLONE} ${SHALLOW_CLONE}
...@@ -73,7 +110,10 @@ ExternalProject_Add( ...@@ -73,7 +110,10 @@ ExternalProject_Add(
CMAKE_CACHE_ARGS -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE} CMAKE_CACHE_ARGS -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
-DCMAKE_INSTALL_PREFIX:PATH=${WARPCTC_INSTALL_DIR} -DCMAKE_INSTALL_PREFIX:PATH=${WARPCTC_INSTALL_DIR}
) )
endif()
IF(WIN32) IF(WIN32)
SET(WARPCTC_LIBRARIES "${WARPCTC_INSTALL_DIR}/bin/warpctc${CMAKE_SHARED_LIBRARY_SUFFIX}" SET(WARPCTC_LIBRARIES "${WARPCTC_INSTALL_DIR}/bin/warpctc${CMAKE_SHARED_LIBRARY_SUFFIX}"
CACHE FILEPATH "Warp-ctc Library" FORCE) CACHE FILEPATH "Warp-ctc Library" FORCE)
......
...@@ -42,5 +42,5 @@ cc_library(heter_wrapper SRCS heter_wrapper.cc DEPS framework_proto device_conte ...@@ -42,5 +42,5 @@ cc_library(heter_wrapper SRCS heter_wrapper.cc DEPS framework_proto device_conte
cc_test(test_fleet_cc SRCS test_fleet.cc DEPS fleet_wrapper gloo_wrapper fs shell) cc_test(test_fleet_cc SRCS test_fleet.cc DEPS fleet_wrapper gloo_wrapper fs shell)
if(WITH_ASCEND) if(WITH_ASCEND)
cc_library(ascend_wrapper SRCS ascend_wrapper.cc DEPS framework_proto lod_tensor ascend ascend_graph) cc_library(ascend_wrapper SRCS ascend_wrapper.cc DEPS framework_proto lod_tensor ascend_ge ascend_graph)
endif(WITH_ASCEND) endif(WITH_ASCEND)
...@@ -37,25 +37,50 @@ limitations under the License. */ ...@@ -37,25 +37,50 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace framework { namespace framework {
// typedef std::vector<std::string> AscendGraphDesc;
typedef ge::Graph AscendGraphDesc; typedef ge::Graph AscendGraphDesc;
#ifdef PADDLE_WITH_ASCEND_STRING
using AscendString = ge::AscendString;
#else
using AscendString = std::string;
#endif
class AscendInstance { class AscendInstance {
public: public:
virtual ~AscendInstance() {} virtual ~AscendInstance() {}
AscendInstance() {} AscendInstance() {}
std::map<std::string, std::string> GetDefaultInitSessionOptions() { std::map<AscendString, AscendString> _GetDefaultInitOptions() {
std::map<std::string, std::string> init_options; std::map<AscendString, AscendString> init_options;
init_options["a"] = "b"; init_options["ge.exec.deviceId"] = "0";
init_options["ge.trainFlag"] = "1"; init_options["ge.graphRunMode"] = "1";
return init_options;
}
std::map<AscendString, AscendString> _GetDefaultInitSessionOptions() {
std::map<AscendString, AscendString> init_options;
// init_options["a"] = "b";
// init_options["ge.trainFlag"] = "1";
return init_options; return init_options;
} }
// add other parameters here to init ge::Status InitGEForUT() {
return ge::GEInitialize(_GetDefaultInitOptions());
}
void InitGlobalResouces() { void InitGlobalResouces() {
session_.reset(new ge::Session(GetDefaultInitSessionOptions())); LOG(INFO) << "Begin ascend InitGlobalResouces";
VLOG(1) << "InitGlobalResouces Done"; session_.reset(new ge::Session(_GetDefaultInitSessionOptions()));
if (session_ == nullptr) {
PADDLE_THROW(platform::errors::Fatal("new session error: nullptr"));
}
LOG(INFO) << "End ascend InitGlobalResouces";
}
void DestroyGlobalResouces() {
LOG(INFO) << "Begin ascend DestroyGlobalResouces";
session_ = nullptr;
LOG(INFO) << "Begin ascend DestroyGlobalResouces";
} }
static std::shared_ptr<AscendInstance> GetInstance() { static std::shared_ptr<AscendInstance> GetInstance() {
...@@ -178,6 +203,6 @@ class AscendInstance { ...@@ -178,6 +203,6 @@ class AscendInstance {
private: private:
static std::shared_ptr<AscendInstance> ascend_instance_; static std::shared_ptr<AscendInstance> ascend_instance_;
}; };
} // end namespace framework } // namespace framework
} // end namespace paddle } // namespace paddle
#endif #endif
...@@ -33,6 +33,8 @@ if (WITH_GPU OR WITH_ROCM) ...@@ -33,6 +33,8 @@ if (WITH_GPU OR WITH_ROCM)
set(AllocatorFacadeDeps gpu_info cuda_allocator pinned_allocator cuda_device_guard thread_local_allocator) set(AllocatorFacadeDeps gpu_info cuda_allocator pinned_allocator cuda_device_guard thread_local_allocator)
elseif(WITH_XPU) elseif(WITH_XPU)
set(AllocatorFacadeDeps xpu_info) set(AllocatorFacadeDeps xpu_info)
elseif(WITH_ASCEND)
set(AllocatorFacadeDeps ascend_npu_info)
else () else ()
set(AllocatorFacadeDeps) set(AllocatorFacadeDeps)
endif() endif()
......
...@@ -19,6 +19,12 @@ if(WITH_NCCL OR WITH_RCCL) ...@@ -19,6 +19,12 @@ if(WITH_NCCL OR WITH_RCCL)
op_library(gen_nccl_id_op DEPS ${COLLECTIVE_DEPS}) op_library(gen_nccl_id_op DEPS ${COLLECTIVE_DEPS})
endif() endif()
if(WITH_ASCEND)
op_library(gen_nccl_id_op)
op_library(c_gen_nccl_id_op)
endif()
if(WITH_GLOO) if(WITH_GLOO)
set(COLLECTIVE_DEPS ${COLLECTIVE_DEPS} gloo_wrapper) set(COLLECTIVE_DEPS ${COLLECTIVE_DEPS} gloo_wrapper)
endif() endif()
......
...@@ -27,6 +27,7 @@ limitations under the License. */ ...@@ -27,6 +27,7 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
static void GenNCCLID(std::vector<ncclUniqueId>* nccl_ids) { static void GenNCCLID(std::vector<ncclUniqueId>* nccl_ids) {
for (size_t i = 0; i < nccl_ids->size(); ++i) { for (size_t i = 0; i < nccl_ids->size(); ++i) {
PADDLE_ENFORCE_CUDA_SUCCESS( PADDLE_ENFORCE_CUDA_SUCCESS(
...@@ -84,6 +85,21 @@ class CGenNCCLIdOp : public framework::OperatorBase { ...@@ -84,6 +85,21 @@ class CGenNCCLIdOp : public framework::OperatorBase {
} }
}; };
#else
class CGenNCCLIdOp : public framework::OperatorBase {
public:
CGenNCCLIdOp(const std::string& type,
const framework::VariableNameMap& inputs,
const framework::VariableNameMap& outputs,
const framework::AttributeMap& attrs)
: OperatorBase(type, inputs, outputs, attrs) {}
void RunImpl(const framework::Scope& scope,
const platform::Place& dev_place) const override {}
};
#endif
class CGenNCCLIdOpMaker : public framework::OpProtoAndCheckerMaker { class CGenNCCLIdOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
......
...@@ -34,6 +34,7 @@ class Scope; ...@@ -34,6 +34,7 @@ class Scope;
namespace paddle { namespace paddle {
namespace operators { namespace operators {
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
static void GenNCCLID(std::vector<ncclUniqueId>* nccl_ids) { static void GenNCCLID(std::vector<ncclUniqueId>* nccl_ids) {
for (size_t i = 0; i < nccl_ids->size(); ++i) { for (size_t i = 0; i < nccl_ids->size(); ++i) {
PADDLE_ENFORCE_CUDA_SUCCESS( PADDLE_ENFORCE_CUDA_SUCCESS(
...@@ -194,6 +195,20 @@ class GenNCCLIdOp : public framework::OperatorBase { ...@@ -194,6 +195,20 @@ class GenNCCLIdOp : public framework::OperatorBase {
} }
}; };
#else
class GenNCCLIdOp : public framework::OperatorBase {
public:
GenNCCLIdOp(const std::string& type, const framework::VariableNameMap& inputs,
const framework::VariableNameMap& outputs,
const framework::AttributeMap& attrs)
: OperatorBase(type, inputs, outputs, attrs) {}
void RunImpl(const framework::Scope& scope,
const platform::Place& dev_place) const override {}
};
#endif
class GenNCCLIdOpMaker : public framework::OpProtoAndCheckerMaker { class GenNCCLIdOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
......
...@@ -10,6 +10,12 @@ ELSE() ...@@ -10,6 +10,12 @@ ELSE()
set(XPU_CTX_DEPS) set(XPU_CTX_DEPS)
endif(WITH_XPU) endif(WITH_XPU)
if(WITH_ASCEND)
set(ASCEND_DEPS xpulib)
ELSE()
set(ASCEND_DEPS)
endif(WITH_ASCEND)
if (WITH_PYTHON) if (WITH_PYTHON)
py_proto_compile(profiler_py_proto SRCS profiler.proto) py_proto_compile(profiler_py_proto SRCS profiler.proto)
add_custom_target(profiler_py_proto_init ALL COMMAND ${CMAKE_COMMAND} -E touch __init__.py) add_custom_target(profiler_py_proto_init ALL COMMAND ${CMAKE_COMMAND} -E touch __init__.py)
...@@ -66,6 +72,10 @@ if(WITH_XPU) ...@@ -66,6 +72,10 @@ if(WITH_XPU)
cc_library(xpu_info SRCS xpu_info.cc DEPS gflags glog enforce xpulib) cc_library(xpu_info SRCS xpu_info.cc DEPS gflags glog enforce xpulib)
endif() endif()
if(WITH_ASCEND)
cc_library(ascend_npu_info SRCS ascend_npu_info.cc DEPS gflags glog enforce atlas_acl)
endif()
add_subdirectory(dynload) add_subdirectory(dynload)
add_subdirectory(stream) add_subdirectory(stream)
......
/* 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. */
#include "paddle/fluid/platform/ascend_npu_info.h"
#include <glog/logging.h>
#include "acl/acl_rt.h"
namespace paddle {
namespace platform {
namespace ascend {
int NPUDevice::GetDeviceCount() {
uint32_t count = 0;
aclError status = aclrtGetDeviceCount(&count);
if (status != 0) {
PADDLE_THROW(platform::errors::InvalidArgument(
"aclrtGetDeviceCount error code: %d", status));
return -1;
}
return count;
}
} // namespace ascend
} // namespace platform
} // namespace paddle
/* 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. */
#pragma once
#ifdef PADDLE_WITH_ASCEND
namespace paddle {
namespace platform {
namespace ascend {
class NPUDevice {
public:
//! Get the total number of XPU devices in system.
static int GetDeviceCount();
};
} // namespace ascend
} // namespace platform
} // namespace paddle
#endif
...@@ -25,6 +25,7 @@ namespace pybind { ...@@ -25,6 +25,7 @@ namespace pybind {
void BindAscendGraph(py::module* m); void BindAscendGraph(py::module* m);
void BindAscendWrapper(py::module* m); void BindAscendWrapper(py::module* m);
void BindAscendDevice(py::module* m);
} // namespace pybind } // namespace pybind
} // namespace paddle } // namespace paddle
......
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string> #include <string>
#ifndef _WIN32
#include <unistd.h>
#endif
#include "paddle/fluid/framework/op_info.h" #include "paddle/fluid/framework/op_info.h"
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
...@@ -23,6 +26,9 @@ ...@@ -23,6 +26,9 @@
#include "paddle/fluid/framework/variable.h" #include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/pybind/pybind.h" #include "paddle/fluid/pybind/pybind.h"
#include "paddle/fluid/string/string_helper.h" #include "paddle/fluid/string/string_helper.h"
#ifdef PADDLE_WITH_ASCEND
#include "paddle/fluid/framework/fleet/ascend_wrapper.h"
#endif
// NOTE(zhiqiu): Commonly, the inputs in auto-generated OP function are // NOTE(zhiqiu): Commonly, the inputs in auto-generated OP function are
// determined by the OP`s proto automatically, i.e., all the inputs registered // determined by the OP`s proto automatically, i.e., all the inputs registered
...@@ -561,6 +567,11 @@ int main(int argc, char* argv[]) { ...@@ -561,6 +567,11 @@ int main(int argc, char* argv[]) {
return -1; return -1;
} }
#ifdef PADDLE_WITH_ASCEND
auto ascend_ptr = paddle::framework::AscendInstance::GetInstance();
ascend_ptr->InitGEForUT();
#endif
std::vector<std::string> headers{"\"paddle/fluid/imperative/tracer.h\""}; std::vector<std::string> headers{"\"paddle/fluid/imperative/tracer.h\""};
std::ofstream out(argv[1], std::ios::out); std::ofstream out(argv[1], std::ios::out);
...@@ -590,5 +601,9 @@ int main(int argc, char* argv[]) { ...@@ -590,5 +601,9 @@ int main(int argc, char* argv[]) {
<< "} // namespace paddle\n"; << "} // namespace paddle\n";
out.close(); out.close();
#ifdef PADDLE_WITH_ASCEND
ge::GEFinalize();
#endif
return 0; return 0;
} }
...@@ -143,6 +143,14 @@ bool IsCompiledWithROCM() { ...@@ -143,6 +143,14 @@ bool IsCompiledWithROCM() {
#endif #endif
} }
bool IsCompiledWithAscend() {
#ifndef PADDLE_WITH_ASCEND
return false;
#else
return true;
#endif
}
bool IsCompiledWithXPU() { bool IsCompiledWithXPU() {
#ifndef PADDLE_WITH_XPU #ifndef PADDLE_WITH_XPU
return false; return false;
...@@ -1756,6 +1764,7 @@ All parameter, weight, gradient are variables in Paddle. ...@@ -1756,6 +1764,7 @@ All parameter, weight, gradient are variables in Paddle.
m.def("init_devices", []() { framework::InitDevices(); }); m.def("init_devices", []() { framework::InitDevices(); });
m.def("is_compiled_with_cuda", IsCompiledWithCUDA); m.def("is_compiled_with_cuda", IsCompiledWithCUDA);
m.def("is_compiled_with_ascend", IsCompiledWithAscend);
m.def("is_compiled_with_rocm", IsCompiledWithROCM); m.def("is_compiled_with_rocm", IsCompiledWithROCM);
m.def("is_compiled_with_xpu", IsCompiledWithXPU); m.def("is_compiled_with_xpu", IsCompiledWithXPU);
m.def("is_compiled_with_mkldnn", IsCompiledWithMKLDNN); m.def("is_compiled_with_mkldnn", IsCompiledWithMKLDNN);
...@@ -2885,6 +2894,7 @@ All parameter, weight, gradient are variables in Paddle. ...@@ -2885,6 +2894,7 @@ All parameter, weight, gradient are variables in Paddle.
#ifdef PADDLE_WITH_ASCEND #ifdef PADDLE_WITH_ASCEND
BindAscendWrapper(&m); BindAscendWrapper(&m);
BindAscendGraph(&m); BindAscendGraph(&m);
BindAscendDevice(&m);
#endif #endif
#ifdef PADDLE_WITH_CRYPTO #ifdef PADDLE_WITH_CRYPTO
BindCrypto(&m); BindCrypto(&m);
......
...@@ -37,6 +37,17 @@ init = fleet.init ...@@ -37,6 +37,17 @@ init = fleet.init
is_first_worker = fleet.is_first_worker is_first_worker = fleet.is_first_worker
worker_index = fleet.worker_index worker_index = fleet.worker_index
worker_num = fleet.worker_num worker_num = fleet.worker_num
node_num = fleet.node_num
rank = fleet.worker_index
nranks = fleet.worker_num
world_size = fleet.worker_num
# device id in current trainer
local_device_ids = fleet.local_device_ids
# device ids in world
world_device_ids = fleet.world_device_ids
# rank in node
local_rank = fleet.local_rank
rank_in_node = local_rank
is_worker = fleet.is_worker is_worker = fleet.is_worker
worker_endpoints = fleet.worker_endpoints worker_endpoints = fleet.worker_endpoints
server_num = fleet.server_num server_num = fleet.server_num
......
...@@ -289,6 +289,18 @@ class Fleet(object): ...@@ -289,6 +289,18 @@ class Fleet(object):
""" """
return self._role_maker._worker_num() return self._role_maker._worker_num()
def node_num(self):
return self._role_maker._get_node_num()
def local_rank(self):
return self._role_maker._get_local_rank()
def local_device_ids(self):
return self._role_maker._get_local_device_ids()
def world_device_ids(self):
return self._role_maker._get_world_device_ids()
def is_worker(self): def is_worker(self):
""" """
Check whether the node is an instance of worker. Check whether the node is an instance of worker.
......
...@@ -622,6 +622,29 @@ class PaddleCloudRoleMaker(RoleMakerBase): ...@@ -622,6 +622,29 @@ class PaddleCloudRoleMaker(RoleMakerBase):
self._generate_role() self._generate_role()
return self._nodes_num return self._nodes_num
def _get_node_num(self):
"""
return the training node number
"""
if not self._role_is_generated:
self._generate_role()
return self._nodes_num
def _get_local_rank(self):
if not self._role_is_generated:
self._generate_role()
return self._local_rank
def _get_local_device_ids(self):
if not self._role_is_generated:
self._generate_role()
return self._local_device_ids
def _get_world_device_ids(self):
if not self._role_is_generated:
self._generate_role()
return self._world_device_ids
def _get_trainer_endpoints(self): def _get_trainer_endpoints(self):
""" """
get endpoint of all trainers get endpoint of all trainers
...@@ -782,6 +805,9 @@ class PaddleCloudRoleMaker(RoleMakerBase): ...@@ -782,6 +805,9 @@ class PaddleCloudRoleMaker(RoleMakerBase):
self._trainers_num = len(self._worker_endpoints) self._trainers_num = len(self._worker_endpoints)
self._nodes_num = len( self._nodes_num = len(
set([x.split(':')[0] for x in self._worker_endpoints])) set([x.split(':')[0] for x in self._worker_endpoints]))
self._local_rank = os.getenv("PADDLE_RANK_IN_NODE")
self._local_device_ids = os.getenv("PADDLE_LOCAL_DEVICE_IDS")
self._world_device_ids = os.getenv("PADDLE_WORLD_DEVICE_IDS")
def _gloo_init(self): def _gloo_init(self):
# PADDLE_WITH_GLOO 1: trainer barrier, 2: all barrier # PADDLE_WITH_GLOO 1: trainer barrier, 2: all barrier
......
...@@ -108,6 +108,21 @@ see: http://www.paddlepaddle.org/documentation/docs/zh/1.6/user_guides/howto/tra ...@@ -108,6 +108,21 @@ see: http://www.paddlepaddle.org/documentation/docs/zh/1.6/user_guides/howto/tra
"In gpu training, it should be less or equal to the gpus number of you system(or you set by --gpus). And so each process can" "In gpu training, it should be less or equal to the gpus number of you system(or you set by --gpus). And so each process can"
" bound to one or average number of gpus.") " bound to one or average number of gpus.")
base_group.add_argument(
"--run_mode",
type=str,
default="collective",
help="run mode of job, can be:collective/ps/ps-heter")
base_group.add_argument(
"--ascend_npus",
type=str,
default=None,
help="It's for ascend npu training."
"For example:"
"--ascend_npus=\"0,1,2,3\" will launch four training processes each bound to one gpu."
)
if fluid.core.is_compiled_with_cuda(): if fluid.core.is_compiled_with_cuda():
base_group.add_argument( base_group.add_argument(
"--gpus", "--gpus",
...@@ -243,6 +258,9 @@ def launch_collective(args): ...@@ -243,6 +258,9 @@ def launch_collective(args):
log_dir=args.log_dir, log_dir=args.log_dir,
envs=global_envs) envs=global_envs)
for idx, proc in enumerate(procs):
print("launch proc_id:{} idx:{}".format(proc.proc.pid, idx))
while True: while True:
alive = watch_local_trainers(procs, cluster.trainers_nranks()) alive = watch_local_trainers(procs, cluster.trainers_nranks())
...@@ -276,6 +294,16 @@ def launch_ps(args, distribute_mode): ...@@ -276,6 +294,16 @@ def launch_ps(args, distribute_mode):
def which_distributed_mode(args): def which_distributed_mode(args):
if args.run_mode is not None:
assert args.run_mode in ["collective", "ps", "ps-heter"]
if args.run_mode == "collective":
return DistributeMode.COLLECTIVE
elif args.run_mode == "ps":
return DistributeMode.PS
elif args.run_mode == "ps-heter":
return DistributeMode.PS_HETER
ps_args = [ ps_args = [
'--worker_num', '--server_num', '--heter_worker_num', '--servers', '--worker_num', '--server_num', '--heter_worker_num', '--servers',
'--workers', '--heter_workers', '--http_port' '--workers', '--heter_workers', '--http_port'
...@@ -298,24 +326,26 @@ def which_distributed_mode(args): ...@@ -298,24 +326,26 @@ def which_distributed_mode(args):
) )
if fluid.core.is_compiled_with_cuda(): if fluid.core.is_compiled_with_cuda():
device_count = fluid.core.get_cuda_device_count() accelerators = fluid.core.get_cuda_device_count()
elif fluid.core.is_compiled_with_ascend():
accelerators = fluid.core.NPUDevice.get_device_count()
elif fluid.core.is_compiled_with_xpu(): elif fluid.core.is_compiled_with_xpu():
device_count = fluid.core.get_xpu_device_count() accelerators = fluid.core.get_xpu_device_count()
else: else:
device_count = 0 accelerators = 0
if len(has_ps_args) > 0: if len(has_ps_args) > 0:
logger.info( logger.info(
"Run parameter-sever mode. pserver arguments:{}, cuda or xpu count:{}". "Run parameter-sever mode. pserver arguments:{}, accelerators count:{}".
format(has_ps_args, device_count)) format(has_ps_args, accelerators))
has_ps_heter_args = list(set(has_ps_args) & set(ps_heter_args)) has_ps_heter_args = list(set(has_ps_args) & set(ps_heter_args))
if len(has_ps_heter_args) > 0: if len(has_ps_heter_args) > 0:
return DistributeMode.PS_HETER return DistributeMode.PS_HETER
else: else:
return DistributeMode.PS return DistributeMode.PS
elif len(has_collective_args) > 0: elif len(has_collective_args) > 0:
logger.info("Run collective gpu mode. gpu arguments:{}, cuda count:{}". logger.info("Run collective mode. gpu arguments:{}, cuda count:{}".
format(has_collective_args, device_count)) format(has_collective_args, accelerators))
return DistributeMode.COLLECTIVE return DistributeMode.COLLECTIVE
else: else:
if not fluid.core.is_compiled_with_cuda( if not fluid.core.is_compiled_with_cuda(
......
...@@ -52,6 +52,8 @@ class DeviceMode(): ...@@ -52,6 +52,8 @@ class DeviceMode():
GPU = 1 GPU = 1
KUNLUN = 2 KUNLUN = 2
XPU = 2 XPU = 2
ASCEND_NPU = 3
UNKNOWN = 3
class Cluster(object): class Cluster(object):
...@@ -98,6 +100,14 @@ class Cluster(object): ...@@ -98,6 +100,14 @@ class Cluster(object):
r.append(t.endpoint) r.append(t.endpoint)
return r return r
def world_device_ids(self):
r = []
for pod in self.pods:
for t in pod.trainers:
str_accelerators = [str(acc) for acc in t.accelerators]
r.append(str_accelerators)
return r
def pods_endpoints(self): def pods_endpoints(self):
r = [] r = []
for pod in self.pods: for pod in self.pods:
...@@ -105,7 +115,6 @@ class Cluster(object): ...@@ -105,7 +115,6 @@ class Cluster(object):
assert pod.port != None and pod.addr != None, "{} not a valid endpoint".format( assert pod.port != None and pod.addr != None, "{} not a valid endpoint".format(
ep) ep)
r.append(ep) r.append(ep)
return r return r
def get_pod_by_id(self, pod_id): def get_pod_by_id(self, pod_id):
...@@ -132,23 +141,23 @@ class JobServer(object): ...@@ -132,23 +141,23 @@ class JobServer(object):
class Trainer(object): class Trainer(object):
def __init__(self): def __init__(self):
self.gpus = [] self.accelerators = []
self.endpoint = None self.endpoint = None
self.rank = None self.rank = None
def __str__(self): def __str__(self):
return "gpu:{} endpoint:{} rank:{}".format(self.gpus, self.endpoint, return "accelerator:{} endpoint:{} rank:{}".format(
self.rank) self.accelerators, self.endpoint, self.rank)
def __eq__(self, t): def __eq__(self, t):
if len(self.gpus) != len(t.gpus): if len(self.accelerators) != len(t.accelerators):
return False return False
if self.endpoint != t.endpoint or \ if self.endpoint != t.endpoint or \
self.rank != t.rank: self.rank != t.rank:
return False return False
for a, b in zip(self.gpus, t.gpus): for a, b in zip(self.accelerators, t.accelerators):
if a != b: if a != b:
return False return False
...@@ -171,12 +180,13 @@ class Pod(object): ...@@ -171,12 +180,13 @@ class Pod(object):
self.servers = [] self.servers = []
self.workers = [] self.workers = []
self.heter_workers = [] self.heter_workers = []
self.gpus = [] self.accelerators = []
self.device_mode = None
def __str__(self): def __str__(self):
return "rank:{} id:{} addr:{} port:{} visible_gpu:{} trainers:{} servers:{} \ return "rank:{} id:{} addr:{} port:{} visible_accelerator:{} trainers:{} servers:{} \
workers:{} heter_workers:{}".format( workers:{} heter_workers:{}".format(
self.rank, self.id, self.addr, self.port, self.gpus, [ self.rank, self.id, self.addr, self.port, self.accelerators, [
str(t) for t in self.trainers str(t) for t in self.trainers
], [str(s) for s in self.servers], [str(w) for w in self.workers], ], [str(s) for s in self.servers], [str(w) for w in self.workers],
[str(h) for h in self.heter_workers]) [str(h) for h in self.heter_workers])
...@@ -231,12 +241,12 @@ class Pod(object): ...@@ -231,12 +241,12 @@ class Pod(object):
def rank(self): def rank(self):
return self.rank return self.rank
def get_visible_gpus(self): def get_visible_accelerators(self):
r = "" r = ""
for g in self.gpus: for g in self.accelerators:
r += "{},".format(g) r += "{},".format(g)
assert r != "", "this pod {} can't see any gpus".format(self) assert r != "", "this pod {} can't see any accelerators".format(self)
r = r[:-1] r = r[:-1]
return r return r
...@@ -264,23 +274,27 @@ def get_cluster(node_ips, node_ip, trainer_endpoints, device_mode, ...@@ -264,23 +274,27 @@ def get_cluster(node_ips, node_ip, trainer_endpoints, device_mode,
pod = Pod() pod = Pod()
pod.rank = node_rank pod.rank = node_rank
pod.addr = ip pod.addr = ip
pod.device_mode = device_mode
cur_node_endpoints = trainer_endpoints[node_rank] cur_node_endpoints = trainer_endpoints[node_rank]
# when use paddlecloud, endpoints may > devices_per_proc(user_defined) # when use paddlecloud, endpoints may > devices_per_proc(user_defined)
assert len(cur_node_endpoints) >= len( assert len(cur_node_endpoints) >= len(
devices_per_proc devices_per_proc
), "current trainer_endpoints size should be greater equal than selected_gpus size." ), "current trainer_endpoints size should be greater equal than acclerators size."
for i in range(len(devices_per_proc)): for i in range(len(devices_per_proc)):
trainer = Trainer() trainer = Trainer()
if device_mode == DeviceMode.GPU: if device_mode == DeviceMode.GPU or device_mode == DeviceMode.ASCEND_NPU:
if isinstance(devices_per_proc[i], (list, tuple)): if isinstance(devices_per_proc[i], (list, tuple)):
trainer.gpus.extend(devices_per_proc[i]) trainer.accelerators.extend(devices_per_proc[i])
pod.accelerators.extend(devices_per_proc[i])
else: else:
trainer.gpus.append(devices_per_proc[i]) trainer.accelerators.append(devices_per_proc[i])
pod.accelerators.append(devices_per_proc[i])
elif device_mode == DeviceMode.XPU: elif device_mode == DeviceMode.XPU:
if isinstance(devices_per_proc[i], (list, tuple)): if isinstance(devices_per_proc[i], (list, tuple)):
trainer.gpus.extend(devices_per_proc[i]) trainer.accelerators.extend(devices_per_proc[i])
else: else:
trainer.gpus.append(devices_per_proc[i]) trainer.accelerators.append(devices_per_proc[i])
trainer.endpoint = "%s" % (cur_node_endpoints[i]) trainer.endpoint = "%s" % (cur_node_endpoints[i])
trainer.rank = trainer_rank trainer.rank = trainer_rank
trainer_rank += 1 trainer_rank += 1
...@@ -451,21 +465,32 @@ def start_local_trainers(cluster, ...@@ -451,21 +465,32 @@ def start_local_trainers(cluster,
current_env.pop("http_proxy", None) current_env.pop("http_proxy", None)
current_env.pop("https_proxy", None) current_env.pop("https_proxy", None)
ids = cluster.world_device_ids()
res = [':'.join(ele) for ele in ids]
procs = [] procs = []
for idx, t in enumerate(pod.trainers): for idx, t in enumerate(pod.trainers):
proc_env = { proc_env = {
"PADDLE_TRAINER_ID": "%d" % t.rank, "PADDLE_TRAINER_ID": "%d" % t.rank,
"PADDLE_CURRENT_ENDPOINT": "%s" % t.endpoint, "PADDLE_CURRENT_ENDPOINT": "%s" % t.endpoint,
"PADDLE_TRAINERS_NUM": "%d" % cluster.trainers_nranks(), "PADDLE_TRAINERS_NUM": "%d" % cluster.trainers_nranks(),
"PADDLE_TRAINER_ENDPOINTS": ",".join(cluster.trainers_endpoints()) "PADDLE_TRAINER_ENDPOINTS": ",".join(cluster.trainers_endpoints()),
"PADDLE_RANK_IN_NODE": str(idx),
"PADDLE_LOCAL_DEVICE_IDS":
",".join([str(acc) for acc in t.accelerators]),
"PADDLE_WORLD_DEVICE_IDS": ",".join(res),
} }
if fluid.core.is_compiled_with_cuda() and len(t.gpus) > 0: if len(t.accelerators) > 0 and pod.device_mode == DeviceMode.GPU:
proc_env["FLAGS_selected_gpus"] = "%s" % ",".join( proc_env["FLAGS_selected_gpus"] = "%s" % ",".join(
[str(g) for g in t.gpus]) [str(g) for g in t.accelerators])
elif fluid.core.is_compiled_with_xpu() and len(t.gpus) > 0:
if len(t.accelerators) > 0:
proc_env["FLAGS_selected_accelerators"] = "%s" % ",".join(
[str(g) for g in t.accelerators])
# to do: same code style in future
if fluid.core.is_compiled_with_xpu() and len(t.accelerators) > 0:
proc_env["FLAGS_selected_xpus"] = "%s" % ",".join( proc_env["FLAGS_selected_xpus"] = "%s" % ",".join(
[str(g) for g in t.gpus]) [str(g) for g in t.accelerators])
current_env.update(proc_env) current_env.update(proc_env)
...@@ -564,6 +589,17 @@ def watch_local_trainers(procs, nranks): ...@@ -564,6 +589,17 @@ def watch_local_trainers(procs, nranks):
return alive return alive
def get_ascend_npus(npus):
if npus is None:
count = fluid.core.NPUDevice.get_device_count()
if count <= 0:
return ret
ret = [x for x in range(count)]
else:
ret = [x.strip() for x in npus.split(',')]
return ret
def get_gpus(gpus): def get_gpus(gpus):
if gpus is None: if gpus is None:
gpus_num = fluid.core.get_cuda_device_count() gpus_num = fluid.core.get_cuda_device_count()
...@@ -623,11 +659,17 @@ def get_xpus(xpus): ...@@ -623,11 +659,17 @@ def get_xpus(xpus):
def get_device_mode(): def get_device_mode():
if fluid.core.is_compiled_with_cuda() and fluid.core.get_cuda_device_count( if fluid.core.is_compiled_with_ascend() and \
) > 0: fluid.core.NPUDevice.get_device_count() > 0:
print("launch train in GPU mode") print("launch train in ascend npu mode!")
return DeviceMode.ASCEND_NPU
if fluid.core.is_compiled_with_cuda() and \
fluid.core.get_cuda_device_count() > 0:
print("launch train in GPU mode!")
return DeviceMode.GPU return DeviceMode.GPU
elif fluid.core.is_compiled_with_xpu() and fluid.core.get_xpu_device_count(
if fluid.core.is_compiled_with_xpu() and fluid.core.get_xpu_device_count(
) > 0: ) > 0:
print("launch train in XPU mode") print("launch train in XPU mode")
return DeviceMode.XPU return DeviceMode.XPU
...@@ -654,6 +696,10 @@ def get_device_proc_info(args): ...@@ -654,6 +696,10 @@ def get_device_proc_info(args):
] ]
else: else:
devices_per_proc = gpus devices_per_proc = gpus
elif device_mode == DeviceMode.ASCEND_NPU:
npus = get_ascend_npus(args.ascend_npus)
assert args.nproc_per_node is None, "ascend_npus need't nproc_per_node arguments"
devices_per_proc = npus
elif device_mode == DeviceMode.XPU: elif device_mode == DeviceMode.XPU:
xpus = get_xpus(args.xpus) xpus = get_xpus(args.xpus)
if args.nproc_per_node is not None: if args.nproc_per_node is not None:
......
# 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.
...@@ -12,16 +12,26 @@ ...@@ -12,16 +12,26 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import paddle.fluid.framework as framework import paddle.fluid.framework as framework
from paddle.fluid.optimizer import Optimizer from paddle.fluid.optimizer import Optimizer
import paddle.fluid.core as core import paddle.fluid.core as core
import numpy as np import numpy as np
import ascend_parser from . import ascend_parser
from paddle.distributed import fleet
import hccl.manage.api as hccl
from collections import namedtuple
HcomGroupConfig = namedtuple('HcomGroupConfig', ['name', 'nranks', 'rank_ids'])
class AscendIRParser(object): class AscendIRParser(object):
def __init__(self): def __init__(self, auto_dp=False, world_rank_size=1):
self.graph_idx = 0 self.graph_idx = 0
self.hcom_endpoints = {}
self.groups_to_create = []
self._auto_dp = auto_dp
self._world_rank_size = world_rank_size
def _construct_input_map(self, input_varlist): def _construct_input_map(self, input_varlist):
ret_map = {} ret_map = {}
...@@ -43,15 +53,52 @@ class AscendIRParser(object): ...@@ -43,15 +53,52 @@ class AscendIRParser(object):
ret_map[var.name] = ge_input ret_map[var.name] = ge_input
return ge_in_operator, ret_map return ge_in_operator, ret_map
def _endpoint_to_world_rank_id(self, endpoint):
world_endpoints = fleet.worker_endpoints()
assert endpoint in world_endpoints, "endpoint (%s) not in worker_endpoints (%s) " % (
endpoint, fleet.world_device_ids())
return world_endpoints.index(endpoint)
def parse_op(self, op): def parse_op(self, op):
if op.type in ascend_parser.registerd_op: if op.type == 'c_gen_nccl_id':
print("Op[%s] has been registered, begin to parse it" % (op.type)) endpoint = op.attr("endpoint")
other_endpoints = op.attr("other_endpoints")
rank = op.attr("rank")
nccl_id = op.output_arg_names[0]
# c_gen_nccl_id operator splits endpoints into local endpoint and other_endpoints
# we should combine these together to produce world_rank_ids
self.hcom_endpoints[nccl_id] = other_endpoints[:]
self.hcom_endpoints[nccl_id].insert(rank, endpoint)
print("nccl_id (%s) registered endpoints %s" %
(nccl_id, self.hcom_endpoints[nccl_id]))
elif op.type == 'c_comm_init':
nccl_id = op.input_arg_names[0]
nranks = op.attr("nranks")
assert nranks == len(self.hcom_endpoints[
nccl_id]), "nranks doesn't match endpoint count"
rank = op.attr("rank")
ring_id = op.attr("ring_id")
group_name = "hcom_group_" + str(ring_id)
global_rank_ids = [
self._endpoint_to_world_rank_id(endpoint)
for endpoint in self.hcom_endpoints[nccl_id]
]
self.groups_to_create.append(
HcomGroupConfig(
name=group_name, nranks=nranks, rank_ids=global_rank_ids))
print("append to create group: %s, with rank_ids: %s" %
(group_name, global_rank_ids))
elif op.type in ascend_parser.registerd_op:
op_parser = self.parser_factory.create_parse( op_parser = self.parser_factory.create_parse(
ascend_parser.registerd_op[op.type]) ascend_parser.registerd_op[op.type])
op_parser.apply(op) op_parser.apply(op)
else: else:
print("Op[%s] has not been registered, so we have to skip it" % assert False, "Op[%s] has not been registered, so we have to skip it" % (
(op.type)) op.type)
def _parse_program(self, def _parse_program(self,
graph_name, graph_name,
...@@ -115,6 +162,17 @@ class AscendIRParser(object): ...@@ -115,6 +162,17 @@ class AscendIRParser(object):
startup_graph = self._parse_program("startup", startup_program) startup_graph = self._parse_program("startup", startup_program)
main_graph = self._parse_program("main", main_program, input_varlist, main_graph = self._parse_program("main", main_program, input_varlist,
fetch_list) fetch_list)
if self._auto_dp and self._world_rank_size > 1:
assert len(self.groups_to_create
) == 0, "can't parse program under auto_dp mode"
from paddle.distributed import fleet
self.groups_to_create.append(
HcomGroupConfig(
name="hcom_group_0",
nranks=fleet.world_size(),
rank_ids=[x for x in range(fleet.world_size())]))
return startup_graph, main_graph return startup_graph, main_graph
...@@ -124,9 +182,14 @@ class AscendOptimizer(Optimizer): ...@@ -124,9 +182,14 @@ class AscendOptimizer(Optimizer):
def __init__(self, optimizer, fetch_list=[]): def __init__(self, optimizer, fetch_list=[]):
self.inner_opt = optimizer self.inner_opt = optimizer
self.fetch_list = fetch_list self.fetch_list = fetch_list
self.ascend_instance = None
def __del__(self): def __del__(self):
print("begin AscendOptimizer del")
if self.ascend_instance is not None:
self.ascend_instance.destroy_global_resources()
core.ge_finalize() core.ge_finalize()
print("end AscendOptimizer del")
def _can_apply(self): def _can_apply(self):
if not self.user_defined_strategy.ascend: if not self.user_defined_strategy.ascend:
...@@ -138,7 +201,7 @@ class AscendOptimizer(Optimizer): ...@@ -138,7 +201,7 @@ class AscendOptimizer(Optimizer):
dist_strategy.ascend = False dist_strategy.ascend = False
dist_strategy.ascend_configs = {} dist_strategy.ascend_configs = {}
def _get_input_varlist(program): def _get_input_varlist(self, program):
ret_list = [] ret_list = []
for var in program.list_vars(): for var in program.list_vars():
if var.is_data or var.persistable: if var.is_data or var.persistable:
...@@ -149,30 +212,56 @@ class AscendOptimizer(Optimizer): ...@@ -149,30 +212,56 @@ class AscendOptimizer(Optimizer):
loss, loss,
startup_program=None, startup_program=None,
parameter_list=None, parameter_list=None,
no_grad_set=None): no_grad_set=None,
auto_dp=False,
rank_table_file=None):
minimized = None
if self.inner_opt:
minimized = self.inner_opt.minimize( minimized = self.inner_opt.minimize(
loss, startup_program=startup_program) loss, startup_program=startup_program)
self.ascend_instance = core.AscendInstance() self.ascend_instance = core.AscendInstance()
from paddle.distributed import fleet
if auto_dp and fleet.world_size() > 1:
from paddle.fluid.transpiler import ascend_transpiler
t = ascend_transpiler.AscendTranspiler(startup_program,
loss.block.program)
t.transpile()
#print(loss.block.program)
# Config about Graph Engine can be found in https://support.huaweicloud.com/ # Config about Graph Engine can be found in https://support.huaweicloud.com/
config = { config = {
"ge.exec.deviceId": "0", "ge.exec.deviceId": str(fleet.local_device_ids()),
"ge.graphRunMode": "1", "ge.graphRunMode": "1",
"ge.exec.precision_mode": "must_keep_origin_dtype" "ge.exec.precision_mode": "must_keep_origin_dtype",
} }
# if multi trainers
if rank_table_file and fleet.world_size() > 1:
config["ge.exec.rankTableFile"] = rank_table_file
config["ge.exec.rankId"] = str(fleet.worker_index())
config["ge.exec.isUseHcom"] = "1"
config["ge.exec.deployMode"] = "0"
print("ge_initialize config:", config)
core.ge_initialize(config) core.ge_initialize(config)
# Init Session # Init Session
self.ascend_instance.init_global_resources() self.ascend_instance.init_global_resources()
main_block = loss.block main_block = loss.block
self.parser = AscendIRParser() self.parser = AscendIRParser(
auto_dp=auto_dp, world_rank_size=fleet.world_size())
input_varlist = self._get_input_varlist(main_block.program)
input_varlist = _get_input_varlist(main_block.program)
startup_graph, main_graph = self.parser.parse_program( startup_graph, main_graph = self.parser.parse_program(
startup_program, main_block.program, input_varlist, self.fetch_list) startup_program, main_block.program, input_varlist, self.fetch_list)
for cfg in self.parser.groups_to_create:
print("create group (%s), nranks: %d, rank_ids: %s" %
(cfg.name, cfg.nranks, cfg.rank_ids))
hccl.create_group(cfg.name, cfg.nranks, cfg.rank_ids)
self.ascend_instance.add_ascend_subgraph(0, startup_graph) self.ascend_instance.add_ascend_subgraph(0, startup_graph)
self.ascend_instance.add_ascend_subgraph(1, main_graph) self.ascend_instance.add_ascend_subgraph(1, main_graph)
......
...@@ -61,8 +61,9 @@ class GraphExecutionOptimizer(MetaOptimizerBase): ...@@ -61,8 +61,9 @@ class GraphExecutionOptimizer(MetaOptimizerBase):
trainer_endpoints_env = ",".join(trainer_endpoints) trainer_endpoints_env = ",".join(trainer_endpoints)
trainers_num = self.role_maker._worker_num() trainers_num = self.role_maker._worker_num()
if trainer_id == 0: # FIXME(wangxi): approve this.
wait_server_ready(other_trainers) #if trainer_id == 0:
# wait_server_ready(other_trainers)
if core.is_compiled_with_cuda(): if core.is_compiled_with_cuda():
comm_id_var = startup_program.global_block().create_var( comm_id_var = startup_program.global_block().create_var(
......
...@@ -40,6 +40,8 @@ list(APPEND MIXED_DIST_TEST_OPS test_fleetrun) ...@@ -40,6 +40,8 @@ list(APPEND MIXED_DIST_TEST_OPS test_fleetrun)
list(APPEND MIXED_DIST_TEST_OPS test_fleet_run_random_port) list(APPEND MIXED_DIST_TEST_OPS test_fleet_run_random_port)
list(APPEND MIXED_DIST_TEST_OPS test_fleet_launch_async) list(APPEND MIXED_DIST_TEST_OPS test_fleet_launch_async)
list(APPEND MIXED_DIST_TEST_OPS test_fleet_launch_cloud) list(APPEND MIXED_DIST_TEST_OPS test_fleet_launch_cloud)
list(APPEND MIXED_DIST_TEST_OPS test_fleet_launch_ascend)
list(APPEND MIXED_DIST_TEST_OPS test_ascend_group)
list(APPEND MIXED_DIST_TEST_OPS test_fleet_launch_nproc) list(APPEND MIXED_DIST_TEST_OPS test_fleet_launch_nproc)
list(APPEND MIXED_DIST_TEST_OPS test_fleet_api_input) list(APPEND MIXED_DIST_TEST_OPS test_fleet_api_input)
list(APPEND MIXED_DIST_TEST_OPS test_collective_optimizer) list(APPEND MIXED_DIST_TEST_OPS test_collective_optimizer)
...@@ -531,6 +533,10 @@ if(WITH_DISTRIBUTE) ...@@ -531,6 +533,10 @@ if(WITH_DISTRIBUTE)
bash_test_modules(test_fleet_launch_async START_BASH test_fleet_launch_async.sh ENVS PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR}) bash_test_modules(test_fleet_launch_async START_BASH test_fleet_launch_async.sh ENVS PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR})
bash_test_modules(test_fleet_launch_cloud START_BASH test_fleet_launch_cloud.sh ENVS PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR}) bash_test_modules(test_fleet_launch_cloud START_BASH test_fleet_launch_cloud.sh ENVS PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR})
bash_test_modules(test_fleet_launch_nproc START_BASH test_fleet_launch_nproc.sh ENVS PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR}) bash_test_modules(test_fleet_launch_nproc START_BASH test_fleet_launch_nproc.sh ENVS PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR})
if(WITH_ASCEND)
bash_test_modules(test_fleet_launch_ascend START_BASH test_fleet_launch_ascend.sh ENVS PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR})
bash_test_modules(test_ascend_group START_BASH test_ascend_group.sh ENVS PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR})
endif()
# port range (20000, 23000) is reserved for dist-ops # port range (20000, 23000) is reserved for dist-ops
set(dist_ut_port 20001) set(dist_ut_port 20001)
...@@ -541,7 +547,8 @@ if(WITH_DISTRIBUTE) ...@@ -541,7 +547,8 @@ if(WITH_DISTRIBUTE)
message(FATAL_ERROR "available ports have been exhausted:${dist_ut_port}") message(FATAL_ERROR "available ports have been exhausted:${dist_ut_port}")
endif() endif()
endforeach(TEST_OP) endforeach(TEST_OP)
bash_test_modules(test_fleet_launch_ps START_BASH test_fleet_launch_ps.sh SERIAL LABELS "RUN_TYPE=EXCLUSIVE" ENVS "PADDLE_DIST_UT_PORT=${dist_ut_port}" PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR} ) # solve it later.
# bash_test_modules(test_fleet_launch_ps START_BASH test_fleet_launch_ps.sh SERIAL LABELS "RUN_TYPE=EXCLUSIVE" ENVS "PADDLE_DIST_UT_PORT=${dist_ut_port}" PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR} )
bash_test_modules(test_new_group START_BASH test_new_group.sh SERIAL LABELS "RUN_TYPE=EXCLUSIVE" ENVS "PADDLE_DIST_UT_PORT=${dist_ut_port}+20" PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR} ) bash_test_modules(test_new_group START_BASH test_new_group.sh SERIAL LABELS "RUN_TYPE=EXCLUSIVE" ENVS "PADDLE_DIST_UT_PORT=${dist_ut_port}+20" PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR} )
endif(NOT APPLE) endif(NOT APPLE)
endif() endif()
......
# 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 sys
import time
import paddle.fluid as fluid
from paddle.fluid import unique_name
import paddle.fluid.core as core
import paddle
from paddle.fluid.layer_helper import LayerHelper
from paddle.distributed import fleet
from paddle.distributed.fleet.meta_optimizers.ascend import ascend_parser, ascend_optimizer
from collections import namedtuple
Block = namedtuple('Block', ['program'])
Loss = namedtuple('Loss', ['block'])
paddle.enable_static()
OpRole = core.op_proto_and_checker_maker.OpRole
OP_ROLE_KEY = core.op_proto_and_checker_maker.kOpRoleAttrName()
OP_ROLE_VAR_KEY = core.op_proto_and_checker_maker.kOpRoleVarAttrName()
role = fleet.PaddleCloudRoleMaker(is_collective=True)
fleet.init(role)
def init_communicator(startup_program, main_program, current_endpoint,
endpoints, ring_id):
nranks = len(endpoints)
other_endpoints = endpoints[:]
other_endpoints.remove(current_endpoint)
group_rank = endpoints.index(current_endpoint)
assert group_rank >= 0
block = startup_program.global_block()
nccl_id_var = block.create_var(
name=unique_name.generate('nccl_id'),
persistable=True,
type=core.VarDesc.VarType.RAW)
block.append_op(
type='c_gen_nccl_id',
inputs={},
outputs={'Out': nccl_id_var},
attrs={
'rank': group_rank,
'endpoint': current_endpoint,
'other_endpoints': other_endpoints,
OP_ROLE_KEY: OpRole.Forward,
})
block.append_op(
type='c_comm_init',
inputs={'X': nccl_id_var},
outputs={},
attrs={
'nranks': nranks,
'rank': group_rank,
'ring_id': ring_id,
OP_ROLE_KEY: OpRole.Forward,
})
with fluid.program_guard(main_program):
op_type = "c_allreduce_sum"
data = fluid.layers.fill_constant(shape=[1], dtype='float32', value=2.5)
helper = LayerHelper(op_type, **locals())
helper.append_op(
type=op_type,
inputs={'X': [data]},
outputs={'Out': [data]},
attrs={'ring_id': ring_id,
'use_calc_stream': True})
print("startup program:", startup_program)
print("main program:", main_program)
def train(world_endpoints, world_device_ids, local_device_ids, local_rank):
startup_programs = []
main_programs = []
#trainer_endpoints=["127.0.0.1:6071","127.0.0.1:6072","127.0.0.1:6073","127.0.0.1:6074"]
trainer_endpoints = world_endpoints
groups = [[], [], []]
groups[0] = [trainer_endpoints[0], trainer_endpoints[1]]
groups[1] = [trainer_endpoints[2], trainer_endpoints[3]]
groups[2] = [trainer_endpoints[0], trainer_endpoints[2]]
print("groups:", groups)
for i in range(len(trainer_endpoints)):
startup_programs.append(fluid.Program())
main_programs.append(fluid.Program())
for idx, group in enumerate(groups):
for te in group:
te_idx = trainer_endpoints.index(te)
startup_program = startup_programs[te_idx]
main_program = main_programs[te_idx]
init_communicator(startup_program, main_program, te, group, idx)
print(len(startup_programs))
print(startup_programs[local_rank])
print(main_programs[local_rank])
print("local rank: ", local_rank)
print("local startup program: ", startup_programs[local_rank])
startup_program = startup_programs[local_rank]
main_program = main_programs[local_rank]
loss = Loss(Block(main_program))
optimizer = ascend_optimizer.AscendOptimizer(None, fetch_list=[])
optimizer.minimize(loss, startup_program, auto_dp=True)
exe = paddle.static.Executor(paddle.CPUPlace())
#exe.run(startup_program)
exe.run(main_program)
worker_endpoints = fleet.worker_endpoints()
world_device_ids = fleet.world_device_ids()
local_device_ids = fleet.local_device_ids()
local_rank = int(fleet.local_rank())
print("worker_endpoints:", worker_endpoints)
print("world_device_ids:", world_device_ids)
print("local_device_ids:", local_device_ids)
print("local_rank:", local_rank)
train(worker_endpoints, world_device_ids, local_device_ids, local_rank)
# 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 sys
import time
def train(prefix):
selected_accelerators = os.getenv("FLAGS_selected_accelerators")
trainer_id = int(os.getenv("PADDLE_TRAINER_ID"))
worker_endpoints_env = os.getenv("PADDLE_TRAINER_ENDPOINTS")
current_endpoint = os.getenv("PADDLE_CURRENT_ENDPOINT")
worker_endpoints = worker_endpoints_env
trainers_num = len(worker_endpoints.split(','))
device_ids = os.getenv("PADDLE_WORLD_DEVICE_IDS")
current_device_id = os.getenv("PADDLE_LOCAL_DEVICE_IDS")
details = "selected_accelerators:{} worker_endpoints:{} trainers_num:{} current_endpoint:{} trainer_id:{} device_ids:{} device_id:{}"\
.format(selected_accelerators, worker_endpoints, trainers_num, current_endpoint,trainer_id,device_ids, current_device_id)
print(details)
with open("multi_process_{}.check_{}.log".format(prefix, trainer_id),
"w") as f:
f.write(details)
if __name__ == '__main__':
prefix = sys.argv[1]
train(prefix)
#!/bin/bash
# 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.
set -e
cluster_node_ips="127.0.0.1"
export PADDLE_TRAINERS_NUM=4
export POD_IP=127.0.0.1
export PADDLE_TRAINERS=127.0.0.1
export PADDLE_TRAINER_ID=0
export PADDLE_PORT=35789
export TRAINER_PORTS_NUM=4
distributed_args="--ips=${cluster_node_ips} --ascend_npus=0,1,2,3 --log_dir=testlog"
python -m paddle.distributed.fleet.launch ${distributed_args} \
ascend_group.py fleetascendgroup
#!/bin/bash
# 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.
set -e
# use paddlecloud
echo "begin test use paddlecloud"
cluster_node_ips="127.0.0.1,127.0.0.2"
export PADDLE_TRAINERS_NUM=2
export POD_IP=127.0.0.1
export PADDLE_TRAINERS=127.0.0.1,127.0.0.2
export PADDLE_TRAINER_ID=0
export PADDLE_PORT=35789
export TRAINER_PORTS_NUM=2
distributed_args="--ips=${cluster_node_ips} --ascend_npus=0,1 --log_dir=testlog"
python -m paddle.distributed.fleet.launch ${distributed_args} ascend_multi_process_collective.py fleetlaunchascend
str1="selected_accelerators:0 worker_endpoints:127.0.0.1:35789,127.0.0.1:35790,127.0.0.2:35789,127.0.0.2:35790 trainers_num:4 current_endpoint:127.0.0.1:35789 trainer_id:0 device_ids:0,1,0,1 device_id:0"
str2="selected_accelerators:1 worker_endpoints:127.0.0.1:35789,127.0.0.1:35790,127.0.0.2:35789,127.0.0.2:35790 trainers_num:4 current_endpoint:127.0.0.1:35790 trainer_id:1 device_ids:0,1,0,1 device_id:1"
file_0="multi_process_fleetlaunchascend.check_0.log"
file_1="multi_process_fleetlaunchascend.check_1.log"
echo "paddlecloud params test"
if grep -q "$str1" "$file_0"; then
echo "find trainer 0"
else
echo "not find trainer 0"
exit -1
fi
if grep -q "$str2" "$file_1"; then
echo "find trainer 1"
else
echo "not find trainer 1"
exit -1
fi
# test async poll process
if [ -f $file_0 ]; then
rm $file_0
fi
if [ -f $file_1 ]; then
rm $file_1
fi
# 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 . import collective
from .. import core
OpRole = core.op_proto_and_checker_maker.OpRole
from paddle.distributed import fleet
class AscendTranspiler(collective.Collective):
def __init__(self, startup_program, main_program):
self.nrings = 1
super(AscendTranspiler, self).__init__(self.nrings)
self._startup_program = startup_program
self._main_program = main_program
def _insert_allreduce_ops(self):
block = self._main_program.global_block()
ring_id = -1
grad = None
for idx, op in reversed(list(enumerate(block.ops))):
if self._is_backward_op(op) and \
self.op_role_var_key in op.attr_names:
op_role_var = op.all_attrs()[self.op_role_var_key]
if len(op_role_var) == 0:
continue
assert len(op_role_var) % 2 == 0
offset = idx
for i in range(0, len(op_role_var), 2):
param = block.vars[op_role_var[i]]
grad = block.vars[op_role_var[i + 1]]
if param.is_distributed:
continue
# As we search ops reversedly, we should insert c_allreduce_sum
# op in the same way to keep the ring_id alternate
ring_id = (ring_id + 1) % self.nrings
block._insert_op(
offset + 1,
type='c_allreduce_sum',
inputs={'X': grad},
outputs={'Out': grad},
attrs={
'ring_id': ring_id,
self.op_role_key: OpRole.Backward
})
block._insert_op(
offset + 2,
type='scale',
inputs={'X': grad},
outputs={'Out': grad},
attrs={
'scale': 1.0 / fleet.worker_num(),
self.op_role_key: OpRole.Backward
})
if grad is None:
return
def transpile(self):
self._insert_allreduce_ops()
...@@ -149,6 +149,7 @@ packages=['paddle', ...@@ -149,6 +149,7 @@ packages=['paddle',
'paddle.distributed.fleet.base', 'paddle.distributed.fleet.base',
'paddle.distributed.fleet.meta_optimizers', 'paddle.distributed.fleet.meta_optimizers',
'paddle.distributed.fleet.meta_optimizers.sharding', 'paddle.distributed.fleet.meta_optimizers.sharding',
'paddle.distributed.fleet.meta_optimizers.ascend',
'paddle.distributed.fleet.runtime', 'paddle.distributed.fleet.runtime',
'paddle.distributed.fleet.dataset', 'paddle.distributed.fleet.dataset',
'paddle.distributed.fleet.data_generator', 'paddle.distributed.fleet.data_generator',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册