[unittest] Added test for incremental forwarding for layers
authorDebadri Samaddar <s.debadri@samsung.com>
Thu, 9 May 2024 11:16:47 +0000 (16:46 +0530)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 23 May 2024 04:28:26 +0000 (13:28 +0900)
Added incremental forwarding as an option for unit testing layers

Signed-off-by: Debadri Samaddar <s.debadri@samsung.com>
nntrainer/layers/cl_layers/fc_layer_cl.cpp
test/unittest/layers/layers_common_tests.h
test/unittest/layers/layers_golden_tests.cpp
test/unittest/layers/unittest_layers_fully_connected_cl.cpp

index 4ee5f6adf0bd96f52a7f436c3e39c3f0e89b77a0..4629dced262da2477138513881df97113115b916 100644 (file)
@@ -37,7 +37,7 @@ std::string fc_dot_cl_kernel_ =
   R"(__kernel void fc_dot_cl(const __global float* A, const __global float* X, unsigned int K, float res) {
         res = 0;
         for (unsigned int i = 0; i < K; i++){
-            res += A[i] * X[i];`
+            res += A[i] * X[i];
         }
     })";
 
index 57f693c0a2ca9f8ec94ad973429b67922b0d5059..d63357c80503da03c82317672dda42a2b53be3c7 100644 (file)
@@ -93,6 +93,7 @@ class LayerPropertySemantics : public LayerSemantics {};
 typedef enum {
   SKIP_CALC_GRAD = 1 << 0,  /**< skip calculating gradient and compare */
   SKIP_CALC_DERIV = 1 << 1, /**< skip calculating derivative and compare */
+  USE_INC_FORWARD = 1 << 2, /**< use incremental forwarding and compare */
 
   FORWARD_MODE_INFERENCE =
     1 << 2, /**< set if layer should be forwarded with inference mode */
@@ -172,6 +173,14 @@ public:
    */
   bool shouldSkipCalcGrad();
 
+  /**
+   * @brief check if given test suite should use incremental forwarding instead
+   * of normal forwarding
+   *
+   * @return bool true if should use incremental forwarding
+   */
+  bool shouldUseIncForward();
+
   /**
    * @brief check if given test suite should skip cosine similarity check
    *
index 64400e6ecdfc51fcec17c20fe555d909efc6c2fe..56d591019bc6b1e922b581f66efe6ebab19351cb 100644 (file)
@@ -364,6 +364,11 @@ bool LayerGoldenTest::shouldSkipCalcGrad() {
          LayerGoldenTestParamOptions::SKIP_CALC_GRAD;
 }
 
+bool LayerGoldenTest::shouldUseIncForward() {
+  return std::get<int>(GetParam()) &
+         LayerGoldenTestParamOptions::USE_INC_FORWARD;
+}
+
 bool LayerGoldenTest::shouldSkipCosineSimilarity() {
   return std::get<int>(GetParam()) &
          LayerGoldenTestParamOptions::SKIP_COSINE_SIMILARITY;
@@ -387,15 +392,31 @@ TEST_P(LayerGoldenTest, run) {
 
   bool skip_calc_grad = shouldSkipCalcGrad();
   bool skip_calc_deriv = shouldSkipCalcDeriv();
+  bool use_inc_forward = shouldUseIncForward();
   bool dropout_compare_60_percent = shouldMatchDropout60Percent();
   bool skip_cos_sim = shouldSkipCosineSimilarity();
 
+  Tensor &input = rc.getInput(0);
+  TensorDim input_dim = input.getDim();
+  size_t inputHeight = input_dim.height();
+
   for (int i = 0; i < 4; ++i) {
     /// warm layer multiple times
+    if (use_inc_forward) {
+      layer->incremental_forwarding(rc, 0, inputHeight,
+                                    !shouldForwardWithInferenceMode());
+    } else {
+      layer->forwarding(rc, !shouldForwardWithInferenceMode());
+    }
+  }
+
+  if (use_inc_forward) {
+    layer->incremental_forwarding(rc, 0, inputHeight,
+                                  !shouldForwardWithInferenceMode());
+  } else {
     layer->forwarding(rc, !shouldForwardWithInferenceMode());
   }
 
-  layer->forwarding(rc, !shouldForwardWithInferenceMode());
   if (!skip_calc_grad) {
     layer->calcGradient(rc);
   }
index aa30fd44da299b8ef376efc5c5fb96ed22fcc1ce..1d89e3a143168c1dc002c1df7038d08e7dafd5a4 100644 (file)
@@ -42,7 +42,8 @@ auto fc_basic_plain_nhwc = LayerGoldenTestParamType(
   nntrainer::createLayer<nntrainer::FullyConnectedLayerCl>, {"unit=5"},
   "3:10:1:1", "fc_plain.nnlayergolden",
   LayerGoldenTestParamOptions::SKIP_CALC_DERIV |
-    LayerGoldenTestParamOptions::SKIP_CALC_GRAD,
+    LayerGoldenTestParamOptions::SKIP_CALC_GRAD |
+    LayerGoldenTestParamOptions::USE_INC_FORWARD,
   "nhwc", "fp32", "fp32");
 
 auto fc_basic_single_batch_nhwc = LayerGoldenTestParamType(