Skip to content

Commit

Permalink
Amdmigraphx fix build error (#9272)
Browse files Browse the repository at this point in the history
* fix build error

* rename a missing api for the MIGraphX EP
  • Loading branch information
scxiao authored Jan 10, 2022
1 parent be9cc40 commit ce103ac
Show file tree
Hide file tree
Showing 42 changed files with 1,146 additions and 464 deletions.
1 change: 0 additions & 1 deletion cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ set(onnxruntime_INTERNAL_LIBRARIES
${PROVIDERS_ARMNN}
${PROVIDERS_COREML}
${PROVIDERS_DML}
${PROVIDERS_MIGRAPHX}
${PROVIDERS_NNAPI}
${PROVIDERS_NUPHAR}
${PROVIDERS_STVM}
Expand Down
43 changes: 35 additions & 8 deletions cmake/onnxruntime_providers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,23 @@ if (onnxruntime_USE_DML)
endif()

if (onnxruntime_USE_MIGRAPHX)
add_definitions(-DUSE_MIGRAPHX=1)
set(BUILD_LIBRARY_ONLY 1)
add_definitions("-DONNX_ML=1")
add_definitions("-DONNX_NAMESPACE=onnx")
include_directories(${PROJECT_SOURCE_DIR}/external/protobuf ${PROJECT_SOURCE_DIR}/external/eigen)
set(MIGRAPHX_ROOT ${onnxruntime_MIGRAPHX_HOME})
include_directories(${ONNXRUNTIME_ROOT}/../cmake/external/onnx)
set(OLD_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
if ( CMAKE_COMPILER_IS_GNUCC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-missing-field-initializers")
endif()
set(CXX_VERSION_DEFINED TRUE)
set(CMAKE_CXX_FLAGS ${OLD_CMAKE_CXX_FLAGS})
if ( CMAKE_COMPILER_IS_GNUCC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter")
endif()

# Add search paths for default rocm installation
list(APPEND CMAKE_PREFIX_PATH /opt/rocm/hcc /opt/rocm/hip /opt/rocm)

Expand All @@ -1033,18 +1050,28 @@ if (onnxruntime_USE_MIGRAPHX)
file(GLOB_RECURSE onnxruntime_providers_migraphx_cc_srcs CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/core/providers/migraphx/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/migraphx/*.cc"
"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.cc"
)

source_group(TREE ${ONNXRUNTIME_ROOT}/core FILES ${onnxruntime_providers_migraphx_cc_srcs})
onnxruntime_add_static_library(onnxruntime_providers_migraphx ${onnxruntime_providers_migraphx_cc_srcs})
target_link_libraries(onnxruntime_providers_migraphx PRIVATE ${migraphx_libs})
set_target_properties(onnxruntime_providers_migraphx PROPERTIES FOLDER "ONNXRuntime")
target_compile_options(onnxruntime_providers_migraphx PRIVATE -Wno-error=sign-compare)
target_include_directories(onnxruntime_providers_migraphx PRIVATE ${ONNXRUNTIME_ROOT})
onnxruntime_add_include_to_target(onnxruntime_providers_migraphx onnxruntime_common onnxruntime_framework onnx flatbuffers)
add_dependencies(onnxruntime_providers_migraphx ${onnxruntime_EXTERNAL_DEPENDENCIES})
onnxruntime_add_shared_library_module(onnxruntime_providers_migraphx ${onnxruntime_providers_migraphx_cc_srcs})
onnxruntime_add_include_to_target(onnxruntime_providers_migraphx onnxruntime_common onnx flatbuffers)
add_dependencies(onnxruntime_providers_migraphx onnxruntime_providers_shared ${onnxruntime_EXTERNAL_DEPENDENCIES})
target_link_libraries(onnxruntime_providers_migraphx PRIVATE ${migraphx_libs} ${ONNXRUNTIME_PROVIDERS_SHARED} onnx flatbuffers)
target_include_directories(onnxruntime_providers_migraphx PRIVATE ${ONNXRUNTIME_ROOT} ${CMAKE_CURRENT_BINARY_DIR})
install(DIRECTORY ${PROJECT_SOURCE_DIR}/../include/onnxruntime/core/providers/migraphx DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/onnxruntime/core/providers)
set_target_properties(onnxruntime_providers_migraphx PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(onnxruntime_providers_migraphx PROPERTIES FOLDER "ONNXRuntime")
target_compile_definitions(onnxruntime_providers_migraphx PRIVATE ONNXIFI_BUILD_LIBRARY=1)
target_compile_options(onnxruntime_providers_migraphx PRIVATE -Wno-error=sign-compare)
set_property(TARGET onnxruntime_providers_migraphx APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-deprecated-declarations")
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")
target_link_libraries(onnxruntime_providers_migraphx PRIVATE nsync_cpp stdc++fs)

install(TARGETS onnxruntime_providers_migraphx
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

if (onnxruntime_USE_ACL)
Expand Down
11 changes: 10 additions & 1 deletion cmake/onnxruntime_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ endif()
target_link_libraries(onnxruntime_pybind11_state PRIVATE
onnxruntime_session
${onnxruntime_libs}
${PROVIDERS_MIGRAPHX}
${PROVIDERS_NUPHAR}
${PROVIDERS_STVM}
${PROVIDERS_VITISAI}
Expand Down Expand Up @@ -603,6 +602,16 @@ if (onnxruntime_USE_TENSORRT)
)
endif()

if (onnxruntime_USE_MIGRAPHX)
add_custom_command(
TARGET onnxruntime_pybind11_state POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:onnxruntime_providers_migraphx>
$<TARGET_FILE:onnxruntime_providers_shared>
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/capi/
)
endif()

if (onnxruntime_USE_OPENVINO)
add_custom_command(
TARGET onnxruntime_pybind11_state POST_BUILD
Expand Down
6 changes: 3 additions & 3 deletions cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ if(onnxruntime_USE_DML)
endif()

if(onnxruntime_USE_MIGRAPHX)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_migraphx)
list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_migraphx)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_migraphx onnxruntime_providers_shared)
endif()

if(onnxruntime_USE_ROCM)
Expand Down Expand Up @@ -483,8 +484,7 @@ set(ONNXRUNTIME_TEST_LIBS
onnxruntime_session
${ONNXRUNTIME_INTEROP_TEST_LIBS}
${onnxruntime_libs}
# CUDA, ROCM, TENSORRT, DNNL, and OpenVINO are dynamically loaded at runtime
${PROVIDERS_MIGRAPHX}
# CUDA, ROCM, TENSORRT, MIGRAPHX, DNNL, and OpenVINO are dynamically loaded at runtime
${PROVIDERS_NUPHAR}
${PROVIDERS_NNAPI}
${PROVIDERS_RKNPU}
Expand Down
8 changes: 4 additions & 4 deletions dockerfiles/Dockerfile.migraphx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ ENV LANG C.UTF-8

# Install rocm
RUN apt-get update && apt-get install -y gnupg2 --no-install-recommends curl && \
curl -sL http://repo.radeon.com/rocm/apt/debian/rocm.gpg.key | apt-key add - && \
sh -c 'echo deb [arch=amd64] http://repo.radeon.com/rocm/apt/4.2/ xenial main > /etc/apt/sources.list.d/rocm.list'
curl -sL http://repo.radeon.com/rocm/rocm.gpg.key | apt-key add - && \
sh -c 'echo deb [arch=amd64] http://repo.radeon.com/rocm/apt/4.5/ ubuntu main > /etc/apt/sources.list.d/rocm.list'

RUN apt-get update &&\
apt-get install -y sudo git bash build-essential rocm-dev libpython3.6-dev python3-pip miopen-hip \
rocblas half aria2
rocblas half aria2 libnuma-dev

RUN aria2c -q -d /tmp -o cmake-3.21.0-linux-x86_64.tar.gz \
https://github.com/Kitware/CMake/releases/download/v3.21.0/cmake-3.21.0-linux-x86_64.tar.gz &&\
Expand All @@ -39,7 +39,7 @@ ENV PATH /opt/miniconda/bin:/code/cmake-3.21.0-linux-x86_64/bin:${PATH}
# Install MIGraphX from source
RUN mkdir -p /migraphx
RUN cd /migraphx && git clone --depth=1 --branch migraphx_for_ort https://github.com/ROCmSoftwarePlatform/AMDMIGraphX src
RUN cd /migraphx && rbuild package --cxx /opt/rocm-4.2.0/llvm/bin/clang++ -d /migraphx/deps -B /migraphx/build -S /migraphx/src/ -DPYTHON_EXECUTABLE=/usr/bin/python3
RUN cd /migraphx && rbuild package --cxx /opt/rocm-4.5.0/llvm/bin/clang++ -d /migraphx/deps -B /migraphx/build -S /migraphx/src/ -DPYTHON_EXECUTABLE=/usr/bin/python3
RUN dpkg -i /migraphx/build/*.deb
RUN rm -rf /migraphx

Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/Dockerfile.rocm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ENV LANG C.UTF-8

# Install rocm
RUN apt-get update && apt-get install -y gnupg2 --no-install-recommends curl && \
curl -sL http://repo.radeon.com/rocm/apt/debian/rocm.gpg.key | apt-key add - && \
curl -sL http://repo.radeon.com/rocm/rocm.gpg.key | apt-key add - && \
sh -c 'echo deb [arch=amd64] http://repo.radeon.com/rocm/apt/4.0/ xenial main > /etc/apt/sources.list.d/rocm.list'

RUN apt-get update &&\
Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ git submodule update --init
```

## MIGraphX
**Ubuntu 16.04, rocm3.3, AMDMIGraphX v0.7**
**Ubuntu 18.04, rocm4.5, AMDMIGraphX v1.2**

1. Build the docker image from the Dockerfile in this repository.
```
Expand Down

This file was deleted.

35 changes: 35 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ typedef struct OrtTensorRTProviderOptions {
int trt_force_sequential_engine_build; // force building TensorRT engine sequentially. Default 0 = false, nonzero = true
} OrtTensorRTProviderOptions;

/** \brief MIGraphX Provider Options
*
* \see OrtApi::SessionOptionsAppendExecutionProvider_MIGraphX
*/
typedef struct OrtMIGraphXProviderOptions {
int device_id; // hip device id.
int migraphx_fp16_enable; // enable MIGraphX FP16 precision. Default 0 = false, nonzero = true
int migraphx_int8_enable; // enable MIGraphX INT8 precision. Default 0 = false, nonzero = true
} OrtMIGraphXProviderOptions;

/** \brief OpenVINO Provider Options
*
* \see OrtApi::SessionOptionsAppendExecutionProvider_OpenVINO
Expand Down Expand Up @@ -3049,6 +3059,9 @@ struct OrtApi {
* \snippet{doc} snippets.dox OrtStatus Return Value
*/
ORT_API2_STATUS(GetSparseTensorIndices, _In_ const OrtValue* ort_value, enum OrtSparseIndicesFormat indices_format, _Out_ size_t* num_indices, _Outptr_ const void** indices);
/// @}
/// \name OrtSessionOptions
/// @{

/**
* \brief Sets out to 1 iff an optional type OrtValue has an element, 0 otherwise (OrtValue is None)
Expand Down Expand Up @@ -3260,6 +3273,17 @@ struct OrtApi {
*/
void(ORT_API_CALL* ReleaseCUDAProviderOptions)(_Frees_ptr_opt_ OrtCUDAProviderOptionsV2* input);

/** \brief Append MIGraphX provider to session options
*
* If MIGraphX is not available (due to a non MIGraphX enabled build, or if MIGraphX is not installed on the system), this function will return failure.
*
* \param[in] options
* \param[in] migraphx_options
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*/
ORT_API2_STATUS(SessionOptionsAppendExecutionProvider_MIGraphX,
_In_ OrtSessionOptions* options, _In_ const OrtMIGraphXProviderOptions* migraphx_options);
/// @}
};

Expand Down Expand Up @@ -3321,6 +3345,17 @@ struct OrtCustomOp {
*/
ORT_API_STATUS(OrtSessionOptionsAppendExecutionProvider_CUDA, _In_ OrtSessionOptions* options, int device_id);

/*
* This is the old way to add the MIGraphX provider to the session, please use
* SessionOptionsAppendExecutionProvider_MIGraphX above to access the latest functionality
* This function always exists, but will only succeed if Onnxruntime was built with
* HIP support and the MIGraphX provider shared library exists
*
* \param device_id HIP device id, starts from zero.
*/
ORT_API_STATUS(OrtSessionOptionsAppendExecutionProvider_MIGraphX, _In_ OrtSessionOptions* options, int device_id);


#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions include/onnxruntime/core/session/onnxruntime_cxx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ struct SessionOptions : Base<OrtSessionOptions> {
SessionOptions& AppendExecutionProvider_ROCM(const OrtROCMProviderOptions& provider_options); ///< Wraps OrtApi::SessionOptionsAppendExecutionProvider_ROCM
SessionOptions& AppendExecutionProvider_OpenVINO(const OrtOpenVINOProviderOptions& provider_options); ///< Wraps OrtApi::SessionOptionsAppendExecutionProvider_OpenVINO
SessionOptions& AppendExecutionProvider_TensorRT(const OrtTensorRTProviderOptions& provider_options); ///< Wraps OrtApi::SessionOptionsAppendExecutionProvider_TensorRT
SessionOptions& AppendExecutionProvider_MIGraphX(const OrtMIGraphXProviderOptions& provider_options); ///< Wraps OrtApi::SessionOptionsAppendExecutionProvider_MIGraphX

SessionOptions& SetCustomCreateThreadFn(OrtCustomCreateThreadFn ort_custom_create_thread_fn); ///< Wraps OrtApi::SessionOptionsSetCustomCreateThreadFn
SessionOptions& SetCustomThreadCreationOptions(void* ort_custom_thread_creation_options); ///< Wraps OrtApi::SessionOptionsSetCustomThreadCreationOptions
Expand Down
5 changes: 5 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@ inline SessionOptions& SessionOptions::AppendExecutionProvider_TensorRT(const Or
return *this;
}

inline SessionOptions& SessionOptions::AppendExecutionProvider_MIGraphX(const OrtMIGraphXProviderOptions& provider_options) {
ThrowOnError(GetApi().SessionOptionsAppendExecutionProvider_MIGraphX(p_, &provider_options));
return *this;
}

inline SessionOptions& SessionOptions::SetCustomCreateThreadFn(OrtCustomCreateThreadFn ort_custom_create_thread_fn) {
ThrowOnError(GetApi().SessionOptionsSetCustomCreateThreadFn(p_, ort_custom_create_thread_fn));
return *this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "onnxruntime/core/providers/stvm/stvm_provider_factory.h"
#include "onnxruntime/core/providers/openvino/openvino_provider_factory.h"
#include "onnxruntime/core/providers/tensorrt/tensorrt_provider_factory.h"
#include "onnxruntime/core/providers/migraphx/migraphx_provider_factory.h"
#include "onnxruntime/core/providers/acl/acl_provider_factory.h"
#include "onnxruntime/core/providers/armnn/armnn_provider_factory.h"
#include "onnxruntime/core/providers/coreml/coreml_provider_factory.h"
Expand Down
1 change: 1 addition & 0 deletions onnxruntime/core/providers/migraphx/exported_symbols.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_GetProvider
25 changes: 13 additions & 12 deletions onnxruntime/core/providers/migraphx/gpu_data_transfer.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include "migraphx_inc.h"
#include "core/providers/shared_library/provider_api.h"
#include "gpu_data_transfer.h"
#include "migraphx_call.h"

namespace onnxruntime {
GPUDataTransfer::GPUDataTransfer() {
GPUDataTransfer::GPUDataTransfer(hipStream_t stream) {
// create streams, default is nullptr
streams_[kHipStreamDefault] = nullptr;
hipStreamCreateWithFlags(&streams_[kHipStreamCopyIn], hipStreamNonBlocking);
hipStreamCreateWithFlags(&streams_[kHipStreamCopyOut], hipStreamNonBlocking);
streams_[kHipStreamDefault] = stream;
HIP_CALL_THROW(hipStreamCreateWithFlags(&streams_[kHipStreamCopyIn], hipStreamNonBlocking));
HIP_CALL_THROW(hipStreamCreateWithFlags(&streams_[kHipStreamCopyOut], hipStreamNonBlocking));
}

GPUDataTransfer::~GPUDataTransfer() {
hipStreamDestroy(streams_[kHipStreamCopyIn]);
hipStreamDestroy(streams_[kHipStreamCopyOut]);
HIP_CALL_THROW(hipStreamDestroy(streams_[kHipStreamCopyIn]));
HIP_CALL_THROW(hipStreamDestroy(streams_[kHipStreamCopyOut]));
}

bool GPUDataTransfer::CanCopy(const OrtDevice& src_device, const OrtDevice& dst_device) const {
Expand All @@ -33,21 +34,21 @@ common::Status GPUDataTransfer::CopyTensor(const Tensor& src, Tensor& dst, int e
if (dst_device.Type() == OrtDevice::GPU) {
if (src_device.Type() == OrtDevice::CPU && src_device.MemType() == OrtDevice::MemType::HIP_PINNED) {
// copy from pinned memory to GPU, this is non-blocking
hipMemcpyAsync(dst_data, src_data, bytes, hipMemcpyHostToDevice, streams_[exec_queue_id]);
HIP_CALL_THROW(hipMemcpyAsync(dst_data, src_data, bytes, hipMemcpyHostToDevice, streams_[exec_queue_id]));
} else if (src_device.Type() == OrtDevice::GPU) {
// copying between GPU, this is non-blocking
hipMemcpyAsync(dst_data, src_data, bytes, hipMemcpyDeviceToDevice, streams_[kHipStreamDefault]);
HIP_CALL_THROW(hipMemcpyAsync(dst_data, src_data, bytes, hipMemcpyDeviceToDevice, streams_[kHipStreamDefault]));
} else {
// copy from other CPU memory to GPU, this is blocking
hipMemcpy(dst_data, src_data, bytes, hipMemcpyHostToDevice);
HIP_CALL_THROW(hipMemcpy(dst_data, src_data, bytes, hipMemcpyHostToDevice));
}
} else if (src_device.Type() == OrtDevice::GPU) {
if (dst_device.Type() == OrtDevice::CPU && dst_device.MemType() == OrtDevice::MemType::HIP_PINNED) {
// copying from GPU to pinned memory, this is non-blocking
hipMemcpyAsync(dst_data, src_data, bytes, hipMemcpyDeviceToHost, streams_[exec_queue_id]);
HIP_CALL_THROW(hipMemcpyAsync(dst_data, src_data, bytes, hipMemcpyDeviceToHost, streams_[exec_queue_id]));
} else {
// copying from GPU to CPU memory, this is blocking
hipMemcpy(dst_data, src_data, bytes, hipMemcpyDeviceToHost);
HIP_CALL_THROW(hipMemcpy(dst_data, src_data, bytes, hipMemcpyDeviceToHost));
}
} else {
// copying between cpu memory
Expand Down
3 changes: 2 additions & 1 deletion onnxruntime/core/providers/migraphx/gpu_data_transfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum HIPStreamType : int {

class GPUDataTransfer : public IDataTransfer {
public:
GPUDataTransfer();
GPUDataTransfer(hipStream_t stream);
~GPUDataTransfer();

bool CanCopy(const OrtDevice& src_device, const OrtDevice& dst_device) const override;
Expand All @@ -30,6 +30,7 @@ class GPUDataTransfer : public IDataTransfer {
}

private:
bool do_copy_in_default_stream_;
hipStream_t streams_[kTotalHipStreams];
};

Expand Down
Loading

0 comments on commit ce103ac

Please sign in to comment.