-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnnet-analyze-test.cc
More file actions
109 lines (90 loc) · 3.56 KB
/
Copy pathnnet-analyze-test.cc
File metadata and controls
109 lines (90 loc) · 3.56 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
// nnet3/nnet-analyze-test.cc
// Copyright 2015 Johns Hopkins University (author: Daniel Povey)
// 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 "nnet3/nnet-nnet.h"
#include "nnet3/nnet-compile.h"
#include "nnet3/nnet-analyze.h"
#include "nnet3/nnet-test-utils.h"
namespace kaldi {
namespace nnet3 {
std::string PrintCommand(int32 num_commands,
int32 command) {
std::ostringstream os;
if (command < 0 || command >= num_commands)
os << command;
else
os << 'c' << command;
return os.str();
}
void UnitTestNnetAnalyze() {
for (int32 n = 0; n < 20; n++) {
struct NnetGenerationOptions gen_config;
std::vector<std::string> configs;
GenerateConfigSequence(gen_config, &configs);
Nnet nnet;
for (size_t j = 0; j < configs.size(); j++) {
KALDI_LOG << "Input config[" << j << "] is: " << configs[j];
std::istringstream is(configs[j]);
nnet.ReadConfig(is);
}
ComputationRequest request;
std::vector<Matrix<BaseFloat> > inputs;
ComputeExampleComputationRequestSimple(nnet, &request, &inputs);
NnetComputation computation;
Compiler compiler(request, nnet);
CompilerOptions opts;
compiler.CreateComputation(opts, &computation);
std::ostringstream os;
computation.Print(os, nnet);
KALDI_LOG << "Generated computation is: " << os.str();
CheckComputationOptions check_config;
// we can do the rewrite check since it's before optimization.
check_config.check_rewrite = true;
ComputationChecker checker(check_config, nnet, computation);
checker.Check();
Analyzer analyzer;
analyzer.Init(nnet, computation);
ComputationAnalysis analysis(computation, analyzer);
// The following output is to be eyeballed by a person.
std::vector<std::string> submatrix_strings;
computation.GetSubmatrixStrings(nnet, &submatrix_strings);
int32 nc = computation.commands.size();
for (int32 n = 0; n < 30; n++) {
int32 s = RandInt(1, computation.submatrices.size() - 1);
int32 c = RandInt(0, nc - 1);
KALDI_LOG << "First nontrivial access of submatrix " << submatrix_strings[s]
<< " is command "
<< PrintCommand(nc, analysis.FirstNontrivialAccess(s));
KALDI_LOG << "Last access of submatrix " << submatrix_strings[s]
<< " is command " << PrintCommand(nc, analysis.LastAccess(s));
KALDI_LOG << "Last write access of submatrix " << submatrix_strings[s]
<< " is command " << PrintCommand(nc, analysis.LastWriteAccess(s));
KALDI_LOG << "Data present in " << submatrix_strings[s]
<< " at command " << c << " is invalidated at command "
<< PrintCommand(nc, analysis.DataInvalidatedCommand(c, s));
}
}
}
} // namespace nnet3
} // namespace kaldi
int main() {
using namespace kaldi;
using namespace kaldi::nnet3;
//SetVerboseLevel(2);
UnitTestNnetAnalyze();
KALDI_LOG << "Nnet tests succeeded.";
return 0;
}