Workaround to pass scalar type tensor in nnapi delegate (#3707)
author오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 28 Nov 2018 00:53:41 +0000 (09:53 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 28 Nov 2018 00:53:41 +0000 (09:53 +0900)
Some model have scalar type input/output, but NNAPI requre tensor type for these operands. (ex. elementwise sub, div)
To satisfy NNAPI spec, change scalar to vector with size 1 in nnapi delegate.

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
libs/support/tflite/src/nnapi_delegate.cpp

index cfeba89..3f8a129 100644 (file)
@@ -123,6 +123,8 @@ int32_t GetAndroidSdkVersionCached() {
   return androidSdkVersion;
 }
 
+static const uint32_t dimension_for_scalar[1] = {1};
+
 }  // namespace
 
 NNAPIAllocation::NNAPIAllocation(const char* filename,
@@ -203,10 +205,10 @@ TfLiteStatus addTensorOperands(tflite::Interpreter* interpreter,
       // WORKAROUND Some model have dimension zero
       switch (tensor->type) {
         case kTfLiteFloat32:
-          nn_type = ANEURALNETWORKS_FLOAT32;
+          nn_type = ANEURALNETWORKS_TENSOR_FLOAT32;
           break;
         case kTfLiteInt32:
-          nn_type = ANEURALNETWORKS_INT32;
+          nn_type = ANEURALNETWORKS_TENSOR_INT32;
           break;
         default:
           logError("NNAPI doesn't support tensors with rank 0 (index %d name %s)",
@@ -227,6 +229,12 @@ TfLiteStatus addTensorOperands(tflite::Interpreter* interpreter,
     ANeuralNetworksOperandType operand_type{
         nn_type, static_cast<uint32_t>(tensor->dims->size),
         reinterpret_cast<uint32_t*>(tensor->dims->data), scale, zeroPoint};
+    if (tensor->dims->size == 0) {
+      // WORKAROUND Some model have dimension zero
+      // Consider scalar as vector size 1
+      operand_type.dimensions = dimension_for_scalar;
+      operand_type.dimensionCount = 1;
+    }
     RETURN_ERROR_IF_NN_FAILED(
         ANeuralNetworksModel_addOperand(nn_model, &operand_type));
     // TODO(aselle): Based on Michael's suggestion, limiting this to read