From 9d5aff4b803d284fc35ac9339d14577da79617ff Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Thu, 5 Nov 2015 16:58:45 +0000 Subject: [PATCH] recycle recombined hypos --- .../other-builds/moses2/Search/Manager.cpp | 1 + contrib/other-builds/moses2/Search/Manager.h | 5 +++++ .../moses2/Search/SearchNormal.cpp | 20 +++++++++++++++++-- .../other-builds/moses2/Search/SearchNormal.h | 2 +- contrib/other-builds/moses2/Search/Stack.cpp | 4 ++-- contrib/other-builds/moses2/Search/Stack.h | 4 ++-- contrib/other-builds/moses2/System.h | 6 ++++++ 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/contrib/other-builds/moses2/Search/Manager.cpp b/contrib/other-builds/moses2/Search/Manager.cpp index d2a1bc027..05f514002 100644 --- a/contrib/other-builds/moses2/Search/Manager.cpp +++ b/contrib/other-builds/moses2/Search/Manager.cpp @@ -19,6 +19,7 @@ using namespace std; Manager::Manager(System &system, const std::string &inputStr) :m_pool(&system.GetManagerPool()) +,m_hypoRecycle(&system.GetHypoRecycle()) ,m_system(system) ,m_initRange(NOT_FOUND, NOT_FOUND) ,m_initPhrase(system.GetManagerPool(), system, 0) diff --git a/contrib/other-builds/moses2/Search/Manager.h b/contrib/other-builds/moses2/Search/Manager.h index c8b1d006b..22088c6c5 100644 --- a/contrib/other-builds/moses2/Search/Manager.h +++ b/contrib/other-builds/moses2/Search/Manager.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -29,6 +30,9 @@ public: MemPool &GetPool() const { return *m_pool; } + std::queue &GetHypoRecycle() const + { return *m_hypoRecycle; } + const System &GetSystem() const { return m_system; } @@ -46,6 +50,7 @@ public: void Decode(); protected: mutable MemPool *m_pool; + mutable std::queue *m_hypoRecycle; const System &m_system; PhraseImpl *m_input; diff --git a/contrib/other-builds/moses2/Search/SearchNormal.cpp b/contrib/other-builds/moses2/Search/SearchNormal.cpp index 68e65ba40..c2121997d 100644 --- a/contrib/other-builds/moses2/Search/SearchNormal.cpp +++ b/contrib/other-builds/moses2/Search/SearchNormal.cpp @@ -98,9 +98,25 @@ void SearchNormal::Extend(const Hypothesis &hypo, size_t numWordsCovered = newBitmap.GetNumWordsCovered(); Stack &stack = m_stacks[numWordsCovered]; - StackAdd stackAdded = stack.Add(newHypo); + StackAdd added = stack.Add(newHypo); - m_arcLists.AddArc(stackAdded.added, newHypo, stackAdded.other); + std::queue &hypoRecycle = m_mgr.GetHypoRecycle(); + + if (added.added) { + // we're winners! + if (added.other) { + // there was a existing losing hypo + hypoRecycle.push(added.other); + } + } + else { + // we're losers! + // there should be a winner, we're not doing beam pruning + UTIL_THROW_IF2(added.other == NULL, "There must have been a winning hypo"); + hypoRecycle.push(newHypo); + } + + //m_arcLists.AddArc(stackAdded.added, newHypo, stackAdded.other); } void SearchNormal::DebugStacks() const diff --git a/contrib/other-builds/moses2/Search/SearchNormal.h b/contrib/other-builds/moses2/Search/SearchNormal.h index 539eb0f38..b3e7a6f16 100644 --- a/contrib/other-builds/moses2/Search/SearchNormal.h +++ b/contrib/other-builds/moses2/Search/SearchNormal.h @@ -31,7 +31,7 @@ public: protected: Manager &m_mgr; std::vector &m_stacks; - ArcLists m_arcLists; + //ArcLists m_arcLists; size_t m_stackSize; diff --git a/contrib/other-builds/moses2/Search/Stack.cpp b/contrib/other-builds/moses2/Search/Stack.cpp index 7aaa4ba8d..c0b29d191 100644 --- a/contrib/other-builds/moses2/Search/Stack.cpp +++ b/contrib/other-builds/moses2/Search/Stack.cpp @@ -38,11 +38,11 @@ StackAdd Stack::Add(const Hypothesis *hypo) std::pair addRet = m_hypos.insert(hypo); assert(addRet.second); - return StackAdd(true, hypoExisting); + return StackAdd(true, const_cast(hypoExisting)); } else { // already storing the best hypo. discard incoming hypo - return StackAdd(false, hypoExisting); + return StackAdd(false, const_cast(hypoExisting)); } } } diff --git a/contrib/other-builds/moses2/Search/Stack.h b/contrib/other-builds/moses2/Search/Stack.h index 180226f0f..3b2eaec88 100644 --- a/contrib/other-builds/moses2/Search/Stack.h +++ b/contrib/other-builds/moses2/Search/Stack.h @@ -15,10 +15,10 @@ class StackAdd { public: bool added; - const Hypothesis *other; + Hypothesis *other; StackAdd(bool vadded, - const Hypothesis *vother) + Hypothesis *vother) :added(vadded) ,other(vother) { diff --git a/contrib/other-builds/moses2/System.h b/contrib/other-builds/moses2/System.h index dd86d9139..3356f177a 100644 --- a/contrib/other-builds/moses2/System.h +++ b/contrib/other-builds/moses2/System.h @@ -6,6 +6,7 @@ */ #pragma once +#include #include #include "FF/FeatureFunctions.h" #include "Weights.h" @@ -16,6 +17,7 @@ class FeatureFunction; class StatefulFeatureFunction; class PhraseTable; +class Hypothesis; class System { public: @@ -25,6 +27,9 @@ public: MemPool &GetManagerPool() const { return m_managerPool; } + std::queue &GetHypoRecycle() const + { return m_hypoRecycle; } + const Moses::Parameter ¶ms; mutable Moses::FactorCollection vocab; FeatureFunctions featureFunctions; @@ -37,6 +42,7 @@ public: protected: mutable MemPool m_managerPool; + mutable std::queue m_hypoRecycle; void LoadWeights(); void LoadMappings();