nnet-analyze-test.cc
Go to the documentation of this file.
1 // nnet3/nnet-analyze-test.cc
2 
3 // Copyright 2015 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "nnet3/nnet-nnet.h"
21 #include "nnet3/nnet-compile.h"
22 #include "nnet3/nnet-analyze.h"
23 #include "nnet3/nnet-test-utils.h"
24 
25 namespace kaldi {
26 namespace nnet3 {
27 
28 std::string PrintCommand(int32 num_commands,
29  int32 command) {
30  std::ostringstream os;
31  if (command < 0 || command >= num_commands)
32  os << command;
33  else
34  os << 'c' << command;
35  return os.str();
36 }
37 
38 
40  for (int32 n = 0; n < 20; n++) {
41  struct NnetGenerationOptions gen_config;
42 
43  std::vector<std::string> configs;
44  GenerateConfigSequence(gen_config, &configs);
45  Nnet nnet;
46  for (size_t j = 0; j < configs.size(); j++) {
47  KALDI_LOG << "Input config[" << j << "] is: " << configs[j];
48  std::istringstream is(configs[j]);
49  nnet.ReadConfig(is);
50  }
51 
52  ComputationRequest request;
53  std::vector<Matrix<BaseFloat> > inputs;
54  ComputeExampleComputationRequestSimple(nnet, &request, &inputs);
55 
56  NnetComputation computation;
57  Compiler compiler(request, nnet);
58 
59  CompilerOptions opts;
60  compiler.CreateComputation(opts, &computation);
61 
62  std::ostringstream os;
63  computation.Print(os, nnet);
64  KALDI_LOG << "Generated computation is: " << os.str();
65 
66  CheckComputationOptions check_config;
67  // we can do the rewrite check since it's before optimization.
68  check_config.check_rewrite = true;
69  ComputationChecker checker(check_config, nnet, computation);
70  checker.Check();
71 
72  Analyzer analyzer;
73  analyzer.Init(nnet, computation);
74  ComputationAnalysis analysis(computation, analyzer);
75  // The following output is to be eyeballed by a person.
76  std::vector<std::string> submatrix_strings;
77  computation.GetSubmatrixStrings(nnet, &submatrix_strings);
78  int32 nc = computation.commands.size();
79  for (int32 n = 0; n < 30; n++) {
80  int32 s = RandInt(1, computation.submatrices.size() - 1);
81  int32 c = RandInt(0, nc - 1);
82  KALDI_LOG << "First nontrivial access of submatrix " << submatrix_strings[s]
83  << " is command "
84  << PrintCommand(nc, analysis.FirstNontrivialAccess(s));
85  KALDI_LOG << "Last access of submatrix " << submatrix_strings[s]
86  << " is command " << PrintCommand(nc, analysis.LastAccess(s));
87  KALDI_LOG << "Last write access of submatrix " << submatrix_strings[s]
88  << " is command " << PrintCommand(nc, analysis.LastWriteAccess(s));
89  KALDI_LOG << "Data present in " << submatrix_strings[s]
90  << " at command " << c << " is invalidated at command "
91  << PrintCommand(nc, analysis.DataInvalidatedCommand(c, s));
92  }
93  }
94 }
95 
96 } // namespace nnet3
97 } // namespace kaldi
98 
99 int main() {
100  using namespace kaldi;
101  using namespace kaldi::nnet3;
102  //SetVerboseLevel(2);
103 
105 
106  KALDI_LOG << "Nnet tests succeeded.";
107 
108  return 0;
109 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 FirstNontrivialAccess(int32 s) const
Returns the first command (read or write) that accesses any part of &#39;s&#39; except for zeroing it (i...
void ReadConfig(std::istream &config_file)
Definition: nnet-nnet.cc:189
This file contains various routines that are useful in test code.
void Print(std::ostream &os, const Nnet &nnet) const
This file contains utilities for analyzing and checking computations, which are used in the optimizat...
kaldi::int32 int32
std::vector< Command > commands
void UnitTestNnetAnalyze()
void ComputeExampleComputationRequestSimple(const Nnet &nnet, ComputationRequest *request, std::vector< Matrix< BaseFloat > > *inputs)
This function computes an example computation request, for testing purposes.
void Init(const Nnet &nnet, const NnetComputation &computation)
std::vector< SubMatrixInfo > submatrices
struct rnnlm::@11::@12 n
int32 LastWriteAccess(int32 s) const
Returns the last command-index that accesses any part of submatrix &#39;s&#39; as a write operation...
int32 DataInvalidatedCommand(int32 c, int32 s) const
Returns (the first command-index after &#39;c&#39; that any part of submatrix &#39;s&#39; is written to); or if there...
int32 LastAccess(int32 s) const
Returns the last non-deallocation command that accesses any part of submatrix &#39;s&#39;; if there is no suc...
std::string PrintCommand(int32 num_commands, int32 command)
void CreateComputation(const CompilerOptions &opts, NnetComputation *computation)
Definition: nnet-compile.cc:50
int main()
This class creates an initial version of the NnetComputation, without any optimization or sharing of ...
Definition: nnet-compile.h:44
void GenerateConfigSequence(const NnetGenerationOptions &opts, std::vector< std::string > *configs)
Generates a sequence of at least one config files, output as strings, where the first in the sequence...
#define KALDI_LOG
Definition: kaldi-error.h:153
This struct exists to set up various pieces of analysis; it helps avoid the repetition of code where ...
Definition: nnet-analyze.h:294
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95
void GetSubmatrixStrings(const Nnet &nnet, std::vector< std::string > *submat_strings) const
This class performs various kinds of specific analysis on top of what class Analyzer gives you immedi...
Definition: nnet-analyze.h:308