[moco-tf] Extract common Pad calculation method (#6252)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Tue, 6 Aug 2019 09:33:07 +0000 (18:33 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 6 Aug 2019 09:33:07 +0000 (18:33 +0900)
* [moco-tf] Extract common Pad calculation method

This will extract common Pad calculation methods for Canonical and TF dialect nodes

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
* use const

* use int64_t

* add guard

compiler/moco-tf/src/Transforms/FixShapeTransform.cpp

index 4462fe4..614bd02 100644 (file)
@@ -92,6 +92,69 @@ bool copy_shapedata(const loco::Node *src, loco::Node *dst)
   return true;
 }
 
+struct FixPadContext
+{
+  uint32_t input_height;
+  uint32_t input_width;
+  uint32_t output_height;
+  uint32_t output_width;
+  uint32_t stride_height;
+  uint32_t stride_width;
+  uint32_t effective_window_height;
+  uint32_t effective_window_width;
+};
+
+template <class T> void calc_node_pad(T *node, const FixPadContext &ctx)
+{
+  assert(node != nullptr);
+  assert(ctx.output_height > 0);
+  assert(ctx.output_width > 0);
+
+  // calculate padding height, width
+  int64_t i_height = (int64_t)(ctx.output_height - 1) * (int64_t)ctx.stride_height +
+                     (int64_t)ctx.effective_window_height - (int64_t)ctx.input_height;
+  int64_t i_width = (int64_t)(ctx.output_width - 1) * (int64_t)ctx.stride_width +
+                    (int64_t)ctx.effective_window_width - (int64_t)ctx.input_width;
+  uint32_t pad_height = i_height >= 0 ? (uint32_t)i_height : 0U;
+  uint32_t pad_width = i_width >= 0 ? (uint32_t)i_width : 0U;
+
+  // set padding values
+  node->pad()->top(pad_height / 2);
+  node->pad()->bottom(pad_height - node->pad()->top());
+  node->pad()->left(pad_width / 2);
+  node->pad()->right(pad_width - node->pad()->left());
+}
+
+template <class T> void calc_annot_paddata(T *node, const FixPadContext &ctx)
+{
+  assert(node != nullptr);
+  assert(ctx.output_height > 0);
+  assert(ctx.output_width > 0);
+
+  // PadData should be null
+  assert(node->template annot<PadData>() == nullptr);
+
+  // calculate padding height, width
+  int64_t i_height = (int64_t)(ctx.output_height - 1) * (int64_t)ctx.stride_height +
+                     (int64_t)ctx.effective_window_height - (int64_t)ctx.input_height;
+  int64_t i_width = (int64_t)(ctx.output_width - 1) * (int64_t)ctx.stride_width +
+                    (int64_t)ctx.effective_window_width - (int64_t)ctx.input_width;
+  uint32_t pad_height = i_height >= 0 ? (uint32_t)i_height : 0U;
+  uint32_t pad_width = i_width >= 0 ? (uint32_t)i_width : 0U;
+
+  // annotation of pad data
+  auto pad_data = stdex::make_unique<PadData>();
+
+  pad_data->pad()->top(pad_height / 2);
+  pad_data->pad()->bottom(pad_height - pad_data->pad()->top());
+  pad_data->pad()->left(pad_width / 2);
+  pad_data->pad()->right(pad_width - pad_data->pad()->left());
+
+  node->annot(std::move(pad_data));
+
+  assert(node->template annot<PadData>() != nullptr);
+}
+
 bool fix_shape(loco::AvgPool2D *node)
 {
   LOGGER(l);