nnet-compile-test.cc
Go to the documentation of this file.
1 // nnet3/nnet-compile-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"
23 #include "nnet3/nnet-test-utils.h"
24 
25 namespace kaldi {
26 namespace nnet3 {
27 
28 
30  for (int32 n = 0; n < 20; n++) {
31  struct NnetGenerationOptions gen_config;
32  std::vector<std::string> configs;
33  GenerateConfigSequence(gen_config, &configs);
34  Nnet nnet;
35  for (size_t j = 0; j < configs.size(); j++) {
36  KALDI_LOG << "Input config[" << j << "] is: " << configs[j];
37  std::istringstream is(configs[j]);
38  nnet.ReadConfig(is);
39  }
40 
41  ComputationRequest request;
42  std::vector<Matrix<BaseFloat> > inputs;
43  ComputeExampleComputationRequestSimple(nnet, &request, &inputs);
44  KALDI_LOG << "Computation request is:";
45  request.Print(std::cerr);
46 
47  NnetComputation computation;
48  Compiler compiler(request, nnet);
49 
50  CompilerOptions opts;
51  compiler.CreateComputation(opts, &computation);
52 
53  std::ostringstream os;
54  computation.Print(os, nnet);
55  KALDI_LOG << "Generated computation is: " << os.str();
56  }
57 }
58 
59 
60 // this tests compilation where there are more than one
61 // computation-request... this is to test some of the
62 // low-level utilities that will be used in looped computation.
64  for (int32 n = 0; n < 20; n++) {
65  struct NnetGenerationOptions gen_config;
66  gen_config.allow_use_of_x_dim = false;
67 
68  std::vector<std::string> configs;
69  GenerateConfigSequence(gen_config, &configs);
70  Nnet nnet;
71  for (size_t j = 0; j < configs.size(); j++) {
72  KALDI_LOG << "Input config[" << j << "] is: " << configs[j];
73  std::istringstream is(configs[j]);
74  nnet.ReadConfig(is);
75  }
76 
77  ComputationRequest request1, request2;
78  std::vector<Matrix<BaseFloat> > inputs1, inputs2;
79  ComputeExampleComputationRequestSimple(nnet, &request1, &inputs1);
80  ComputeExampleComputationRequestSimple(nnet, &request2, &inputs2);
81 
82 
83  KALDI_LOG << "Computation request 1 is:";
84  request1.Print(std::cerr);
85  KALDI_LOG << "Computation request 2 is:";
86  request2.Print(std::cerr);
87 
88  std::vector<const ComputationRequest*> requests;
89  request2.store_component_stats = request1.store_component_stats;
90  request1.need_model_derivative = false;
91  request2.need_model_derivative = false;
92  requests.push_back(&request1);
93  requests.push_back(&request2);
94 
95  // set all the x indexes to 1 for request 2 (they would otherwise
96  // be zero). This ensures that there is no overlap
97  // between the inputs and outputs on the two requests.
98  for (int32 i = 0; i < request2.inputs.size(); i++)
99  for (int32 j = 0; j < request2.inputs[i].indexes.size(); j++)
100  request2.inputs[i].indexes[j].x = 1;
101  for (int32 i = 0; i < request2.outputs.size(); i++)
102  for (int32 j = 0; j < request2.outputs[i].indexes.size(); j++)
103  request2.outputs[i].indexes[j].x = 1;
104 
105 
106  NnetComputation computation;
107  Compiler compiler(requests, nnet);
108 
109  CompilerOptions opts;
110  compiler.CreateComputation(opts, &computation);
111 
112  std::ostringstream os;
113  computation.Print(os, nnet);
114  KALDI_LOG << "Generated computation is: " << os.str();
115  }
116 }
117 
118 
119 
121  for (int32 n = 0; n < 20; n++) {
122  struct NnetGenerationOptions gen_config;
123  gen_config.allow_ivector = true;
124 
125  std::vector<std::string> configs;
126  GenerateConfigSequence(gen_config, &configs);
127  Nnet nnet;
128  for (size_t j = 0; j < configs.size(); j++) {
129  KALDI_LOG << "Input config[" << j << "] is: " << configs[j];
130  std::istringstream is(configs[j]);
131  nnet.ReadConfig(is);
132  }
133 
134  ComputationRequest request1, request2, request3;
135  int32 chunk_size_min = RandInt(5, 15);
136  int32 frame_subsampling_factor = RandInt(1, 3),
137  extra_left_context_begin = RandInt(0, 10),
138  extra_right_context = RandInt(0, 10),
139  num_sequences = RandInt(1, 2);
140  int32 chunk_size = GetChunkSize(nnet, frame_subsampling_factor,
141  chunk_size_min),
142  ivector_period = chunk_size;
143 
144 
145 
146  ModifyNnetIvectorPeriod(ivector_period, &nnet);
147  KALDI_LOG << "Nnet info after modifying ivector period is: "
148  << nnet.Info();
150  nnet, chunk_size, frame_subsampling_factor,
151  ivector_period, extra_left_context_begin, extra_right_context,
152  num_sequences, &request1, &request2, &request3);
153 
154  KALDI_LOG << "Computation request 1 is:";
155  request1.Print(std::cerr);
156  KALDI_LOG << "Computation request 2 is:";
157  request2.Print(std::cerr);
158  KALDI_LOG << "Computation request 3 is:";
159  request3.Print(std::cerr);
160 
161  NnetOptimizeOptions optimize_opts;
162  // todo: set optimize-looped=true.
163  NnetComputation computation;
164  CompileLooped(nnet, optimize_opts,
165  request1, request2, request3,
166  &computation);
167  KALDI_LOG << "Compiled looped computation is ";
168  computation.Print(std::cerr, nnet);
169  }
170 }
171 
172 
173 
174 } // namespace nnet3
175 } // namespace kaldi
176 
177 int main() {
178  using namespace kaldi;
179  using namespace kaldi::nnet3;
180  SetVerboseLevel(4);
181 
185 
186 
187  KALDI_LOG << "Nnet tests succeeded.";
188 
189  return 0;
190 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void ModifyNnetIvectorPeriod(int32 ivector_period, Nnet *nnet)
This function modifies the descriptors in the neural network to change the periodicity with which it ...
bool store_component_stats
you should set need_component_stats to true if you need the average-activation and average-derivative...
void ReadConfig(std::istream &config_file)
Definition: nnet-nnet.cc:189
bool need_model_derivative
if need_model_derivative is true, then we&#39;ll be doing either model training or model-derivative compu...
int32 GetChunkSize(const Nnet &nnet, int32 frame_subsampling_factor, int32 advised_chunk_size)
This file contains various routines that are useful in test code.
void Print(std::ostream &os, const Nnet &nnet) const
kaldi::int32 int32
std::vector< IoSpecification > inputs
std::string Info() const
returns some human-readable information about the network, mostly for debugging purposes.
Definition: nnet-nnet.cc:821
void SetVerboseLevel(int32 i)
This should be rarely used, except by programs using Kaldi as library; command-line programs set the ...
Definition: kaldi-error.h:64
void UnitTestNnetCompileLooped()
void ComputeExampleComputationRequestSimple(const Nnet &nnet, ComputationRequest *request, std::vector< Matrix< BaseFloat > > *inputs)
This function computes an example computation request, for testing purposes.
void CompileLooped(const Nnet &nnet, const NnetOptimizeOptions &optimize_opts, const ComputationRequest &request1, const ComputationRequest &request2, const ComputationRequest &request3, NnetComputation *computation)
CompileLooped() provides an internal interface for &#39;looped&#39; computation.
struct rnnlm::@11::@12 n
void CreateLoopedComputationRequestSimple(const Nnet &nnet, int32 chunk_size, int32 frame_subsampling_factor, int32 ivector_period, int32 extra_left_context_begin, int32 extra_right_context, int32 num_sequences, ComputationRequest *request1, ComputationRequest *request2, ComputationRequest *request3)
This function is deprecated.
void CreateComputation(const CompilerOptions &opts, NnetComputation *computation)
Definition: nnet-compile.cc:50
std::vector< IoSpecification > outputs
void UnitTestNnetCompileMulti()
This class creates an initial version of the NnetComputation, without any optimization or sharing of ...
Definition: nnet-compile.h:44
void UnitTestNnetCompile()
int main()
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...
void Print(std::ostream &os) const
This function is for printing info about the computation request in a human-readable way...
#define KALDI_LOG
Definition: kaldi-error.h:153
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95