From f9bae489bdd4e662495cf6e9d27872e775b2218c Mon Sep 17 00:00:00 2001 From: shahasad <43590019+shahasad@users.noreply.github.com> Date: Sun, 24 Feb 2019 21:06:54 -0800 Subject: [PATCH] cleanup extra header from c api and sanitize C api test (#517) * cleaned up the additional header in C-api * ensure test failure surfaces in the build pipeline * sanitized runtest.bat * cleanup unneeded headers * formatting and typos --- .../Microsoft.ML.OnnxRuntime.csproj | 6 - .../InferenceTestCapi.cpp | 116 ++++++++++-------- .../runtest.bat | 19 +-- .../core/providers/cpu/cpu_provider_factory.h | 3 - .../core/session/onnxruntime_c_api.h | 6 + 5 files changed, 80 insertions(+), 70 deletions(-) diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj b/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj index 42196852a8..0e88654972 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj +++ b/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj @@ -37,12 +37,6 @@ CopyToOutputDirectory="Never" Visible="false" /> - #include -#include +wchar_t* GetWideString(const char* c) { + const size_t cSize = strlen(c) + 1; + wchar_t* wc = new wchar_t[cSize]; + mbstowcs(wc, c, cSize); + + return wc; +} #define ORT_ABORT_ON_ERROR(expr) \ - do { \ + { \ OrtStatus* onnx_status = (expr); \ if (onnx_status != NULL) { \ const char* msg = OrtGetErrorMessage(onnx_status); \ fprintf(stderr, "%s\n", msg); \ OrtReleaseStatus(onnx_status); \ - abort(); \ + wchar_t* wmsg = GetWideString(msg); \ + Assert::Fail(L"Failed on ORT_ABORT_ON_ERROR"); \ + free(wmsg); \ } \ - } while (0); + } using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace UnitTest1 { - TEST_CLASS(UnitTest1) - { - public: - - int run_inference(OrtSession* session) { - size_t input_height = 224; - size_t input_width = 224; - float* model_input = (float *) malloc (sizeof (float) * 224 * 224 * 3); - size_t model_input_ele_count = 224 * 224 * 3; + TEST_CLASS(UnitTest1) + { + public: + + int run_inference(OrtSession* session) { + size_t input_height = 224; + size_t input_width = 224; + float* model_input = (float *) malloc (sizeof (float) * 224 * 224 * 3); + size_t model_input_ele_count = 224 * 224 * 3; - // initialize to values between 0.0 and 1.0 - for (unsigned int i = 0; i < model_input_ele_count; i++) - model_input[i] = (float)i / (float)(model_input_ele_count + 1); + // initialize to values between 0.0 and 1.0 + for (unsigned int i = 0; i < model_input_ele_count; i++) + model_input[i] = (float)i / (float)(model_input_ele_count + 1); - OrtAllocatorInfo* allocator_info; - ORT_ABORT_ON_ERROR(OrtCreateCpuAllocatorInfo(OrtArenaAllocator, OrtMemTypeDefault, &allocator_info)); - const size_t input_shape[] = { 1, 3, 224, 224 }; - const size_t input_shape_len = sizeof(input_shape) / sizeof(input_shape[0]); - const size_t model_input_len = model_input_ele_count * sizeof(float); + OrtAllocatorInfo* allocator_info; + ORT_ABORT_ON_ERROR(OrtCreateCpuAllocatorInfo(OrtArenaAllocator, OrtMemTypeDefault, &allocator_info)); + const size_t input_shape[] = { 1, 3, 224, 224 }; + const size_t input_shape_len = sizeof(input_shape) / sizeof(input_shape[0]); + const size_t model_input_len = model_input_ele_count * sizeof(float); - OrtValue* input_tensor = NULL; - ORT_ABORT_ON_ERROR(OrtCreateTensorWithDataAsOrtValue(allocator_info, model_input, model_input_len, input_shape, input_shape_len, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &input_tensor)); - assert(input_tensor != NULL); - assert(OrtIsTensor(input_tensor)); - OrtReleaseAllocatorInfo(allocator_info); - const char* input_names[] = { "data_0" }; - const char* output_names[] = { "softmaxout_1" }; - OrtValue* output_tensor = NULL; - ORT_ABORT_ON_ERROR(OrtRun(session, NULL, input_names, (const OrtValue* const*)&input_tensor, 1, output_names, 1, &output_tensor)); - assert(output_tensor != NULL); - assert(OrtIsTensor(output_tensor)); + OrtValue* input_tensor = NULL; + ORT_ABORT_ON_ERROR(OrtCreateTensorWithDataAsOrtValue(allocator_info, model_input, model_input_len, input_shape, input_shape_len, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &input_tensor)); + assert(input_tensor != NULL); + assert(OrtIsTensor(input_tensor)); + OrtReleaseAllocatorInfo(allocator_info); + const char* input_names[] = { "data_0" }; + const char* output_names[] = { "softmaxout_1" }; + OrtValue* output_tensor = NULL; + ORT_ABORT_ON_ERROR(OrtRun(session, NULL, input_names, (const OrtValue* const*)&input_tensor, 1, output_names, 1, &output_tensor)); + assert(output_tensor != NULL); + assert(OrtIsTensor(output_tensor)); - OrtReleaseValue(output_tensor); - OrtReleaseValue(input_tensor); - free(model_input); - return 0; - } + OrtReleaseValue(output_tensor); + OrtReleaseValue(input_tensor); + free(model_input); + return 0; + } - int test() - { - const wchar_t * model_path = L"squeezenet.onnx"; - OrtEnv* env; - ORT_ABORT_ON_ERROR(OrtCreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env)); - OrtSessionOptions* session_option = OrtCreateSessionOptions(); - OrtSession* session; - ORT_ABORT_ON_ERROR(OrtCreateSession(env, model_path, session_option, &session)); + int test() + { + const wchar_t * model_path = L"squeezenet.onnx"; + OrtEnv* env; + ORT_ABORT_ON_ERROR(OrtCreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env)); + OrtSessionOptions* session_option = OrtCreateSessionOptions(); + OrtSession* session; + ORT_ABORT_ON_ERROR(OrtCreateSession(env, model_path, session_option, &session)); - OrtSetSessionThreadPoolSize(session_option, 1); - return run_inference(session); - } + OrtSetSessionThreadPoolSize(session_option, 1); + return run_inference(session); + } - TEST_METHOD(TestMethod1) - { - int res = test(); - Assert::AreEqual(res, 0); - } - }; + TEST_METHOD(TestMethod1) + { + int res = test(); + Assert::AreEqual(res, 0); + } + }; } diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/runtest.bat b/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/runtest.bat index fd4f129860..c0a042c034 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/runtest.bat +++ b/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/runtest.bat @@ -38,26 +38,31 @@ echo on REM Restore NuGet Packages nuget restore -PackagesDirectory ..\packages -Source %LocalNuGetRepo% Microsoft.ML.OnnxRuntime.EndToEndTests.Capi.vcxproj if NOT %ERRORLEVEL% EQU 0 ( - echo "Error:Nuget restore failed" - EXIT /B 1 + echo "Error:Nuget restore failed" + popd + EXIT /B 1 ) REM Build Native project msbuild Microsoft.ML.OnnxRuntime.EndToEndTests.Capi.vcxproj if NOT %ERRORLEVEL% EQU 0 ( - echo "Error:MSBuild failed to compile project" - EXIT /B 1 + echo "Error:MSBuild failed to compile project" + popd + EXIT /B 1 ) REM Run Unit Tests -dir -cd x64\debug -dir +pushd x64\Debug vstest.console.exe /platform:x64 Microsoft.ML.OnnxRuntime.EndToEndTests.Capi.dll if NOT %ERRORLEVEL% EQU 0 ( echo "Unit test failure: %ERRORLEVEL%" + popd + popd + EXIT /B 1 ) popd +popd + EXIT /B 0 diff --git a/include/onnxruntime/core/providers/cpu/cpu_provider_factory.h b/include/onnxruntime/core/providers/cpu/cpu_provider_factory.h index 32289eb5bb..360de99b5c 100644 --- a/include/onnxruntime/core/providers/cpu/cpu_provider_factory.h +++ b/include/onnxruntime/core/providers/cpu/cpu_provider_factory.h @@ -13,9 +13,6 @@ extern "C" { ORT_API_STATUS(OrtSessionOptionsAppendExecutionProvider_CPU, _In_ OrtSessionOptions* options, int use_arena) ORT_ALL_ARGS_NONNULL; -ORT_API_STATUS(OrtCreateCpuAllocatorInfo, enum OrtAllocatorType type, enum OrtMemType mem_type1, _Out_ OrtAllocatorInfo** out) -ORT_ALL_ARGS_NONNULL; - #ifdef __cplusplus } #endif diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index 25dbb4fd26..32168d2dd7 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -385,6 +385,12 @@ typedef enum OrtMemType { ORT_API_STATUS(OrtCreateAllocatorInfo, _In_ const char* name1, enum OrtAllocatorType type, int id1, enum OrtMemType mem_type1, _Out_ OrtAllocatorInfo** out); +/** + * Convenience function for special case of OrtCreateAllocatorInfo, for the CPU allocator. Uses name = "Cpu" and id = 0. + */ +ORT_API_STATUS(OrtCreateCpuAllocatorInfo, enum OrtAllocatorType type, enum OrtMemType mem_type1, _Out_ OrtAllocatorInfo** out) +ORT_ALL_ARGS_NONNULL; + /** * Test if two allocation info are equal * \return 0, equal. zero, not equal