Commit a4e6ab64 by xiaotong

fix the memory leak bug

parent 06053721
......@@ -42,9 +42,8 @@ using namespace transformer;
int main( int argc, const char ** argv )
{
//_CrtSetBreakAlloc(896);
//BackwardTest();
//return 0;
//_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(2708);
if(argc > 1 && !strcmp(argv[1], "-fnnlm"))
FNNLMMain(argc - 1, argv + 1);
......
......@@ -31,6 +31,10 @@ namespace transformer
/* constructor */
AttDecoder::AttDecoder()
{
attentions = NULL;
fnns = NULL;
attLayerNorms = NULL;
fnnLayerNorms = NULL;
attentionsEnde = NULL;
attEndeLayerNorms = NULL;
}
......@@ -38,6 +42,10 @@ AttDecoder::AttDecoder()
/* de-constructor */
AttDecoder::~AttDecoder()
{
delete[] attentions;
delete[] fnns;
delete[] attLayerNorms;
delete[] fnnLayerNorms;
delete[] attentionsEnde;
delete[] attEndeLayerNorms;
}
......
......@@ -159,7 +159,7 @@ void T2TPredictor::Predict(T2TStateBundle * next, XTensor * encoding,
XTensor * inputLast = (XTensor*)s->layersDec.GetItem(0);
/* word indices of positions up to next state */
XTensor &inputDec = *NewTensor();
XTensor inputDec;
/* the first token */
XTensor first;
......
......@@ -218,6 +218,14 @@ void T2TSearch::Score(T2TStateBundle * prev, T2TStateBundle * beam)
/* score = log-prob/lp */
_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,
prev->endMark.order, prev->endMark.dimSize, X_FLOAT, 1.0F,
prev->endMark.devID, prev->endMark.mem);
......@@ -390,9 +398,10 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
it needs much more coding work and the speed-up is not obvious. */
for(int i = 0; i < beam->stateNum; i += beamSize){
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;
T2TState * last = prev->states + pid * beamSize + offset;
......@@ -412,12 +421,12 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
}
/* scores */
state.modelScore = modelScore.Get(i);
state.prob = prob.Get(i);
state.probPath = probPath.Get(i);
state.modelScore = modelScore.Get(k);
state.prob = prob.Get(k);
state.probPath = probPath.Get(k);
/* prediction */
state.prediction = prediction.GetInt(i);
state.prediction = prediction.GetInt(k);
CheckNTErrors(state.prediction >= 0, "Illegal prediction!");
......@@ -425,7 +434,7 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
state.isEnd = IsEnd(state.prediction);
/* 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)
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:
/* set end symbols for search */
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论