[concat] Move validity checks to init
authorParichay Kapoor <pk.kapoor@samsung.com>
Tue, 3 Nov 2020 04:33:05 +0000 (13:33 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Wed, 4 Nov 2020 05:42:01 +0000 (14:42 +0900)
Move the dimension checks for validity of the various inputs
for concat layer are moved to initialize().
They are kept back in forwarding() under DEBUG conditional.

**Self evaluation:**
1. Build test: [x]Passed [ ]Failed [ ]Skipped
2. Run test: [x]Passed [ ]Failed [ ]Skipped

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
nntrainer/include/tensor_dim.h
nntrainer/src/concat_layer.cpp
nntrainer/src/tensor_dim.cpp

index 42e0429..0ff0e62 100644 (file)
@@ -99,7 +99,8 @@ public:
   bool isEmpty() const { return len == 0; }
   unsigned int rank() const;
 
-  unsigned int &operator[](unsigned int index);
+  unsigned int &operator[](const unsigned int index);
+  const unsigned int &operator[](const unsigned int index) const;
 
   /**
    * @brief Calculate standard strides
index 1181d2f..2f3cbad 100644 (file)
@@ -24,12 +24,22 @@ namespace nntrainer {
 int ConcatLayer::initialize() {
   int status = ML_ERROR_NONE;
   unsigned int channel = 0;
+
   if (num_inputs == 0) {
     ml_loge("Error: number of inputs are not initialized");
     return ML_ERROR_INVALID_PARAMETER;
   }
 
-  for (unsigned int idx = 0; idx < num_inputs; ++idx) {
+  const TensorDim &d = input_dim[0];
+  channel += d.channel();
+  for (unsigned int idx = 1; idx < num_inputs; ++idx) {
+    const TensorDim &dim = input_dim[idx];
+
+    for (unsigned int i = 2; i < d.rank(); ++i) {
+      if (d[i] != dim[i])
+        throw std::runtime_error("Error: concat layer requires same "
+                                 "shape from  all input layers");
+    }
     channel += input_dim[idx].channel();
   }
 
@@ -40,35 +50,30 @@ int ConcatLayer::initialize() {
 }
 
 sharedConstTensors ConcatLayer::forwarding(sharedConstTensors in) {
+  hidden = Tensor(output_dim[0]);
 
-  TensorDim d = in[0]->getDim();
-  unsigned int concat_channel = d.channel();
-
+#ifdef DEBUG
+  const TensorDim &d = in[0]->getDim();
+  channel += d.channel();
   for (unsigned int idx = 1; idx < num_inputs; ++idx) {
-    TensorDim dim = in[idx]->getDim();
+    const TensorDim &dim = in[idx]->getDim();
 
-#ifdef DEBUG
     for (unsigned int i = 2; i < d.rank(); ++i) {
       if (d[i] != dim[i])
         throw std::runtime_error("Error: concat layer requires same "
                                  "shape from  all input layers");
     }
-#endif
-    concat_channel += dim.channel();
+    channel += input_dim[idx].channel();
   }
 
-#ifdef DEBUG
-  if (concat_channel != output_dim[0].channel()) {
+  if (channel != output_dim[0].channel())
     throw std::runtime_error(
       "Error: Sum of channel of input layers is not same with output channel");
-  }
 #endif
 
-  hidden = Tensor(output_dim[0]);
-
-  unsigned int f_size = d.width() * d.height() * (concat_channel);
+  unsigned int f_size = output_dim[0].getFeatureLen();
 
-  for (unsigned int b = 0; b < d.batch(); ++b) {
+  for (unsigned int b = 0; b < input_dim[0].batch(); ++b) {
     unsigned int position = 0;
     for (unsigned int idx = 0; idx < num_inputs; ++idx) {
       TensorDim in_dim = in[idx]->getDim();
index 236e9b8..a815731 100644 (file)
@@ -114,7 +114,14 @@ unsigned int TensorDim::rank() const {
   return rank;
 }
 
-unsigned int &TensorDim::operator[](unsigned int index) {
+unsigned int &TensorDim::operator[](const unsigned int index) {
+  if (index >= MAXDIM)
+    throw std::out_of_range(
+      "[TensorDim] Tensor Dimension index should be between 0 and 4");
+  return dim[index];
+}
+
+const unsigned int &TensorDim::operator[](const unsigned int index) const {
   if (index >= MAXDIM)
     throw std::out_of_range(
       "[TensorDim] Tensor Dimension index should be between 0 and 4");