Skip to content

Commit ca433cb

Browse files
authored
Refactor the unit tests and cmake build script (#726)
* refine the build script * complete the unit tests. * remove the commented code
1 parent b60df02 commit ca433cb

File tree

14 files changed

+255
-102
lines changed

14 files changed

+255
-102
lines changed

CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,11 @@ function(set_msvc_c_cpp_compiler_warning_level warning_level)
285285
endif()
286286
endfunction()
287287

288+
if (NOT ONNXRUNTIME_INCLUDE_DIR)
289+
include(ext_ortlib)
290+
endif()
288291
# set default MSVC warning level to 3 for external dependencies
289292
set_msvc_c_cpp_compiler_warning_level(3)
290-
include(ext_ortlib)
291293
include(gsl)
292294

293295
macro(standardize_output_folder bin_target)
@@ -681,7 +683,6 @@ endif()
681683

682684
if(OCOS_ENABLE_GPT2_TOKENIZER OR OCOS_ENABLE_WORDPIECE_TOKENIZER)
683685
target_include_directories(ocos_operators PUBLIC ${nlohmann_json_SOURCE_DIR}/single_include)
684-
list(APPEND ocos_libraries nlohmann_json::nlohmann_json)
685686
endif()
686687

687688
# If building a shared library we can't throw an internal exception type across the library boundary as the type
@@ -695,8 +696,6 @@ if(ANDROID)
695696
list(APPEND ocos_libraries log)
696697
endif()
697698

698-
list(APPEND ocos_libraries Microsoft.GSL::GSL)
699-
700699
list(REMOVE_DUPLICATES OCOS_COMPILE_DEFINITIONS)
701700
target_compile_definitions(noexcep_operators PRIVATE ${OCOS_COMPILE_DEFINITIONS})
702701
if(NOT OCOS_ENABLE_CPP_EXCEPTIONS)
@@ -899,7 +898,7 @@ if (_ORTX_STANDALONE_PROJECT)
899898
# Run CPack to generate the NuGet package
900899
include(CPack)
901900

902-
if(OCOS_ENABLE_CTEST)
901+
if(OCOS_ENABLE_CTEST AND NOT MAC_CATALYST)
903902
include(ext_tests)
904903
endif()
905904
endif()

base/file_sys.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@ namespace ort_extensions {
1717
class path {
1818
public:
1919
path() = default;
20-
path(const std::string& path) : path_(path){};
20+
path(const std::string& path) : path_(path) {
21+
#ifdef _WIN32
22+
w_path_ = to_wstring();
23+
#endif // _WIN32
24+
};
25+
26+
#ifdef _WIN32
27+
path(const std::wstring& wpath) {
28+
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wpath.c_str(), -1, nullptr, 0, nullptr, nullptr);
29+
std::string utf8_str(size_needed, 0);
30+
WideCharToMultiByte(CP_UTF8, 0, wpath.c_str(), -1, &utf8_str[0], size_needed, nullptr, nullptr);
31+
path_ = utf8_str;
32+
}
33+
#endif // _WIN32
2134

2235
static constexpr char separator =
2336
#ifdef _WIN32
@@ -30,7 +43,7 @@ class path {
3043
std::ifstream open(ios_base::openmode mode = ios_base::in) const {
3144
// if Windows, need to convert the string to UTF-16
3245
#ifdef _WIN32
33-
return std::ifstream(to_wstring(), mode);
46+
return std::ifstream(w_path_, mode);
3447
#else
3548
return std::ifstream(path_, mode);
3649
#endif // _WIN32
@@ -55,7 +68,7 @@ class path {
5568
bool is_directory() const {
5669
#ifdef _WIN32
5770
struct _stat64 info;
58-
if (_wstat64(to_wstring().c_str(), &info) != 0) {
71+
if (_wstat64(w_path_.c_str(), &info) != 0) {
5972
return false;
6073
}
6174
#else
@@ -69,8 +82,9 @@ class path {
6982

7083
private:
7184
std::string path_;
72-
7385
#ifdef _WIN32
86+
std::wstring w_path_;
87+
7488
std::wstring to_wstring() const {
7589
int size_needed = MultiByteToWideChar(CP_UTF8, 0, path_.c_str(), -1, nullptr, 0);
7690
std::wstring utf16_str(size_needed, 0);

cmake/ext_tests.cmake

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
if (NOT MAC_CATALYST)
2-
31
if (OCOS_ENABLE_SELECTED_OPLIST)
42
# currently the tests don't handle operator exclusion cleanly.
53
message(FATAL_ERROR "Due to usage of OCOS_ENABLE_SELECTED_OPLIST excluding operators the tests are unable to be built and run")
@@ -55,7 +53,7 @@ function(add_test_target)
5553
"${TEST_SRC_DIR}/unittest_main/test_main.cc")
5654
target_link_libraries(${ARG_TARGET} PRIVATE
5755
${ARG_LIBRARIES}
58-
gtest gmock)
56+
gtest)
5957

6058
if(OCOS_USE_CUDA)
6159
target_link_directories(${ARG_TARGET} PRIVATE ${CUDAToolkit_LIBRARY_DIR})
@@ -93,7 +91,7 @@ function(add_test_target)
9391

9492
target_link_libraries(${ARG_TARGET} PRIVATE
9593
${ARG_LIBRARIES}
96-
gtest gmock)
94+
gtest)
9795

9896
set(test_data_destination_root_directory $<TARGET_FILE_DIR:${dummy_testee_target}>)
9997

@@ -130,9 +128,40 @@ add_test_target(TARGET ocos_test
130128
LIBRARIES ortcustomops ${ocos_libraries})
131129
target_compile_definitions(ocos_test PRIVATE ${OCOS_COMPILE_DEFINITIONS})
132130

131+
if (OCOS_ENABLE_C_API)
132+
file(GLOB pp_api_TEST_SRC
133+
"${TEST_SRC_DIR}/pp_api_test/*.c"
134+
"${TEST_SRC_DIR}/pp_api_test/*.cc"
135+
"${TEST_SRC_DIR}/pp_api_test/*.h")
136+
137+
add_test_target(TARGET pp_api_test
138+
TEST_SOURCES ${pp_api_TEST_SRC}
139+
LIBRARIES onnxruntime_extensions ${ocos_libraries}
140+
TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data)
141+
142+
target_compile_definitions(pp_api_test PRIVATE ${OCOS_COMPILE_DEFINITIONS})
143+
target_include_directories(pp_api_test PRIVATE
144+
${PROJECT_SOURCE_DIR}/
145+
"$<TARGET_PROPERTY:ortcustomops,INTERFACE_INCLUDE_DIRECTORIES>"
146+
"$<TARGET_PROPERTY:ocos_operators,INTERFACE_INCLUDE_DIRECTORIES>")
147+
148+
if (ORTX_TEST_DATA2)
149+
file(TO_NATIVE_PATH "${ORTX_TEST_DATA2}/tests/data2" _TEST_DATA2)
150+
add_custom_command(TARGET pp_api_test POST_BUILD
151+
COMMAND ${CMAKE_COMMAND} -E create_symlink ${_TEST_DATA2} ${onnxruntime_extensions_BINARY_DIR}/data2)
152+
endif()
153+
endif()
154+
155+
133156
# -- shared test (needs onnxruntime) --
134-
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
135-
find_library(ONNXRUNTIME onnxruntime HINTS "${ONNXRUNTIME_LIB_DIR}")
157+
# avoid blindling searching for onnxruntime library
158+
# wbhich leads to a unpredictable result
159+
if (NOT ONNXRUNTIME_LIB_DIR)
160+
set(ONNXRUNTIME "ONNXRUNTIME-NOTFOUND")
161+
else()
162+
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
163+
find_library(ONNXRUNTIME onnxruntime HINTS "${ONNXRUNTIME_LIB_DIR}")
164+
endif()
136165

137166
if("${ONNXRUNTIME}" STREQUAL "ONNXRUNTIME-NOTFOUND")
138167
message(WARNING "The prebuilt onnxruntime library was not found, extensions_test will be skipped.")
@@ -197,25 +226,10 @@ else()
197226
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ONNXRUNTIME} ${CMAKE_BINARY_DIR}/lib
198227
)
199228
endif()
200-
endblock()
201-
202-
if (OCOS_ENABLE_C_API)
203-
file(GLOB pp_api_TEST_SRC
204-
"${TEST_SRC_DIR}/pp_api_test/*.c"
205-
"${TEST_SRC_DIR}/pp_api_test/*.cc"
206-
"${TEST_SRC_DIR}/pp_api_test/*.h")
207-
208-
add_test_target(TARGET pp_api_test
209-
TEST_SOURCES ${pp_api_TEST_SRC}
210-
LIBRARIES onnxruntime_extensions ${ocos_libraries}
211-
TEST_DATA_DIRECTORIES ${TEST_SRC_DIR}/data)
212-
213-
target_compile_definitions(pp_api_test PRIVATE ${OCOS_COMPILE_DEFINITIONS})
214-
target_include_directories(pp_api_test PRIVATE
215-
${PROJECT_SOURCE_DIR}/
216-
"$<TARGET_PROPERTY:ortcustomops,INTERFACE_INCLUDE_DIRECTORIES>"
217-
"$<TARGET_PROPERTY:ocos_operators,INTERFACE_INCLUDE_DIRECTORIES>")
218-
endif()
219-
endif()
220229

230+
if (OCOS_ENABLE_C_API)
231+
# avoid copying the same data directory at the same time.
232+
add_dependencies(extensions_test pp_api_test)
233+
endif()
234+
endblock()
221235
endif()

cmake/externals/googletest.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ FetchContent_Declare(
44
URL_HASH SHA1=06096d3900c356e468ba060a609642c635131106
55
)
66

7+
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
8+
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
79
FetchContent_MakeAvailable(googletest)
8-
set_target_properties(gmock PROPERTIES FOLDER "externals/gtest")
9-
set_target_properties(gmock_main PROPERTIES FOLDER "externals/gtest")
1010
set_target_properties(gtest PROPERTIES FOLDER "externals/gtest")
1111
set_target_properties(gtest_main PROPERTIES FOLDER "externals/gtest")

cmake/externals/gsl.cmake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@ else()
1414
)
1515
endif()
1616

17-
FetchContent_MakeAvailable(GSL)
18-
get_target_property(GSL_INCLUDE_DIR Microsoft.GSL::GSL INTERFACE_INCLUDE_DIRECTORIES)
17+
FetchContent_GetProperties(GSL)
18+
string(TOLOWER "GSL" lcName)
19+
if(NOT ${lcName}_POPULATED)
20+
FetchContent_Populate(GSL)
21+
# add_subdirectory(${GSL_SOURCE_DIR} ${GSL_BINARY_DIR} EXCLUDE_FROM_ALL)
22+
endif()
23+
24+
set(GSL_INCLUDE_DIR ${gsl_SOURCE_DIR}/include)
25+
26+
#get_target_property(GSL_INCLUDE_DIR Microsoft.GSL::GSL INTERFACE_INCLUDE_DIRECTORIES)

cmake/externals/json.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ set(JSON_BuildTests OFF CACHE INTERNAL "")
77
FetchContent_GetProperties(nlohmann_json)
88
if(NOT nlohmann_json_POPULATED)
99
FetchContent_Populate(nlohmann_json)
10-
add_subdirectory(${nlohmann_json_SOURCE_DIR} ${nlohmann_json_BINARY_DIR} EXCLUDE_FROM_ALL)
1110
endif()

cmake/externals/opencv.cmake

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ set(BUILD_TESTS OFF CACHE INTERNAL "")
107107
set(CV_TRACE OFF CACHE INTERNAL "")
108108

109109
set(CV_DISABLE_OPTIMIZATION ON CACHE INTERNAL "")
110+
set(BUILD_PERF_TESTS OFF CACHE INTERNAL "")
111+
set(BUILD_opencv_java_bindings_generator OFF CACHE INTERNAL "")
112+
set(BUILD_opencv_js_bindings_generator OFF CACHE INTERNAL "")
113+
set(BUILD_opencv_objc_bindings_generator OFF CACHE INTERNAL "")
114+
set(BUILD_opencv_python_bindings_generator OFF CACHE INTERNAL "")
115+
set(BUILD_opencv_python_tests OFF CACHE INTERNAL "")
116+
117+
set(WITH_ADE OFF CACHE INTERNAL "")
118+
set(VIDEOIO_ENABLE_PLUGINS OFF CACHE INTERNAL "")
119+
set(HIGHGUI_ENABLE_PLUGINS OFF CACHE INTERNAL "")
110120

111121
if(IOS)
112122
# copy what OpenCV's platforms/ios/build_framework.py does and set CPU_BASELINE=DETECT
@@ -157,13 +167,3 @@ endif()
157167

158168
# unset it to avoid affecting other projects.
159169
unset(EXECUTABLE_OUTPUT_PATH CACHE)
160-
161-
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
162-
set(opencv_projs gen_opencv_java_source gen_opencv_js_source gen_opencv_python_source)
163-
list(APPEND opencv_projs gen_opencv_objc_source gen_opencv_objc_source_ios gen_opencv_objc_source_osx)
164-
list(APPEND opencv_projs opencv_highgui_plugins opencv_videoio_plugins)
165-
foreach(p ${opencv_projs})
166-
set_target_properties(${p} PROPERTIES FOLDER "externals/opencv")
167-
set_target_properties(${p} PROPERTIES EXCLUDE_FROM_ALL TRUE EXCLUDE_FROM_DEFAULT_BUILD TRUE)
168-
endforeach()
169-
endif()

operators/vision/draw_bounding_box.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class BoxArray {
4848
private:
4949
void SortBoxesByScore(gsl::span<const float> data) {
5050
boxes_by_score_.reserve(NumBoxes());
51-
for (size_t i = 0; i < NumBoxes(); ++i) {
51+
for (size_t i = 0; i < static_cast<size_t>(NumBoxes()); ++i) {
5252
boxes_by_score_.push_back(data.subspan(i * shape_[1], shape_[1]));
5353
}
5454

@@ -188,7 +188,7 @@ void DrawBoxesForNumClasses(ImageView& image, const BoxArray& boxes, int64_t thi
188188
std::unordered_map<float, size_t> color_used;
189189
std::vector<std::pair<size_t, int64_t>> box_reverse;
190190
box_reverse.reserve(boxes.NumBoxes());
191-
for (size_t i = 0; i < boxes.NumBoxes(); ++i) {
191+
for (size_t i = 0; i < static_cast<size_t>(boxes.NumBoxes()); ++i) {
192192
const auto box = boxes.GetBox(i);
193193
if (color_used.find(box[kBoxClassIndex]) == color_used.end()) {
194194
if (color_used.size() >= KBGRColorMap.size()) {

shared/api/image_processor.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ LoadRawImages(const std::initializer_list<const char*>& image_paths) {
3838
} // namespace ort_extensions
3939

4040
Operation::KernelRegistry ImageProcessor::kernel_registry_ = {
41-
{"DecodeImage", []() { return DefineKernelFunction(image_decoder); }},
42-
{"ConvertRGB", []() { return DefineKernelFunction(convert_to_rgb); }},
43-
{"Phi3ImageTransform", []() { return DefineKernelFunction(phi3_hd_transform); }},
41+
{"DecodeImage", []() { return CreateKernelInstance(image_decoder); }},
42+
{"ConvertRGB", []() { return CreateKernelInstance(&ConvertToRGB::Compute); }},
43+
{"Phi3ImageTransform", []() { return CreateKernelInstance(phi3_hd_transform); }},
4444
};
4545

4646
OrtxStatus ImageProcessor::Init(std::string_view processor_def) {

shared/api/image_transforms.hpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,32 @@ constexpr int image_resized_height = 336;
1313
constexpr float OPENAI_CLIP_MEAN[] = {0.48145466f, 0.4578275f, 0.40821073f};
1414
constexpr float OPENAI_CLIP_STD[] = {0.26862954f, 0.26130258f, 0.27577711f};
1515

16-
inline OrtxStatus convert_to_rgb(const ortc::Tensor<uint8_t>& input,
17-
ortc::Tensor<uint8_t>& output) {
18-
auto& dimensions = input.Shape();
19-
if (dimensions.size() != 3ULL || dimensions[2] != 3) {
20-
return {kOrtxErrorInvalidArgument, "[ConvertToRGB]: input is not (H, W, C)"};
21-
}
22-
23-
std::uint8_t* p_output_image = output.Allocate(dimensions);
24-
auto* input_data = input.Data();
25-
auto h = dimensions[0];
26-
auto w = dimensions[1];
27-
auto c = dimensions[2];
16+
struct ConvertToRGB {
17+
OrtxStatus Compute(const ortc::Tensor<uint8_t>& input,
18+
ortc::Tensor<uint8_t>& output) {
19+
auto& dimensions = input.Shape();
20+
if (dimensions.size() != 3ULL || dimensions[2] != 3) {
21+
return {kOrtxErrorInvalidArgument, "[ConvertToRGB]: input is not (H, W, C)"};
22+
}
2823

29-
// convert BGR channel layouts to RGB
30-
for (int64_t j = 0; j < h; ++j) {
31-
for (int64_t k = 0; k < w; ++k) {
32-
auto c0_index = j * w * c + k * c;
33-
std::tie(p_output_image[c0_index], p_output_image[c0_index + 1], p_output_image[c0_index + 2]) =
34-
std::make_tuple(input_data[c0_index + 2], input_data[c0_index + 1], input_data[c0_index]);
24+
std::uint8_t* p_output_image = output.Allocate(dimensions);
25+
auto* input_data = input.Data();
26+
auto h = dimensions[0];
27+
auto w = dimensions[1];
28+
auto c = dimensions[2];
29+
30+
// convert BGR channel layouts to RGB
31+
for (int64_t j = 0; j < h; ++j) {
32+
for (int64_t k = 0; k < w; ++k) {
33+
auto c0_index = j * w * c + k * c;
34+
std::tie(p_output_image[c0_index], p_output_image[c0_index + 1], p_output_image[c0_index + 2]) =
35+
std::make_tuple(input_data[c0_index + 2], input_data[c0_index + 1], input_data[c0_index]);
36+
}
3537
}
36-
}
3738

38-
return {};
39-
}
39+
return {};
40+
}
41+
};
4042

4143
inline cv::Mat padding_336(const cv::Mat& image) {
4244
// def padding_336(b):

0 commit comments

Comments
 (0)