[layer] Add MoL layer unittest
[platform/core/ml/nntrainer.git] / test / unittest / models / unittest_models.cpp
1 // SPDX-License-Identifier: Apache-2.0
2 /**
3  * Copyright (C) 2021 Parichay Kapoor <pk.kapoor@samsung.com>
4  *
5  * @file unittest_models_v2.cpp
6  * @date 25 Nov 2021
7  * @brief unittest models for v2 version
8  * @see https://github.com/nnstreamer/nntrainer
9  * @author Parichay Kapoor <pk.kapoor@samsung.com>
10  * @bug No known bugs except for NYI items
11  */
12
13 #include <gtest/gtest.h>
14
15 #include <memory>
16
17 #include <ini_wrapper.h>
18 #include <neuralnet.h>
19 #include <nntrainer_test_util.h>
20
21 #include <models_golden_test.h>
22
23 using namespace nntrainer;
24
25 static inline constexpr const int NOT_USED_ = 1;
26
27 static IniSection nn_base("model", "type = NeuralNetwork");
28 static std::string fc_base = "type = Fully_connected";
29 static std::string red_mean_base = "type = reduce_mean";
30 static IniSection sgd_base("optimizer", "Type = sgd");
31 static IniSection constant_loss("loss", "type = constant_derivative");
32
33 IniWrapper reduce_mean_last("reduce_mean_last",
34                             {
35                               nn_base + "batch_size=3",
36                               sgd_base + "learning_rate=0.1",
37                               IniSection("fc_1") + fc_base +
38                                 "unit=7 | input_shape=1:1:2",
39                               IniSection("red_mean") + red_mean_base + "axis=3",
40                               constant_loss,
41                             });
42
43 static std::unique_ptr<NeuralNetwork> makeMolAttention() {
44   std::unique_ptr<NeuralNetwork> nn(new NeuralNetwork());
45   nn->setProperty({"batch_size=3"});
46
47   auto outer_graph = makeGraph({
48     {"input", {"name=in3", "input_shape=1:1:5"}},
49     {"input", {"name=in2", "input_shape=1:4:6"}},
50     {"input", {"name=in1", "input_shape=1:1:6"}},
51     {"mol_attention",
52      {"name=mol", "input_layers=in1,in2,in3", "unit=8", "mol_k=5"}},
53     {"constant_derivative", {"name=loss", "input_layers=mol"}},
54   });
55
56   for (auto &node : outer_graph) {
57     nn->addLayer(node);
58   }
59
60   nn->setOptimizer(ml::train::createOptimizer("sgd", {"learning_rate = 0.1"}));
61   return nn;
62 }
63
64 INSTANTIATE_TEST_CASE_P(
65   model, nntrainerModelTest,
66   ::testing::ValuesIn({
67     mkModelIniTc(reduce_mean_last, DIM_UNUSED, NOT_USED_,
68                  ModelTestOption::COMPARE_V2),
69     mkModelTc_V2(makeMolAttention, "mol_attention",
70                  ModelTestOption::COMPARE_V2),
71   }),
72   [](const testing::TestParamInfo<nntrainerModelTest::ParamType> &info) {
73     return std::get<1>(info.param);
74   });