summaryrefslogtreecommitdiff
path: root/runtime/onert/test/core/exec/ExecInstance.cc
blob: 7242486a029930c4e54d2f99621f231a20769107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include <thread>

#include "ir/Graph.h"
#include "compiler/Compiler.h"
#include "exec/Execution.h"
#include "ir/operation/Add.h"

namespace
{

using namespace onert::ir;

class CompiledMockUpModel
{
public:
  CompiledMockUpModel()
  {
    // Model: two elementwise add operation
    // model input: lhs, rhs1
    // model output: second add result (result2)
    // constant: rhs2
    // result1 <= (lhs + rhs)
    // result2 <= (result1 + rhs2)
    // lhs, rhs1, rh2, result1, result2 shape: {1, 2, 2, 1}
    // activation: none (constant)
    graph = std::make_shared<Graph>();
    // 1st add operands (result1 <= lhs + rhs1)
    Shape shape{1, 2, 2, 1};
    TypeInfo type{DataType::FLOAT32};
    static float rhs2_data[4] = {3, 1, -1, 5};
    auto operand_lhs = graph->addOperand(shape, type);
    auto operand_rhs1 = graph->addOperand(shape, type);
    auto operand_result1 = graph->addOperand(shape, type);
    auto operand_rhs2 = graph->addOperand(shape, type);
    auto operand_result2 = graph->addOperand(shape, type);
    graph->operands()
        .at(operand_rhs2)
        .data(std::make_unique<CachedData>(reinterpret_cast<const uint8_t *>(&rhs2_data), 16));
    // 2nd add operations (result2 <= result1 + rhs2)
    operation::Add::Param param1;
    param1.activation = Activation::NONE;
    auto input_set1 = OperandIndexSequence{operand_lhs, operand_rhs1};
    auto output_set1 = OperandIndexSequence{operand_result1};
    graph->addOperation(std::make_unique<operation::Add>(input_set1, output_set1, param1));
    operation::Add::Param param2;
    param2.activation = Activation::NONE;
    auto input_set2 = OperandIndexSequence{operand_result1, operand_rhs2};
    auto output_set2 = OperandIndexSequence{operand_result2};
    graph->addOperation(std::make_unique<operation::Add>(input_set2, output_set2, param2));
    // Identify model inputs and outputs
    graph->addInput(operand_lhs);
    graph->addInput(operand_rhs1);
    graph->addOutput(operand_result2);
    graph->finishBuilding();

    // Compile
    auto compiler = new onert::compiler::Compiler{graph};
    compiler->compile();
    compiler->release(executor);
    delete compiler;
  }

public:
  std::shared_ptr<Graph> graph;
  std::shared_ptr<onert::exec::IExecutor> executor;
};

TEST(ExecInstance, simple)
{
  auto mockup = CompiledMockUpModel();
  auto graph = mockup.graph;
  auto executor = mockup.executor;

  auto input1 = IOIndex{0};
  auto input2 = IOIndex{1};
  auto output = IOIndex{0};

  const float input1_buffer[4] = {1, 0, -1, -2};
  const float input2_buffer[4] = {1, -3, 2, -4};
  float output_buffer[4] = {};
  const float output_expected[4] = {5, -2, 0, -1};

  auto execution = new onert::exec::Execution(executor);

  execution->setInput(input1, reinterpret_cast<const void *>(input1_buffer), 16);
  execution->setInput(input2, reinterpret_cast<const void *>(input2_buffer), 16);
  execution->setOutput(output, reinterpret_cast<void *>(output_buffer), 16);
  execution->execute();

  for (auto i = 0; i < 4; i++)
  {
    EXPECT_EQ(output_buffer[i], output_expected[i]);
  }

  delete execution;
}

TEST(ExecInstance, twoCompile)
{
  auto mockup = CompiledMockUpModel();
  auto graph = mockup.graph;
  auto executor1 = mockup.executor;
  auto execution1 = new onert::exec::Execution(executor1);

  auto input1 = IOIndex{0};
  auto input2 = IOIndex{1};
  auto output = IOIndex{0};

  const float exe1_input1_buffer[4] = {1, 0, -1, -2};
  const float exe1_input2_buffer[4] = {1, -3, 2, -4};
  float exe1_output_buffer[4] = {};
  const float exe1_output_expected[4] = {5, -2, 0, -1};

  execution1->setInput(input1, reinterpret_cast<const void *>(exe1_input1_buffer), 16);
  execution1->setInput(input2, reinterpret_cast<const void *>(exe1_input2_buffer), 16);
  execution1->setOutput(output, reinterpret_cast<void *>(exe1_output_buffer), 16);

  // Make new executor: compile again
  auto compiler = new onert::compiler::Compiler{graph};
  compiler->compile();
  std::shared_ptr<onert::exec::IExecutor> executor2;
  compiler->release(executor2);
  auto execution2 = new onert::exec::Execution(executor2);

  const float exe2_input1_buffer[4] = {2, 1, -2, 0};
  const float exe2_input2_buffer[4] = {-3, 3, 1, 2};
  float exe2_output_buffer[4] = {};
  const float exe2_output_expected[4] = {2, 5, -2, 7};

  execution2->setInput(input1, reinterpret_cast<const void *>(exe2_input1_buffer), 16);
  execution2->setInput(input2, reinterpret_cast<const void *>(exe2_input2_buffer), 16);
  execution2->setOutput(output, reinterpret_cast<void *>(exe2_output_buffer), 16);

  execution1->execute();
  execution2->execute();

  for (auto i = 0; i < 4; i++)
  {
    EXPECT_EQ(exe1_output_buffer[i], exe1_output_expected[i]);
    EXPECT_EQ(exe2_output_buffer[i], exe2_output_expected[i]);
  }

  delete compiler;
  delete execution1;
  delete execution2;
}

// Support two initialized execution instance then ordered execution
TEST(ExecInstance, twoExecution)
{
  auto mockup = CompiledMockUpModel();
  auto executor = mockup.executor;
  auto input1 = IOIndex{0};
  auto input2 = IOIndex{1};
  auto output1 = IOIndex{0};

  const float exe1_input1_buffer[4] = {1, 0, -1, -2};
  const float exe1_input2_buffer[4] = {1, -3, 2, -4};
  float exe1_output_buffer[4] = {};
  const float exe1_output_expected[4] = {5, -2, 0, -1};
  const float exe2_output_expected[4] = {2, 5, -2, 7};

  auto execution1 = new onert::exec::Execution(executor);
  execution1->setInput(input1, reinterpret_cast<const void *>(exe1_input1_buffer), 16);
  execution1->setInput(input2, reinterpret_cast<const void *>(exe1_input2_buffer), 16);
  execution1->setOutput(output1, reinterpret_cast<void *>(exe1_output_buffer), 16);

  const float exe2_input1_buffer[4] = {2, 1, -2, 0};
  const float exe2_input2_buffer[4] = {-3, 3, 1, 2};
  float exe2_output_buffer[4] = {};

  // Make new execution
  auto execution2 = new onert::exec::Execution(executor);
  execution2->setInput(input1, reinterpret_cast<const void *>(exe2_input1_buffer), 16);
  execution2->setInput(input2, reinterpret_cast<const void *>(exe2_input2_buffer), 16);
  execution2->setOutput(output1, reinterpret_cast<void *>(exe2_output_buffer), 16);

  execution1->execute();
  execution2->execute();

  for (auto i = 0; i < 4; i++)
  {
    EXPECT_EQ(exe1_output_buffer[i], exe1_output_expected[i]);
    EXPECT_EQ(exe2_output_buffer[i], exe2_output_expected[i]);
  }

  delete execution1;
  delete execution2;
}

class Inference
{
public:
  Inference(const float (&input1)[4], const float (&input2)[4], float (&output)[4],
            std::shared_ptr<onert::exec::IExecutor> &executor)
      : _input1{input1}, _input2{input2}, _output{output}, _executor{executor}
  {
    // DO NOTHING
  }

  void inference(void)
  {
    auto input1 = IOIndex{0};
    auto input2 = IOIndex{1};
    auto output1 = IOIndex{0};

    auto execution = new onert::exec::Execution(_executor);
    execution->setInput(input1, reinterpret_cast<const void *>(_input1), 16);
    execution->setInput(input2, reinterpret_cast<const void *>(_input2), 16);
    execution->setOutput(output1, reinterpret_cast<void *>(_output), 16);

    execution->execute();

    delete execution;
  }

private:
  const float (&_input1)[4];
  const float (&_input2)[4];
  float (&_output)[4];
  std::shared_ptr<onert::exec::IExecutor> &_executor;
};

// Support multi-thread execution
TEST(ExecInstance, twoThreads)
{
  auto mockup = CompiledMockUpModel();
  auto executor = mockup.executor;

  const float exe1_input1_buffer[4] = {1, 0, -1, -2};
  const float exe1_input2_buffer[4] = {1, -3, 2, -4};
  float exe1_output_buffer[4] = {};
  const float exe1_output_expected[4] = {5, -2, 0, -1};

  Inference execution1{exe1_input1_buffer, exe1_input2_buffer, exe1_output_buffer, executor};

  const float exe2_input1_buffer[4] = {2, 1, -2, 0};
  const float exe2_input2_buffer[4] = {-3, 3, 1, 2};
  float exe2_output_buffer[4] = {};
  const float exe2_output_expected[4] = {2, 5, -2, 7};

  Inference execution2{exe2_input1_buffer, exe2_input2_buffer, exe2_output_buffer, executor};

  std::thread t1{&Inference::inference, &execution1};
  std::thread t2{&Inference::inference, &execution2};

  t1.join();
  t2.join();

  for (auto i = 0; i < 4; i++)
  {
    EXPECT_EQ(exe1_output_buffer[i], exe1_output_expected[i]);
    EXPECT_EQ(exe2_output_buffer[i], exe2_output_expected[i]);
  }
}

// Support asynchronous execution
TEST(ExecInstance, async)
{
  auto mockup = CompiledMockUpModel();
  auto graph = mockup.graph;
  auto executor = mockup.executor;

  auto input1 = IOIndex{0};
  auto input2 = IOIndex{1};
  auto output = IOIndex{0};

  const float input1_buffer[4] = {1, 0, -1, -2};
  const float input2_buffer[4] = {1, -3, 2, -4};
  float output_buffer[4] = {};
  const float output_expected[4] = {5, -2, 0, -1};

  auto execution = new onert::exec::Execution(executor);

  execution->setInput(input1, reinterpret_cast<const void *>(input1_buffer), 16);
  execution->setInput(input2, reinterpret_cast<const void *>(input2_buffer), 16);
  execution->setOutput(output, reinterpret_cast<void *>(output_buffer), 16);
  execution->startExecute();
  execution->waitFinish();

  for (auto i = 0; i < 4; i++)
  {
    EXPECT_EQ(output_buffer[i], output_expected[i]);
  }

  delete execution;
}

} // namespace