Commit dea97945 by xiaotong

bug fixes

parent fde02e21
......@@ -36,7 +36,6 @@ XTensor T2TLengthPenalizer::GNMT(const XTensor & length, float alpha)
XTensor lp;
base = (length + 5)/(1 + 5);
lp = Power(base, alpha);
return lp;
......
......@@ -227,6 +227,8 @@ void T2TPredictor::Predict(T2TStateBundle * next, XTensor * encoding,
/* generate the output probabilities */
m->outputLayer->Make(decodingStep, output);
_LogMe(&output);
next->layersEnc.AddList(&s->layersEnc);
next->layersDec.Add(&inputDec);
......
......@@ -73,8 +73,10 @@ search for the most promising states
>> input - input of the model
>> padding - padding of the input
>> output - output that represents the sequences as rows
>> score - score of the sequences
*/
void T2TSearch::Search(T2TModel * model, XTensor * input, XTensor * padding, XTensor * output)
void T2TSearch::Search(T2TModel * model, XTensor * input, XTensor * padding,
XTensor * output, XTensor * score)
{
T2TPredictor predictor;
XTensor maskEnc;
......@@ -155,7 +157,7 @@ void T2TSearch::Search(T2TModel * model, XTensor * input, XTensor * padding, XTe
/* fill the heap with imcomplete hypotheses if neccesary */
FillHeap(next);
Dump(output);
Dump(output, score);
delete[] states;
}
......@@ -237,6 +239,7 @@ void T2TSearch::Score(T2TStateBundle * prev, T2TStateBundle * beam)
InitTensor(&mask,
prev->endMark.order, prev->endMark.dimSize, X_FLOAT, 1.0F,
prev->endMark.devID, prev->endMark.mem);
mask.SetZeroAll();
_SetDataFixedCond(&mask, &prev->endMark, -1e9F);
mask.Reshape(mask.unitNum);
......@@ -514,23 +517,28 @@ void T2TSearch::FillHeap(T2TStateBundle * beam)
/*
save the output sequences in a tensor
>> output - output sequences (for return)
>> score - score of thes sequences
*/
void T2TSearch::Dump(XTensor * output)
void T2TSearch::Dump(XTensor * output, XTensor * score)
{
int dims[3] = {batchSize, beamSize, maxLength};
int * words = new int[maxLength];
InitTensor(output, 3, dims, X_INT);
InitTensor(score, 2, dims, X_FLOAT);
SetDataFixedInt(*output, -1);
score->SetZeroAll();
/* heap for an input sentence in the batch */
for(int h = 0; h < batchSize; h++){
XHeap<MIN_HEAP, float> &heap = fullHypos[h];
int c = heap.Count();
/* for each output in the beam */
for(int i = 0; i < beamSize && heap.Count() > 0; i++){
T2TState * state = (T2TState *)heap.Pop().index;
HeapNode<float> node = heap.Pop();
T2TState * state = (T2TState *)node.index;
int count = 0;
bool isCompleted = true;
......@@ -548,7 +556,9 @@ void T2TSearch::Dump(XTensor * output)
/* dump the sentence to the output tensor */
for(int w = 0; w < count; w++)
output->Set3DInt(words[count - w - 1], h, beamSize - i - 1, w);
output->Set3DInt(words[count - w - 1], h, c - i - 1, w);
score->Set2D(node.value, h, c - i - 1);
}
}
......
......@@ -73,7 +73,8 @@ public:
void Init(int argc, char ** argv);
/* search for the most promising states */
void Search(T2TModel * model, XTensor * input, XTensor * padding, XTensor * output);
void Search(T2TModel * model, XTensor * input, XTensor * padding,
XTensor * output, XTensor * score);
/* preparation */
void Prepare(int myBatchSize,int myBeamSize);
......@@ -94,7 +95,7 @@ public:
void FillHeap(T2TStateBundle * beam);
/* save the output sequences in a tensor */
void Dump(XTensor * output);
void Dump(XTensor * output, XTensor * score);
/* check if the token is an end symbol */
bool IsEnd(int token);
......
......@@ -112,10 +112,12 @@ void T2TTester::Test(const char * fn, const char * ofn, T2TModel * model)
CheckNTErrors(!model->isLM, "Only MT model is supported!");
XTensor output;
XTensor score;
seacher.Search(model, &batchEnc, &paddingEnc, &output);
seacher.Search(model, &batchEnc, &paddingEnc, &output, &score);
Dump(ofile, &output);
//score.Dump(ofile, "score:");
float prob = 0;
......
......@@ -238,12 +238,12 @@ void _SetDataFixedCond(XTensor * tensor, XTensor * condition, DTYPE p)
int num = tensor->unitNum;
CheckNTErrors(num == condition->unitNum, "Wrong size of the condition tensor!");
CheckNTErrors(condition->unitSize == sizeof(float), "TODO!");
CheckNTErrors(condition->unitSize == sizeof(int), "TODO!");
if(tensor->dataType == DEFAULT_DTYPE){
if(tensor->devID < 0){
DTYPE * data = (DTYPE*)tensor->data;
DTYPE * cond = (DTYPE*)condition->data;
int * cond = (int*)condition->data;
for(int i = 0; i < num; i++){
if(cond[i] != 0)
data[i] = p;
......
......@@ -159,7 +159,7 @@ if the condition entry is non-zero
>> p - the initial value
*/
__global__
void KernelSetDataFixedCondFloat(float * d, float * c, int size, float p)
void KernelSetDataFixedCondFloat(float * d, int * c, int size, float p)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
......@@ -178,7 +178,7 @@ if the condition entry is non-zero
void _CudaSetDataFixedCondFloat(XTensor * tensor, XTensor * condition, float p)
{
CheckNTErrors(tensor->dataType == X_FLOAT, "the tensor must be in X_FLOAT!");
CheckNTErrors(condition->unitSize == sizeof(float), "TODO!");
CheckNTErrors(condition->unitSize == sizeof(int), "TODO!");
int gridSize[3];
int blockSize[3];
......@@ -191,7 +191,7 @@ void _CudaSetDataFixedCondFloat(XTensor * tensor, XTensor * condition, float p)
int devIDBackup;
ProtectCudaDev(tensor->devID, devIDBackup);
KernelSetDataFixedCondFloat <<<blocks, threads >>>((float*)tensor->data, (float*)condition->data,
KernelSetDataFixedCondFloat <<<blocks, threads >>>((float*)tensor->data, (int*)condition->data,
tensor->unitNum, p);
BacktoCudaDev(tensor->devID, devIDBackup);
......@@ -206,7 +206,7 @@ if the condition entry is non-zero
>> p - the initial value
*/
__global__
void KernelSetDataFixedCondInt(int * d, float * c, int size, int p)
void KernelSetDataFixedCondInt(int * d, int * c, int size, int p)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
......@@ -225,7 +225,7 @@ if the condition entry is non-zero
void _CudaSetDataFixedCondInt(XTensor * tensor, XTensor * condition, int p)
{
CheckNTErrors(tensor->dataType == X_FLOAT, "the tensor must be in X_FLOAT!");
CheckNTErrors(condition->unitSize == sizeof(float), "TODO!");
CheckNTErrors(condition->unitSize == sizeof(int), "TODO!");
int gridSize[3];
int blockSize[3];
......@@ -238,7 +238,7 @@ void _CudaSetDataFixedCondInt(XTensor * tensor, XTensor * condition, int p)
int devIDBackup;
ProtectCudaDev(tensor->devID, devIDBackup);
KernelSetDataFixedCondInt <<<blocks, threads >>>((int*)tensor->data, (float*)condition->data,
KernelSetDataFixedCondInt <<<blocks, threads >>>((int*)tensor->data, (int*)condition->data,
tensor->unitNum, p);
BacktoCudaDev(tensor->devID, devIDBackup);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论