Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#24 from mthreads/support_dynload
Browse files Browse the repository at this point in the history
Support dynload module for MUSA
  • Loading branch information
caizhi-mt authored and mt-robot committed Aug 7, 2023
2 parents 76797b7 + 903c8f7 commit 6a656f5
Show file tree
Hide file tree
Showing 27 changed files with 895 additions and 21 deletions.
7 changes: 7 additions & 0 deletions paddle/fluid/platform/dynload/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ if(WITH_ROCM)
list(APPEND HIP_SRCS rocblas.cc miopen.cc hiprand.cc hipfft.cc)
endif()

if(WITH_MUSA)
list(APPEND MUSA_SRCS mublas.cc murand.cc musparse.cc)
endif()

# There is no macOS version of NCCL.
# Disable nvrtc and cuda_driver api on MacOS, and only do a early test on Linux and Windows.
if(NOT APPLE)
Expand All @@ -41,6 +45,9 @@ if(NOT APPLE)
endif()
if(WITH_MUSA)
list(APPEND MUSA_SRCS musa_driver.cc musartc.cc)
if(WITH_MCCL)
list(APPEND MUSA_SRCS mccl.cc)
endif()
endif()
endif()

Expand Down
27 changes: 27 additions & 0 deletions paddle/fluid/platform/dynload/mccl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) 2016 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/dynload/mccl.h"

namespace paddle {
namespace platform {
namespace dynload {

#define DEFINE_WRAP(__name) DynLoad__##__name __name

MCCL_RAND_ROUTINE_EACH(DEFINE_WRAP);

} // namespace dynload
} // namespace platform
} // namespace paddle
57 changes: 57 additions & 0 deletions paddle/fluid/platform/dynload/mccl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright (c) 2016 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

#include <mccl.h>

#include <mutex> // NOLINT

#include "paddle/phi/backends/dynload/mccl.h"

namespace paddle {
namespace platform {
namespace dynload {

#define PLATFORM_DECLARE_DYNAMIC_LOAD_MCCL_WRAP(__name) \
using DynLoad__##__name = phi::dynload::DynLoad__##__name; \
extern DynLoad__##__name __name

#define MCCL_RAND_ROUTINE_EACH(__macro) \
__macro(mcclCommInitAll); \
__macro(mcclGetUniqueId); \
__macro(mcclCommInitRank); \
__macro(mcclCommDestroy); \
__macro(mcclCommCount); \
__macro(mcclCommCuDevice); \
__macro(mcclCommUserRank); \
__macro(mcclAllReduce); \
__macro(mcclBcast); \
__macro(mcclAllGather); \
__macro(mcclGroupStart); \
__macro(mcclGroupEnd); \
__macro(mcclReduce); \
__macro(mcclReduceScatter); \
__macro(mcclGetErrorString); \
__macro(mcclBroadcast); \
__macro(mcclGetVersion); \
__macro(mcclSend); \
__macro(mcclRecv); \
__macro(mcclRedOpCreatePreMulSum); \
__macro(mcclRedOpDestroy);

MCCL_RAND_ROUTINE_EACH(PLATFORM_DECLARE_DYNAMIC_LOAD_MCCL_WRAP)

} // namespace dynload
} // namespace platform
} // namespace paddle
27 changes: 27 additions & 0 deletions paddle/fluid/platform/dynload/mublas.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) 2016 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/dynload/mublas.h"

namespace paddle {
namespace platform {
namespace dynload {

#define DEFINE_WRAP(__name) DynLoad__##__name __name

MUBLAS_BLAS_ROUTINE_EACH(DEFINE_WRAP);

} // namespace dynload
} // namespace platform
} // namespace paddle
78 changes: 78 additions & 0 deletions paddle/fluid/platform/dynload/mublas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* Copyright (c) 2016 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

#include <mublas.h>
#include <musa.h>

#include <mutex> // NOLINT
#include <type_traits>

#include "paddle/phi/backends/dynload/mublas.h"

namespace paddle {
namespace platform {
namespace dynload {

/**
* The following macro definition can generate structs
* (for each function) to dynamic load mublas routine
* via operator overloading.
*
* note: default dynamic linked libs
*/
#define PLATFORM_DECLARE_DYNAMIC_LOAD_MUBLAS_WRAP(__name) \
using DynLoad__##__name = phi::dynload::DynLoad__##__name; \
extern DynLoad__##__name __name

#define MUBLAS_BLAS_ROUTINE_EACH(__macro) \
__macro(mublasSaxpy); \
__macro(mublasDaxpy); \
__macro(mublasCaxpy); \
__macro(mublasZaxpy); \
__macro(mublasSscal); \
__macro(mublasDscal); \
__macro(mublasScopy); \
__macro(mublasDcopy); \
__macro(mublasSgemv); \
__macro(mublasDgemv); \
__macro(mublasCgemv); \
__macro(mublasZgemv); \
__macro(mublasSgemm); \
__macro(mublasDgemm); \
__macro(mublasCgemm); \
__macro(mublasZgemm); \
__macro(mublasHgemm); \
__macro(mublasSgeam); \
__macro(mublasDgeam); \
__macro(mublasDtrsm); \
__macro(mublasCtrsm); \
__macro(mublasZtrsm); \
__macro(mublasCreate); \
__macro(mublasDestroy); \
__macro(mublasSetStream); \
__macro(mublasSetPointerMode); \
__macro(mublasGetPointerMode); \
__macro(mublasSgemmBatched); \
__macro(mublasDgemmBatched); \
__macro(mublasCgemmBatched); \
__macro(mublasZgemmBatched);

MUBLAS_BLAS_ROUTINE_EACH(PLATFORM_DECLARE_DYNAMIC_LOAD_MUBLAS_WRAP)

#undef PLATFORM_DECLARE_DYNAMIC_LOAD_MUBLAS_WRAP
} // namespace dynload
} // namespace platform
} // namespace paddle
27 changes: 27 additions & 0 deletions paddle/fluid/platform/dynload/murand.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) 2016 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/dynload/murand.h"

namespace paddle {
namespace platform {
namespace dynload {

#define DEFINE_WRAP(__name) DynLoad__##__name __name

MURAND_RAND_ROUTINE_EACH(DEFINE_WRAP);

} // namespace dynload
} // namespace platform
} // namespace paddle
43 changes: 43 additions & 0 deletions paddle/fluid/platform/dynload/murand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Copyright (c) 2016 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

#include <murand.h>

#include <mutex> // NOLINT

#include "paddle/phi/backends/dynload/murand.h"

namespace paddle {
namespace platform {
namespace dynload {

#define PLATFORM_DECLARE_DYNAMIC_LOAD_MURAND_WRAP(__name) \
using DynLoad__##__name = phi::dynload::DynLoad__##__name; \
extern DynLoad__##__name __name

#define MURAND_RAND_ROUTINE_EACH(__macro) \
__macro(murandCreateGenerator); \
__macro(murandSetStream); \
__macro(murandSetPseudoRandomGeneratorSeed); \
__macro(murandGenerateUniform); \
__macro(murandGenerateUniformDouble); \
__macro(murandGenerateNormal); \
__macro(murandDestroyGenerator);

MURAND_RAND_ROUTINE_EACH(PLATFORM_DECLARE_DYNAMIC_LOAD_MURAND_WRAP);

} // namespace dynload
} // namespace platform
} // namespace paddle
11 changes: 9 additions & 2 deletions paddle/fluid/platform/dynload/musa_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ 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/dynload/musa_driver.h"

#include "paddle/phi/backends/dynload/musa_driver.h"

namespace paddle {
namespace platform {
namespace dynload {

bool HasCUDADriver() { return false; }
#define DEFINE_WRAP(__name) DynLoad__##__name __name

MUSA_ROUTINE_EACH(DEFINE_WRAP);

bool HasCUDADriver() { return phi::dynload::HasCUDADriver(); }

} // namespace dynload
} // namespace platform
} // namespace paddle

33 changes: 33 additions & 0 deletions paddle/fluid/platform/dynload/musa_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,45 @@ limitations under the License. */

#pragma once

#include <musa.h>

#include <mutex> // NOLINT

#include "paddle/phi/backends/dynload/musa_driver.h"

namespace paddle {
namespace platform {
namespace dynload {

extern bool HasCUDADriver();

#define PLATFORM_DECLARE_DYNAMIC_LOAD_MUSA_WRAP(__name) \
using DynLoad__##__name = phi::dynload::DynLoad__##__name; \
extern DynLoad__##__name __name

/**
* include all needed musa driver functions
**/
#define MUSA_ROUTINE_EACH(__macro) \
__macro(muInit); \
__macro(muDriverGetVersion); \
__macro(muGetErrorString); \
__macro(muModuleLoadData); \
__macro(muModuleGetFunction); \
__macro(muModuleUnload); \
__macro(muOccupancyMaxActiveBlocksPerMultiprocessor); \
__macro(muLaunchKernel); \
__macro(muCtxCreate); \
__macro(muCtxGetCurrent); \
__macro(muDeviceGetCount); \
__macro(muDevicePrimaryCtxGetState); \
__macro(muDeviceGetAttribute); \
__macro(muDeviceGet)

MUSA_ROUTINE_EACH(PLATFORM_DECLARE_DYNAMIC_LOAD_MUSA_WRAP);

#undef PLATFORM_DECLARE_DYNAMIC_LOAD_MUSA_WRAP

} // namespace dynload
} // namespace platform
} // namespace paddle
Expand Down
29 changes: 29 additions & 0 deletions paddle/fluid/platform/dynload/musparse.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* 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/dynload/musparse.h"

namespace paddle {
namespace platform {
namespace dynload {

#define DEFINE_WRAP(__name) DynLoad__##__name __name

#ifdef MUSPARSE_ROUTINE_EACH
MUSPARSE_ROUTINE_EACH(DEFINE_WRAP);
#endif

} // namespace dynload
} // namespace platform
} // namespace paddle
Loading

0 comments on commit 6a656f5

Please sign in to comment.