Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/moses
diff options
context:
space:
mode:
authorMatthias Huck <huck@i6.informatik.rwth-aachen.de>2015-02-26 02:13:15 +0300
committerMatthias Huck <huck@i6.informatik.rwth-aachen.de>2015-02-26 02:13:15 +0300
commit342abcf8fcea17685aa41f9836c1e05dc61de2aa (patch)
treeec11fcf2a74622d849fc9ef5d9460099b01af3f5 /moses
parentda45709cf6f2e85e75d1b0032d953548ee86d1fc (diff)
Model1Feature: score caching for improved efficiency
Diffstat (limited to 'moses')
-rw-r--r--moses/FF/Model1Feature.cpp59
-rw-r--r--moses/FF/Model1Feature.h9
2 files changed, 62 insertions, 6 deletions
diff --git a/moses/FF/Model1Feature.cpp b/moses/FF/Model1Feature.cpp
index 1c375bc41..b3390d36b 100644
--- a/moses/FF/Model1Feature.cpp
+++ b/moses/FF/Model1Feature.cpp
@@ -199,19 +199,66 @@ void Model1Feature::EvaluateWithSourceContext(const InputType &input
if ( !wordT.IsNonTerminal() )
{
float thisWordProb = m_model1.GetProbability(m_emptyWord,wordT[0]); // probability conditioned on empty word
- for (size_t posS=1; posS<sentence.GetSize()-1; ++posS) // ignore <s> and </s>
+
+ // cache lookup
+ bool foundInCache = false;
+ {
+ #ifdef WITH_THREADS //try read-only lock
+ boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
+ #endif
+ boost::unordered_map<const InputType*, boost::unordered_map<const Factor*, float> >::const_iterator sentenceCache = m_cache.find(&input);
+ if (sentenceCache != m_cache.end())
+ {
+ boost::unordered_map<const Factor*, float>::const_iterator cacheHit = sentenceCache->second.find(wordT[0]);
+ if (cacheHit != sentenceCache->second.end())
+ {
+ foundInCache = true;
+ score += cacheHit->second;
+ FEATUREVERBOSE(3, "Cached score( " << wordT << " ) = " << cacheHit->second << std::endl);
+ }
+ }
+ }
+
+ if (!foundInCache)
{
- const Word &wordS = sentence.GetWord(posS);
- float modelProb = m_model1.GetProbability(wordS[0],wordT[0]);
- FEATUREVERBOSE(3, "p( " << wordT << " | " << wordS << " ) = " << modelProb << std::endl);
- thisWordProb += modelProb;
+ for (size_t posS=1; posS<sentence.GetSize()-1; ++posS) // ignore <s> and </s>
+ {
+ const Word &wordS = sentence.GetWord(posS);
+ float modelProb = m_model1.GetProbability(wordS[0],wordT[0]);
+ FEATUREVERBOSE(4, "p( " << wordT << " | " << wordS << " ) = " << modelProb << std::endl);
+ thisWordProb += modelProb;
+ }
+ float thisWordScore = TransformScore(thisWordProb) - norm;
+ FEATUREVERBOSE(3, "score( " << wordT << " ) = " << thisWordScore << std::endl);
+ {
+ #ifdef WITH_THREADS
+ // need to update cache; write lock
+ boost::unique_lock<boost::shared_mutex> lock(m_accessLock);
+ #endif
+ m_cache[&input][wordT[0]] = thisWordScore;
+ }
+ score += thisWordScore;
}
- score += TransformScore(thisWordProb) - norm;
}
}
scoreBreakdown.PlusEquals(this, score);
}
+
+void Model1Feature::CleanUpAfterSentenceProcessing(const InputType& source)
+{
+ #ifdef WITH_THREADS
+ // need to update cache; write lock
+ boost::unique_lock<boost::shared_mutex> lock(m_accessLock);
+ #endif
+ // clear cache
+ boost::unordered_map<const InputType*, boost::unordered_map<const Factor*, float> >::iterator sentenceCache = m_cache.find(&source);
+ if (sentenceCache != m_cache.end())
+ {
+ sentenceCache->second.clear();
+ m_cache.erase(sentenceCache);
+ }
+}
}
diff --git a/moses/FF/Model1Feature.h b/moses/FF/Model1Feature.h
index 13732db6d..3ff4c6b96 100644
--- a/moses/FF/Model1Feature.h
+++ b/moses/FF/Model1Feature.h
@@ -86,6 +86,8 @@ public:
ScoreComponentCollection* accumulator) const
{}
+ void CleanUpAfterSentenceProcessing(const InputType& source);
+
private:
std::string m_fileNameVcbS;
std::string m_fileNameVcbT;
@@ -94,6 +96,13 @@ private:
const Factor* m_emptyWord;
void Load();
+
+ // cache
+ mutable boost::unordered_map<const InputType*, boost::unordered_map<const Factor*, float> > m_cache;
+ #ifdef WITH_THREADS
+ // reader-writer lock
+ mutable boost::shared_mutex m_accessLock;
+ #endif
};