Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better AMD GPUs support through ROCm/HIP #115

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ DetectionSample
settings.ini
logo.rc
*.aps
.cache

*.json
4 changes: 4 additions & 0 deletions CLI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ if(Build_CLI)
endif()
endif()

if (Enable_HIP)
target_link_libraries(${PROJECT_NAME} PRIVATE hip::host)
endif()

include(${TOP_DIR}/cmake/ThirdPartyForCLI.cmake)

install(
Expand Down
57 changes: 54 additions & 3 deletions CLI/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace filesystem = std::filesystem;

enum class GPGPU
{
OpenCL, CUDA, NCNN
OpenCL, CUDA, NCNN, HIP
};

static bool checkFFmpeg()
Expand Down Expand Up @@ -132,7 +132,7 @@ static void showVersionInfo()

static void showGPUList()
{
#if !defined(ENABLE_OPENCL) && !defined(ENABLE_CUDA) && !defined(ENABLE_NCNN)
#if !defined(ENABLE_OPENCL) && !defined(ENABLE_CUDA) && !defined(ENABLE_HIP) && !defined(ENABLE_NCNN)
std::cerr << "Error: No GPU acceleration mode supported\n";
#endif

Expand All @@ -154,6 +154,15 @@ static void showGPUList()
std::cout << CUDAGPUList();
#endif

#ifdef ENABLE_HIP
std::cout << "\nHIP:\n";
Anime4KCPP::Hip::GPUList HIPGPUList = Anime4KCPP::Hip::listGPUs();
if (HIPGPUList.devices == 0)
std::cerr << "Error: No HIP GPU found\n";
else
std::cout << HIPGPUList();
#endif

#ifdef ENABLE_NCNN
std::cout << "\nncnn:\n";
Anime4KCPP::NCNN::GPUList NCNNGPUList = Anime4KCPP::NCNN::listGPUs();
Expand Down Expand Up @@ -184,6 +193,12 @@ static void benchmark(const int pID, const int dID)
double CudaScoreFHD = Anime4KCPP::benchmark<Anime4KCPP::Cuda::ACNet, 1920, 1080>(dID);
#endif

#ifdef ENABLE_HIP
double HipScoreDVD = Anime4KCPP::benchmark<Anime4KCPP::Hip::ACNet, 720, 480>(dID);
double HipScoreHD = Anime4KCPP::benchmark<Anime4KCPP::Hip::ACNet, 1280, 720>(dID);
double HipScoreFHD = Anime4KCPP::benchmark<Anime4KCPP::Hip::ACNet, 1920, 1080>(dID);
#endif

#ifdef ENABLE_NCNN
double NCNNCPUScoreDVD = Anime4KCPP::benchmark<Anime4KCPP::NCNN::ACNet, 720, 480>(-1, Anime4KCPP::CNNType::ACNetHDNL0, 4);
double NCNNCPUScoreHD = Anime4KCPP::benchmark<Anime4KCPP::NCNN::ACNet, 1280, 720>(-1, Anime4KCPP::CNNType::ACNetHDNL0, 4);
Expand Down Expand Up @@ -216,6 +231,14 @@ static void benchmark(const int pID, const int dID)
<< " FHD(1080P->2160P): " << CudaScoreFHD << " FPS\n" << std::endl;
#endif

#ifdef ENABLE_HIP
std::cout
<< "HIP score: (dID = " << dID << ")\n"
<< " DVD(480P->960P): " << HipScoreDVD << " FPS\n"
<< " HD(720P->1440P): " << HipScoreHD << " FPS\n"
<< " FHD(1080P->2160P): " << HipScoreFHD << " FPS\n" << std::endl;
#endif

#ifdef ENABLE_NCNN
std::cout
<< "NCNN CPU score:\n"
Expand Down Expand Up @@ -443,6 +466,8 @@ int main(int argc, char* argv[])
GPGPUModel = GPGPU::OpenCL;
#elif defined(ENABLE_CUDA)
GPGPUModel = GPGPU::CUDA;
#elif defined(ENABLE_HIP)
GPGPUModel = GPGPU::HIP;
#elif defined(ENABLE_NCNN)
GPGPUModel = GPGPU::NCNN;
#else
Expand All @@ -454,11 +479,13 @@ int main(int argc, char* argv[])
}
else if (GPGPUModelString == "cuda")
GPGPUModel = GPGPU::CUDA;
else if (GPGPUModelString == "hip")
GPGPUModel = GPGPU::HIP;
else if (GPGPUModelString == "ncnn")
GPGPUModel = GPGPU::NCNN;
else
{
logErrorAndExit(R"(Unknown GPGPU model, it must be "ncnn", "cuda" or "opencl")");
logErrorAndExit(R"(Unknown GPGPU model, it must be "ncnn", "cuda", "hip" or "opencl")");
}

if (ncnn)
Expand Down Expand Up @@ -574,6 +601,13 @@ int main(int argc, char* argv[])
logErrorAndExit("CUDA is not supported");
#else
initializer.pushManager<Anime4KCPP::Cuda::Manager>(dID);
#endif
break;
case GPGPU::HIP:
#ifndef ENABLE_HIP
logErrorAndExit("HIP is not supported");
#else
initializer.pushManager<Anime4KCPP::Hip::Manager>(dID);
#endif
break;
case GPGPU::NCNN:
Expand Down Expand Up @@ -646,6 +680,23 @@ int main(int argc, char* argv[])
ac = Anime4KCPP::ACCreator::createUP(parameters, Anime4KCPP::Processor::Type::Cuda_ACNet);
else
ac = Anime4KCPP::ACCreator::createUP(parameters, Anime4KCPP::Processor::Type::Cuda_Anime4K09);
#endif
}
break;
case GPGPU::HIP:
{
#ifdef ENABLE_HIP
Anime4KCPP::Hip::GPUInfo ret = Anime4KCPP::Hip::checkGPUSupport(dID);
if (!ret)
{
logErrorAndExit(ret());
}
else
std::cout << ret() << '\n';
if (CNN)
ac = Anime4KCPP::ACCreator::createUP(parameters, Anime4KCPP::Processor::Type::Hip_ACNet);
else
ac = Anime4KCPP::ACCreator::createUP(parameters, Anime4KCPP::Processor::Type::Hip_Anime4K09);
#endif
}
break;
Expand Down
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ option(Other_Optimization_For_Other "Enable other optimizatio flag for other if
option(Enable_IPO "Enable link time optimization, may cause unknown bugs even slow performance" OFF)
option(Maximum_Optimization "Enable maximum optimizations for the current platform" OFF)
option(Enable_CUDA "Enable CUDA module" OFF)
option(Enable_HIP "Enable HIP module" OFF)
option(Enable_OpenCL "Enable OpenCL module" ON)
option(Enable_NCNN "Enable ncnn module" OFF)
option(Enable_Fast_Math "Enable fast math" OFF)
Expand Down Expand Up @@ -89,6 +90,9 @@ if(Enable_CUDA)
set(CMAKE_CUDA_ARCHITECTURES ${CUDA_Minimum_CC}-virtual ${CUDA_CC}-real)
endif()
enable_language(CUDA)
elseif(Enable_HIP)
enable_language(HIP)
find_package(hip REQUIRED)
endif()

if(Use_Eigen3)
Expand Down Expand Up @@ -209,7 +213,7 @@ set(VapourSynth_SDK_PATH "VapourSynth SDK path" CACHE PATH "Where to look for Va
set(AviSynthPlus_SDK_PATH "AviSynthPlus SDK path" CACHE PATH "Where to look for AviSynthPlus SDK")

if(NOT Disable_Parallel)
set(Parallel_Library "Auto" CACHE STRING "Auto, PPL, OpenMP, TBB or Built-in")
set(Parallel_Library "TBB" CACHE STRING "Auto, PPL, OpenMP, TBB or Built-in")

if(NOT Parallel_Library MATCHES "^(Auto|PPL|OpenMP|TBB|Built-in)$")
message (
Expand Down Expand Up @@ -308,6 +312,7 @@ message(STATUS
" Enable IPO ${Enable_IPO}\n"
" Enable OpenCL ${Enable_OpenCL}\n"
" Enable CUDA ${Enable_CUDA}\n"
" Enable HIP ${Enable_HIP}\n"
" Enable NCNN ${Enable_NCNN}\n"
" Enable OpenCV DNN ${Enable_OpenCV_DNN}\n"
" Enable video ${Enable_Video}\n"
Expand Down
2 changes: 2 additions & 0 deletions CWrapper/include/AC.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ extern "C"
AC_OpenCL_ACNet,
AC_Cuda_Anime4K09,
AC_Cuda_ACNet,
AC_Hip_Anime4K09,
AC_Hip_ACNet,
//deprecated, use AC_CPU_Anime4K09
AC_CPU = AC_CPU_Anime4K09,
//deprecated, use AC_CPU_ACNet
Expand Down
2 changes: 1 addition & 1 deletion DSFilter/include/ACProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace GPGPU
{
static constexpr int CPU = 0, OpenCL = 1, CUDA = 2;
static constexpr int CPU = 0, OpenCL = 1, CUDA = 2, HIP = 3;
};

struct ACPropData
Expand Down
15 changes: 15 additions & 0 deletions DSFilter/src/Anime4KCPPDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ Anime4KCPPDS::Anime4KCPPDS(TCHAR* tszName,
GetPrivateProfileString(L"Anime4KCPP for DirectShow Config", L"GPGPUModel", L"OpenCL", _GPGPUModelString, 10, lpPath);
#elif defined(ENABLE_CUDA)
GetPrivateProfileString(L"Anime4KCPP for DirectShow Config", L"GPGPUModel", L"CUDA", _GPGPUModelString, 10, lpPath);
#elif defined(ENABLE_HIP)
GetPrivateProfileString(L"Anime4KCPP for DirectShow Config", L"GPGPUModel", L"HIP", _GPGPUModelString, 10, lpPath);
#else
GetPrivateProfileString(L"Anime4KCPP for DirectShow Config", L"GPGPUModel", L"CPU", _GPGPUModelString, 10, lpPath);
#endif
Expand Down Expand Up @@ -209,6 +211,11 @@ Anime4KCPPDS::Anime4KCPPDS(TCHAR* tszName,
initializer.pushManager<Anime4KCPP::Cuda::Manager>(data.dID);
#endif
break;
case GPGPU::HIP:
#ifdef ENABLE_HIP
initializer.pushManager<Anime4KCPP::Hip::Manager>(data.dID);
#endif
break;
case GPGPU::CPU:
initializer.pushManager<Anime4KCPP::CPU::Manager>();
break;
Expand Down Expand Up @@ -523,6 +530,14 @@ HRESULT Anime4KCPPDS::Transform(IMediaSample* pIn, IMediaSample* pOut)
ac = Anime4KCPP::ACCreator::createUP(parameters, Anime4KCPP::Processor::Type::Cuda_Anime4K09);
#endif
break;
case GPGPU::HIP:
#ifdef ENABLE_HIP
if (data.CNN)
ac = Anime4KCPP::ACCreator::createUP(parameters, Anime4KCPP::Processor::Type::Hip_ACNet);
else
ac = Anime4KCPP::ACCreator::createUP(parameters, Anime4KCPP::Processor::Type::Hip_Anime4K09);
#endif
break;
case GPGPU::CPU:
if (data.CNN)
ac = Anime4KCPP::ACCreator::createUP(parameters, Anime4KCPP::Processor::Type::CPU_ACNet);
Expand Down
5 changes: 5 additions & 0 deletions VapourSynth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ if(Build_VapourSynth_Plugin)

aux_source_directory(src SOURCE)

find_package(PkgConfig REQUIRED)
pkg_check_modules(VAPOURSYNTH REQUIRED IMPORTED_TARGET vapoursynth)

add_library(${PROJECT_NAME} SHARED ${SOURCE})
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::VAPOURSYNTH)
target_include_directories(${PROJECT_NAME} PUBLIC ${VAPOURSYNTH_INCLUDE_DIRS})

if(Other_Optimization_For_Other)
if(MSVC)
Expand Down
18 changes: 18 additions & 0 deletions cmake/ThirdPartyForCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ if(NOT Disable_Parallel)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
elseif(Parallel_Library_Type STREQUAL TBB)
find_package(TBB REQUIRED)
# if (NOT TBB_FOUND OR TBB_VERSION VERSION_LESS 2021.5)
# message(WARNING "TBB not found or has version lower than 2021.5, fetching external TBB")
# include(FetchContent)
# set (TBB_TEST OFF CACHE INTERNAL "Turn off TBB tests")
# set (TBB_INSTALL OFF CACHE INTERNAL "Turn off TBB install")
# # Store the old value of the 'BUILD_SHARED_LIBS'
# set(BUILD_SHARED_LIBS_OLD ${BUILD_SHARED_LIBS})
# # Make subproject to use 'BUILD_SHARED_LIBS=OFF' setting.
# set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Build SHARED libraries")
# FetchContent_Declare(TBB
# URL https://github.com/oneapi-src/oneTBB/archive/refs/tags/v2021.11.0.tar.gz
# URL_HASH SHA256=782ce0cab62df9ea125cdea253a50534862b563f1d85d4cda7ad4e77550ac363
# )
# FetchContent_MakeAvailable(TBB)
# # Restore the old value of the parameter
# set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_OLD} CACHE BOOL "Type of libraries to build" FORCE)
# endif()
# # message("TBB version: ${TBB_VERSION}")
target_link_libraries(${PROJECT_NAME} PUBLIC TBB::tbb)
endif()
elseif(Enable_Video)
Expand Down
9 changes: 9 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ if(Build_Static_Core)
add_library(${PROJECT_NAME} STATIC
${INCLUDE} ${SOURCE}
$<$<BOOL:${Enable_CUDA}>:$<TARGET_OBJECTS:CUDA_Module>>
$<$<BOOL:${Enable_HIP}>:$<TARGET_OBJECTS:HIP_Module>>
)
else()
add_library(${PROJECT_NAME} SHARED
${INCLUDE} ${SOURCE}
$<$<BOOL:${Enable_CUDA}>:$<TARGET_OBJECTS:CUDA_Module>>
$<$<BOOL:${Enable_HIP}>:$<TARGET_OBJECTS:HIP_Module>>
)
endif()

Expand All @@ -21,6 +23,9 @@ target_compile_definitions(
PUBLIC
$<$<BOOL:${Enable_OpenCL}>:ENABLE_OPENCL>
$<$<BOOL:${Enable_CUDA}>:ENABLE_CUDA>
$<$<BOOL:${Enable_HIP}>:ENABLE_HIP>
# # This allows HIP to emulate as CUDA
# $<$<BOOL:${Enable_HIP}>:ENABLE_CUDA>
$<$<BOOL:${Enable_NCNN}>:ENABLE_NCNN>
$<$<BOOL:${Enable_Video}>:ENABLE_VIDEO>
$<$<BOOL:${Enable_Preview_GUI}>:ENABLE_PREVIEW_GUI>
Expand Down Expand Up @@ -67,6 +72,10 @@ if(Other_Optimization_For_Core)
endif()
endif()

if (Enable_HIP)
target_link_libraries(${PROJECT_NAME} PRIVATE hip::host)
endif()

include(${TOP_DIR}/cmake/ThirdPartyForCore.cmake)

install(
Expand Down
4 changes: 4 additions & 0 deletions core/include/ACCreator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include "ACCuda.hpp"
#endif

#ifdef ENABLE_HIP
#include "ACHip.hpp"
#endif

#ifdef ENABLE_NCNN
#include "ACNCNN.hpp"
#endif
Expand Down
72 changes: 72 additions & 0 deletions core/include/ACHip.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef ANIME4KCPP_CORE_AC_HIP_HPP
#define ANIME4KCPP_CORE_AC_HIP_HPP

#ifdef ENABLE_HIP

#include "HipAnime4K09.hpp"
#include "HipACNet.hpp"
#include "ACManager.hpp"

namespace Anime4KCPP
{
namespace Hip
{
class AC_EXPORT Manager;

struct AC_EXPORT GPUList;
struct AC_EXPORT GPUInfo;

//return platforms, devices of each platform, all devices information
AC_EXPORT GPUList listGPUs() noexcept;
//return result and information
AC_EXPORT GPUInfo checkGPUSupport(int dID) noexcept;
}

namespace Processor
{
template<>
struct GetManager<Hip::ACNet> {
using Manager = Hip::Manager;
};
template<>
struct GetManager<Hip::Anime4K09> {
using Manager = Hip::Manager;
};
}
}

class Anime4KCPP::Hip::Manager : public Anime4KCPP::Processor::Manager
{
public:
Manager(int dID = 0) noexcept;
void init() override;
void release() noexcept override;
bool isInitialized() noexcept override;
bool isSupport() noexcept override;
const char* name() noexcept override { return "HIP Processor Manager"; };
private:
int dID;
};

struct Anime4KCPP::Hip::GPUList
{
int devices;
std::string message;

GPUList(int devices, std::string message);
std::string& operator()() noexcept;
};

struct Anime4KCPP::Hip::GPUInfo
{
bool supported;
std::string message;

GPUInfo(bool supported, std::string message);
std::string& operator()() noexcept;
operator bool() const noexcept;
};

#endif

#endif // !ANIME4KCPP_CORE_AC_HIP_HPP
Loading