From 4c6af3bb2907e6b448405e6a054594dd00e7fd1a Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Thu, 18 Feb 2016 10:46:19 -0500 Subject: [PATCH] Revert "put lm caching code in LM" This reverts commit 8dcdd4c1b750894bb4222e6c6eb38f26031f3c02. --- contrib/other-builds/moses2/LM/KENLM.cpp | 46 +++++++------------ contrib/other-builds/moses2/LM/KENLM.h | 12 +---- .../other-builds/moses2/Search/Manager.cpp | 24 ++++++++++ contrib/other-builds/moses2/Search/Manager.h | 9 +++- 4 files changed, 50 insertions(+), 41 deletions(-) diff --git a/contrib/other-builds/moses2/LM/KENLM.cpp b/contrib/other-builds/moses2/LM/KENLM.cpp index 7a6cc15ff..b11c9f932 100644 --- a/contrib/other-builds/moses2/LM/KENLM.cpp +++ b/contrib/other-builds/moses2/LM/KENLM.cpp @@ -102,14 +102,13 @@ void KENLM::Load(System &system) void KENLM::InitializeForInput(const Manager &mgr) const { - CacheColl &cache = GetCache(); - cache.clear(); - mgr.lmCache = &cache; + } // clean up temporary memory, called after processing each sentence void KENLM::CleanUpAfterSentenceProcessing(const Manager &mgr) const { + } void KENLM::SetParameter(const std::string& key, const std::string& value) @@ -303,35 +302,22 @@ lm::WordIndex *KENLM::LastIDs(const Hypothesis &hypo, lm::WordIndex *indices) co float KENLM::ScoreAndCache(const Manager &mgr, const lm::ngram::State &in_state, const lm::WordIndex new_word, lm::ngram::State &out_state) const { - //cerr << "score="; - float score; + //cerr << "score="; + float score; + if (mgr.FindLMCache(in_state, new_word, score, out_state)) { + // found in cache. score & set set in the call + //cerr << "in cache "; + } + else { + //cerr << "not cache "; + score = m_ngram->Score(in_state, new_word, out_state); + mgr.AddLMCache(in_state, new_word, score, out_state); + } - CacheColl &m_lmCache = *((CacheColl*)mgr.lmCache); - LMCacheKey key(in_state, new_word); - CacheColl::iterator iter; - iter = m_lmCache.find(key); - if (iter == m_lmCache.end()) { - score = m_ngram->Score(in_state, new_word, out_state); + //score = m_ngram->Score(in_state, new_word, out_state); - LMCacheValue &val = m_lmCache[key]; - val.first = score; - val.second = out_state; - } - else { - const LMCacheValue &val = iter->second; - score = val.first; - out_state = val.second; - } - - //score = m_ngram->Score(in_state, new_word, out_state); - - //cerr << score << " " << (int) out_state.length << endl; - return score; -} - -KENLM::CacheColl &KENLM::GetCache() const -{ - return GetThreadSpecificObj(m_cache); + //cerr << score << " " << (int) out_state.length << endl; + return score; } } diff --git a/contrib/other-builds/moses2/LM/KENLM.h b/contrib/other-builds/moses2/LM/KENLM.h index 9d9e804c9..bd31e37a1 100644 --- a/contrib/other-builds/moses2/LM/KENLM.h +++ b/contrib/other-builds/moses2/LM/KENLM.h @@ -9,7 +9,6 @@ #define FF_LM_KENLM_H_ #include -#include #include "../FF/StatefulFeatureFunction.h" #include "lm/model.hh" #include "../legacy/Factor.h" @@ -78,12 +77,6 @@ protected: typedef lm::ngram::ProbingModel Model; boost::shared_ptr m_ngram; - std::vector m_lmIdLookup; - - typedef std::pair LMCacheKey; - typedef std::pair LMCacheValue; - typedef boost::unordered_map CacheColl; - mutable boost::thread_specific_ptr m_cache; void CalcScore(const Phrase &phrase, float &fullScore, float &ngramScore, std::size_t &oovCount) const; @@ -95,10 +88,9 @@ protected: // Convert last words of hypothesis into vocab ids, returning an end pointer. lm::WordIndex *LastIDs(const Hypothesis &hypo, lm::WordIndex *indices) const; + std::vector m_lmIdLookup; + float ScoreAndCache(const Manager &mgr, const lm::ngram::State &in_state, const lm::WordIndex new_word, lm::ngram::State &out_state) const; - - CacheColl &GetCache() const; - }; } diff --git a/contrib/other-builds/moses2/Search/Manager.cpp b/contrib/other-builds/moses2/Search/Manager.cpp index 925b45159..18c72638b 100644 --- a/contrib/other-builds/moses2/Search/Manager.cpp +++ b/contrib/other-builds/moses2/Search/Manager.cpp @@ -226,5 +226,29 @@ void Manager::OutputBest() const //cerr << endl; } +void Manager::AddLMCache(const lm::ngram::State &in_state, const lm::WordIndex new_word, float score, const lm::ngram::State &out_state) const +{ + LMCacheKey key(in_state, new_word); + LMCacheValue &val = m_lmCache[key]; + val.first = score; + val.second = out_state; +} + +bool Manager::FindLMCache(const lm::ngram::State &in_state, const lm::WordIndex new_word, float &score, lm::ngram::State &out_state) const +{ + LMCacheKey key(in_state, new_word); + boost::unordered_map::iterator iter; + iter = m_lmCache.find(key); + if (iter == m_lmCache.end()) { + return false; + } + else { + const LMCacheValue &val = iter->second; + score = val.first; + out_state = val.second; + return true; + } +} + } diff --git a/contrib/other-builds/moses2/Search/Manager.h b/contrib/other-builds/moses2/Search/Manager.h index f49f63866..ca0572166 100644 --- a/contrib/other-builds/moses2/Search/Manager.h +++ b/contrib/other-builds/moses2/Search/Manager.h @@ -69,7 +69,10 @@ public: void OutputBest() const; - mutable void *lmCache; + //void AddLMCache(const lm::ngram::State &in_state, const lm::WordIndex new_word) const; + bool FindLMCache(const lm::ngram::State &in_state, const lm::WordIndex new_word, float &score, lm::ngram::State &out_state) const; + void AddLMCache(const lm::ngram::State &in_state, const lm::WordIndex new_word, float score, const lm::ngram::State &out_state) const; + protected: mutable MemPool *m_pool, *m_systemPool; mutable Recycler *m_hypoRecycle; @@ -84,6 +87,10 @@ protected: Search *m_search; + typedef std::pair LMCacheKey; + typedef std::pair LMCacheValue; + mutable boost::unordered_map m_lmCache; + // must be run in same thread as Decode() void Init();