Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / test / core / exec / ExecTime.test.cc
1 /*
2  * Copyright (c) 2019 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 "exec/ExecTime.h"
18 #include "backend/IConfig.h"
19 #include "backend/Backend.h"
20 #include <gtest/gtest.h>
21 #include <string>
22
23 namespace
24 {
25 using namespace onert;
26 using namespace exec;
27 using namespace backend;
28
29 struct MockConfig : public IConfig
30 {
31   std::string id() override { return "b1"; }
32   bool initialize() override { return true; };
33   bool supportPermutation() override { return false; }
34   ir::Layout supportLayout(const ir::Operation &, ir::Layout) override
35   {
36     return ir::Layout::UNKNOWN;
37   }
38   bool supportDynamicTensor() override { return false; }
39   bool supportFP16() override { return false; }
40 };
41
42 struct MockBackend : public ::onert::backend::Backend
43 {
44   std::shared_ptr<onert::backend::IConfig> config() const override
45   {
46     return std::make_shared<MockConfig>();
47   }
48   std::unique_ptr<BackendContext> newContext(const ir::Graph &,
49                                              const std::shared_ptr<custom::IKernelBuilder> &kb,
50                                              bool) const override
51   {
52     return nullptr;
53   }
54 };
55
56 TEST(ExecTime, roundtrip_ok)
57 {
58   const auto *b = new MockBackend();
59   std::vector<const Backend *> bs = {b};
60   {
61     ExecTime et(bs);
62     et.updateOperationExecTime(b, "op1", true, 100, 100);
63     et.updateOperationExecTime(b, "op1", true, 200, 200);
64     et.updateOperationExecTime(b, "op1", false, 100, 888);
65     et.storeOperationsExecTime();
66   }
67   {
68     ExecTime et(bs);
69     auto time = et.getOperationExecTime(b, "op1", true, 100);
70     ASSERT_EQ(time, 100);
71     // Check interpolation
72     time = et.getOperationExecTime(b, "op1", true, 150);
73     ASSERT_EQ(time, 150);
74     time = et.getOperationExecTime(b, "op1", false, 100);
75     ASSERT_EQ(time, 888);
76     et.storeOperationsExecTime();
77   }
78   // clean up
79   EXPECT_EQ(remove("exec_time.json"), 0);
80 }
81
82 TEST(ExecTime, structure)
83 {
84
85   const auto *b = new MockBackend();
86   std::vector<const Backend *> bs = {b};
87   {
88     ExecTime et(bs);
89     et.updateOperationExecTime(b, "op1", true, 100, 100);
90     et.updateOperationExecTime(b, "op1", true, 200, 200);
91     et.storeOperationsExecTime();
92   }
93   {
94     ExecTime et(bs);
95     auto time = et.getOperationExecTime(b, "op1", true, 100);
96     ASSERT_EQ(time, 100);
97     // Check interpolation
98     time = et.getOperationExecTime(b, "op1", true, 200);
99     ASSERT_EQ(time, 200);
100     et.storeOperationsExecTime();
101   }
102   // clean up
103   EXPECT_EQ(remove("exec_time.json"), 0);
104 }
105 } // unnamed namespace