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
This commit is contained in:
shahasad 2019-02-24 21:06:54 -08:00 committed by GitHub
parent 668fcf22d8
commit f9bae489bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 70 deletions

View file

@ -37,12 +37,6 @@
CopyToOutputDirectory="Never"
Visible="false"
/>
<None Include="$(OnnxRuntimeCsharpRoot)\..\include\onnxruntime\core\providers\cpu\cpu_provider_factory.h"
PackagePath="\build\native\include\core\providers\cpu"
Pack="true"
CopyToOutputDirectory="Never"
Visible="false"
/>
<None Include="$(NativeBuildOutputDir)\onnxruntime.lib"
PackagePath="\runtimes\win-x64\native"
Pack="true"

View file

@ -4,79 +4,87 @@
#include "CppUnitTest.h"
#include <assert.h>
#include <core/session/onnxruntime_c_api.h>
#include <core/providers/cpu/cpu_provider_factory.h>
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);
}
};
}

View file

@ -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

View file

@ -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

View file

@ -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