SquaredDifference nnapi delegate and Custom ops addition (#2569)
authorPrasanna R/System SW /SRI-Bangalore/Engineer/삼성전자 <prasanna.r@samsung.com>
Wed, 5 Sep 2018 04:16:47 +0000 (09:46 +0530)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 5 Sep 2018 04:16:47 +0000 (13:16 +0900)
This patch adds changes for SquaredDifference in nnapi delegate
and also adds related CustomOps changes.
SquaredDifference kernel has been added.

Signed-off-by: prasannar <prasanna.r@samsung.com>
include/support/tflite/kernels/CustomOps.h
include/support/tflite/kernels/SquaredDifference.h [new file with mode: 0644]
libs/support/tflite/src/kernels/SquaredDifference.cpp [new file with mode: 0644]
libs/support/tflite/src/kernels/register.cpp
libs/support/tflite/src/nnapi_delegate.cpp

index a28d951..36c1c97 100644 (file)
@@ -20,6 +20,7 @@
 #include "tensorflow/contrib/lite/context.h"
 #include "support/tflite/kernels/TensorFlowMax.h"
 #include "support/tflite/kernels/RSQRT.h"
+#include "support/tflite/kernels/SquaredDifference.h"
 
 namespace tflite
 {
@@ -40,6 +41,7 @@ namespace nnfw
 
 REGISTER_FUNCTION(TensorFlowMax)
 REGISTER_FUNCTION(RSQRT)
+REGISTER_FUNCTION(SquaredDifference)
 #undef REGISTER_FUNCTION
 
 }  // namespace nnfw
diff --git a/include/support/tflite/kernels/SquaredDifference.h b/include/support/tflite/kernels/SquaredDifference.h
new file mode 100644 (file)
index 0000000..d0b7d3e
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NNFW_SUPPORT_TFLITE_KERNELS_SQUARED_DIFFERENCE_H__
+#define __NNFW_SUPPORT_TFLITE_KERNELS_SQUARED_DIFFERENCE_H__
+
+#include "tensorflow/contrib/lite/context.h"
+
+namespace tflite
+{
+namespace ops
+{
+namespace custom
+{
+namespace nnfw
+{
+namespace SquaredDifference
+{
+
+  void *InitSquaredDifference(TfLiteContext *context, const char *buffer, size_t length);
+  void FreeSquaredDifference(TfLiteContext *context, void *buffer);
+  TfLiteStatus PrepareSquaredDifference(TfLiteContext *context, TfLiteNode *node);
+  TfLiteStatus EvalSquaredDifference(TfLiteContext *context, TfLiteNode *node);
+
+} // namespace SquaredDifference
+} // namespace nnfw
+} // namespace custom
+} // namespace ops
+} // namespace tflite
+
+#endif
diff --git a/libs/support/tflite/src/kernels/SquaredDifference.cpp b/libs/support/tflite/src/kernels/SquaredDifference.cpp
new file mode 100644 (file)
index 0000000..a2f64c6
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "support/tflite/kernels/SquaredDifference.h"
+#include "tensorflow/contrib/lite/kernels/kernel_util.h"
+#include "tensorflow/contrib/lite/kernels/internal/tensor.h"
+
+#include <iostream>
+
+namespace tflite
+{
+namespace ops
+{
+namespace custom
+{
+namespace nnfw
+{
+namespace SquaredDifference
+{
+
+void *InitSquaredDifference(TfLiteContext *context, const char *buffer, size_t length)
+{
+  return nullptr;
+}
+
+void FreeSquaredDifference(TfLiteContext *context, void *buffer) {}
+
+TfLiteStatus PrepareSquaredDifference(TfLiteContext *context, TfLiteNode *node)
+{
+  TF_LITE_ENSURE_EQ(context, NumInputs(node), 2);
+  TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
+
+  const TfLiteTensor *input1 = GetInput(context, node, 0);
+  const TfLiteTensor *input2 = GetInput(context, node, 1);
+  TfLiteTensor *output = GetOutput(context, node, 0);
+
+  TF_LITE_ENSURE_EQ(context, input1->type, input2->type);
+  TF_LITE_ENSURE_EQ(context, input1->type, output->type);
+
+  return context->ResizeTensor(context, output, TfLiteIntArrayCopy(input1->dims));
+}
+
+TfLiteStatus EvalSquaredDifference(TfLiteContext *context, TfLiteNode *node)
+{
+
+  const TfLiteTensor *input1 = GetInput(context, node, 0);
+  const TfLiteTensor *input2 = GetInput(context, node, 1);
+
+  TfLiteTensor *output = GetOutput(context, node, 0);
+
+  size_t elements = NumElements(input1);
+
+  switch (input1->type)
+  {
+    case kTfLiteFloat32:
+    {
+      const float *in1 = GetTensorData<float>(input1);
+      const float *in2 = GetTensorData<float>(input2);
+      const float *in_end1 = in1 + elements;
+      float *out = output->data.f;
+
+      for (; in1 < in_end1; in1++, in2++, out++)
+        *out = ((*in1 - *in2) * (*in1 - *in2));
+
+      return kTfLiteOk;
+    }
+    case kTfLiteInt32:
+    {
+      const int *in1 = GetTensorData<int>(input1);
+      const int *in2 = GetTensorData<int>(input2);
+      const int *in_end1 = in1 + elements;
+      int *out = output->data.i32;
+
+      for (; in1 < in_end1; in1++, in2++, out++)
+        *out = ((*in1 - *in2) * (*in1 - *in2));
+
+      return kTfLiteOk;
+    }
+    case kTfLiteInt64:
+    {
+      const int64_t *in1 = GetTensorData<int64_t>(input1);
+      const int64_t *in2 = GetTensorData<int64_t>(input2);
+      const int64_t *in_end1 = in1 + elements;
+      int64_t *out = output->data.i64;
+
+      for (; in1 < in_end1; in1++, in2++, out++)
+        *out = ((*in1 - *in2) * (*in1 - *in2));
+
+      return kTfLiteOk;
+    }
+    default:
+    {
+      context->ReportError(context, "InputType is %d Unsupported", input1->type);
+      return kTfLiteError;
+    }
+  }
+}
+
+} // namespace SquaredDifference
+} // nnfw
+} // namespace custom
+} // namespace ops
+} // namespace tflite
index 0a28af3..6700b4d 100644 (file)
@@ -161,6 +161,7 @@ BuiltinOpResolver::BuiltinOpResolver()
 
   AddCustom("TensorFlowMax", tflite::ops::custom::nnfw::Register_TensorFlowMax());
   AddCustom("RSQRT", tflite::ops::custom::nnfw::Register_RSQRT());
+  AddCustom("SquaredDifference", tflite::ops::custom::nnfw::Register_SquaredDifference());
 }
 
 } // namespace builtin
index debd4f8..b97529e 100644 (file)
@@ -570,6 +570,15 @@ void AddOpsAndParams(tflite::Interpreter* interpreter,
               reinterpret_cast<uint32_t*>(node.outputs->data)));
           continue;
         }
+        else if (custom_name.compare("SquaredDifference") == 0) {
+          CHECK_NN(ANeuralNetworksModel_addOperationEx(
+              nn_model, ANEURALNETWORKS_SQUARED_DIFFERENCE_EX,
+              static_cast<uint32_t>(augmented_inputs.size()),
+              augmented_inputs.data(),
+              static_cast<uint32_t>(node.outputs->size),
+              reinterpret_cast<uint32_t*>(node.outputs->data)));
+          continue;
+        }
 
         FATAL("Custom operations are not supported when using NNAPI.");
         nn_op_type = -1;  // set to invalid