From 0823a7bc8b6c46a866d1e54f8cb96ccaab192bf2 Mon Sep 17 00:00:00 2001 From: Krzysztof Binias Date: Wed, 15 May 2019 07:24:46 +0200 Subject: [PATCH] Optimize the sequence padding op (#17403) test=develop --- .../fluid/operators/math/sequence_padding.cc | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/operators/math/sequence_padding.cc b/paddle/fluid/operators/math/sequence_padding.cc index 25f06a25a0..4630689dec 100644 --- a/paddle/fluid/operators/math/sequence_padding.cc +++ b/paddle/fluid/operators/math/sequence_padding.cc @@ -59,6 +59,22 @@ void CopyValidData(framework::Tensor* dst_tensor, } } +template +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 class PaddingLoDTensorFunctor { public: @@ -87,9 +103,8 @@ class PaddingLoDTensorFunctor { T* pad_data = pad_tensor->data(); const T* pad_value_data = pad_value.data(); if (pad_value.numel() == 1) { - for (int i = 0; i < pad_tensor->numel(); ++i) { - pad_data[i] = *pad_value_data; - } + fast_mem_init(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)); -- GitLab