diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index cc01c0a8bc..b01a925503 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -467,7 +467,6 @@ if (onnxruntime_USE_MKLML) endif() if (onnxruntime_USE_MKLDNN) - add_definitions(-DUSE_MKLDNN=1) list(APPEND onnxruntime_EXTERNAL_LIBRARIES mkldnn) list(APPEND onnxruntime_EXTERNAL_DEPENDENCIES project_mkldnn) link_directories(${MKLDNN_LIB_DIR}) diff --git a/cmake/onnxruntime_python.cmake b/cmake/onnxruntime_python.cmake index 7081edcf2e..484e36a893 100644 --- a/cmake/onnxruntime_python.cmake +++ b/cmake/onnxruntime_python.cmake @@ -48,6 +48,10 @@ if(onnxruntime_PYBIND_EXPORT_OPSCHEMA) target_compile_definitions(onnxruntime_pybind11_state PRIVATE onnxruntime_PYBIND_EXPORT_OPSCHEMA) endif() +if (onnxruntime_USE_MKLDNN) + target_compile_definitions(onnxruntime_pybind11_state PRIVATE USE_MKLDNN=1) +endif() + target_include_directories(onnxruntime_pybind11_state PRIVATE ${ONNXRUNTIME_ROOT} ${PYTHON_INCLUDE_DIR} ${NUMPY_INCLUDE_DIR}) target_include_directories(onnxruntime_pybind11_state PRIVATE ${pybind11_INCLUDE_DIRS}) onnxruntime_add_include_to_target(onnxruntime_pybind11_state gsl) diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake index 3914679c4c..fe210d551a 100644 --- a/cmake/onnxruntime_unittests.cmake +++ b/cmake/onnxruntime_unittests.cmake @@ -219,6 +219,9 @@ file(GLOB onnxruntime_test_framework_src ${onnxruntime_test_framework_src_patter #with auto initialize onnxruntime add_library(onnxruntime_test_utils_for_framework ${onnxruntime_test_utils_src}) onnxruntime_add_include_to_target(onnxruntime_test_utils_for_framework onnxruntime_framework gtest gsl onnx onnx_proto) +if (onnxruntime_USE_MKLDNN) + target_compile_definitions(onnxruntime_test_utils_for_framework PUBLIC USE_MKLDNN=1) +endif() add_dependencies(onnxruntime_test_utils_for_framework ${onnxruntime_EXTERNAL_DEPENDENCIES} eigen) target_include_directories(onnxruntime_test_utils_for_framework PUBLIC "${TEST_SRC_DIR}/util/include" PRIVATE ${eigen_INCLUDE_DIRS} ${ONNXRUNTIME_ROOT}) # Add the define for conditionally using the framework Environment class in TestEnvironment @@ -228,6 +231,9 @@ set_target_properties(onnxruntime_test_utils_for_framework PROPERTIES FOLDER "ON #without auto initialize onnxruntime add_library(onnxruntime_test_utils ${onnxruntime_test_utils_src}) onnxruntime_add_include_to_target(onnxruntime_test_utils onnxruntime_framework gtest gsl onnx onnx_proto) +if (onnxruntime_USE_MKLDNN) + target_compile_definitions(onnxruntime_test_utils PUBLIC USE_MKLDNN=1) +endif() add_dependencies(onnxruntime_test_utils ${onnxruntime_EXTERNAL_DEPENDENCIES} eigen) target_include_directories(onnxruntime_test_utils PUBLIC "${TEST_SRC_DIR}/util/include" PRIVATE ${eigen_INCLUDE_DIRS} ${ONNXRUNTIME_ROOT}) set_target_properties(onnxruntime_test_utils PROPERTIES FOLDER "ONNXRuntimeTest") diff --git a/cmake/onnxruntime_util.cmake b/cmake/onnxruntime_util.cmake index f0a7e3b3c0..20e3a1a847 100644 --- a/cmake/onnxruntime_util.cmake +++ b/cmake/onnxruntime_util.cmake @@ -9,7 +9,7 @@ file(GLOB_RECURSE onnxruntime_util_srcs source_group(TREE ${ONNXRUNTIME_ROOT}/core FILES ${onnxruntime_util_srcs}) add_library(onnxruntime_util ${onnxruntime_util_srcs}) -target_include_directories(onnxruntime_util PRIVATE ${ONNXRUNTIME_ROOT} ${eigen_INCLUDE_DIRS} ${MKLDNN_INCLUDE_DIR} ${MKLML_INCLUDE_DIR}) +target_include_directories(onnxruntime_util PRIVATE ${ONNXRUNTIME_ROOT} ${eigen_INCLUDE_DIRS} ${MKLML_INCLUDE_DIR}) onnxruntime_add_include_to_target(onnxruntime_util onnxruntime_common onnxruntime_framework gsl onnx onnx_proto protobuf::libprotobuf) if(UNIX) target_compile_options(onnxruntime_util PUBLIC "-Wno-error=comment") diff --git a/onnxruntime/core/util/math_cpu.cc b/onnxruntime/core/util/math_cpu.cc index 9ba7af5e2d..12624b7726 100644 --- a/onnxruntime/core/util/math_cpu.cc +++ b/onnxruntime/core/util/math_cpu.cc @@ -44,10 +44,6 @@ #include "core/mlas/inc/mlas.h" #endif -#ifdef USE_MKLDNN -#include "mkldnn.h" -#endif - namespace onnxruntime { namespace math { @@ -110,7 +106,7 @@ void GemmEigen( // will delegate the Caffe math functions that are BLAS-related to either the // CBLAS call or the Eigen implementation. //////////////////////////////////////////////////////////////////////////////// -// when USE_MKLDNN and USE_MKLML are defined, use cblas APIs for MKLML +// when USE_MKLML is defined, use cblas APIs for MKLML #if defined(USE_EIGEN_FOR_BLAS) && !defined(USE_MKLML_FOR_BLAS) // Caffe2 gemm provides a simpler interface to the gemm functions, with the @@ -142,23 +138,7 @@ void Gemm( float* C, CPUMathUtil* /*provider*/, MLDataType /*math_type*/) { -#if defined(USE_MKLDNN) - int lda = (int)((TransA == CblasTrans) ? M : K); - int ldb = (int)((TransB == CblasTrans) ? K : N); - int M_ = (int)M; - int N_ = (int)N; - int K_ = (int)K; - // mkldnn_sgemm expects col major matrices, so we need to swap the operands A and B - auto status = mkldnn_sgemm(TransB == CblasNoTrans ? "N" : "T", - TransA == CblasNoTrans ? "N" : "T", - &N_, &M_, &K_, - &alpha, B, &ldb, - A, &lda, - &beta, C, &N_); - if (status != mkldnn_success) { - ORT_THROW("mkldnn_sgemm failed with status: ", status); - } -#elif defined(USE_MLAS) +#if defined(USE_MLAS) int lda = (int)((TransA == CblasNoTrans) ? K : M); int ldb = (int)((TransB == CblasNoTrans) ? N : K); MlasSgemm(TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, N); @@ -273,18 +253,7 @@ void GemmEx( float* C, const int ldc, CPUMathUtil*) { -#if defined(USE_MKLDNN) - // mkldnn_sgemm expects col major matrices, so we need to swap the operands A and B - auto status = mkldnn_sgemm(TransB == CblasNoTrans ? "N" : "T", - TransA == CblasNoTrans ? "N" : "T", - &N, &M, &K, - &alpha, B, &ldb, - A, &lda, - &beta, C, &ldc); - if (status != mkldnn_success) { - ORT_THROW("mkldnn_sgemm failed with status: ", status); - } -#elif defined(USE_MLAS) +#if defined(USE_MLAS) MlasSgemm(TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc); #else using OuterStride = Eigen::OuterStride;