Commit a4e6ab64 by xiaotong

fix the memory leak bug

parent 06053721
...@@ -42,9 +42,8 @@ using namespace transformer; ...@@ -42,9 +42,8 @@ using namespace transformer;
int main( int argc, const char ** argv ) int main( int argc, const char ** argv )
{ {
//_CrtSetBreakAlloc(896); //_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
//BackwardTest(); //_CrtSetBreakAlloc(2708);
//return 0;
if(argc > 1 && !strcmp(argv[1], "-fnnlm")) if(argc > 1 && !strcmp(argv[1], "-fnnlm"))
FNNLMMain(argc - 1, argv + 1); FNNLMMain(argc - 1, argv + 1);
......
...@@ -31,6 +31,10 @@ namespace transformer ...@@ -31,6 +31,10 @@ namespace transformer
/* constructor */ /* constructor */
AttDecoder::AttDecoder() AttDecoder::AttDecoder()
{ {
attentions = NULL;
fnns = NULL;
attLayerNorms = NULL;
fnnLayerNorms = NULL;
attentionsEnde = NULL; attentionsEnde = NULL;
attEndeLayerNorms = NULL; attEndeLayerNorms = NULL;
} }
...@@ -38,6 +42,10 @@ AttDecoder::AttDecoder() ...@@ -38,6 +42,10 @@ AttDecoder::AttDecoder()
/* de-constructor */ /* de-constructor */
AttDecoder::~AttDecoder() AttDecoder::~AttDecoder()
{ {
delete[] attentions;
delete[] fnns;
delete[] attLayerNorms;
delete[] fnnLayerNorms;
delete[] attentionsEnde; delete[] attentionsEnde;
delete[] attEndeLayerNorms; delete[] attEndeLayerNorms;
} }
......
...@@ -159,7 +159,7 @@ void T2TPredictor::Predict(T2TStateBundle * next, XTensor * encoding, ...@@ -159,7 +159,7 @@ void T2TPredictor::Predict(T2TStateBundle * next, XTensor * encoding,
XTensor * inputLast = (XTensor*)s->layersDec.GetItem(0); XTensor * inputLast = (XTensor*)s->layersDec.GetItem(0);
/* word indices of positions up to next state */ /* word indices of positions up to next state */
XTensor &inputDec = *NewTensor(); XTensor inputDec;
/* the first token */ /* the first token */
XTensor first; XTensor first;
......
...@@ -218,6 +218,14 @@ void T2TSearch::Score(T2TStateBundle * prev, T2TStateBundle * beam) ...@@ -218,6 +218,14 @@ void T2TSearch::Score(T2TStateBundle * prev, T2TStateBundle * beam)
/* score = log-prob/lp */ /* score = log-prob/lp */
_DivDim(&probPath, &lp, &score, 0); _DivDim(&probPath, &lp, &score, 0);
if (prev->isStart) {
XTensor firstMask = MakeFirstMask(beam);
firstMask.Reshape(firstMask.unitNum);
/* mask the hypotheses in the beam expect the first one */
_SumDim(&score, &firstMask, &score, 0);
}
InitTensor(&mask, InitTensor(&mask,
prev->endMark.order, prev->endMark.dimSize, X_FLOAT, 1.0F, prev->endMark.order, prev->endMark.dimSize, X_FLOAT, 1.0F,
prev->endMark.devID, prev->endMark.mem); prev->endMark.devID, prev->endMark.mem);
...@@ -390,9 +398,10 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam) ...@@ -390,9 +398,10 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
it needs much more coding work and the speed-up is not obvious. */ it needs much more coding work and the speed-up is not obvious. */
for(int i = 0; i < beam->stateNum; i += beamSize){ for(int i = 0; i < beam->stateNum; i += beamSize){
for(int j = 0; j < beamSize; j++){ for(int j = 0; j < beamSize; j++){
T2TState & state = states[i + j]; int k = i + j;
T2TState & state = states[k];
int offset = id.GetInt(i + j); int offset = id.GetInt(k);
int pid = i / beamSize; int pid = i / beamSize;
T2TState * last = prev->states + pid * beamSize + offset; T2TState * last = prev->states + pid * beamSize + offset;
...@@ -412,12 +421,12 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam) ...@@ -412,12 +421,12 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
} }
/* scores */ /* scores */
state.modelScore = modelScore.Get(i); state.modelScore = modelScore.Get(k);
state.prob = prob.Get(i); state.prob = prob.Get(k);
state.probPath = probPath.Get(i); state.probPath = probPath.Get(k);
/* prediction */ /* prediction */
state.prediction = prediction.GetInt(i); state.prediction = prediction.GetInt(k);
CheckNTErrors(state.prediction >= 0, "Illegal prediction!"); CheckNTErrors(state.prediction >= 0, "Illegal prediction!");
...@@ -425,7 +434,7 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam) ...@@ -425,7 +434,7 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
state.isEnd = IsEnd(state.prediction); state.isEnd = IsEnd(state.prediction);
/* set the ending mark */ /* set the ending mark */
endMarkCPU.SetInt(state.isEnd, i); endMarkCPU.SetInt(state.isEnd, k);
} }
} }
...@@ -554,4 +563,31 @@ void T2TSearch::SetEnd(const int * tokens, const int tokenNum) ...@@ -554,4 +563,31 @@ void T2TSearch::SetEnd(const int * tokens, const int tokenNum)
endSymbolNum = tokenNum; endSymbolNum = tokenNum;
} }
/*
make a mask to prevent duplicated entries in beam expansion for the first position
>> beam - the beam that keeps the searching states
*/
XTensor T2TSearch::MakeFirstMask(T2TStateBundle * beam)
{
XTensor &prob = beam->prob;
XTensor mask;
int order = prob.order;
int dims[MAX_TENSOR_DIM_NUM];
for (int i = 0; i < order - 1; i++)
dims[i] = prob.GetDim(i);
InitTensor(&mask, order - 1, dims, X_FLOAT);
mask.SetZeroAll();
for (int i = 0; i < mask.unitNum; i++) {
if(i % beamSize != 0)
mask.Set(-1e9, i);
}
mask.SetDevice(prob.devID, prob.mem);
return mask;
}
} }
...@@ -101,6 +101,9 @@ public: ...@@ -101,6 +101,9 @@ public:
/* set end symbols for search */ /* set end symbols for search */
void SetEnd(const int * tokens, const int tokenNum); void SetEnd(const int * tokens, const int tokenNum);
/* make a mask to prevent duplicated entries in beam expansion for the first position */
XTensor MakeFirstMask(T2TStateBundle * beam);
}; };
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论