add inter-stack pruning

This commit is contained in:
Hieu Hoang 2016-12-05 18:04:26 +00:00
parent f7cf9a84ed
commit fc4fa0f19c
5 changed files with 68 additions and 8 deletions

View File

@ -48,13 +48,17 @@ const HypothesisBase *HypothesisColl::GetBestHypo() const
}
void HypothesisColl::Add(
const System &system,
const ManagerBase &mgr,
HypothesisBase *hypo,
Recycler<HypothesisBase*> &hypoRecycle,
ArcLists &arcLists)
{
size_t maxStackSize = system.options.search.stack_size;
//cerr << "stackSize=" << stackSize << endl;
size_t maxStackSize = mgr.system.options.search.stack_size;
//cerr << "maxStackSize=" << maxStackSize << endl;
if (GetSize() * 2 > maxStackSize) {
PruneHypos(mgr, mgr.arcLists);
}
SCORE futureScore = hypo->GetFutureScore();
/*
@ -96,7 +100,7 @@ void HypothesisColl::Add(
StackAdd added = Add(hypo);
size_t nbestSize = system.options.nbest.nbest_size;
size_t nbestSize = mgr.system.options.nbest.nbest_size;
if (nbestSize) {
arcLists.AddArc(added.added, hypo, added.other);
}
@ -219,6 +223,60 @@ void HypothesisColl::SortAndPruneHypos(const ManagerBase &mgr,
*/
}
void HypothesisColl::PruneHypos(const ManagerBase &mgr, ArcLists &arcLists) const
{
size_t stackSize = mgr.system.options.search.stack_size;
Recycler<HypothesisBase*> &recycler = mgr.GetHypoRecycle();
/*
cerr << "UNSORTED hypos: ";
BOOST_FOREACH(const HypothesisBase *hypo, m_coll) {
cerr << hypo << "(" << hypo->GetFutureScore() << ")" << " ";
}
cerr << endl;
*/
vector<const HypothesisBase*> sortedHypos;
size_t ind = 0;
BOOST_FOREACH(const HypothesisBase *hypo, m_coll){
sortedHypos[ind] = hypo;
++ind;
}
vector<const HypothesisBase*>::iterator iterMiddle;
iterMiddle =
(stackSize == 0 || sortedHypos.size() < stackSize) ?
sortedHypos.end() : sortedHypos.begin() + stackSize;
std::partial_sort(sortedHypos.begin(), iterMiddle, sortedHypos.end(),
HypothesisFutureScoreOrderer());
// prune
if (stackSize && sortedHypos.size() > stackSize) {
for (size_t i = stackSize; i < sortedHypos.size(); ++i) {
HypothesisBase *hypo = const_cast<HypothesisBase*>((sortedHypos)[i]);
recycler.Recycle(hypo);
// delete from arclist
if (mgr.system.options.nbest.nbest_size) {
arcLists.Delete(hypo);
}
// delete from collection
//Delete(hypo);
}
sortedHypos.resize(stackSize);
}
/*
cerr << "sorted hypos: ";
for (size_t i = 0; i < sortedHypos.size(); ++i) {
const HypothesisBase *hypo = sortedHypos[i];
cerr << hypo << " ";
}
cerr << endl;
*/
}
void HypothesisColl::Clear()
{
m_sortedHypos = NULL;

View File

@ -25,7 +25,7 @@ class HypothesisColl
public:
HypothesisColl(const ManagerBase &mgr);
void Add(const System &system,
void Add(const ManagerBase &mgr,
HypothesisBase *hypo,
Recycler<HypothesisBase*> &hypoRecycle,
ArcLists &arcLists);
@ -67,6 +67,8 @@ protected:
StackAdd Add(const HypothesisBase *hypo);
void SortAndPruneHypos(const ManagerBase &mgr, ArcLists &arcLists) const;
void PruneHypos(const ManagerBase &mgr, ArcLists &arcLists) const;
};
} /* namespace Moses2 */

View File

@ -47,7 +47,7 @@ void Stack::Add(Hypothesis *hypo, Recycler<HypothesisBase*> &hypoRecycle,
{
HypoCoverage key(&hypo->GetBitmap(), hypo->GetInputPath().range.GetEndPos());
Moses2::HypothesisColl &coll = GetMiniStack(key);
coll.Add(m_mgr.system, hypo, hypoRecycle, arcLists);
coll.Add(m_mgr, hypo, hypoRecycle, arcLists);
}
const Hypothesis *Stack::GetBestHypo() const

View File

@ -60,7 +60,7 @@ void Stacks::Add(Hypothesis *hypo, Recycler<HypothesisBase*> &hypoRecycle,
size_t numWordsCovered = hypo->GetBitmap().GetNumWordsCovered();
//cerr << "numWordsCovered=" << numWordsCovered << endl;
Stack &stack = *m_stacks[numWordsCovered];
stack.Add(m_mgr.system, hypo, hypoRecycle, arcLists);
stack.Add(m_mgr, hypo, hypoRecycle, arcLists);
}
}

View File

@ -33,7 +33,7 @@ void Stack::Add(SCFG::Hypothesis *hypo, Recycler<HypothesisBase*> &hypoRecycle,
//cerr << "lhs=" << lhs << endl;
HypothesisColl &coll = GetColl(lhs);
coll.Add(m_mgr.system, hypo, hypoRecycle, arcLists);
coll.Add(m_mgr, hypo, hypoRecycle, arcLists);
}
size_t Stack::GetSize() const