Enable tests using ASharedMemory_create in TestValidation.cpp (#1164)
author김수진/동작제어Lab(SR)/Engineer/삼성전자 <sjsujin.kim@samsung.com>
Mon, 14 May 2018 00:03:01 +0000 (09:03 +0900)
committer서상민/동작제어Lab(SR)/Senior Engineer/삼성전자 <sangmin7.seo@samsung.com>
Mon, 14 May 2018 00:03:01 +0000 (09:03 +0900)
* 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 <sjsujin.kim@samsung.com>
runtimes/nn/CMakeLists.txt
runtimes/nn/runtime/ExecutionBuilder.cpp
runtimes/nn/runtime/ModelBuilder.cpp
runtimes/tests/neural_networks_test/TestValidation.cpp

index 7834594..5a3badd 100644 (file)
@@ -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)
index 5cf2485..adc2911 100644 (file)
@@ -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;
index 3e14250..d1b92bc 100644 (file)
@@ -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 = {
index 5a8c26c..55be266 100644 (file)
@@ -19,6 +19,7 @@
 #include "NeuralNetworksOEM.h"
 #include <android/sharedmem.h>
 #endif
+#include <cutils/ashmem.h>
 #include <gtest/gtest.h>
 #include <string>
 #include <sys/mman.h>
@@ -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);