Skip to content

Commit

Permalink
[MTAI-484] feat(build): support cpp test for MUSA (PaddlePaddle#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
caizhi-mt authored and mt-robot committed Aug 16, 2023
1 parent f8bc349 commit 51c52af
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 4 deletions.
40 changes: 36 additions & 4 deletions cmake/generic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ function(cc_binary TARGET_NAME)
if(WITH_ROCM)
target_link_libraries(${TARGET_NAME} ${ROCM_HIPRTC_LIB})
endif()
if(WITH_MUSA)
target_link_libraries(${TARGET_NAME} ${MUSARTC_LIB})
endif()

check_coverage_opt(${TARGET_NAME} ${cc_binary_SRCS})

Expand Down Expand Up @@ -452,6 +455,9 @@ function(cc_test_build TARGET_NAME)
if(WITH_ROCM)
target_link_libraries(${TARGET_NAME} ${ROCM_HIPRTC_LIB})
endif()
if(WITH_MUSA)
target_link_libraries(${TARGET_NAME} ${MUSARTC_LIB})
endif()
check_coverage_opt(${TARGET_NAME} ${cc_test_SRCS})
endif()
endfunction()
Expand Down Expand Up @@ -840,17 +846,43 @@ function(musa_test TARGET_NAME)
set(multiValueArgs SRCS DEPS)
cmake_parse_arguments(musa_test "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
add_executable(${TARGET_NAME} ${musa_test_SRCS})
musa_add_executable(${TARGET_NAME} ${musa_test_SRCS})
# "-pthread -ldl -lrt" is defined in CMAKE_CXX_LINK_EXECUTABLE
target_link_options(${TARGET_NAME} PRIVATE -pthread -ldl -lrt)
get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES)
target_link_libraries(${TARGET_NAME} ${musa_test_DEPS}
${os_dependency_modules} paddle_gtest_main phi)
add_dependencies(${TARGET_NAME} ${musa_test_DEPS} paddle_gtest_main)
target_link_libraries(
${TARGET_NAME}
${musa_test_DEPS}
paddle_gtest_main
lod_tensor
memory
gtest
glog
phi
${os_dependency_modules})
add_dependencies(
${TARGET_NAME}
${musa_test_DEPS}
paddle_gtest_main
lod_tensor
memory
gtest
phi
glog)
common_link(${TARGET_NAME})
add_test(${TARGET_NAME} ${TARGET_NAME})
set_property(TEST ${TARGET_NAME} PROPERTY ENVIRONMENT
FLAGS_cpu_deterministic=true)
set_property(TEST ${TARGET_NAME} PROPERTY ENVIRONMENT
FLAGS_init_allocated_mem=true)
set_property(TEST ${TARGET_NAME} PROPERTY ENVIRONMENT
FLAGS_cudnn_deterministic=true)
set_property(
TEST ${TARGET_NAME}
PROPERTY
ENVIRONMENT
"LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/python/paddle/libs:$LD_LIBRARY_PATH"
)
endif()
endfunction()

Expand Down
4 changes: 4 additions & 0 deletions cmake/musa.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ if(CMAKE_BUILD_TYPE MATCHES Debug)
list(APPEND MUSA_MCC_FLAGS -g2)
list(APPEND MUSA_MCC_FLAGS -O0)
endif()

set(musa_runtime_library_name musart)
find_library(MUSARTC_LIB ${musa_runtime_library_name} HINTS ${MUSA_PATH}/lib)
message(STATUS "MUSARTC_LIB: ${MUSARTC_LIB}")
13 changes: 13 additions & 0 deletions paddle/fluid/memory/allocation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ if(WITH_ROCM)
SRCS thread_local_allocator_test.cc
DEPS allocator)
endif()
if(WITH_MUSA)
musa_test(thread_local_allocator_test SRCS thread_local_allocator_test.cc
DEPS allocator)
endif()

if(WITH_GPU)
nv_test(
Expand All @@ -102,6 +106,15 @@ elseif(WITH_ROCM)
best_fit_allocator_test
SRCS best_fit_allocator_test.cc best_fit_allocator_test.cu
DEPS allocator memcpy)
elseif(WITH_MUSA)
musa_test(
best_fit_allocator_test
SRCS
best_fit_allocator_test.cc
best_fit_allocator_test.cu
DEPS
allocator
memcpy)
else()
cc_test_old(best_fit_allocator_test SRCS best_fit_allocator_test.cc DEPS
allocator)
Expand Down
9 changes: 9 additions & 0 deletions paddle/fluid/memory/allocation/buddy_allocator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ TEST(BuddyAllocator, AllocFromAvailable) {
#ifdef PADDLE_WITH_HIP
hipError_t result = hipMalloc(&p, available >> 1);
EXPECT_TRUE(result == hipSuccess);
#elif defined(PADDLE_WITH_MUSA)
musaError_t result = musaMalloc(&p, available >> 1);
EXPECT_TRUE(result == musaSuccess);
#else
cudaError_t result = cudaMalloc(&p, available >> 1);
EXPECT_TRUE(result == cudaSuccess);
Expand All @@ -265,6 +268,8 @@ TEST(BuddyAllocator, AllocFromAvailable) {
if (p) {
#ifdef PADDLE_WITH_HIP
EXPECT_TRUE(hipFree(p) == hipSuccess);
#elif defined(PADDLE_WITH_MUSA)
EXPECT_TRUE(musaFree(p) == musaSuccess);
#else
EXPECT_TRUE(cudaFree(p) == cudaSuccess);
#endif
Expand All @@ -280,6 +285,8 @@ TEST(BuddyAllocator, AllocFromAvailableWhenFractionIsOne) {

#ifdef PADDLE_WITH_HIP
EXPECT_TRUE(hipMalloc(&p, static_cast<size_t>(1) << 30) == hipSuccess);
#elif defined(PADDLE_WITH_MUSA)
EXPECT_TRUE(musaMalloc(&p, static_cast<size_t>(1) << 30) == musaSuccess);
#else
EXPECT_TRUE(cudaMalloc(&p, static_cast<size_t>(1) << 30) == cudaSuccess);
#endif
Expand All @@ -296,6 +303,8 @@ TEST(BuddyAllocator, AllocFromAvailableWhenFractionIsOne) {
if (p) {
#ifdef PADDLE_WITH_HIP
EXPECT_TRUE(hipFree(p) == hipSuccess);
#elif defined(PADDLE_WITH_MUSA)
EXPECT_TRUE(musaFree(p) == musaSuccess);
#else
EXPECT_TRUE(cudaFree(p) == cudaSuccess);
#endif
Expand Down
48 changes: 48 additions & 0 deletions paddle/fluid/platform/enforce_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,54 @@ TEST(enforce, hip_success) {
EXPECT_TRUE(CheckCudaStatusFailure(ncclSystemError, "Rccl error"));
#endif
}
#elif defined(PADDLE_WITH_MUSA)
TEST(enforce, musa_success) {
EXPECT_TRUE(CheckCudaStatusSuccess(musaSuccess));
EXPECT_TRUE(CheckCudaStatusFailure(musaErrorInvalidValue, "MUSA error"));

EXPECT_TRUE(CheckCudaStatusFailure(musaErrorMemoryAllocation, "MUSA error"));

EXPECT_TRUE(CheckCudaStatusFailure(
musaErrorInsufficientDriver,
"This indicates that the installed MooreThreads MUSA driver is older "
"than the "
"MUSA runtime library. This is not a supported configuration.Users "
"should install an updated MooreThreads display driver to allow the "
"application to run"));
EXPECT_TRUE(CheckCudaStatusFailure(
musaErrorContextIsDestroyed,
"This error indicates that the context current to the calling thread has "
"been destroyed using muCtxDestroy, or is a primary context which has "
"not yet been initialized"));

EXPECT_TRUE(CheckCudaStatusSuccess(MURAND_STATUS_SUCCESS));
EXPECT_TRUE(
CheckCudaStatusFailure(MURAND_STATUS_VERSION_MISMATCH, "MURAND error"));
EXPECT_TRUE(
CheckCudaStatusFailure(MURAND_STATUS_NOT_CREATED, "MURAND error"));
EXPECT_TRUE(
CheckCudaStatusFailure(MURAND_STATUS_LENGTH_NOT_MULTIPLE,
"Length requested is not a multple of dimension"));

EXPECT_TRUE(CheckCudaStatusSuccess(MUBLAS_STATUS_SUCCESS));
EXPECT_TRUE(
CheckCudaStatusFailure(MUBLAS_STATUS_NOT_IMPLEMENTED, "MUBLAS error"));
EXPECT_TRUE(
CheckCudaStatusFailure(MUBLAS_STATUS_INVALID_VALUE, "MUBLAS error"));

#if !defined(__APPLE__) && defined(PADDLE_WITH_MCCL)
EXPECT_TRUE(CheckCudaStatusSuccess(mcclSuccess));
EXPECT_TRUE(CheckCudaStatusFailure(mcclUnhandledMusaError, "MCCL error"));
EXPECT_TRUE(CheckCudaStatusFailure(mcclSystemError, "MCCL error"));
EXPECT_TRUE(CheckCudaStatusFailure(mcclInternalError,
"An internal check failed. This is either "
"a bug in MCCL or due to memory "
"corruption"));
EXPECT_TRUE(CheckCudaStatusFailure(mcclInvalidUsage,
"The call to MCCL is incorrect. This is "
"usually reflecting a programming error"));
#endif
}
#else
TEST(enforce, cuda_success) {
EXPECT_TRUE(CheckCudaStatusSuccess(cudaSuccess));
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/backends/dynload/dynamic_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ void* GetNVRTCDsoHandle() {
return GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libnvrtc.dylib", false);
#elif defined(PADDLE_WITH_HIP)
return GetDsoHandleFromSearchPath(FLAGS_rocm_dir, "libamdhip64.so", false);
#elif defined(PADDLE_WITH_MUSA)
return GetDsoHandleFromSearchPath(FLAGS_musa_dir, "libmusart.so", false);
#else
return GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libnvrtc.so", false);
#endif
Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ if(${len} GREATER_EQUAL 1)
if(WITH_ROCM)
target_link_libraries(${test_name} ${ROCM_HIPRTC_LIB})
endif()
if(WITH_MUSA)
target_link_libraries(${test_name} ${MUSARTC_LIB})
endif()
if(APPLE)
target_link_libraries(
${test_name}
Expand Down

0 comments on commit 51c52af

Please sign in to comment.