Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / luci / pass / src / QuantizeWeightsPass.test.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "luci/Pass/QuantizeWeightsPass.h"
18 #include <luci/IR/CircleNodes.h>
19
20 #include <gtest/gtest.h>
21
22 namespace
23 {
24 struct QuantizeWeightsPassTest : public ::testing::Test
25 {
26   /**
27    *  nconv graph
28    *
29    *        [CircleInput]
30    *              |
31    *              |
32    *        [CircleConv2D]
33    *              |
34    *              |
35    *        [CircleOutput]
36    */
37   void MakeGraph()
38   {
39     const int N = 1;
40     const int H = 4;
41     const int W = 4;
42     const int C = 3; // IC = OC
43
44     // graph input and output
45     auto graph_input = _g.inputs()->create();
46     auto graph_output = _g.outputs()->create();
47
48     // CircleInput
49     auto input = _g.nodes()->create<luci::CircleInput>();
50     input->index(graph_input->index());
51     input->shape({N, H, W, C});
52     input->dtype(loco::DataType::FLOAT32);
53     input->name("input");
54
55     // CircleConv2D
56     auto conv = _g.nodes()->create<luci::CircleConv2D>();
57     conv->input(input);
58     auto bias = _g.nodes()->create<luci::CircleConst>();
59     bias->dtype(loco::DataType::FLOAT32);
60     bias->shape({C});
61     bias->name("conv_bias");
62     conv->bias(bias);
63     auto weight = _g.nodes()->create<luci::CircleConst>();
64     weight->dtype(loco::DataType::FLOAT32);
65     weight->shape({C, H, W, C});
66     weight->size<loco::DataType::FLOAT32>(C * H * W * C);
67     conv->filter(weight);
68     conv->padding(luci::Padding::SAME);
69     conv->fusedActivationFunction(luci::FusedActFunc::NONE);
70     conv->dtype(loco::DataType::FLOAT32);
71     conv->name("nconv");
72
73     // CircleOutput
74     auto output = _g.nodes()->create<luci::CircleOutput>();
75     output->index(graph_output->index());
76     output->from(conv);
77     output->shape({N, H, W, C});
78     output->dtype(loco::DataType::FLOAT32);
79     output->name("output");
80   }
81   virtual void SetUp() { MakeGraph(); }
82   loco::Graph _g;
83 };
84
85 } // namespace
86
87 TEST_F(QuantizeWeightsPassTest, name)
88 {
89   luci::QuantizeWeightsPass pass(loco::DataType::FLOAT32, loco::DataType::S8,
90                                  luci::QuantizationGranularity::ChannelWise);
91   auto const name = pass.name();
92   ASSERT_NE(nullptr, name);
93 }
94
95 TEST_F(QuantizeWeightsPassTest, name_ctx)
96 {
97   auto ctx = std::make_unique<luci::QuantizeWeightsPass::Context>();
98   {
99     ctx->input_model_dtype = loco::DataType::FLOAT32;
100     ctx->output_model_dtype = loco::DataType::S8;
101     ctx->granularity = luci::QuantizationGranularity::ChannelWise;
102   }
103
104   luci::QuantizeWeightsPass pass(std::move(ctx));
105   auto const name = pass.name();
106   ASSERT_NE(nullptr, name);
107 }
108
109 TEST_F(QuantizeWeightsPassTest, run_input_U8_NEG)
110 {
111   loco::Graph g;
112   luci::QuantizeWeightsPass pass(loco::DataType::U8, loco::DataType::S8,
113                                  luci::QuantizationGranularity::ChannelWise);
114   EXPECT_THROW(pass.run(&_g), std::runtime_error);
115 }
116
117 TEST_F(QuantizeWeightsPassTest, run_output_f32_NEG)
118 {
119   loco::Graph g;
120   luci::QuantizeWeightsPass pass(loco::DataType::FLOAT32, loco::DataType::FLOAT32,
121                                  luci::QuantizationGranularity::ChannelWise);
122   EXPECT_THROW(pass.run(&_g), std::runtime_error);
123 }