Skip to content

Commit 7429067

Browse files
committed
clean code
1 parent cdbc5e7 commit 7429067

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

paddle/fluid/operators/math/sequence_pooling.cc

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,34 +113,35 @@ class LastFirstSeqPoolFunctor {
113113
auto* in_data = input.data<T>();
114114
auto* out_data = output->data<T>();
115115

116-
//Calculate length of each word
117-
int64_t word_len = input.numel() / input.dims()[0];
116+
//Calculate the size of each item in sequence
117+
int64_t item_size = input.numel() / input.dims()[0];
118118
auto lod = input.lod()[0];
119+
int seq_num = static_cast<int>(lod.size()) - 1;
119120
if (pooltype == "LAST"){
120-
for (int i=0; i < static_cast<int>(lod.size()) - 1; ++i ){
121+
for (int i=0; i < seq_num; ++i ){
121122
//Calculate the length of each sequence
122123
int64_t seq_len = static_cast<int64_t>(lod[i + 1] - lod[i]);
123124
//Point to the begin of next sequence
124-
in_data += seq_len* word_len;
125-
//Copy the last words to output
126-
std::memcpy(out_data,(in_data-word_len),word_len*sizeof(T));
127-
out_data += word_len;
128-
125+
in_data += seq_len* item_size;
126+
//Copy the last item to output
127+
std::memcpy(out_data,(in_data-item_size),item_size*sizeof(T));
128+
out_data += item_size;
129129
}
130130
}
131131
else if(pooltype == "FIRST"){
132-
for (int i=0; i < static_cast<int>(lod.size()) - 1; ++i ){
132+
for (int i=0; i < seq_num; ++i ){
133133
//Calculate the length of each sequence
134134
int64_t seq_len = static_cast<int64_t>(lod[i + 1] - lod[i]);
135-
//Copy the first words of sequence to output
136-
std::memcpy(out_data,in_data,word_len*sizeof(T));
135+
//Copy the first item of sequence to output
136+
std::memcpy(out_data,in_data,item_size*sizeof(T));
137137
//Point to the next sequence
138-
in_data += seq_len * word_len;
139-
out_data += word_len;
140-
138+
in_data += seq_len * item_size;
139+
out_data += item_size;
141140
}
142-
143141
}
142+
else {
143+
PADDLE_THROW("it's not LAST or FIRST pool type");
144+
}
144145
}
145146
};
146147

0 commit comments

Comments
 (0)