[Test] Add recurrent model test
authorJihoon Lee <jhoon.it.lee@samsung.com>
Tue, 5 Oct 2021 11:12:45 +0000 (20:12 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 7 Oct 2021 10:20:47 +0000 (19:20 +0900)
This patch contains intial test for the recurrent model.

In this patch, there are three fc layers sharing the same weights

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

Signed-off-by: Jihoon Lee <jhoon.it.lee@samsung.com>
test/unittest/meson.build
test/unittest/models/meson.build [new file with mode: 0644]
test/unittest/models/unittest_models_recurrent.cpp [new file with mode: 0644]

index 66bcc2b..8364a76 100644 (file)
@@ -61,3 +61,4 @@ subdir('memory')
 subdir('compiler')
 subdir('layers')
 subdir('datasets')
+subdir('models')
diff --git a/test/unittest/models/meson.build b/test/unittest/models/meson.build
new file mode 100644 (file)
index 0000000..22cc67a
--- /dev/null
@@ -0,0 +1,23 @@
+test_name = 'unittest_models'
+
+test_target = []
+
+models_targets = [
+  'unittest_models_recurrent.cpp',
+]
+
+test_target += models_targets
+exe = executable(
+  test_name,
+  test_target,
+  dependencies: [
+    nntrainer_test_main_deps,
+    nntrainer_capi_dep
+  ],
+  install: get_option('enable-test'),
+  install_dir: application_install_dir
+)
+
+test(test_name, exe,
+  args: '--gtest_output=xml:@0@/@1@.xml'.format(meson.build_root(), test_name)
+)
diff --git a/test/unittest/models/unittest_models_recurrent.cpp b/test/unittest/models/unittest_models_recurrent.cpp
new file mode 100644 (file)
index 0000000..e455b2c
--- /dev/null
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file unittest_models_recurrent.cpp
+ * @date 05 Oct 2021
+ * @brief unittest models for recurrent ones
+ * @see        https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+
+#include <gtest/gtest.h>
+
+#include <memory>
+
+#include <databuffer.h>
+#include <dataset.h>
+#include <ini_wrapper.h>
+#include <neuralnet.h>
+#include <nntrainer_test_util.h>
+
+using namespace nntrainer;
+
+static nntrainer::IniSection nn_base("model", "type = NeuralNetwork");
+static std::string fc_base = "type = Fully_connected";
+static nntrainer::IniSection sgd_base("optimizer", "Type = sgd");
+static nntrainer::IniSection constant_loss("loss",
+                                           "type = constant_derivative");
+
+int getSample(float **outVec, float **outLabel, bool *last, void *user_data) {
+  **outVec = 1;
+  **outLabel = 1;
+  *last = true;
+  return 0;
+};
+
+TEST(FcOnly, fcHandUnrolled) {
+  ScopedIni fc_only_hand_unrolled(
+    "fc_only_hand_unrolled", {nn_base, sgd_base,
+                              IniSection("fc_1") + fc_base +
+                                "unit=1 | weight_initializer=ones | "
+                                "bias_initializer=ones | input_shape=1:1:1",
+                              IniSection("fc_2") + fc_base +
+                                "unit=1 | weight_initializer=ones | "
+                                "bias_initializer=ones | shared_from = fc_1",
+                              IniSection("fc_3") + fc_base +
+                                "unit=1 | weight_initializer=ones | "
+                                "bias_initializer=ones | shared_from = fc_1",
+                              constant_loss});
+
+  NeuralNetwork nn;
+  nn.load(fc_only_hand_unrolled.getIniName(),
+          ml::train::ModelFormat::MODEL_FORMAT_INI);
+
+  EXPECT_EQ(nn.compile(), 0);
+  EXPECT_EQ(nn.initialize(), 0);
+
+  auto db = ml::train::createDataset(ml::train::DatasetType::GENERATOR,
+                                     getSample, nullptr);
+
+  nn.setDataset(DatasetModeType::MODE_TRAIN, std::move(db));
+  EXPECT_EQ(nn.train(), 0);
+}