[ann] Support Div operation (#2800)
author박천교/On-Device Lab(SR)/Engineer/삼성전자 <ch.bahk@samsung.com>
Thu, 10 Jan 2019 04:16:14 +0000 (13:16 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 10 Jan 2019 04:16:14 +0000 (13:16 +0900)
* [ann] Support Div operation

This commit would support Div operation in ann

Signed-off-by: Cheongyo Bahk <ch.bahk@samsung.com>
* Change indent and minor refactoring

contrib/ann/runtimes/ref/src/Executor.cpp
contrib/ann/runtimes/ref/src/ops/Div.cpp [new file with mode: 0644]
contrib/ann/runtimes/ref/src/ops/Div.float.cpp [new file with mode: 0644]
contrib/ann/runtimes/ref/src/ops/Div.float.h [new file with mode: 0644]
contrib/ann/runtimes/ref/src/ops/Div.h [new file with mode: 0644]

index 70c3fe5..888fc9c 100644 (file)
@@ -46,6 +46,8 @@
 #include "ops/Pad.h"
 #include "ops/Sub.h"
 #include "ops/Sub.float.h"
+#include "ops/Div.h"
+#include "ops/Div.float.h"
 
 #include "Logging.h"
 #include "Assert.h"
@@ -774,6 +776,29 @@ int Executor::executeOperation(const Operation &operation)
       }
     }
     break;
+    case OperationType::DIV:
+    {
+      if (!allParametersPresent(3, 1))
+      {
+        return ANEURALNETWORKS_BAD_DATA;
+      }
+      const RunTimeOperandInfo &in1 = mOperands[ins[0]];
+      const RunTimeOperandInfo &in2 = mOperands[ins[1]];
+      int32_t activation = getScalarData<int32_t>(mOperands[ins[2]]);
+
+      RunTimeOperandInfo &out = mOperands[outs[0]];
+      Shape outShape = out.shape();
+
+      ASSERT(in1.type == OperandType::TENSOR_FLOAT32);
+      {
+        success = divPrepare(in1.shape(), in2.shape(), &outShape) &&
+                  setInfoAndAllocateIfNeeded(&out, outShape) &&
+                  divFloat32(reinterpret_cast<const float *>(in1.buffer), in1.shape(),
+                       reinterpret_cast<const float *>(in2.buffer), in2.shape(), activation,
+                       reinterpret_cast<float *>(out.buffer), outShape);
+      }
+    }
+    break;
     default:
       NYI(getOperationName(operation.type));
       break;
diff --git a/contrib/ann/runtimes/ref/src/ops/Div.cpp b/contrib/ann/runtimes/ref/src/ops/Div.cpp
new file mode 100644 (file)
index 0000000..250e72b
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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 "Div.h"
+#include "Assert.h"
+
+bool divPrepare(const Shape &in1, const Shape &in2, Shape *out)
+{
+  ASSERT(getNumberOfDimensions(in1) <= 4 && getNumberOfDimensions(in2) <= 4);
+  ASSERT(in1.type == in2.type);
+  if (SameShape(in1, in2))
+  {
+    return SetShape(in1, out);
+  }
+  else
+  {
+    // Broadcast needed
+    uint32_t numberOfDims1 = getNumberOfDimensions(in1);
+    uint32_t numberOfDims2 = getNumberOfDimensions(in2);
+    uint32_t maxDims = std::max(numberOfDims1, numberOfDims2);
+    out->dimensions = std::vector<uint32_t>(maxDims);
+    for (uint32_t i = 1; i <= maxDims; i++)
+    {
+      uint32_t dim1 = 1;
+      if (i <= numberOfDims1)
+      {
+        dim1 = getSizeOfDimension(in1, numberOfDims1 - i);
+      }
+      uint32_t dim2 = 1;
+      if (i <= numberOfDims2)
+      {
+        dim2 = getSizeOfDimension(in2, numberOfDims2 - i);
+      }
+      if (dim1 != dim2 && dim1 != 1 && dim2 != 1)
+      {
+        LOG(ERROR) << "Dimensions mismatch for BroadcastDiv";
+        return false;
+      }
+      out->dimensions[maxDims - i] = std::max(dim1, dim2);
+    }
+  }
+  return true;
+}
diff --git a/contrib/ann/runtimes/ref/src/ops/Div.float.cpp b/contrib/ann/runtimes/ref/src/ops/Div.float.cpp
new file mode 100644 (file)
index 0000000..19c7ef6
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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 "Div.float.h"
+
+#include "internal/Array.h"
+#include "internal/NDArray.h"
+#include "internal/Matrix.h"
+#include "internal/Fused.h"
+#include "internal/ActivationUtils.h"
+
+template <FusedActivationFunctionType Ac>
+void Div(const float *input1_data, const Dims<4> &input1_dims, const float *input2_data,
+         const Dims<4> &input2_dims, float *output_data, const Dims<4> &output_dims)
+{
+  MatchingArraySize(input1_dims, 3, input2_dims, 3, output_dims, 3);
+  MatchingArraySize(input1_dims, 2, input2_dims, 2, output_dims, 2);
+  MatchingArraySize(input1_dims, 1, input2_dims, 1, output_dims, 1);
+  MatchingArraySize(input1_dims, 0, input2_dims, 0, output_dims, 0);
+  DCHECK(IsPackedWithoutStrides(input1_dims));
+  DCHECK(IsPackedWithoutStrides(input2_dims));
+  DCHECK(IsPackedWithoutStrides(output_dims));
+
+  const int size = input1_dims.sizes[3] * input1_dims.strides[3];
+
+  for (int i = 0; i < size; i++)
+  {
+    auto x = input1_data[i] * input2_data[i];
+    output_data[i] = ActivationFunction<Ac>(x);
+  }
+}
+
+// TODO: We can implement BroadcastDiv on buffers of arbitrary
+// dimensionality if the runtime code does a single loop over one dimension
+// that handles broadcasting as the base case. The code generator would then
+// generate max(D1, D2) nested for loops.
+// TODO: BroadcastDiv is intentionally duplicated from
+// reference_ops.h. Once an optimized version is implemented and NdArrayDesc<T>
+// is no longer referenced in this file, move NdArrayDesc<T> from types.h to
+// reference_ops.h.
+template <FusedActivationFunctionType Ac>
+void BroadcastDiv(const float *input1_data, const Dims<4> &input1_dims, const float *input2_data,
+                  const Dims<4> &input2_dims, float *output_data, const Dims<4> &output_dims)
+{
+  NdArrayDesc<4> desc1;
+  NdArrayDesc<4> desc2;
+  NdArrayDescsForElementwiseBroadcast(input1_dims, input2_dims, &desc1, &desc2);
+
+  // In Tensorflow, the dimensions are canonically named (batch_number, row,
+  // col, channel), with extents (batches, height, width, depth), with the
+  // trailing dimension changing most rapidly (channels has the smallest stride,
+  // typically 1 element).
+  //
+  // In generated C code, we store arrays with the dimensions reversed. The
+  // first dimension has smallest stride.
+  //
+  // We name our variables by their Tensorflow convention, but generate C code
+  // nesting loops such that the innermost loop has the smallest stride for the
+  // best cache behavior.
+  for (int b = 0; b < ArraySize(output_dims, 3); ++b)
+  {
+    for (int y = 0; y < ArraySize(output_dims, 2); ++y)
+    {
+      for (int x = 0; x < ArraySize(output_dims, 1); ++x)
+      {
+        for (int c = 0; c < ArraySize(output_dims, 0); ++c)
+        {
+          output_data[Offset(output_dims, c, x, y, b)] =
+              ActivationFunction<Ac>(input1_data[SubscriptToIndex(desc1, c, x, y, b)] /
+                                     input2_data[SubscriptToIndex(desc2, c, x, y, b)]);
+        }
+      }
+    }
+  }
+}
+
+bool divFloat32(const float *in1, const Shape &shape1, const float *in2, const Shape &shape2,
+                int32_t activation, float *out, const Shape &shapeOut)
+{
+  bool needBroadcast = !SameShape(shape1, shape2);
+
+#define ANDROID_NN_NORMAL_DIV(activation)                                        \
+  Div<FusedActivationFunctionType::activation>(in1, convertShapeToDims(shape1),  \
+                                               in2, convertShapeToDims(shape2),  \
+                                               out, convertShapeToDims(shapeOut))
+
+#define ANDROID_NN_BROADCAST_DIV(activation)              \
+  BroadcastDiv<FusedActivationFunctionType::activation>(  \
+      in1, convertShapeToDims(shape1),                    \
+      in2, convertShapeToDims(shape2),                    \
+      out, convertShapeToDims(shapeOut))
+
+  if (needBroadcast)
+  {
+    ANDROID_NN_MACRO_DISPATCH(ANDROID_NN_BROADCAST_DIV)
+  }
+  else
+  {
+    ANDROID_NN_MACRO_DISPATCH(ANDROID_NN_NORMAL_DIV)
+  }
+
+#undef ANDROID_NN_NORMAL_ADD
+#undef ANDROID_NN_BROADCAST_ADD
+  return true;
+}
diff --git a/contrib/ann/runtimes/ref/src/ops/Div.float.h b/contrib/ann/runtimes/ref/src/ops/Div.float.h
new file mode 100644 (file)
index 0000000..a2aa7e1
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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 __OP_DIV_FLOAT_H__
+#define __OP_DIV_FLOAT_H__
+
+#include "Shape.h"
+
+#include <cstdint>
+
+bool divFloat32(const float *in1, const Shape &shape1, const float *in2, const Shape &shape2,
+                int32_t activation, float *out, const Shape &shapeOut);
+
+#endif // __OP_DIV_FLOAT_H__
diff --git a/contrib/ann/runtimes/ref/src/ops/Div.h b/contrib/ann/runtimes/ref/src/ops/Div.h
new file mode 100644 (file)
index 0000000..5eb98a3
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * 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 __OP_DIV_H__
+#define __OP_DIV_H__
+
+#include "Shape.h"
+
+bool divPrepare(const Shape &in1, const Shape &in2, Shape *out);
+
+#endif // __OP_DIV_H__