From 589e87686018bff75423e1e0bf22bcb0aae3e4a2 Mon Sep 17 00:00:00 2001 From: pranav-159 Date: Sat, 20 Jan 2024 11:14:36 +0530 Subject: [PATCH 1/5] changes to make c library along with c++ library --- CMakeLists.txt | 24 +++++++++++++----------- MLModelRunner/CMakeLists.txt | 14 ++++++++------ SerDes/CMakeLists.txt | 2 +- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5db8d60..5318571 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ find_package(Protobuf CONFIG REQUIRED) set(protobuf_MODULE_COMPATIBLE TRUE) find_package(Protobuf CONFIG REQUIRED) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib CACHE PATH "Output directory for static libraries") + include_directories(${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/include) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fPIC") set (CMAKE_CXX_STANDARD 17) @@ -28,7 +30,6 @@ if(NOT LLVM_MLBRIDGE) find_package(LLVM 10.0.0 REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) link_directories(${LLVM_LIBRARY_DIR}) - add_compile_definitions(C_LIBRARY) endif() if(MLBRIDGE_DEBUG_MODE) @@ -65,25 +66,26 @@ else() llvm_map_components_to_libnames(llvm_libs support core irreader analysis TransformUtils) add_library(MLCompilerBridge STATIC tools.cpp) - target_link_libraries(MLCompilerBridge PUBLIC SerDesLib ModelRunnerLib ONNXModelRunnerLib ${llvm_libs}) - - add_executable(MLCompilerBridgeTest $) - target_link_libraries(MLCompilerBridgeTest PUBLIC MLCompilerBridge) + target_link_libraries(MLCompilerBridge PUBLIC SerDesLib ModelRunnerLib ONNXModelRunnerLib ${llvm_libs} tf_xla_runtime protobuf::libprotobuf) + set_target_properties(MLCompilerBridge PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set_property(TARGET MLCompilerBridge PROPERTY POSITION_INDEPENDENT_CODE 1) - set_target_properties(MLCompilerBridgeTest PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - install(TARGETS MLCompilerBridgeTest DESTINATION bin) + install(TARGETS MLCompilerBridge DESTINATION lib) + add_library(MLCompilerBridgeC STATIC $) target_link_libraries(MLCompilerBridgeC PUBLIC SerDesCLib ModelRunnerCLib ONNXModelRunnerLib ${llvm_libs}) target_include_directories(MLCompilerBridgeC PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${LLVM_INCLUDE_DIRS}) - target_compile_features(MLCompilerBridgeC PRIVATE cxx_std_17) + target_compile_definitions(MLCompilerBridgeC PRIVATE C_LIBRARY) set_property(TARGET MLCompilerBridgeC PROPERTY POSITION_INDEPENDENT_CODE 1) - - set_target_properties(MLCompilerBridge PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set_target_properties(MLCompilerBridgeC PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) install(TARGETS MLCompilerBridgeC DESTINATION lib) - install(TARGETS MLCompilerBridge DESTINATION lib) + + add_executable(MLCompilerBridgeTest $) + target_link_libraries(MLCompilerBridgeTest PUBLIC MLCompilerBridge) + set_target_properties(MLCompilerBridgeTest PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + install(TARGETS MLCompilerBridgeTest DESTINATION bin) + endif(LLVM_MLBRIDGE) install(DIRECTORY include/ DESTINATION include) diff --git a/MLModelRunner/CMakeLists.txt b/MLModelRunner/CMakeLists.txt index 88950e6..069e769 100755 --- a/MLModelRunner/CMakeLists.txt +++ b/MLModelRunner/CMakeLists.txt @@ -19,22 +19,24 @@ add_subdirectory(C) # # python3 -m pip install --upgrade pip && python3 -m pip install --user tf_nightly==2.3.0.dev20200528 # # Then set TENSORFLOW_AOT_PATH to the package install - usually it's ~/.local/lib/python3.7/site-packages/tensorflow # # -if(LLVM_MLBRIDGE) set(TENSORFLOW_AOT_PATH "" CACHE PATH "Path to TensorFlow pip install dir") if (NOT TENSORFLOW_AOT_PATH STREQUAL "") include_directories(${TENSORFLOW_AOT_PATH}/include) add_subdirectory(${TENSORFLOW_AOT_PATH}/xla_aot_runtime_src ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/tf_runtime) - install(TARGETS tf_xla_runtime EXPORT LLVMExports - ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT tf_xla_runtime) - set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS tf_xla_runtime) -endif() - + if(LLVM_MLBRIDGE) + install(TARGETS tf_xla_runtime EXPORT LLVMExports + ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT tf_xla_runtime) + set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS tf_xla_runtime) + else() + install(TARGETS tf_xla_runtime ARCHIVE DESTINATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}) + endif() endif() add_library(ModelRunnerCLib OBJECT PipeModelRunner.cpp) target_link_libraries(ModelRunnerCLib PUBLIC ModelRunnerCUtils ONNXModelRunnerLib) +target_compile_definitions(ModelRunnerCLib PRIVATE C_LIBRARY) if(LLVM_MLBRIDGE) add_llvm_library(ModelRunnerLib PipeModelRunner.cpp) diff --git a/SerDes/CMakeLists.txt b/SerDes/CMakeLists.txt index e3ccbe1..d157ab2 100755 --- a/SerDes/CMakeLists.txt +++ b/SerDes/CMakeLists.txt @@ -12,8 +12,8 @@ if(LLVM_MLBRIDGE) ) else() add_library(SerDesLib OBJECT TensorSpec.cpp jsonSerDes.cpp bitstreamSerDes.cpp protobufSerDes.cpp tensorflowSerDes.cpp JSON.cpp) + target_include_directories(SerDesLib PRIVATE ${TENSORFLOW_AOT_PATH}/include) add_library(SerDesCLib OBJECT TensorSpec.cpp jsonSerDes.cpp bitstreamSerDes.cpp JSON.cpp) endif() -target_include_directories(SerDesLib PRIVATE ${TENSORFLOW_AOT_PATH}/include) From 7d87634b2769fe5c2344d10bd9be12d21c17d647 Mon Sep 17 00:00:00 2001 From: pranav-159 Date: Sat, 20 Jan 2024 11:36:26 +0530 Subject: [PATCH 2/5] include tensorflow files in both llvm and C++ library --- SerDes/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerDes/CMakeLists.txt b/SerDes/CMakeLists.txt index d157ab2..f7a9a31 100755 --- a/SerDes/CMakeLists.txt +++ b/SerDes/CMakeLists.txt @@ -12,8 +12,8 @@ if(LLVM_MLBRIDGE) ) else() add_library(SerDesLib OBJECT TensorSpec.cpp jsonSerDes.cpp bitstreamSerDes.cpp protobufSerDes.cpp tensorflowSerDes.cpp JSON.cpp) - target_include_directories(SerDesLib PRIVATE ${TENSORFLOW_AOT_PATH}/include) - + add_library(SerDesCLib OBJECT TensorSpec.cpp jsonSerDes.cpp bitstreamSerDes.cpp JSON.cpp) endif() +target_include_directories(SerDesLib PRIVATE ${TENSORFLOW_AOT_PATH}/include) From 1de0954f05d96d2e9602440fdbdc5c4948a8a60b Mon Sep 17 00:00:00 2001 From: pranav-159 Date: Sat, 20 Jan 2024 11:57:51 +0530 Subject: [PATCH 3/5] formating changes --- MLModelRunner/CMakeLists.txt | 2 +- SerDes/CMakeLists.txt | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/MLModelRunner/CMakeLists.txt b/MLModelRunner/CMakeLists.txt index 069e769..57a1cde 100755 --- a/MLModelRunner/CMakeLists.txt +++ b/MLModelRunner/CMakeLists.txt @@ -31,7 +31,7 @@ if (NOT TENSORFLOW_AOT_PATH STREQUAL "") set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS tf_xla_runtime) else() install(TARGETS tf_xla_runtime ARCHIVE DESTINATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}) - endif() + endif() endif() add_library(ModelRunnerCLib OBJECT PipeModelRunner.cpp) diff --git a/SerDes/CMakeLists.txt b/SerDes/CMakeLists.txt index f7a9a31..d06ef68 100755 --- a/SerDes/CMakeLists.txt +++ b/SerDes/CMakeLists.txt @@ -12,8 +12,7 @@ if(LLVM_MLBRIDGE) ) else() add_library(SerDesLib OBJECT TensorSpec.cpp jsonSerDes.cpp bitstreamSerDes.cpp protobufSerDes.cpp tensorflowSerDes.cpp JSON.cpp) - + add_library(SerDesCLib OBJECT TensorSpec.cpp jsonSerDes.cpp bitstreamSerDes.cpp JSON.cpp) endif() target_include_directories(SerDesLib PRIVATE ${TENSORFLOW_AOT_PATH}/include) - From b5314d437d2ccc2818a75f082532be17133f258c Mon Sep 17 00:00:00 2001 From: pranav-159 Date: Sat, 20 Jan 2024 12:04:18 +0530 Subject: [PATCH 4/5] restoring deleted code used by eviction, inlining in llvm 17 --- include/MLModelRunner/TFModelRunner.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/include/MLModelRunner/TFModelRunner.h b/include/MLModelRunner/TFModelRunner.h index 8d269bf..755a8e2 100644 --- a/include/MLModelRunner/TFModelRunner.h +++ b/include/MLModelRunner/TFModelRunner.h @@ -80,6 +80,33 @@ template class TFModelRunner final : public MLModelRunner { std::unique_ptr CompiledModel; }; +/// A mock class satisfying the interface expected by ReleaseModeModelRunner for +/// its `TGen` parameter. Useful to avoid conditional compilation complexity, as +/// a compile-time replacement for a real AOT-ed model. +class NoopSavedModelImpl final { +#define NOOP_MODEL_ERRMSG \ + "The mock AOT-ed saved model is a compile-time stub and should not be " \ + "called." + +public: + NoopSavedModelImpl() = default; + int LookupArgIndex(const std::string &) { + llvm_unreachable(NOOP_MODEL_ERRMSG); + } + int LookupResultIndex(const std::string &) { + llvm_unreachable(NOOP_MODEL_ERRMSG); + } + void Run() { llvm_unreachable(NOOP_MODEL_ERRMSG); } + void *result_data(int) { llvm_unreachable(NOOP_MODEL_ERRMSG); } + void *arg_data(int) { llvm_unreachable(NOOP_MODEL_ERRMSG); } +#undef NOOP_MODEL_ERRMSG +}; + +template bool isEmbeddedModelEvaluatorValid() { return true; } + +template <> inline bool isEmbeddedModelEvaluatorValid() { + return false; +} } // namespace MLBridge #endif // TFMODELRUNNER_H From 22e5ea10e585126d21807afa99314db7f9ebb92e Mon Sep 17 00:00:00 2001 From: pranav-159 Date: Sat, 20 Jan 2024 12:05:47 +0530 Subject: [PATCH 5/5] resloving llvm::join missing symbol problem when merged tfmodel into release/17.x branch --- SerDes/TensorSpec.cpp | 1 + 1 file changed, 1 insertion(+) mode change 100755 => 100644 SerDes/TensorSpec.cpp diff --git a/SerDes/TensorSpec.cpp b/SerDes/TensorSpec.cpp old mode 100755 new mode 100644 index d518332..bda9af9 --- a/SerDes/TensorSpec.cpp +++ b/SerDes/TensorSpec.cpp @@ -16,6 +16,7 @@ #include "SerDes/TensorSpec.h" #include "MLModelRunner/Utils/JSON.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Support/Debug.h" #include