From 6e89150914078826ee51fbdaf1b42e19b60407b1 Mon Sep 17 00:00:00 2001 From: Dogan Can Date: Sat, 3 Mar 2018 12:51:48 -0800 Subject: [PATCH 1/2] Fix macro redefinition problems in tensorflow rnnlm. --- src/tfrnnlm/tensorflow-rnnlm.cc | 12 ++++---- src/tfrnnlm/tensorflow-rnnlm.h | 28 ++++++++++++++++++- .../lattice-lmrescore-tf-rnnlm-pruned.cc | 10 ++++--- src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm.cc | 4 ++- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/tfrnnlm/tensorflow-rnnlm.cc b/src/tfrnnlm/tensorflow-rnnlm.cc index 4842d3fbaa8..3f11564c7c6 100644 --- a/src/tfrnnlm/tensorflow-rnnlm.cc +++ b/src/tfrnnlm/tensorflow-rnnlm.cc @@ -21,14 +21,16 @@ #include #include -#include "tensorflow/core/public/session.h" -#include "tensorflow/core/platform/env.h" -#include "tensorflow/core/protobuf/meta_graph.pb.h" - #include "tfrnnlm/tensorflow-rnnlm.h" #include "util/stl-utils.h" #include "util/text-utils.h" +// Tensorflow includes were moved after tfrnnlm/tensorflow-rnnlm.h include to +// avoid macro redefinitions. See also the note in tfrnnlm/tensorflow-rnnlm.h. +#include "tensorflow/core/public/session.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" + namespace kaldi { using std::ifstream; using tf_rnnlm::KaldiTfRnnlmWrapper; @@ -316,7 +318,7 @@ void TfRnnlmDeterministicFst::Clear() { for (int i = 1; i < state_to_cell_.size(); i++) { delete state_to_cell_[i]; } - + state_to_context_.resize(1); state_to_cell_.resize(1); state_to_wseq_.resize(1); diff --git a/src/tfrnnlm/tensorflow-rnnlm.h b/src/tfrnnlm/tensorflow-rnnlm.h index 4c15229fe9d..90b68755964 100644 --- a/src/tfrnnlm/tensorflow-rnnlm.h +++ b/src/tfrnnlm/tensorflow-rnnlm.h @@ -1,4 +1,4 @@ -// tensorflow-rnnlm-lib.h +// tensorflow-rnnlm.h // Copyright (C) 2017 Intellisist, Inc. (Author: Hainan Xu) @@ -27,6 +27,32 @@ #include "base/kaldi-common.h" #include "fstext/deterministic-fst.h" #include "util/common-utils.h" + +// Following macros are defined in both OpenFst and Tensorflow headers. Here we +// undef them before including "tensorflow/core/public/session.h" to silence +// compiler warnings. Note that this is not a panacea. We should still pay +// attention to the order of includes in other places in the codebase to avoid +// using the wrong macro definitions. Any OpenFst header or any header including +// an OpenFst header should be included before tfrnnlm/tensorflow-rnnlm.h. Also, +// to avoid macro redefinitions, any Tensorflow header should be included after +// tfrnnlm/tensorflow-rnnlm.h. +#undef LOG +#undef VLOG +#undef CHECK +#undef CHECK_EQ +#undef CHECK_LT +#undef CHECK_GT +#undef CHECK_LE +#undef CHECK_GE +#undef CHECK_NE +#undef DCHECK +#undef DCHECK_EQ +#undef DCHECK_LT +#undef DCHECK_GT +#undef DCHECK_LE +#undef DCHECK_GE +#undef DCHECK_NE + #include "tensorflow/core/public/session.h" using tensorflow::Session; diff --git a/src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm-pruned.cc b/src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm-pruned.cc index b707ca85977..ff9e3ce9581 100644 --- a/src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm-pruned.cc +++ b/src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm-pruned.cc @@ -20,12 +20,14 @@ #include "base/kaldi-common.h" #include "fstext/fstext-lib.h" -#include "tfrnnlm/tensorflow-rnnlm.h" -#include "util/common-utils.h" -#include "lm/const-arpa-lm.h" #include "lat/kaldi-lattice.h" #include "lat/lattice-functions.h" #include "lat/compose-lattice-pruned.h" +#include "lm/const-arpa-lm.h" +#include "util/common-utils.h" + +// This should come after any OpenFst includes to avoid using the wrong macros. +#include "tfrnnlm/tensorflow-rnnlm.h" int main(int argc, char *argv[]) { try { @@ -139,7 +141,7 @@ int main(int argc, char *argv[]) { int32 n_done = 0, n_fail = 0; - TfRnnlmDeterministicFst* lm_to_add_orig = + TfRnnlmDeterministicFst* lm_to_add_orig = new TfRnnlmDeterministicFst(max_ngram_order, &rnnlm); for (; !compact_lattice_reader.Done(); compact_lattice_reader.Next()) { diff --git a/src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm.cc b/src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm.cc index 178674a3a8e..5d500b5651d 100644 --- a/src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm.cc +++ b/src/tfrnnlmbin/lattice-lmrescore-tf-rnnlm.cc @@ -22,9 +22,11 @@ #include "fstext/fstext-lib.h" #include "lat/kaldi-lattice.h" #include "lat/lattice-functions.h" -#include "tfrnnlm/tensorflow-rnnlm.h" #include "util/common-utils.h" +// This should come after any OpenFst includes to avoid using the wrong macros. +#include "tfrnnlm/tensorflow-rnnlm.h" + int main(int argc, char *argv[]) { try { using namespace kaldi; From f503b94e5b78d2e03ad803b073f9664264aceb71 Mon Sep 17 00:00:00 2001 From: Dogan Can Date: Mon, 5 Mar 2018 17:18:14 -0800 Subject: [PATCH 2/2] Fix CPU swap methods of CuVector and CuPackedMatrix --- src/cudamatrix/cu-packed-matrix.cc | 2 +- src/cudamatrix/cu-vector.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cudamatrix/cu-packed-matrix.cc b/src/cudamatrix/cu-packed-matrix.cc index eeccae94fa2..64f8afe0616 100644 --- a/src/cudamatrix/cu-packed-matrix.cc +++ b/src/cudamatrix/cu-packed-matrix.cc @@ -118,7 +118,7 @@ void CuPackedMatrix::Swap(PackedMatrix *mat) { this->Swap(&temp); // now temp is full, *this is empty. mat->Swap(&temp); // now mat has data from *this, temp has // data from mat. - this->Swap(mat); // copy data in mat to *this, which is now empty. + this->Swap(&temp); // copy data in mat to *this, which is now empty. } else { // *this is full but *mat is empty. mat->Resize(this->num_rows_, kUndefined); this->CopyToPacked(mat); diff --git a/src/cudamatrix/cu-vector.cc b/src/cudamatrix/cu-vector.cc index f61fd4408db..f85d20d37f1 100644 --- a/src/cudamatrix/cu-vector.cc +++ b/src/cudamatrix/cu-vector.cc @@ -387,7 +387,7 @@ void CuVectorBase::ApplyCeiling(Real ceiling_val, MatrixIndexT *ceiled_cou // vector as a matrix with a single row. ::MatrixDim dim = {1, Dim(), 1}; cuda_apply_ceiling(dimGrid, dimBlock, data_, ceiling_val, dim); - + CuDevice::Instantiate().AccuProfile("CuVectorBase::ApplyCeilingNoCount", tim); } else { if (dim_ == 0) { *ceiled_count = 0; return; } @@ -991,7 +991,7 @@ void CuVector::Swap(Vector *vec) { this->Swap(&temp); // now temp is full, *this is empty. vec->Swap(&temp); // now vec has data from *this, temp has // data from vec. - Swap(vec); // copy data in vec to *this, which is now empty. + Swap(&temp); // copy data in vec to *this, which is now empty. } else { // *this is full but *vec is empty. vec->Resize(this->dim_, kUndefined); this->CopyToVec(vec);