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
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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 <sstream>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "ngraph/codegen/compiler.hpp"
#include "ngraph/codegen/execution_engine.hpp"
#include "ngraph/file_util.hpp"
#include "ngraph/log.hpp"
#include "ngraph/ops/concat.hpp"
#include "ngraph/runtime/backend.hpp"
#include "ngraph/runtime/call_frame.hpp"
#include "ngraph/runtime/cpu/cpu_call_frame.hpp"
#include "ngraph/runtime/manager.hpp"
#include "ngraph/serializer.hpp"
#include "ngraph/util.hpp"
#include "util/benchmark.hpp"
#include "util/random.hpp"
#include "util/test_tools.hpp"
using namespace std;
using namespace ngraph;
TEST(benchmark, mxnet_mnist_mlp_forward)
{
const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/mnist_mlp_forward.json");
run_benchmark(json_path, "CPU", 1000);
}
TEST(benchmark, gpu_mxnet_mnist_mlp_forward)
{
const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/mnist_mlp_forward.json");
run_benchmark(json_path, "GPU", 1000);
}
TEST(benchmark, mxnet_10_bucket_lstm)
{
const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/10_bucket_LSTM.json");
run_benchmark(json_path, "CPU", 10);
}
TEST(benchmark, mxnet_lstm_backward)
{
const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/LSTM_backward.json");
run_benchmark(json_path, "CPU", 10);
}
TEST(benchmark, mxnet_lstm_forward)
{
const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/LSTM_forward.json");
run_benchmark(json_path, "CPU", 10);
}
TEST(benchmark, mxnet_seq2seq_forward)
{
const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/Seq2Seq_forward.json");
run_benchmark(json_path, "CPU", 10);
}
TEST(benchmark, mxnet_seq2seq_backward)
{
const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/Seq2Seq_backward.json");
run_benchmark(json_path, "CPU", 10);
}
TEST(benchmark, mxnet_sockeye_seq2seq_forward)
{
const string json_path =
file_util::path_join(SERIALIZED_ZOO, "mxnet/Sockeye_Seq2Seq_forward.json");
run_benchmark(json_path, "CPU", 10);
}
TEST(benchmark, mxnet_sockeye_seq2seq_backward)
{
const string json_path =
file_util::path_join(SERIALIZED_ZOO, "mxnet/Sockeye_Seq2Seq_backward.json");
run_benchmark(json_path, "CPU", 10);
}
//
// Benchmarks a graph that concatenates six 32x1x200 arrays along the middle axis.
//
TEST(benchmark, concat_32x1x200_axis1_6)
{
const size_t n_arrays = 6;
Shape shape_of_each_array = Shape{32, 1, 200};
size_t concatenation_axis = 1;
Shape result_shape;
result_shape = shape_of_each_array;
result_shape[concatenation_axis] *= n_arrays;
size_t elements_per_array = 1;
for (size_t d : shape_of_each_array)
{
elements_per_array *= d;
}
vector<vector<float>> data_arrays(n_arrays);
for (size_t i = 0; i < n_arrays; i++)
{
data_arrays[i] = vector<float>(elements_per_array);
for (size_t j = 0; j < elements_per_array; j++)
{
data_arrays[i][j] = float(j + 1);
}
}
bool using_ref_kernels = (std::getenv("NGRAPH_CPU_USE_REF_KERNELS") != nullptr);
vector<std::string> backend_names{"INTERPRETER", "CPU"};
vector<int> n_runs{200, 200, using_ref_kernels ? 200 : 200000}; // one for each backend
vector<std::function<void()>> test_callbacks; // one for each backend
vector<std::shared_ptr<runtime::TensorView>> result_tvs; // one for each backend
for (std::string backend_name : backend_names)
{
vector<std::shared_ptr<op::Parameter>> params(n_arrays);
vector<std::shared_ptr<Node>> params_as_nodes(n_arrays);
for (size_t i = 0; i < n_arrays; i++)
{
auto param = make_shared<op::Parameter>(element::f32, shape_of_each_array);
params[i] = param;
params_as_nodes[i] = param;
}
auto concat = make_shared<op::Concat>(params_as_nodes, concatenation_axis);
auto f = make_shared<Function>(concat, params);
auto manager = runtime::Manager::get(backend_name);
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
vector<shared_ptr<runtime::TensorView>> input_vals;
for (size_t i = 0; i < n_arrays; i++)
{
auto tv = backend->make_primary_tensor_view(element::f32, shape_of_each_array);
copy_data(tv, data_arrays[i]);
input_vals.push_back(tv);
}
auto result_tv = backend->make_primary_tensor_view(element::f32, result_shape);
result_tvs.push_back(result_tv);
std::function<void()> cb = [input_vals, result_tv, cf]() {
cf->call(input_vals, {result_tv});
};
test_callbacks.push_back(cb);
}
for (size_t i = 0; i < backend_names.size(); i++)
{
std::cout << backend_names[i] << ": " << n_runs[i] << " tests in " << std::flush;
stopwatch sw;
std::function<void()> cb = test_callbacks[i];
sw.start();
for (int j = 0; j < n_runs[i]; j++)
{
cb();
}
sw.stop();
std::cout << sw.get_milliseconds() << "ms (" << (sw.get_microseconds() / n_runs[i])
<< " us/test)" << std::endl;
}
for (size_t i = 1; i < backend_names.size(); i++)
{
std::cout << "Verifying " << backend_names[i] << " result against " << backend_names[0]
<< "..." << std::flush;
if (read_vector<float>(result_tvs[i]) == read_vector<float>(result_tvs[0]))
{
std::cout << " OK" << std::endl;
}
else
{
std::cout << " FAILED" << std::endl;
ADD_FAILURE();
}
}
}