forked from google/lyra
-
Notifications
You must be signed in to change notification settings - Fork 2
/
conv1d_layer_wrapper_test.cc
319 lines (277 loc) · 12 KB
/
conv1d_layer_wrapper_test.cc
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
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2021 Google LLC
//
// 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 "conv1d_layer_wrapper.h"
#include <algorithm>
#include <vector>
// Placeholder for get runfiles header.
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "include/ghc/filesystem.hpp"
#include "layer_wrapper.h"
#include "layer_wrapper_test_common.h"
#include "sparse_matmul/sparse_matmul.h"
namespace chromemedia {
namespace codec {
namespace {
static constexpr char kConv1DLayerPrefix[] = "lyra_conv1d_";
template <typename ComputeType>
class Conv1DLayerWrapperTest : public ::testing::Test {
public:
Conv1DLayerWrapperTest()
: testdata_dir_(ghc::filesystem::current_path() / "testdata"),
conv1d_params_{.num_input_channels = kFeatureDepth,
.num_filters = kNumCondHidden,
.length = 1,
.kernel_size = kConv1DKernel,
.dilation = 1,
.stride = 1,
.relu = false,
.skip_connection = false,
.type = LayerType::kConv1D,
.num_threads = kNumThreads,
.per_column_barrier = false,
.from =
LayerParams::FromDisk{
.path = testdata_dir_.string(),
.zipped = true,
},
.prefix = kConv1DLayerPrefix},
spin_barrier_(kNumThreads) {}
protected:
using OutputType = typename LayerWrapperPeer<ComputeType>::OutputType;
using RhsType = typename LayerWrapperPeer<ComputeType>::RhsType;
const int kFeatureDepth = 3;
const int kConv1DKernel = 3;
const int kNumCondHidden = 8;
const int kNumThreads = 1;
const ghc::filesystem::path testdata_dir_;
const LayerParams conv1d_params_;
csrblocksparse::SpinBarrier spin_barrier_;
csrblocksparse::FatCacheAlignedVector<OutputType> output_buffer_;
};
using ComputeTypes = ::testing::Types<float, csrblocksparse::fixed16_type>;
TYPED_TEST_SUITE(Conv1DLayerWrapperTest, ComputeTypes);
TYPED_TEST(Conv1DLayerWrapperTest, CreateSucceeds) {
EXPECT_NE(LayerWrapperPeer<TypeParam>::Create(this->conv1d_params_), nullptr);
}
TYPED_TEST(Conv1DLayerWrapperTest, CreateWithBadParamsReturnNullptr) {
// Skip connections are not supported.
LayerParams skip_connection_params(this->conv1d_params_);
skip_connection_params.skip_connection = true;
EXPECT_EQ(LayerWrapperPeer<TypeParam>::Create(skip_connection_params),
nullptr);
// |dilation| != 1 is not supported.
LayerParams bad_dilation_params(this->conv1d_params_);
bad_dilation_params.dilation = 2;
EXPECT_EQ(LayerWrapperPeer<TypeParam>::Create(bad_dilation_params), nullptr);
}
TYPED_TEST(Conv1DLayerWrapperTest, ResetShiftsInputBufferUp) {
auto params = this->conv1d_params_;
// Set up the input buffer expectations.
std::vector<std::vector<float>> expected_input_buffer(3);
// For |kernel_size| = 3 and |num_input_channels| = 3, the input buffer has
// 9 elements total.
// For |stride| = 1, each input loading pushes in
// |stride * num_input_channels| = 3 numbers, so after 3 loadings the input
// buffer should look like this:
// | 0 |
// | 0 |
// | 0 |
// | 1 |
// | 1 |
// | 1 |
// | 2 |
// | 2 |
// | 2 |
// I.e. input 0, input 1, input 2 are all in the buffer.
expected_input_buffer[0] = {0, 0, 0, 1, 1, 1, 2, 2, 2};
// For |stride| = 2, each loading pushes in 6 numbers, and the input buffer
// should look like this in the end:
// | 1 |
// | 1 |
// | 1 |
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// I.e. input 0 has been pushed out, so have the first 3 elements of input 1.
expected_input_buffer[1] = {1, 1, 1, 2, 2, 2, 2, 2, 2};
// For |stride| = 3, each loading pushes in 9 numbers, and the input buffer
// should look like this in the end:
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// | 2 |
// I.e. input 0 and input 1 have all been pushed out, leaving only input 2.
expected_input_buffer[2] = {2, 2, 2, 2, 2, 2, 2, 2, 2};
using RhsType = typename LayerWrapperPeer<TypeParam>::RhsType;
for (int stride = 1; stride <= 3; ++stride) {
params.stride = stride;
auto layer = LayerWrapperPeer<TypeParam>::Create(params);
// Load input 3 times (each time loading |stride * num_input_channels|
// elements) and calling Reset() in between.
auto input_view = layer->InputViewToUpdate();
const int input_view_size = input_view.rows() * input_view.cols();
std::fill_n(input_view.data(), input_view_size, static_cast<RhsType>(0.0f));
layer->Reset(0, &this->spin_barrier_);
std::fill_n(input_view.data(), input_view_size, static_cast<RhsType>(1.0f));
layer->Reset(0, &this->spin_barrier_);
std::fill_n(input_view.data(), input_view_size, static_cast<RhsType>(2.0f));
// Verify the input buffer is as expected.
EXPECT_THAT(std::vector<float>(layer->input_buffer().data(),
layer->input_buffer().data() + 9),
testing::ElementsAreArray(expected_input_buffer[stride - 1]));
}
}
TYPED_TEST(Conv1DLayerWrapperTest, LayerLoadSucceeds) {
const LayerParams params = this->conv1d_params_;
auto layer = LayerWrapperPeer<TypeParam>::Create(params);
EXPECT_GE(layer->bytes(), 0);
// Verify that the weight matrix's shape is
// [|num_filters|, |kernel_size| * |num_input_channels|].
EXPECT_EQ(layer->rows(), params.num_filters);
EXPECT_EQ(layer->cols(), params.kernel_size * params.num_input_channels);
// Verify that the input buffer is of shape
// [|kernel_size| * |num_input_channels|, |dilation|]
auto input_buffer = layer->input_buffer();
EXPECT_EQ(input_buffer.rows(),
params.kernel_size * params.num_input_channels);
EXPECT_EQ(input_buffer.cols(), params.dilation);
}
TYPED_TEST(Conv1DLayerWrapperTest, LayerLoadDynamicDimensionsSucceeds) {
LayerParams dynamic_params = this->conv1d_params_;
// Setting to zeros means to dynamically decide the dimensions.
dynamic_params.num_input_channels = 0;
dynamic_params.num_filters = 0;
auto layer = LayerWrapperPeer<TypeParam>::Create(dynamic_params);
EXPECT_GE(layer->bytes(), 0);
// Verify that the weight matrix's shape is
// [|num_filters|, |kernel_size| * |num_input_channels|] of the original
// non-zero params.
const auto params = this->conv1d_params_;
EXPECT_EQ(layer->rows(), params.num_filters);
EXPECT_EQ(layer->cols(), params.kernel_size * params.num_input_channels);
// Verify that the input buffer is of shape
// [|kernel_size| * |num_input_channels|, |dilation|] of the original
// non-zero params.
auto input_buffer = layer->input_buffer();
EXPECT_EQ(input_buffer.rows(),
params.kernel_size * params.num_input_channels);
EXPECT_EQ(input_buffer.cols(), params.dilation);
}
TYPED_TEST(Conv1DLayerWrapperTest, LayerCreateWithConstantSucceeds) {
auto params = this->conv1d_params_;
params.from = LayerParams::FromConstant{
.value = 0.5f,
.sparsity = -1.0f,
};
auto layer = LayerWrapperPeer<TypeParam>::Create(params);
EXPECT_NE(layer, nullptr);
EXPECT_GE(layer->bytes(), 0);
// Verify that the weight matrix's shape is
// [|num_filters|, |kernel_size| * |num_input_channels|].
EXPECT_EQ(layer->rows(), params.num_filters);
EXPECT_EQ(layer->cols(), params.kernel_size * params.num_input_channels);
}
TYPED_TEST(Conv1DLayerWrapperTest, LayerRuns) {
const LayerParams params = this->conv1d_params_;
auto layer = LayerWrapperPeer<TypeParam>::Create(params);
// Only a part of the input buffer corresponding to time t is updated each
// time, i.e. the bottom |num_input_channels| rows out of the total
// |kernel_size| * |num_input_channels| rows of the current column.
auto output_view = PrepareInputOutput(
/*expected_input_rows=*/params.num_input_channels,
/*expected_input_cols=*/1,
/*expected_output_rows=*/params.num_filters,
/*expected_output_cols=*/1, 1.0f, layer->InputViewToUpdate(),
&this->output_buffer_);
// Check that Run() writes some non-zero results to the output buffer.
layer->Run(0, &this->spin_barrier_, output_view);
EXPECT_THAT(std::vector<float>(
this->output_buffer_.data(),
this->output_buffer_.data() + this->output_buffer_.size()),
testing::Contains(testing::Ne(0.0f)));
}
TYPED_TEST(Conv1DLayerWrapperTest, MultipleThreadsYieldSameResults) {
auto params = this->conv1d_params_;
VerifyMultipleThreadsYeldSameResults<LayerWrapperPeer<TypeParam>>(
/*iterations=*/8, /*threads_to_test=*/{1, 2, 4}, params,
/*expected_input_rows=*/params.num_input_channels,
/*expected_input_cols=*/1,
/*expected_output_rows=*/params.num_filters,
/*expected_output_cols=*/1);
// Turn on per-column barrier and the result should still be the same.
params.per_column_barrier = true;
VerifyMultipleThreadsYeldSameResults<LayerWrapperPeer<TypeParam>>(
/*iterations=*/8, /*threads_to_test=*/{1, 2, 4}, params,
/*expected_input_rows=*/params.num_input_channels,
/*expected_input_cols=*/1,
/*expected_output_rows=*/params.num_filters,
/*expected_output_cols=*/1);
}
TYPED_TEST(Conv1DLayerWrapperTest, NumericalResults) {
const LayerParams params{.num_input_channels = 3,
.num_filters = 2,
.length = 1,
.kernel_size = 2,
.dilation = 1,
.stride = 2,
.relu = false,
.skip_connection = false,
.type = LayerType::kConv1D,
.num_threads = 1,
.per_column_barrier = false,
.from =
LayerParams::FromDisk{
.path = this->testdata_dir_.string(),
.zipped = true,
},
.prefix = "test_conv1d_"};
auto layer = LayerWrapperPeer<TypeParam>::Create(params);
using RhsType = typename LayerWrapperPeer<TypeParam>::RhsType;
const std::vector<std::vector<RhsType>> inputs = {
{RhsType(1.0f), RhsType(2.0f), RhsType(3.0f), RhsType(4.0f),
RhsType(5.0f), RhsType(6.0f)},
{RhsType(7.0f), RhsType(8.0f), RhsType(9.0f), RhsType(10.0f),
RhsType(11.0f), RhsType(12.0f)}};
using OutputType = typename LayerWrapperPeer<TypeParam>::OutputType;
const std::vector<std::vector<OutputType>> expected_outputs = {
{OutputType(21.0f), OutputType(42.0f)},
{OutputType(57.0f), OutputType(114.0f)},
};
csrblocksparse::FatCacheAlignedVector<OutputType> output_buffer(2, 1);
for (int i = 0; i < 2; ++i) {
std::copy(inputs[i].begin(), inputs[i].end(),
layer->InputViewToUpdate().data());
layer->Run(0, &this->spin_barrier_,
csrblocksparse::MutableVectorView<OutputType>(&output_buffer));
// Convert to float because fixed points do not have comparison operators.
const std::vector<float> actual_output_float(
output_buffer.data(), output_buffer.data() + output_buffer.size());
const std::vector<float> expected_output_float(expected_outputs[i].begin(),
expected_outputs[i].end());
EXPECT_THAT(actual_output_float,
testing::Pointwise(testing::FloatEq(), expected_output_float));
}
}
} // namespace
} // namespace codec
} // namespace chromemedia