nnet-compile-utils-test.cc
14.4 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// nnet3/nnet-compile-utils-test.cc
// Copyright 2015 Johns Hopkins University (author: Vijayaditya Peddinti)
// See ../../COPYING for clarification regarding multiple authors
//
// 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
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "util/common-utils.h"
#include "nnet3/nnet-compile-utils.h"
namespace kaldi {
namespace nnet3 {
struct ComparePair : public std::unary_function<std::pair<int32, int32>, bool>
{
explicit ComparePair(const std::pair<int32, int32> &correct_pair):
correct_pair_(correct_pair) {}
bool operator() (std::pair<int32, int32> const &arg)
{ return (arg.first == correct_pair_.first) &&
(arg.second == correct_pair_.second); }
std::pair<int32, int32> correct_pair_;
};
struct PairIsEqualComparator :
public std::unary_function<std::pair<int32, int32>, bool>
{
explicit PairIsEqualComparator(const std::pair<int32, int32> pair):
pair_(pair) {}
bool operator() (std::pair<int32, int32> const &arg)
{
if (pair_.first == arg.first)
return pair_.second == arg.second;
return false;
}
std::pair<int32, int32> pair_;
};
void PrintVectorVectorPair(
std::vector<std::vector<std::pair<int32, int32> > > vec_vec_pair) {
std::ostringstream ostream;
for (int32 i = 0; i < vec_vec_pair.size(); i++) {
for (int32 j = 0; j < vec_vec_pair[i].size(); j++) {
ostream << "(" << vec_vec_pair[i][j].first << ","
<< vec_vec_pair[i][j].second << ") ";
}
ostream << std::endl;
}
KALDI_LOG << ostream.str();
}
// Function to check SplitLocationsBackward() method
// checks if the submat_lists and split_lists have the same non-dummy elements
// checks if the submat_lists are split into same first_element lists wherever
// possible
// checks if the split_lists satisfy either "unique contiguous segments"
// property or unique pairs property (see SplitLocationsBackward in
// nnet-compile-utils.h for more details)
void UnitTestSplitLocationsBackward(bool verbose) {
int32 minibatch_size = Rand() % 1024 + 100;
int32 num_submat_indexes = Rand() % 10 + 1;
int32 max_submat_list_size = Rand() % 10 + 1;
int32 min_num_kaddrows = Rand() % 2; // minimum number of kAddRows compatible
// lists expected in the final split lists. This value will be used to
// create input submat_lists so that this is guaranteed
max_submat_list_size = min_num_kaddrows + max_submat_list_size;
std::vector<std::pair<int32, int32> > all_pairs;
all_pairs.reserve(minibatch_size * max_submat_list_size);
std::vector<std::vector<std::pair<int32, int32> > >
submat_lists(minibatch_size),
split_lists;
std::vector<int32> submat_indexes(num_submat_indexes);
for (int32 i = 0; i < num_submat_indexes; i++) {
submat_indexes[i] = Rand();
}
// generating submat_lists
int32 max_generated_submat_list_size = 0;
for (int32 i = 0; i < minibatch_size; i++) {
int32 num_locations = Rand() % max_submat_list_size + 1;
max_generated_submat_list_size =
max_generated_submat_list_size < num_locations ?
num_locations : max_generated_submat_list_size;
submat_lists[i].reserve(num_locations);
for (int32 j = 0; j < num_locations; j++) {
if (j <= min_num_kaddrows && j < num_submat_indexes)
// since we need min_num_kaddrows in the split_lists we ensure that
// we add a pair with the same first element in all the submat_lists
submat_lists[i].push_back(std::make_pair(submat_indexes[j],
Rand() % minibatch_size));
submat_lists[i].push_back(
std::make_pair(submat_indexes[Rand() % num_submat_indexes],
Rand() % minibatch_size));
}
all_pairs.insert(all_pairs.end(), submat_lists[i].begin(),
submat_lists[i].end());
}
SplitLocationsBackward(submat_lists, &split_lists);
// Checking split_lists has all the necessary properties
for (int32 i = 0; i < split_lists.size(); i++) {
int32 first_value;
std::vector<int32> second_values;
if (ConvertToIndexes(split_lists[i], &first_value, &second_values)) {
// checking for contiguity and uniqueness of .second elements
std::vector<int32> occurred_values;
int32 prev_value = -10; // using a negative value as all indices are > 0
for (int32 j = 0; j < second_values.size(); j++) {
if (second_values[j] == -1)
continue;
if (second_values[j] != prev_value) {
std::vector<int32>::iterator iter = std::find(occurred_values.begin(),
occurred_values.end(),
second_values[j]);
KALDI_ASSERT(iter == occurred_values.end());
}
}
} else {
std::vector<std::pair<int32, int32> > list_of_pairs;
// checking for uniques of elements in the list
for (int32 j = 0; j < split_lists[i].size(); j++) {
if (split_lists[i][j].first == -1)
continue;
std::vector<std::pair<int32, int32> >::const_iterator iter =
std::find_if(list_of_pairs.begin(), list_of_pairs.end(),
PairIsEqualComparator(split_lists[i][j]));
KALDI_ASSERT(iter == list_of_pairs.end());
list_of_pairs.push_back(split_lists[i][j]);
}
}
}
if (verbose) {
KALDI_LOG << "submat_list";
PrintVectorVectorPair(submat_lists);
KALDI_LOG << "split_lists";
PrintVectorVectorPair(split_lists);
KALDI_LOG << "===========================";
}
int32 num_kaddrows_in_output = 0;
int32 first_value;
std::vector<int32> second_values;
// ensure that elements in submat_lists are also present
// in split_lists
for (int32 i = 0 ; i < split_lists.size(); i++) {
second_values.clear();
if (ConvertToIndexes(split_lists[i], &first_value, &second_values)) {
// Checking if ConvertToIndexes did a proper conversion of the indexes
KALDI_ASSERT(second_values.size() == split_lists[i].size());
for (int32 j = 0; j < second_values.size(); j++) {
if (split_lists[i][j].first != -1)
KALDI_ASSERT((split_lists[i][j].first == first_value) &&
(split_lists[i][j].second == second_values[j]));
}
num_kaddrows_in_output++;
}
for (int32 j = 0; j < split_lists[i].size(); j++) {
if (split_lists[i][j].first == -1)
continue;
std::vector<std::pair<int32, int32> >::iterator iter =
std::find_if(all_pairs.begin(), all_pairs.end(),
ComparePair(split_lists[i][j]));
KALDI_ASSERT(iter != all_pairs.end());
all_pairs.erase(iter);
}
}
KALDI_ASSERT(all_pairs.size() == 0);
// ensure that there are at least as many kAddRows compatible split_lists as
// specified
KALDI_ASSERT(num_kaddrows_in_output >= min_num_kaddrows);
}
void UnitTestHasContiguousProperty() {
for (int32 k = 0; k < 10; k++) {
int32 size = RandInt(0, 5);
std::vector<int32> indexes(size);
for (int32 i = 0; i < size; i++)
indexes[i] = RandInt(-1, 4);
std::vector<std::pair<int32, int32> > reverse_indexes;
bool ans = HasContiguousProperty(indexes, &reverse_indexes);
if (!ans) { // doesn't have contiguous propety.
KALDI_LOG << "no.";
bool found_example = false;
for (int32 i = 0; i < size; i++) {
if (indexes[i] != -1) {
bool found_not_same = false;
for (int32 j = i + 1; j < size; j++) {
if (indexes[j] != indexes[i]) found_not_same = true;
else if (found_not_same) found_example = true; // found something like x y x.
}
}
}
KALDI_ASSERT(found_example);
} else {
KALDI_LOG << "yes.";
for (int32 i = 0; i < reverse_indexes.size(); i++) {
for (int32 j = reverse_indexes[i].first;
j < reverse_indexes[i].second; j++) {
KALDI_ASSERT(indexes[j] == i);
indexes[j] = -1;
}
}
for (int32 i = 0; i < size; i++) // make sure all indexes covered.
KALDI_ASSERT(indexes[i] == -1);
}
}
}
void UnitTestEnsureContiguousProperty() {
for (int32 k = 0; k < 10; k++) {
int32 size = RandInt(0, 5);
std::vector<int32> indexes(size);
for (int32 i = 0; i < size; i++)
indexes[i] = RandInt(-1, 4);
std::vector<std::pair<int32, int32> > reverse_indexes;
bool ans = HasContiguousProperty(indexes, &reverse_indexes);
if (ans) { // has contiguous property -> EnsureContiguousProperty should do
// nothing.
std::vector<std::vector<int32> > indexes_split;
EnsureContiguousProperty(indexes, &indexes_split);
if (indexes.size() == 0 ||
*std::max_element(indexes.begin(), indexes.end()) == -1) {
KALDI_ASSERT(indexes_split.size() == 0);
} else {
KALDI_ASSERT(indexes_split.size() == 1 &&
indexes_split[0] == indexes);
}
} else {
std::vector<std::vector<int32> > indexes_split;
EnsureContiguousProperty(indexes, &indexes_split);
KALDI_ASSERT(indexes_split.size() > 1);
for (int32 i = 0; i < indexes.size(); i++) {
int32 this_val = indexes[i];
bool found = (this_val == -1); // not looking for anything if
// this_val is -1.
for (int32 j = 0; j < indexes_split.size(); j++) {
if (found) {
KALDI_ASSERT(indexes_split[j][i] == -1);
} else {
if (indexes_split[j][i] == this_val) {
found = true;
} else {
KALDI_ASSERT(indexes_split[j][i] == -1);
}
}
}
KALDI_ASSERT(found);
for (int32 j = 0; j < indexes_split.size(); j++) {
KALDI_ASSERT(indexes_split[j].size() == indexes.size() &&
HasContiguousProperty(indexes_split[j], &reverse_indexes));
}
}
}
}
}
// Function to check SplitLocations() method
// checks if the submat_lists and split_lists have the same non-dummy elements
// checks if the submat_lists are split into same first_element lists wherever
// possible
void UnitTestSplitLocations(bool verbose) {
int32 minibatch_size = Rand() % 1024 + 100;
int32 num_submat_indexes = Rand() % 10 + 1;
int32 max_submat_list_size = Rand() % 10 + 1;
int32 min_num_kaddrows = Rand() % 2; // minimum number of kAddRows compatible
// lists expected in the final split lists. This value will be used to
// create input submat_lists so that this is guaranteed
max_submat_list_size = min_num_kaddrows + max_submat_list_size;
std::vector<std::pair<int32, int32> > all_pairs;
all_pairs.reserve(minibatch_size * max_submat_list_size);
std::vector<std::vector<std::pair<int32, int32> > >
submat_lists(minibatch_size),
split_lists;
std::vector<int32> submat_indexes(num_submat_indexes);
for (int32 i = 0; i < num_submat_indexes; i++) {
submat_indexes[i] = Rand();
}
// generating submat_lists
int32 max_generated_submat_list_size = 0;
for (int32 i = 0; i < minibatch_size; i++) {
int32 num_locations = Rand() % max_submat_list_size + 1;
max_generated_submat_list_size =
max_generated_submat_list_size < num_locations ?
num_locations : max_generated_submat_list_size;
submat_lists[i].reserve(num_locations);
for (int32 j = 0; j < num_locations; j++) {
// note from dan: I edited the following line to resolve a valgrind error
// but cannot really understand at this point what this code is doing.
if (j <= min_num_kaddrows && j < num_submat_indexes) {
// since we need min_num_kaddrows in the split_lists we ensure that
// we add a pair with the same first element in all the submat_lists
submat_lists[i].push_back(std::make_pair(submat_indexes[j],
Rand() % minibatch_size));
}
submat_lists[i].push_back(
std::make_pair(submat_indexes[Rand() % num_submat_indexes],
Rand() % minibatch_size));
}
all_pairs.insert(all_pairs.end(), submat_lists[i].begin(),
submat_lists[i].end());
}
SplitLocations(submat_lists, &split_lists);
if (verbose) {
KALDI_LOG << "submat_list";
PrintVectorVectorPair(submat_lists);
KALDI_LOG << "split_lists";
PrintVectorVectorPair(split_lists);
KALDI_LOG << "===========================";
KALDI_LOG << split_lists.size();
}
int32 num_kaddrows_in_output = 0;
int32 first_value;
std::vector<int32> second_values;
// ensure that elements in submat_lists are also present
// in split_lists
for (int32 i = 0 ; i < split_lists.size(); i++) {
second_values.clear();
if (ConvertToIndexes(split_lists[i], &first_value, &second_values)) {
// Checking if ConvertToIndexes did a proper conversion of the indexes
for (int32 j = 0; j < second_values.size(); j++) {
if (split_lists[i][j].first != -1)
KALDI_ASSERT((split_lists[i][j].first == first_value) &&
(split_lists[i][j].second == second_values[j]));
}
num_kaddrows_in_output++;
}
for (int32 j = 0; j < split_lists[i].size(); j++) {
if (split_lists[i][j].first == -1)
continue;
std::vector<std::pair<int32, int32> >::iterator iter =
std::find_if(all_pairs.begin(), all_pairs.end(),
ComparePair(split_lists[i][j]));
KALDI_ASSERT(iter != all_pairs.end());
all_pairs.erase(iter);
}
}
KALDI_ASSERT(all_pairs.size() == 0);
// ensure that there are at least as many kAddRows compatible split_lists as
// specified
KALDI_ASSERT(num_kaddrows_in_output >= min_num_kaddrows);
}
} // namespace nnet2
} // namespace kaldi
int main() {
using namespace kaldi;
using namespace kaldi::nnet3;
bool verbose = false;
for (int32 loop = 0; loop < 10; loop++) {
UnitTestSplitLocations(verbose);
UnitTestSplitLocationsBackward(verbose);
UnitTestHasContiguousProperty();
UnitTestEnsureContiguousProperty();
}
KALDI_LOG << "Tests passed.";
return 0;
}