Commit d936dce2 authored by Jan Trmal's avatar Jan Trmal
Browse files

(trunk) KALDI_ASSERT with potentially improved performance + adding some new files into svn:ignore

git-svn-id: https://svn.code.sf.net/p/kaldi/code/trunk@5212 5e6a8d80-dfce-4ca6-a32a-6e07a63d50c8
parent 1a59ece8
......@@ -116,9 +116,30 @@ class KaldiErrorMessage {
#define __func__ __FUNCTION__
#endif
// Note on KALDI_ASSERT and KALDI_PARANOID_ASSERT
// The original (simple) version of the code was this
//
// #define KALDI_ASSERT(cond) if (!(cond)) kaldi::KaldiAssertFailure_(__func__, __FILE__, __LINE__, #cond);
//
// That worked well, but we were concerned that it
// could potentially cause a performance issue due to failed branch
// prediction (best practice is to have the if branch be the commonly
// taken one).
// Therefore, we decided to move the call into the else{} branch.
// A single block {} around if /else does not work, because it causes
// syntax error (unmatched else block) in the following code:
//
// if (condition)
// KALDI_ASSERT(condition2);
// else
// SomethingElse();
//
// do {} while(0) -- note there is no semicolon at the end! --- works nicely
// and compilers will be able to optimize the loop away (as the condition
// is always false).
#ifndef NDEBUG
#define KALDI_ASSERT(cond) \
if (!(cond)) kaldi::KaldiAssertFailure_(__func__, __FILE__, __LINE__, #cond);
do { if ((cond)) ; else kaldi::KaldiAssertFailure_(__func__, __FILE__, __LINE__, #cond);} while(0)
#else
#define KALDI_ASSERT(cond)
#endif
......@@ -127,11 +148,12 @@ class KaldiErrorMessage {
// also defined there.
#ifdef KALDI_PARANOID // some more expensive asserts only checked if this defined
#define KALDI_PARANOID_ASSERT(cond) \
if (!(cond)) kaldi::KaldiAssertFailure_(__func__, __FILE__, __LINE__, #cond);
do { if ((cond)) ; else kaldi::KaldiAssertFailure_(__func__, __FILE__, __LINE__, #cond);} while(0)
#else
#define KALDI_PARANOID_ASSERT(cond)
#endif
#define KALDI_ERR kaldi::KaldiErrorMessage(__func__, __FILE__, __LINE__).stream()
#define KALDI_WARN kaldi::KaldiWarnMessage(__func__, __FILE__, __LINE__).stream()
#define KALDI_LOG kaldi::KaldiLogMessage(__func__, __FILE__, __LINE__).stream()
......
......@@ -463,7 +463,7 @@ template<typename Real> void CuVectorUnitTestApplyExp() {
vector.ApplyExp();
for(int32 j = 0; j < dim; j++) {
//std::cout<<"diff is "<<exp(vector2(j))-vector(j)<<std::endl;;
KALDI_ASSERT(std::abs(exp(vector2(j))-vector(j)) < 0.000001 )
KALDI_ASSERT(std::abs(exp(vector2(j))-vector(j)) < 0.000001 );
}
}
......@@ -482,7 +482,7 @@ template<typename Real> void CuVectorUnitTestApplyLog() {
vector.ApplyLog();
for(int32 j = 0; j < dim; j++) {
//std::cout<<"diff is "<<exp(vector2(j))-vector(j)<<std::endl;;
KALDI_ASSERT(std::abs(log(vector2(j))-vector(j)) < 0.000001 )
KALDI_ASSERT(std::abs(log(vector2(j))-vector(j)) < 0.000001 );
}
}
......
......@@ -183,7 +183,8 @@ void TestOnlineMfcc() {
for (int32 num_piece = 5; num_piece < 10; num_piece++) {
OnlineMfcc online_mfcc(op);
std::vector<int32> piece_length(num_piece);
KALDI_ASSERT(RandomSplit(waveform.Dim(), &piece_length, num_piece));
bool ret = RandomSplit(waveform.Dim(), &piece_length, num_piece);
KALDI_ASSERT(ret);
int32 offset_start = 0;
for (int32 i = 0; i < num_piece; i++) {
......@@ -231,7 +232,8 @@ void TestOnlinePlp() {
for (int32 num_piece = 5; num_piece < 10; num_piece++) {
OnlinePlp online_plp(op);
std::vector<int32> piece_length(num_piece);
KALDI_ASSERT(RandomSplit(waveform.Dim(), &piece_length, num_piece));
bool ret = RandomSplit(waveform.Dim(), &piece_length, num_piece);
KALDI_ASSERT(ret);
int32 offset_start = 0;
for (int32 i = 0; i < num_piece; i++) {
......@@ -342,8 +344,8 @@ void TestOnlineAppendFeature() {
OnlineAppendFeature online_mfcc_plp(&online_mfcc, &online_plp);
std::vector<int32> piece_length(num_piece);
KALDI_ASSERT(RandomSplit(waveform.Dim(), &piece_length, num_piece));
bool ret = RandomSplit(waveform.Dim(), &piece_length, num_piece);
KALDI_ASSERT(ret);
int32 offset_start = 0;
for (int32 i = 0; i < num_piece; i++) {
Vector<BaseFloat> wave_piece(
......
......@@ -100,7 +100,7 @@ void UnitTestFullGmmEst() {
predicted_change = 0.5 * num_params / num_frames; // Was there
KALDI_LOG << "Objf change per frame was " << change << " vs. predicted "
<< predicted_change;
KALDI_ASSERT(change < 2.0 * predicted_change && change > 0.0)
KALDI_ASSERT(change < 2.0 * predicted_change && change > 0.0);
}
......
......@@ -91,7 +91,7 @@ int main(int argc, char *argv[]) {
} else {
const Matrix<BaseFloat> &mat1 = feature1_reader.Value();
const Matrix<BaseFloat> &mat2 = feature2_reader.Value(key);
KALDI_ASSERT(mat1.NumRows() == mat2.NumRows())
KALDI_ASSERT(mat1.NumRows() == mat2.NumRows());
if (new_dim == 0) {
new_dim = mat2.NumCols();
gmm_accs.Init(am_gmm, new_dim, kGmmAll);
......
......@@ -71,7 +71,7 @@ void Xent::Eval(const VectorBase<BaseFloat> &frame_weights,
KALDI_ASSERT(KALDI_ISFINITE(target.Sum()));
double num_frames = frame_weights.Sum();
KALDI_ASSERT(num_frames >= 0.0)
KALDI_ASSERT(num_frames >= 0.0);
// get frame_weights to GPU,
frame_weights_ = frame_weights;
......@@ -182,7 +182,7 @@ void Mse::Eval(const VectorBase<BaseFloat> &frame_weights,
KALDI_ASSERT(KALDI_ISFINITE(target.Sum()));
int32 num_frames = frame_weights.Sum();
KALDI_ASSERT(num_frames >= 0.0)
KALDI_ASSERT(num_frames >= 0.0);
// get frame_weights to GPU,
frame_weights_ = frame_weights;
......
......@@ -507,8 +507,8 @@ std::string Nnet::InfoBackPropagate() const {
void Nnet::Check() const {
// check we have correct number of buffers,
KALDI_ASSERT(propagate_buf_.size() == NumComponents()+1)
KALDI_ASSERT(backpropagate_buf_.size() == NumComponents()+1)
KALDI_ASSERT(propagate_buf_.size() == NumComponents()+1);
KALDI_ASSERT(backpropagate_buf_.size() == NumComponents()+1);
// check dims,
for (size_t i = 0; i + 1 < components_.size(); i++) {
KALDI_ASSERT(components_[i] != NULL);
......
......@@ -427,7 +427,7 @@ void MaxoutComponent::Init(int32 input_dim, int32 output_dim) {
if (input_dim_ == 0)
input_dim_ = 10 * output_dim_; // default group size : 10
KALDI_ASSERT(input_dim_ > 0 && output_dim_ >= 0);
KALDI_ASSERT(input_dim_ % output_dim_ == 0)
KALDI_ASSERT(input_dim_ % output_dim_ == 0);
}
void MaxoutComponent::InitFromString(std::string args) {
......@@ -521,7 +521,7 @@ void PnormComponent::Init(int32 input_dim, int32 output_dim, BaseFloat p) {
input_dim_ = 10 * output_dim_; // default group size : 10
p_ = p;
KALDI_ASSERT(input_dim_ > 0 && output_dim_ >= 0 && p_ >= 0);
KALDI_ASSERT(input_dim_ % output_dim_ == 0)
KALDI_ASSERT(input_dim_ % output_dim_ == 0);
}
void PnormComponent::InitFromString(std::string args) {
......@@ -2585,7 +2585,7 @@ int32 ChunkInfo::GetIndex(int32 offset) const {
std::vector<int32>::const_iterator iter =
std::lower_bound(offsets_.begin(), offsets_.end(), offset);
// make sure offset is present in the vector
KALDI_ASSERT(iter != offsets_.end() && *iter == offset)
KALDI_ASSERT(iter != offsets_.end() && *iter == offset);
return static_cast<int32>(iter - offsets_.begin());
}
}
......@@ -2596,7 +2596,7 @@ int32 ChunkInfo::GetOffset(int32 index) const {
KALDI_ASSERT((offset <= last_offset_) && (offset >= first_offset_));
return offset;
} else {
KALDI_ASSERT((index >= 0) && (index < offsets_.size()))
KALDI_ASSERT((index >= 0) && (index < offsets_.size()));
return offsets_[index];
}
}
......@@ -3052,7 +3052,7 @@ void DctComponent::Init(int32 dim, int32 dct_dim, bool reorder, int32 dct_keep_d
KALDI_ASSERT(dim > 0 && dct_dim > 0);
KALDI_ASSERT(dim % dct_dim == 0); // dct_dim must divide dim.
KALDI_ASSERT(dct_dim >= dct_keep_dim_)
KALDI_ASSERT(dct_dim >= dct_keep_dim_);
dim_ = dim;
dct_mat_.Resize(dct_keep_dim_, dct_dim);
reorder_ = reorder;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment