diff --git a/paddle/fluid/operators/math/sequence_pooling.cc b/paddle/fluid/operators/math/sequence_pooling.cc index 1ffbe3d820b03dafa0bf7d0f2edba064487cd4ed..f48c321c731a94894895a735737f6d3e34ba0853 100644 --- a/paddle/fluid/operators/math/sequence_pooling.cc +++ b/paddle/fluid/operators/math/sequence_pooling.cc @@ -109,24 +109,32 @@ class LastFirstSeqPoolFunctor { void operator()(const platform::CPUDeviceContext& context, const framework::LoDTensor& input, framework::Tensor* output, const std::string pooltype) { + //Create pointers to input and output data auto* in_data = input.data(); auto* out_data = output->data(); + + //Calculate length of each word int64_t word_len = input.numel() / input.dims()[0]; auto lod = input.lod()[0]; - auto dims = input.dims(); if (pooltype == "LAST"){ for (int i=0; i < static_cast(lod.size()) - 1; ++i ){ + //Calculate the length of each sequence int64_t seq_len = static_cast(lod[i + 1] - lod[i]); + //Point to the begin of next sequence in_data += seq_len* word_len; - std::memcpy(out_data,(in_data-word_len),word_len*sizeof(int)); + //Copy the last words to output + std::memcpy(out_data,(in_data-word_len),word_len*sizeof(T)); out_data += word_len; } } else if(pooltype == "FIRST"){ for (int i=0; i < static_cast(lod.size()) - 1; ++i ){ + //Calculate the length of each sequence int64_t seq_len = static_cast(lod[i + 1] - lod[i]); - std::memcpy(out_data,in_data,word_len*sizeof(int)); + //Copy the first words of sequence to output + std::memcpy(out_data,in_data,word_len*sizeof(T)); + //Point to the next sequence in_data += seq_len * word_len; out_data += word_len;