[SplitLayer] Support flexible dimension
authorJihoon Lee <jhoon.it.lee@samsung.com>
Wed, 2 Jun 2021 07:19:59 +0000 (16:19 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 10 Jun 2021 11:13:53 +0000 (20:13 +0900)
This patch adds support for the flexible dimension in split layer.

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

Cc: Parichay Kapoor <pk.kapoor@samsung.com>
Signed-off-by: Jihoon Lee <jhoon.it.lee@samsung.com>
Applications/ProductRatings/jni/main.cpp
Applications/ProductRatings/jni/meson.build
Applications/ProductRatings/res/product_ratings_model.ini
nntrainer/layers/split_layer.cpp
nntrainer/layers/split_layer.h
nntrainer/tensor/tensor.cpp

index b7b6787..9ad8395 100644 (file)
@@ -212,8 +212,8 @@ int main(int argc, char *argv[]) {
 
     std::cout << "Input dimension: " << NN.getInputDimension()[0];
 
-  } catch (...) {
-    std::cerr << "Unexpected Error during init" << std::endl;
+  } catch (std::exception &e) {
+    std::cerr << "Unexpected Error during init " << e.what() << std::endl;
     return 1;
   }
 
index f5029fe..91fc573 100644 (file)
@@ -9,7 +9,7 @@ e = executable('nntrainer_product_ratings',
 )
 
 # test split example
-test('nntrainer_product_ratings', e, args: ['train',
+test('app_product_ratings', e, args: ['train',
   nntr_app_resdir / 'ProductRatings' / 'product_ratings_model.ini',
   nntr_app_resdir / 'ProductRatings' / 'sample_product_ratings.txt']
 )
index 3616451..499f99e 100644 (file)
@@ -5,7 +5,6 @@ Epochs = 100            # Epochs
 Loss = mse     # Loss function : mse (mean squared error)
                         #                       cross ( cross entropy )
 # Save_Path = "product_ratings_model.bin" # enable this to save result
-batch_size = 20                # batch size
 
 [Optimizer]
 Type = adam
index 72710ce..97a0b39 100644 (file)
@@ -72,10 +72,12 @@ int SplitLayer::initialize(Manager &manager) {
    * together and all the dimensions after the split_dimension to faciliate
    * easier splitting of the data.
    */
-  input_reshape_helper = {1, 1, 1, 1};
-  for (unsigned int idx = 0; idx < split_dimension; ++idx) {
-    input_reshape_helper.batch(input_reshape_helper.batch() *
-                               in_dim.getTensorDim(idx));
+  leading_helper_dim = 1;
+  input_reshape_helper.channel(1);
+  input_reshape_helper.height(1);
+  input_reshape_helper.width(1);
+  for (unsigned int idx = 1; idx < split_dimension; ++idx) {
+    leading_helper_dim *= in_dim.getTensorDim(idx);
   }
 
   input_reshape_helper.height(in_dim.getTensorDim(split_dimension));
@@ -92,9 +94,17 @@ int SplitLayer::initialize(Manager &manager) {
   output_reshape_helper = input_reshape_helper;
   output_reshape_helper.height(1);
 
+  setBatch(in_dim.batch());
+
   return status;
 }
 
+void SplitLayer::setBatch(unsigned int batch) {
+  Layer::setBatch(batch);
+  input_reshape_helper.batch(batch * leading_helper_dim);
+  output_reshape_helper.batch(batch * leading_helper_dim);
+}
+
 void SplitLayer::forwarding(bool training) {
   Tensor &input_ = net_input[0]->getVariableRef();
 
@@ -153,6 +163,8 @@ void SplitLayer::setProperty(const PropertyType type,
   case PropertyType::split_dimension: {
     if (!value.empty()) {
       status = setUint(split_dimension, value);
+      NNTR_THROW_IF(split_dimension == 0, std::invalid_argument)
+        << "[Split] Batch dimension cannot be split dimension";
       throw_status(status);
     }
   } break;
index ff91798..a18d3ad 100644 (file)
@@ -83,6 +83,11 @@ public:
    */
   void calcDerivative() override;
 
+  /**
+   * @copydoc Layer::setBatch(unsigned int batch)
+   */
+  void setBatch(unsigned int batch) override;
+
   using Layer::setProperty;
 
   /**
@@ -101,6 +106,8 @@ public:
 
 private:
   unsigned int split_dimension; /** dimension along which to split the input */
+  unsigned int leading_helper_dim; /**< batch dimension of helper dimension not
+                                containing the actual batch */
   TensorDim input_reshape_helper;  /** helper dimension to reshape input */
   TensorDim output_reshape_helper; /** helper dimension to reshape outputs */
 };
index 9254763..a3dcca6 100644 (file)
@@ -991,9 +991,10 @@ Tensor Tensor::clone() const {
 }
 
 void Tensor::reshape(const TensorDim &d) {
-  if (d.getDataLen() != dim.getDataLen()) {
-    throw std::invalid_argument("Error: reshape cannot change the tensor size");
-  }
+  NNTR_THROW_IF(d.getDataLen() != dim.getDataLen(), std::invalid_argument)
+    << "[Tensor]: reshape cannot change the buffer size, trying reshaping "
+       "\nfrom "
+    << getDim() << " to " << d;
 
   dim = d;
   strides = d.computeStrides();