add phrase table caching

This commit is contained in:
Hieu Hoang 2013-08-15 20:20:44 +01:00
parent 5fa6e506b1
commit 841ce108a5
6 changed files with 81 additions and 1 deletions

View File

@ -71,6 +71,9 @@
</toolChain>
</folderInfo>
<fileInfo id="cdt.managedbuild.config.gnu.exe.debug.656913512.511477442" name="Rand.h" rcbsApplicability="disable" resourcePath="LM/Rand.h" toolsToInvoke=""/>
<fileInfo id="cdt.managedbuild.config.gnu.exe.debug.656913512.1742823107" name="ChartTranslationOption.cpp" rcbsApplicability="disable" resourcePath="ChartTranslationOption.cpp" toolsToInvoke="cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug.1774992327.1616881050">
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug.1774992327.1616881050" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug.1774992327"/>
</fileInfo>
<sourceEntries>
<entry excluding="FF/PhraseLengthFeatureTest.cpp|PhraseLengthFeatureTest.cpp|LM/BackwardTest.cpp|LM/BackwardLMState.h|LM/BackwardLMState.cpp|LM/Backward.h|LM/Backward.cpp|FeatureVectorTest.cpp|LM/ParallelBackoff.h|LM/ParallelBackoff.cpp|src/SyntacticLanguageModelState.h|src/SyntacticLanguageModelFiles.h|src/SyntacticLanguageModel.h|src/SyntacticLanguageModel.cpp|src/LM/SRI.h|src/LM/SRI.cpp|src/LM/Rand.h|src/LM/Rand.cpp|src/LM/LDHT.h|src/LM/LDHT.cpp|SyntacticLanguageModelState.h|SyntacticLanguageModelFiles.h|SyntacticLanguageModel.h|SyntacticLanguageModel.cpp|LM/LDHT.h|LM/LDHT.cpp" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries>

View File

@ -379,6 +379,10 @@ void IOWrapper::OutputBestHypo(const Hypothesis *hypo, long /*translationId*/, c
Backtrack(hypo);
VERBOSE(3,"0" << std::endl);
if (!m_surpressSingleBestOutput) {
if (StaticData::Instance().GetOutputHypoScore()) {
cout << hypo->GetTotalScore() << " ";
}
if (StaticData::Instance().IsPathRecoveryEnabled()) {
OutputInput(cout, hypo);
cout << "||| ";

View File

@ -34,6 +34,15 @@ struct CompareTargetPhrase {
}
};
TargetPhraseCollection::TargetPhraseCollection(const TargetPhraseCollection &copy)
{
for (const_iterator iter = copy.begin(); iter != copy.end(); ++iter) {
const TargetPhrase &origTP = **iter;
TargetPhrase *newTP = new TargetPhrase(origTP);
Add(newTP);
}
}
void TargetPhraseCollection::NthElement(size_t tableLimit)
{
CollType::iterator nth;

View File

@ -57,6 +57,11 @@ public:
return m_collection.end();
}
TargetPhraseCollection()
{}
explicit TargetPhraseCollection(const TargetPhraseCollection &copy);
~TargetPhraseCollection() {
Remove();
}

View File

@ -28,18 +28,34 @@ namespace Moses
PhraseDictionaryTreeAdaptor::
PhraseDictionaryTreeAdaptor(const std::string &line)
: PhraseDictionary("PhraseDictionaryBinary", line)
, m_useCache(true)
{
ReadParameters();
}
PhraseDictionaryTreeAdaptor::~PhraseDictionaryTreeAdaptor()
{
std::map<size_t, const TargetPhraseCollection*>::const_iterator iter;
for (iter = m_cache.begin(); iter != m_cache.end(); ++iter) {
const TargetPhraseCollection *coll = iter->second;
delete coll;
}
}
void PhraseDictionaryTreeAdaptor::Load()
{
SetFeaturesToApply();
}
void PhraseDictionaryTreeAdaptor::SetParameter(const std::string& key, const std::string& value)
{
if (key == "use-cache") {
m_useCache = Scan<bool>(value);
} else {
PhraseDictionary::SetParameter(key, value);
}
}
void PhraseDictionaryTreeAdaptor::InitializeForInput(InputType const& source)
{
const StaticData &staticData = StaticData::Instance();
@ -74,7 +90,39 @@ void PhraseDictionaryTreeAdaptor::CleanUpAfterSentenceProcessing(InputType const
TargetPhraseCollection const*
PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(Phrase const &src) const
{
return GetImplementation().GetTargetPhraseCollection(src);
const TargetPhraseCollection *ret;
if (m_useCache) {
size_t hash = hash_value(src);
std::map<size_t, const TargetPhraseCollection*>::const_iterator iter;
{ // scope of read lock
#ifdef WITH_THREADS
boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
#endif
iter = m_cache.find(hash);
}
if (iter == m_cache.end()) {
ret = GetImplementation().GetTargetPhraseCollection(src);
if (ret) {
ret = new TargetPhraseCollection(*ret);
}
#ifdef WITH_THREADS
boost::unique_lock<boost::shared_mutex> lock(m_accessLock);
#endif
m_cache[hash] = ret;
}
else {
ret = iter->second;
}
}
else {
ret = GetImplementation().GetTargetPhraseCollection(src);
}
return ret;
}
void PhraseDictionaryTreeAdaptor::EnableCache()

View File

@ -11,6 +11,7 @@
#ifdef WITH_THREADS
#include <boost/thread/tss.hpp>
#include <boost/thread/shared_mutex.hpp>
#else
#include <boost/scoped_ptr.hpp>
#endif
@ -44,11 +45,21 @@ class PhraseDictionaryTreeAdaptor : public PhraseDictionary
PDTAimp& GetImplementation();
const PDTAimp& GetImplementation() const;
// cache
bool m_useCache;
mutable std::map<size_t, const TargetPhraseCollection*> m_cache;
#ifdef WITH_THREADS
//reader-writer lock
mutable boost::shared_mutex m_accessLock;
#endif
public:
PhraseDictionaryTreeAdaptor(const std::string &line);
virtual ~PhraseDictionaryTreeAdaptor();
void Load();
void SetParameter(const std::string& key, const std::string& value);
// enable/disable caching
// you enable caching if you request the target candidates for a source phrase multiple times
// if you do caching somewhere else, disable it