1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
#include "Manager.h"
#include "Util.h"
#include "SearchCubePruning.h"
#include "StaticData.h"
#include "InputType.h"
#include "TranslationOptionCollection.h"
using namespace std;
namespace Moses
{
class BitmapContainerOrderer
{
public:
bool operator()(const BitmapContainer* A, const BitmapContainer* B) const {
if (B->Empty()) {
if (A->Empty()) {
return A < B;
}
return false;
}
if (A->Empty()) {
return true;
}
// Compare the top hypothesis of each bitmap container using the TotalScore, which includes future cost
const float scoreA = A->Top()->GetHypothesis()->GetTotalScore();
const float scoreB = B->Top()->GetHypothesis()->GetTotalScore();
if (scoreA < scoreB) {
return true;
} else if (scoreA > scoreB) {
return false;
} else {
return A < B;
}
}
};
SearchCubePruning::SearchCubePruning(Manager& manager, const InputType &source, const TranslationOptionCollection &transOptColl)
:Search(manager)
,m_source(source)
,m_hypoStackColl(source.GetSize() + 1)
,m_initialTargetPhrase(source.m_initialTargetPhrase)
,m_start(clock())
,m_transOptColl(transOptColl)
{
const StaticData &staticData = StaticData::Instance();
/* constraint search not implemented in cube pruning
long sentenceID = source.GetTranslationId();
m_constraint = staticData.GetConstrainingPhrase(sentenceID);
*/
std::vector < HypothesisStackCubePruning >::iterator iterStack;
for (size_t ind = 0 ; ind < m_hypoStackColl.size() ; ++ind) {
HypothesisStackCubePruning *sourceHypoColl = new HypothesisStackCubePruning(m_manager);
sourceHypoColl->SetMaxHypoStackSize(staticData.GetMaxHypoStackSize());
sourceHypoColl->SetBeamWidth(staticData.GetBeamWidth());
m_hypoStackColl[ind] = sourceHypoColl;
}
}
SearchCubePruning::~SearchCubePruning()
{
RemoveAllInColl(m_hypoStackColl);
}
/**
* Main decoder loop that translates a sentence by expanding
* hypotheses stack by stack, until the end of the sentence.
*/
void SearchCubePruning::ProcessSentence()
{
const StaticData &staticData = StaticData::Instance();
// initial seed hypothesis: nothing translated, no words produced
Hypothesis *hypo = Hypothesis::Create(m_manager,m_source, m_initialTargetPhrase);
HypothesisStackCubePruning &firstStack = *static_cast<HypothesisStackCubePruning*>(m_hypoStackColl.front());
firstStack.AddInitial(hypo);
// Call this here because the loop below starts at the second stack.
firstStack.CleanupArcList();
CreateForwardTodos(firstStack);
const size_t PopLimit = StaticData::Instance().GetCubePruningPopLimit();
VERBOSE(3,"Cube Pruning pop limit is " << PopLimit << std::endl)
const size_t Diversity = StaticData::Instance().GetCubePruningDiversity();
VERBOSE(3,"Cube Pruning diversity is " << Diversity << std::endl)
// go through each stack
size_t stackNo = 1;
std::vector < HypothesisStack* >::iterator iterStack;
for (iterStack = ++m_hypoStackColl.begin() ; iterStack != m_hypoStackColl.end() ; ++iterStack) {
// check if decoding ran out of time
double _elapsed_time = GetUserTime();
if (_elapsed_time > staticData.GetTimeoutThreshold()) {
VERBOSE(1,"Decoding is out of time (" << _elapsed_time << "," << staticData.GetTimeoutThreshold() << ")" << std::endl);
return;
}
HypothesisStackCubePruning &sourceHypoColl = *static_cast<HypothesisStackCubePruning*>(*iterStack);
// priority queue which has a single entry for each bitmap container, sorted by score of top hyp
std::priority_queue< BitmapContainer*, std::vector< BitmapContainer* >, BitmapContainerOrderer> BCQueue;
_BMType::const_iterator bmIter;
const _BMType &accessor = sourceHypoColl.GetBitmapAccessor();
for(bmIter = accessor.begin(); bmIter != accessor.end(); ++bmIter) {
bmIter->second->InitializeEdges();
BCQueue.push(bmIter->second);
// old algorithm
// bmIter->second->EnsureMinStackHyps(PopLimit);
}
// main search loop, pop k best hyps
for (size_t numpops = 1; numpops <= PopLimit && !BCQueue.empty(); numpops++) {
BitmapContainer *bc = BCQueue.top();
BCQueue.pop();
bc->ProcessBestHypothesis();
if (!bc->Empty())
BCQueue.push(bc);
}
// ensure diversity, a minimum number of inserted hyps for each bitmap container;
// NOTE: diversity doesn't ensure they aren't pruned at some later point
if (Diversity > 0) {
for(bmIter = accessor.begin(); bmIter != accessor.end(); ++bmIter) {
bmIter->second->EnsureMinStackHyps(Diversity);
}
}
// the stack is pruned before processing (lazy pruning):
VERBOSE(3,"processing hypothesis from next stack");
// VERBOSE("processing next stack at ");
sourceHypoColl.PruneToSize(staticData.GetMaxHypoStackSize());
VERBOSE(3,std::endl);
sourceHypoColl.CleanupArcList();
CreateForwardTodos(sourceHypoColl);
stackNo++;
}
PrintBitmapContainerGraph();
// some more logging
IFVERBOSE(2) {
m_manager.GetSentenceStats().SetTimeTotal( clock()-m_start );
}
VERBOSE(2, m_manager.GetSentenceStats());
}
void SearchCubePruning::CreateForwardTodos(HypothesisStackCubePruning &stack)
{
const _BMType &bitmapAccessor = stack.GetBitmapAccessor();
_BMType::const_iterator iterAccessor;
size_t size = m_source.GetSize();
stack.AddHypothesesToBitmapContainers();
for (iterAccessor = bitmapAccessor.begin() ; iterAccessor != bitmapAccessor.end() ; ++iterAccessor) {
const WordsBitmap &bitmap = iterAccessor->first;
BitmapContainer &bitmapContainer = *iterAccessor->second;
if (bitmapContainer.GetHypothesesSize() == 0) {
// no hypothese to expand. don't bother doing it
continue;
}
// Sort the hypotheses inside the Bitmap Container as they are being used by now.
bitmapContainer.SortHypotheses();
// check bitamp and range doesn't overlap
size_t startPos, endPos;
for (startPos = 0 ; startPos < size ; startPos++) {
if (bitmap.GetValue(startPos))
continue;
// not yet covered
WordsRange applyRange(startPos, startPos);
if (CheckDistortion(bitmap, applyRange)) {
// apply range
CreateForwardTodos(bitmap, applyRange, bitmapContainer);
}
size_t maxSize = size - startPos;
size_t maxSizePhrase = StaticData::Instance().GetMaxPhraseLength();
maxSize = std::min(maxSize, maxSizePhrase);
for (endPos = startPos+1; endPos < startPos + maxSize; endPos++) {
if (bitmap.GetValue(endPos))
break;
WordsRange applyRange(startPos, endPos);
if (CheckDistortion(bitmap, applyRange)) {
// apply range
CreateForwardTodos(bitmap, applyRange, bitmapContainer);
}
}
}
}
}
void SearchCubePruning::CreateForwardTodos(const WordsBitmap &bitmap, const WordsRange &range, BitmapContainer &bitmapContainer)
{
WordsBitmap newBitmap = bitmap;
newBitmap.SetValue(range.GetStartPos(), range.GetEndPos(), true);
size_t numCovered = newBitmap.GetNumWordsCovered();
const TranslationOptionList &transOptList = m_transOptColl.GetTranslationOptionList(range);
const SquareMatrix &futureScore = m_transOptColl.GetFutureScore();
if (transOptList.size() > 0) {
HypothesisStackCubePruning &newStack = *static_cast<HypothesisStackCubePruning*>(m_hypoStackColl[numCovered]);
newStack.SetBitmapAccessor(newBitmap, newStack, range, bitmapContainer, futureScore, transOptList);
}
}
bool SearchCubePruning::CheckDistortion(const WordsBitmap &hypoBitmap, const WordsRange &range) const
{
// since we check for reordering limits, its good to have that limit handy
int maxDistortion = StaticData::Instance().GetMaxDistortion();
// if there are reordering limits, make sure it is not violated
// the coverage bitmap is handy here (and the position of the first gap)
const size_t hypoFirstGapPos = hypoBitmap.GetFirstGapPos()
, startPos = range.GetStartPos()
, endPos = range.GetEndPos();
// if reordering constraints are used (--monotone-at-punctuation or xml), check if passes all
if (! m_source.GetReorderingConstraint().Check( hypoBitmap, startPos, endPos ) ) {
return false;
}
// no limit of reordering: no problem
if (maxDistortion < 0) {
return true;
}
bool leftMostEdge = (hypoFirstGapPos == startPos);
// any length extension is okay if starting at left-most edge
if (leftMostEdge) {
return true;
}
// starting somewhere other than left-most edge, use caution
// the basic idea is this: we would like to translate a phrase starting
// from a position further right than the left-most open gap. The
// distortion penalty for the following phrase will be computed relative
// to the ending position of the current extension, so we ask now what
// its maximum value will be (which will always be the value of the
// hypothesis starting at the left-most edge). If this vlaue is than
// the distortion limit, we don't allow this extension to be made.
WordsRange bestNextExtension(hypoFirstGapPos, hypoFirstGapPos);
int required_distortion =
m_source.ComputeDistortionDistance(range, bestNextExtension);
if (required_distortion > maxDistortion) {
return false;
}
return true;
}
/**
* Find best hypothesis on the last stack.
* This is the end point of the best translation, which can be traced back from here
*/
const Hypothesis *SearchCubePruning::GetBestHypothesis() const
{
// const HypothesisStackCubePruning &hypoColl = m_hypoStackColl.back();
const HypothesisStack &hypoColl = *m_hypoStackColl.back();
return hypoColl.GetBestHypothesis();
}
/**
* Logging of hypothesis stack sizes
*/
void SearchCubePruning::OutputHypoStackSize()
{
std::vector < HypothesisStack* >::const_iterator iterStack = m_hypoStackColl.begin();
TRACE_ERR( "Stack sizes: " << (int)(*iterStack)->size());
for (++iterStack; iterStack != m_hypoStackColl.end() ; ++iterStack) {
TRACE_ERR( ", " << (int)(*iterStack)->size());
}
TRACE_ERR( endl);
}
void SearchCubePruning::PrintBitmapContainerGraph()
{
HypothesisStackCubePruning &lastStack = *static_cast<HypothesisStackCubePruning*>(m_hypoStackColl.back());
const _BMType &bitmapAccessor = lastStack.GetBitmapAccessor();
_BMType::const_iterator iterAccessor;
for (iterAccessor = bitmapAccessor.begin(); iterAccessor != bitmapAccessor.end(); ++iterAccessor) {
cerr << iterAccessor->first << endl;
//BitmapContainer &container = *iterAccessor->second;
}
}
/**
* Logging of hypothesis stack contents
* \param stack number of stack to be reported, report all stacks if 0
*/
void SearchCubePruning::OutputHypoStack(int stack)
{
if (stack >= 0) {
TRACE_ERR( "Stack " << stack << ": " << endl << m_hypoStackColl[stack] << endl);
} else {
// all stacks
int i = 0;
vector < HypothesisStack* >::iterator iterStack;
for (iterStack = m_hypoStackColl.begin() ; iterStack != m_hypoStackColl.end() ; ++iterStack) {
HypothesisStackCubePruning &hypoColl = *static_cast<HypothesisStackCubePruning*>(*iterStack);
TRACE_ERR( "Stack " << i++ << ": " << endl << hypoColl << endl);
}
}
}
const std::vector < HypothesisStack* >& SearchCubePruning::GetHypothesisStacks() const
{
return m_hypoStackColl;
}
}
|