Skip to content

Commit 8650f6f

Browse files
authored
Merge pull request #12898 from luotao1/expand
remove broadcast in sequence_expand
2 parents 0875ccb + b61cf7a commit 8650f6f

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

paddle/fluid/operators/sequence_expand_op.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,27 @@ struct SequenceExpandFunctor<platform::CPUDeviceContext, T> {
5353
const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/
5454
LoDTensor* out) {
5555
int out_offset = 0;
56-
auto& eigen_place = *context.eigen_device();
56+
int x_item_length = x.numel() / x.dims()[0];
57+
auto out_data = out->data<T>();
58+
auto x_data = x.data<T>();
5759
for (size_t i = 1; i < ref_lod.size(); ++i) {
5860
int repeat_num = ref_lod[i] - ref_lod[i - 1];
5961
int x_start = x_lod[i - 1];
6062
int x_end = x_lod[i];
6163
int x_seq_len = x_end - x_start;
6264
if (repeat_num > 0) {
63-
auto x_sub_tensor = x.Slice(x_start, x_end);
64-
x_sub_tensor.Resize({1, x_sub_tensor.numel()});
6565
int out_start = out_offset;
6666
if (out->lod().size() == 1) {
6767
out_start = out->lod()[0][out_offset];
6868
}
69-
auto out_sub_tensor =
70-
out->Slice(out_start, out_start + x_seq_len * repeat_num);
71-
out_sub_tensor.Resize({repeat_num, x_sub_tensor.dims()[1]});
72-
EigenMatrix<T>::From(out_sub_tensor).device(eigen_place) =
73-
EigenMatrix<T>::From(x_sub_tensor)
74-
.broadcast(Eigen::array<int, 2>({{repeat_num, 1}}));
69+
for (int j = 0; j < repeat_num; j++) {
70+
for (int k = 0; k < x_seq_len; k++) {
71+
for (int l = 0; l < x_item_length; l++) {
72+
out_data[(out_start + j * x_seq_len + k) * x_item_length + l] =
73+
x_data[(x_start + k) * x_item_length + l];
74+
}
75+
}
76+
}
7577
}
7678
out_offset += repeat_num;
7779
}

0 commit comments

Comments
 (0)