Skip to content

Commit 4ac4cd2

Browse files
Migraphx ep windows build (microsoft#21284)
### Description Repeat of microsoft#21084 with removal of policy CMP0144 to suppress warnings which uses CMake 3.27.0. ### Motivation and Context Already approved PR: microsoft#21084 Removed the added policy from CMake 3.27.0.
1 parent 42b7ced commit 4ac4cd2

22 files changed

+410
-161
lines changed

cmake/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,9 +1488,6 @@ if (onnxruntime_USE_CUDA)
14881488
endif()
14891489

14901490
if (onnxruntime_USE_MIGRAPHX)
1491-
if (WIN32)
1492-
message(FATAL_ERROR "MIGraphX does not support build in Windows!")
1493-
endif()
14941491
set(AMD_MIGRAPHX_HOME ${onnxruntime_MIGRAPHX_HOME})
14951492
endif()
14961493

@@ -1560,7 +1557,7 @@ if (UNIX OR onnxruntime_USE_NCCL)
15601557
if (onnxruntime_USE_NCCL)
15611558
if (onnxruntime_USE_CUDA)
15621559
set(NCCL_LIBNAME "nccl")
1563-
elseif (onnxruntime_USE_ROCM)
1560+
elseif (onnxruntime_USE_ROCM OR onnxruntime_USE_MIGRAPHX)
15641561
set(NCCL_LIBNAME "rccl")
15651562
endif()
15661563
find_path(NCCL_INCLUDE_DIR

cmake/onnxruntime_providers_migraphx.cmake

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@
1919
endif()
2020

2121
# Add search paths for default rocm installation
22-
list(APPEND CMAKE_PREFIX_PATH /opt/rocm/hcc /opt/rocm/hip /opt/rocm)
22+
list(APPEND CMAKE_PREFIX_PATH /opt/rocm/hcc /opt/rocm/hip /opt/rocm $ENV{HIP_PATH})
2323

24-
find_package(hip)
25-
find_package(migraphx PATHS ${AMD_MIGRAPHX_HOME})
24+
# Suppress the warning about the small capitals of the package name - Enable when support to CMake 3.27.0 is used
25+
# cmake_policy(SET CMP0144 NEW)
2626

27-
find_package(miopen)
28-
find_package(rocblas)
27+
if(WIN32 AND NOT HIP_PLATFORM)
28+
set(HIP_PLATFORM "amd")
29+
endif()
30+
31+
find_package(hip REQUIRED)
32+
find_package(migraphx REQUIRED PATHS ${AMD_MIGRAPHX_HOME})
2933

30-
set(migraphx_libs migraphx::c hip::host MIOpen roc::rocblas)
34+
set(migraphx_libs migraphx::c hip::host)
3135

3236
file(GLOB_RECURSE onnxruntime_providers_migraphx_cc_srcs CONFIGURE_DEPENDS
3337
"${ONNXRUNTIME_ROOT}/core/providers/migraphx/*.h"
3438
"${ONNXRUNTIME_ROOT}/core/providers/migraphx/*.cc"
3539
"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.h"
3640
"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.cc"
37-
"${ONNXRUNTIME_ROOT}/core/providers/rocm/rocm_stream_handle.h"
38-
"${ONNXRUNTIME_ROOT}/core/providers/rocm/rocm_stream_handle.cc"
3941
)
4042
source_group(TREE ${ONNXRUNTIME_ROOT}/core FILES ${onnxruntime_providers_migraphx_cc_srcs})
4143
onnxruntime_add_shared_library_module(onnxruntime_providers_migraphx ${onnxruntime_providers_migraphx_cc_srcs})
@@ -46,18 +48,16 @@
4648
set_target_properties(onnxruntime_providers_migraphx PROPERTIES LINKER_LANGUAGE CXX)
4749
set_target_properties(onnxruntime_providers_migraphx PROPERTIES FOLDER "ONNXRuntime")
4850
target_compile_definitions(onnxruntime_providers_migraphx PRIVATE ONNXIFI_BUILD_LIBRARY=1)
49-
target_compile_options(onnxruntime_providers_migraphx PRIVATE -Wno-error=sign-compare)
50-
set_property(TARGET onnxruntime_providers_migraphx APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-deprecated-declarations")
51-
set_property(TARGET onnxruntime_providers_migraphx APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker --version-script=${ONNXRUNTIME_ROOT}/core/providers/migraphx/version_script.lds -Xlinker --gc-sections")
52-
target_link_libraries(onnxruntime_providers_migraphx PRIVATE nsync::nsync_cpp)
53-
54-
include(CheckLibraryExists)
55-
check_library_exists(migraphx::c "migraphx_program_run_async" "/opt/rocm/migraphx/lib" HAS_STREAM_SYNC)
56-
if(HAS_STREAM_SYNC)
57-
target_compile_definitions(onnxruntime_providers_migraphx PRIVATE -DMIGRAPHX_STREAM_SYNC)
58-
message(STATUS "MIGRAPHX GPU STREAM SYNC is ENABLED")
51+
if(MSVC)
52+
set_property(TARGET onnxruntime_providers_migraphx APPEND_STRING PROPERTY LINK_FLAGS /DEF:${ONNXRUNTIME_ROOT}/core/providers/migraphx/symbols.def)
53+
target_link_libraries(onnxruntime_providers_migraphx PRIVATE ws2_32)
5954
else()
60-
message(STATUS "MIGRAPHX GPU STREAM SYNC is DISABLED")
55+
target_compile_options(onnxruntime_providers_migraphx PRIVATE -Wno-error=sign-compare)
56+
set_property(TARGET onnxruntime_providers_migraphx APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-deprecated-declarations")
57+
endif()
58+
if(UNIX)
59+
set_property(TARGET onnxruntime_providers_migraphx APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker --version-script=${ONNXRUNTIME_ROOT}/core/providers/migraphx/version_script.lds -Xlinker --gc-sections")
60+
target_link_libraries(onnxruntime_providers_migraphx PRIVATE nsync::nsync_cpp stdc++fs)
6161
endif()
6262

6363
if (onnxruntime_ENABLE_TRAINING_OPS)
@@ -68,8 +68,16 @@
6868
endif()
6969
endif()
7070

71-
install(TARGETS onnxruntime_providers_migraphx
72-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
73-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
74-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
75-
)
71+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
72+
install(TARGETS onnxruntime_providers_migraphx
73+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
74+
LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR}
75+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
76+
)
77+
else()
78+
install(TARGETS onnxruntime_providers_migraphx
79+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
80+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
81+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
82+
)
83+
endif()

onnxruntime/core/providers/migraphx/gpu_data_transfer.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,7 @@ common::Status GPUDataTransfer::CopyTensorAsync(const Tensor& src, Tensor& dst,
6060
HIP_CALL_THROW(hipMemcpy(dst_data, src_data, bytes, hipMemcpyHostToDevice));
6161
}
6262
} else if (src_device.Type() == OrtDevice::GPU) {
63-
#ifndef MIGRAPHX_STREAM_SYNC
64-
if (dst_device.Type() == OrtDevice::CPU && dst_device.MemType() == OrtDevice::MemType::HIP_PINNED) {
65-
// copying from GPU to pinned memory, this is non-blocking
66-
HIP_CALL_THROW(hipMemcpyAsync(dst_data, src_data, bytes, hipMemcpyDeviceToHost, static_cast<hipStream_t>(stream.GetHandle())));
67-
} else {
68-
// copying from GPU to CPU memory, this is blocking
69-
HIP_CALL_THROW(hipMemcpy(dst_data, src_data, bytes, hipMemcpyDeviceToHost));
70-
}
71-
#else
7263
HIP_CALL_THROW(hipMemcpyAsync(dst_data, src_data, bytes, hipMemcpyDeviceToHost, static_cast<hipStream_t>(stream.GetHandle())));
73-
#endif
7464
} else {
7565
// copying between cpu memory
7666
memcpy(dst_data, src_data, bytes);

onnxruntime/core/providers/migraphx/hip_allocator.cc renamed to onnxruntime/core/providers/migraphx/migraphx_allocator.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
#include "core/providers/shared_library/provider_api.h"
55
#include "migraphx_call.h"
6-
#include "hip_allocator.h"
6+
#include "migraphx_allocator.h"
77
#include "core/common/status.h"
88
#include "core/framework/float16.h"
99
#include "core/common/status.h"
1010
#include "gpu_data_transfer.h"
1111

1212
namespace onnxruntime {
1313

14-
void HIPAllocator::CheckDevice() const {
14+
void MIGraphXAllocator::CheckDevice() const {
1515
#ifndef NDEBUG
1616
// check device to match at debug build
1717
// if it's expected to change, call hipSetDevice instead of the check
@@ -23,7 +23,7 @@ void HIPAllocator::CheckDevice() const {
2323
#endif
2424
}
2525

26-
void* HIPAllocator::Alloc(size_t size) {
26+
void* MIGraphXAllocator::Alloc(size_t size) {
2727
CheckDevice();
2828
void* p = nullptr;
2929
if (size > 0) {
@@ -32,12 +32,12 @@ void* HIPAllocator::Alloc(size_t size) {
3232
return p;
3333
}
3434

35-
void HIPAllocator::Free(void* p) {
35+
void MIGraphXAllocator::Free(void* p) {
3636
CheckDevice();
3737
(void)hipFree(p); // do not throw error since it's OK for hipFree to fail during shutdown
3838
}
3939

40-
void* HIPExternalAllocator::Alloc(size_t size) {
40+
void* MIGraphXExternalAllocator::Alloc(size_t size) {
4141
void* p = nullptr;
4242
if (size > 0) {
4343
p = alloc_(size);
@@ -49,7 +49,7 @@ void* HIPExternalAllocator::Alloc(size_t size) {
4949
return p;
5050
}
5151

52-
void HIPExternalAllocator::Free(void* p) {
52+
void MIGraphXExternalAllocator::Free(void* p) {
5353
free_(p);
5454
std::lock_guard<OrtMutex> lock(lock_);
5555
auto it = reserved_.find(p);
@@ -59,7 +59,7 @@ void HIPExternalAllocator::Free(void* p) {
5959
}
6060
}
6161

62-
void* HIPExternalAllocator::Reserve(size_t size) {
62+
void* MIGraphXExternalAllocator::Reserve(size_t size) {
6363
void* p = Alloc(size);
6464
if (!p) return nullptr;
6565
std::lock_guard<OrtMutex> lock(lock_);

onnxruntime/core/providers/migraphx/hip_allocator.h renamed to onnxruntime/core/providers/migraphx/migraphx_allocator.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
namespace onnxruntime {
1111

12-
class HIPAllocator : public IAllocator {
12+
class MIGraphXAllocator : public IAllocator {
1313
public:
14-
HIPAllocator(int device_id, const char* name)
14+
MIGraphXAllocator(int device_id, const char* name)
1515
: IAllocator(
1616
OrtMemoryInfo(name, OrtAllocatorType::OrtDeviceAllocator,
17-
OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, device_id),
17+
OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, static_cast<OrtDevice::DeviceId>(device_id)),
1818
device_id, OrtMemTypeDefault)) {}
1919

2020
virtual void* Alloc(size_t size) override;
@@ -24,14 +24,14 @@ class HIPAllocator : public IAllocator {
2424
void CheckDevice() const;
2525
};
2626

27-
class HIPExternalAllocator : public HIPAllocator {
27+
class MIGraphXExternalAllocator : public MIGraphXAllocator {
2828
typedef void* (*ExternalAlloc)(size_t size);
2929
typedef void (*ExternalFree)(void* p);
3030
typedef void (*ExternalEmptyCache)();
3131

3232
public:
33-
HIPExternalAllocator(OrtDevice::DeviceId device_id, const char* name, void* alloc, void* free, void* empty_cache)
34-
: HIPAllocator(device_id, name) {
33+
MIGraphXExternalAllocator(OrtDevice::DeviceId device_id, const char* name, void* alloc, void* free, void* empty_cache)
34+
: MIGraphXAllocator(device_id, name) {
3535
alloc_ = reinterpret_cast<ExternalAlloc>(alloc);
3636
free_ = reinterpret_cast<ExternalFree>(free);
3737
empty_cache_ = reinterpret_cast<ExternalEmptyCache>(empty_cache);
@@ -55,7 +55,7 @@ class HIPPinnedAllocator : public IAllocator {
5555
HIPPinnedAllocator(int device_id, const char* name)
5656
: IAllocator(
5757
OrtMemoryInfo(name, OrtAllocatorType::OrtDeviceAllocator,
58-
OrtDevice(OrtDevice::CPU, OrtDevice::MemType::HIP_PINNED, device_id),
58+
OrtDevice(OrtDevice::CPU, OrtDevice::MemType::HIP_PINNED, static_cast<OrtDevice::DeviceId>(device_id)),
5959
device_id, OrtMemTypeCPUOutput)) {}
6060

6161
virtual void* Alloc(size_t size) override;

onnxruntime/core/providers/migraphx/migraphx_call.cc

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
#ifdef _WIN32
5+
#include <winsock.h>
6+
#else
47
#include <unistd.h>
5-
#include <string.h>
6-
#include <miopen/miopen.h>
7-
#include <rocblas/rocblas.h>
8+
#endif
9+
10+
#include <string>
811
#include "core/common/common.h"
912
#include "core/common/status.h"
1013
#include "core/providers/shared_library/provider_api.h"
@@ -34,16 +37,20 @@ std::conditional_t<THRW, void, Status> RocmCall(
3437
ERRTYPE retCode, const char* exprString, const char* libName, ERRTYPE successCode, const char* msg, const char* file, const int line) {
3538
if (retCode != successCode) {
3639
try {
37-
char hostname[HOST_NAME_MAX];
38-
if (gethostname(hostname, HOST_NAME_MAX) != 0)
39-
strcpy(hostname, "?");
40+
#ifdef _WIN32
41+
// According to the POSIX spec, 255 is the safe minimum value.
42+
static constexpr int HOST_NAME_MAX = 255;
43+
#endif
44+
std::string hostname(HOST_NAME_MAX, 0);
45+
if (gethostname(hostname.data(), HOST_NAME_MAX) != 0)
46+
hostname = "?";
4047
int currentHipDevice;
4148
(void)hipGetDevice(&currentHipDevice);
4249
(void)hipGetLastError(); // clear last HIP error
4350
static char str[1024];
4451
snprintf(str, 1024, "%s failure %d: %s ; GPU=%d ; hostname=%s ; file=%s ; line=%d ; expr=%s; %s",
4552
libName, (int)retCode, RocmErrString(retCode), currentHipDevice,
46-
hostname,
53+
hostname.c_str(),
4754
file, line, exprString, msg);
4855
if constexpr (THRW) {
4956
// throw an exception with the error info
@@ -68,9 +75,5 @@ std::conditional_t<THRW, void, Status> RocmCall(
6875

6976
template Status RocmCall<hipError_t, false>(hipError_t retCode, const char* exprString, const char* libName, hipError_t successCode, const char* msg, const char* file, const int line);
7077
template void RocmCall<hipError_t, true>(hipError_t retCode, const char* exprString, const char* libName, hipError_t successCode, const char* msg, const char* file, const int line);
71-
template Status RocmCall<rocblas_status, false>(rocblas_status retCode, const char* exprString, const char* libName, rocblas_status successCode, const char* msg, const char* file, const int line);
72-
template void RocmCall<rocblas_status, true>(rocblas_status retCode, const char* exprString, const char* libName, rocblas_status successCode, const char* msg, const char* file, const int line);
73-
template Status RocmCall<miopenStatus_t, false>(miopenStatus_t retCode, const char* exprString, const char* libName, miopenStatus_t successCode, const char* msg, const char* file, const int line);
74-
template void RocmCall<miopenStatus_t, true>(miopenStatus_t retCode, const char* exprString, const char* libName, miopenStatus_t successCode, const char* msg, const char* file, const int line);
7578

7679
} // namespace onnxruntime

onnxruntime/core/providers/migraphx/migraphx_call.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#pragma once
55
#include "migraphx_inc.h"
66

7-
#pragma once
8-
97
namespace onnxruntime {
108

119
// -----------------------------------------------------------------------

0 commit comments

Comments
 (0)