From 755233c751b31d38b6a5d1533863cdcbf1504c09 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EA=B9=80=EC=88=98=EC=A7=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84?= =?utf8?q?=EC=9E=90?= Date: Mon, 14 May 2018 09:03:01 +0900 Subject: [PATCH] Enable tests using ASharedMemory_create in TestValidation.cpp (#1164) * Enable tests using ASharedMemory_create in TestValidation.cpp for NN Runtime tests We've commented out the tests using `ASharedMemory_create` in `TestValidation.cpp` for NN Runtime tests. But I figured out it's ok to use `ashmem_create_region` instead of `ASharedMemory_create` likes [Memory.cpp](https://github.sec.samsung.net/STAR/nnfw/blob/master/runtimes/nn/runtime/Memory.cpp#L34) So it seems it will be able to enable the tests using `ASharedMemory_create` in `TestValidation.cpp`. - Add target includes in nn runtime - Check bad states - Enable tests Close : #1163 Signed-off-by: sjsujinkim --- runtimes/nn/CMakeLists.txt | 1 + runtimes/nn/runtime/ExecutionBuilder.cpp | 7 +++++++ runtimes/nn/runtime/ModelBuilder.cpp | 7 +++++++ runtimes/tests/neural_networks_test/TestValidation.cpp | 11 ++++------- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/runtimes/nn/CMakeLists.txt b/runtimes/nn/CMakeLists.txt index 7834594..5a3badd 100644 --- a/runtimes/nn/CMakeLists.txt +++ b/runtimes/nn/CMakeLists.txt @@ -17,6 +17,7 @@ SET(INC_DIRS ${INC_DIRS} include) include_directories(${LIB_RUNTIME} PRIVATE ${INC_DIRS}) add_library(${LIB_RUNTIME} SHARED ${SRCS}) +target_include_directories(${LIB_RUNTIME} PUBLIC ${INC_DIRS}) if(TARGET kernelacl) target_compile_definitions(${LIB_RUNTIME} PRIVATE USE_NNFW_ACL_KERNELS) target_link_libraries(${LIB_RUNTIME} kernelacl) diff --git a/runtimes/nn/runtime/ExecutionBuilder.cpp b/runtimes/nn/runtime/ExecutionBuilder.cpp index 5cf2485..adc2911 100644 --- a/runtimes/nn/runtime/ExecutionBuilder.cpp +++ b/runtimes/nn/runtime/ExecutionBuilder.cpp @@ -59,6 +59,13 @@ int ModelArgumentInfo::setFromMemory(const Operand& operand, const ANeuralNetwor if (n != ANEURALNETWORKS_NO_ERROR) { return n; } + uint32_t neededLength = sizeOfData(operand.type, dimensions); + if (operand.type != OperandType::OEM && neededLength != length) { + LOG(ERROR) << "Setting argument with invalid length: " << length + << ", expected length: " << neededLength; + return ANEURALNETWORKS_BAD_DATA; + } + state = ModelArgumentInfo::MEMORY; locationAndLength = {.poolIndex = poolIndex, .offset = offset, .length = length}; buffer = nullptr; diff --git a/runtimes/nn/runtime/ModelBuilder.cpp b/runtimes/nn/runtime/ModelBuilder.cpp index 3e14250..d1b92bc 100644 --- a/runtimes/nn/runtime/ModelBuilder.cpp +++ b/runtimes/nn/runtime/ModelBuilder.cpp @@ -163,6 +163,10 @@ int ModelBuilder::copyLargeValuesToSharedMemory() { int ModelBuilder::setOperandValueFromMemory(uint32_t index, const Memory* memory, uint32_t offset, size_t length) { VLOG(MODEL) << __func__ << " for operand " << index << " offset " << offset << " size " << length; + if (badState("setOperandValueFromMemory")) { + return ANEURALNETWORKS_BAD_STATE; + } + if (index >= operandCount()) { LOG(ERROR) << "ANeuralNetworksModel_setOperandValueFromMemory setting operand " << index << " of " << operandCount(); @@ -175,6 +179,9 @@ int ModelBuilder::setOperandValueFromMemory(uint32_t index, const Memory* memory << " bytes when needing " << neededLength; return ANEURALNETWORKS_BAD_DATA; } + if (!memory->validateSize(offset, length)) { + return ANEURALNETWORKS_BAD_DATA; + } // TODO validate does not exceed length of memory operand.lifetime = OperandLifeTime::CONSTANT_REFERENCE; operand.location = { diff --git a/runtimes/tests/neural_networks_test/TestValidation.cpp b/runtimes/tests/neural_networks_test/TestValidation.cpp index 5a8c26c..55be266 100644 --- a/runtimes/tests/neural_networks_test/TestValidation.cpp +++ b/runtimes/tests/neural_networks_test/TestValidation.cpp @@ -19,6 +19,7 @@ #include "NeuralNetworksOEM.h" #include #endif +#include #include #include #include @@ -176,7 +177,6 @@ TEST_F(ValidationTestModel, SetOperandValue) { EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 0, buffer, sizeof(float)), ANEURALNETWORKS_BAD_STATE); } -#if 0 // TODO-NNRT : Enable if we support 'ASharedMemory_create' TEST_F(ValidationTestModel, SetOperandValueFromMemory) { uint32_t dimensions[]{1}; ANeuralNetworksOperandType floatType{ @@ -185,7 +185,7 @@ TEST_F(ValidationTestModel, SetOperandValueFromMemory) { .dimensions = dimensions}; EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &floatType), ANEURALNETWORKS_NO_ERROR); const size_t memorySize = 20; - int memoryFd = ASharedMemory_create("nnMemory", memorySize); + int memoryFd = ashmem_create_region("nnMemory", memorySize); ASSERT_GT(memoryFd, 0); ANeuralNetworksMemory* memory; EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, @@ -226,7 +226,6 @@ TEST_F(ValidationTestModel, SetOperandValueFromMemory) { sizeof(float)), ANEURALNETWORKS_BAD_STATE); } -#endif // TODO-NNRT #if 0 // TODO-NNRT : Enable if we support OEM OP. TEST_F(ValidationTestModel, AddOEMOperand) { ANeuralNetworksOperandType OEMScalarType{ @@ -398,12 +397,11 @@ TEST_F(ValidationTestExecution, SetOutput) { EXPECT_EQ(ANeuralNetworksExecution_setOutput(execution, -1, nullptr, buffer, sizeof(float)), ANEURALNETWORKS_BAD_DATA); } -#if 0 // TODO-NNRT : Enable if we support 'ASharedMemory_create' TEST_F(ValidationTestExecution, SetInputFromMemory) { ANeuralNetworksExecution* execution; EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR); const size_t memorySize = 20; - int memoryFd = ASharedMemory_create("nnMemory", memorySize); + int memoryFd = ashmem_create_region("nnMemory", memorySize); ASSERT_GT(memoryFd, 0); ANeuralNetworksMemory* memory; EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, @@ -440,7 +438,7 @@ TEST_F(ValidationTestExecution, SetOutputFromMemory) { ANeuralNetworksExecution* execution; EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR); const size_t memorySize = 20; - int memoryFd = ASharedMemory_create("nnMemory", memorySize); + int memoryFd = ashmem_create_region("nnMemory", memorySize); ASSERT_GT(memoryFd, 0); ANeuralNetworksMemory* memory; EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, @@ -473,7 +471,6 @@ TEST_F(ValidationTestExecution, SetOutputFromMemory) { memory, memorySize - 3, sizeof(float)), ANEURALNETWORKS_BAD_DATA); } -#endif // TODO-NNRT TEST_F(ValidationTestExecution, StartCompute) { ANeuralNetworksExecution* execution; EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR); -- 2.7.4