From c7e82f90e8293c10789d3724594d01938729b064 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Prasanna=20R/System=20SW=20/SRI-Bangalore/Engineer/?= =?utf8?q?=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Wed, 5 Sep 2018 09:46:47 +0530 Subject: [PATCH] SquaredDifference nnapi delegate and Custom ops addition (#2569) This patch adds changes for SquaredDifference in nnapi delegate and also adds related CustomOps changes. SquaredDifference kernel has been added. Signed-off-by: prasannar --- include/support/tflite/kernels/CustomOps.h | 2 + include/support/tflite/kernels/SquaredDifference.h | 44 ++++++++ .../tflite/src/kernels/SquaredDifference.cpp | 116 +++++++++++++++++++++ libs/support/tflite/src/kernels/register.cpp | 1 + libs/support/tflite/src/nnapi_delegate.cpp | 9 ++ 5 files changed, 172 insertions(+) create mode 100644 include/support/tflite/kernels/SquaredDifference.h create mode 100644 libs/support/tflite/src/kernels/SquaredDifference.cpp diff --git a/include/support/tflite/kernels/CustomOps.h b/include/support/tflite/kernels/CustomOps.h index a28d951..36c1c97 100644 --- a/include/support/tflite/kernels/CustomOps.h +++ b/include/support/tflite/kernels/CustomOps.h @@ -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 index 0000000..d0b7d3e --- /dev/null +++ b/include/support/tflite/kernels/SquaredDifference.h @@ -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 index 0000000..a2f64c6 --- /dev/null +++ b/libs/support/tflite/src/kernels/SquaredDifference.cpp @@ -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 + +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(input1); + const float *in2 = GetTensorData(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(input1); + const int *in2 = GetTensorData(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(input1); + const int64_t *in2 = GetTensorData(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 diff --git a/libs/support/tflite/src/kernels/register.cpp b/libs/support/tflite/src/kernels/register.cpp index 0a28af3..6700b4d 100644 --- a/libs/support/tflite/src/kernels/register.cpp +++ b/libs/support/tflite/src/kernels/register.cpp @@ -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 diff --git a/libs/support/tflite/src/nnapi_delegate.cpp b/libs/support/tflite/src/nnapi_delegate.cpp index debd4f8..b97529e 100644 --- a/libs/support/tflite/src/nnapi_delegate.cpp +++ b/libs/support/tflite/src/nnapi_delegate.cpp @@ -570,6 +570,15 @@ void AddOpsAndParams(tflite::Interpreter* interpreter, reinterpret_cast(node.outputs->data))); continue; } + else if (custom_name.compare("SquaredDifference") == 0) { + CHECK_NN(ANeuralNetworksModel_addOperationEx( + nn_model, ANEURALNETWORKS_SQUARED_DIFFERENCE_EX, + static_cast(augmented_inputs.size()), + augmented_inputs.data(), + static_cast(node.outputs->size), + reinterpret_cast(node.outputs->data))); + continue; + } FATAL("Custom operations are not supported when using NNAPI."); nn_op_type = -1; // set to invalid -- 2.7.4