Skip to content

Commit

Permalink
Optimize the sequence padding op (PaddlePaddle#17403)
Browse files Browse the repository at this point in the history
test=develop
  • Loading branch information
kbinias authored and luotao1 committed May 15, 2019
1 parent 1ce7b45 commit 0823a7b
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions paddle/fluid/operators/math/sequence_padding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ void CopyValidData(framework::Tensor* dst_tensor,
}
}

template <typename T>
static void fast_mem_init(void* dest, size_t dest_size, const T* src,
size_t num_bytes) {
if (dest == nullptr || dest_size == 0 || src == nullptr) return;

memcpy(dest, src, num_bytes);

dest_size *= num_bytes;
while (dest_size > num_bytes) {
size_t remaining = dest_size - num_bytes;
size_t count = (remaining > num_bytes) ? num_bytes : remaining;
memcpy((unsigned char*)dest + num_bytes, dest, count);
num_bytes += count;
}
}

template <typename T>
class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
public:
Expand Down Expand Up @@ -87,9 +103,8 @@ class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
T* pad_data = pad_tensor->data<T>();
const T* pad_value_data = pad_value.data<T>();
if (pad_value.numel() == 1) {
for (int i = 0; i < pad_tensor->numel(); ++i) {
pad_data[i] = *pad_value_data;
}
fast_mem_init<T>(pad_data, pad_tensor->numel(), pad_value_data,
sizeof(T));
} else {
for (int i = 0; i < pad_tensor->numel(); i += step_width) {
memcpy(pad_data + i, pad_value_data, step_width * sizeof(T));
Expand Down

0 comments on commit 0823a7b

Please sign in to comment.