提交 bc0629a5 编写于 作者: C chenjiaoAngel

fix format, test=develop

上级 7f2b4391
......@@ -32,91 +32,30 @@ void seq_pool_sum<float>(const float* din,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
LOG(INFO) << "size: " << lod.size() - 1;
for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
const float* din_ptr = din + lod[i] * width;
float* dout_ptr = dout + i * width;
int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
if (height > 0) {
if (width == 1) {
float sum = 0.f;
for (int h = 0; h < height; ++h) {
sum += din_ptr[h];
}
*dout_ptr = sum;
} else {
memcpy(dout_ptr, din_ptr, width * sizeof(float));
din_ptr += width;
height = height - 1;
/* for (int h = 0; h < height; h++) {
for (int w = 0; w < width; ++w) {
dout_ptr[w] += din_ptr[w];
if (width == 1) {
float sum = 0.f;
for (int h = 0; h < height; ++h) {
sum += din_ptr[h];
}
*dout_ptr = sum;
} else {
memcpy(dout_ptr, din_ptr, width * sizeof(float));
din_ptr += width;
}
*/
// continue;
if (height == 0) return;
int cnt_w = width >> 2;
int remain_w = width & 3;
int cnt_h = height >> 2;
int remain_h = height & 3;
int stride = width << 2;
for (int w = 0; w < cnt_w; w++) {
const float* din_ptr0 = din_ptr + w * 4;
float32x4_t dout_val = vld1q_f32(dout_ptr);
const float* din_ptr1 = din_ptr0 + width;
const float* din_ptr2 = din_ptr1 + width;
const float* din_ptr3 = din_ptr2 + width;
for (int h = 0; h < cnt_h; h++) {
float32x4_t din0 = vld1q_f32(din_ptr0);
float32x4_t din1 = vld1q_f32(din_ptr1);
float32x4_t din2 = vld1q_f32(din_ptr2);
float32x4_t din3 = vld1q_f32(din_ptr3);
dout_val = vaddq_f32(din0, dout_val);
float32x4_t tmp = vaddq_f32(din1, din2);
din_ptr0 += stride;
din_ptr1 += stride;
dout_val = vaddq_f32(din3, dout_val);
din_ptr2 += stride;
din_ptr3 += stride;
dout_val = vaddq_f32(tmp, dout_val);
}
for (int h = 0; h < remain_h; h++) {
float32x4_t din0 = vld1q_f32(din_ptr0);
dout_val = vaddq_f32(din0, dout_val);
din_ptr0 += width;
}
vst1q_f32(dout_ptr, dout_val);
dout_ptr += 4;
}
const float* din_ptr00 = din_ptr + cnt_w * 4;
for (int w = 0; w < remain_w; w++) {
const float* din_ptr0 = din_ptr00 + w;
const float* din_ptr1 = din_ptr0 + width;
const float* din_ptr2 = din_ptr1 + width;
const float* din_ptr3 = din_ptr2 + width;
for (int h = 0; h < cnt_h; h++) {
*dout_ptr += din_ptr0[0];
float tmp = din_ptr1[0] + din_ptr2[0];
din_ptr0 += stride;
din_ptr1 += stride;
*dout_ptr += din_ptr3[0];
din_ptr2 += stride;
din_ptr3 += stride;
*dout_ptr += tmp;
}
for (int h = 0; h < remain_h; h++) {
*dout_ptr += din_ptr0[0];
din_ptr0 += width;
height = height - 1;
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; ++w) {
dout_ptr[w] += din_ptr[w];
}
din_ptr += width;
}
dout_ptr++;
}
}
}
}
printf("end--\n");
}
template <>
......
......@@ -29,10 +29,10 @@ namespace math {
template <>
void seq_pool_sum_grad<float>(const float* din,
const float* din_grad,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
const float* din_grad,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
for (int i = 0; i < static_cast<int>(lod.size()) - 1; i++) {
int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
const float* din_ptr = din + lod[i] * width;
......@@ -49,8 +49,8 @@ void seq_pool_sum_grad<float>(const float* din,
dout_ptr[h] = *din_grad_ptr;
dout_ptr += width;
}
din_grad_ptr++;
}
din_grad_ptr++;
}
}
}
}
......@@ -58,10 +58,10 @@ void seq_pool_sum_grad<float>(const float* din,
template <>
void seq_pool_average_grad<float>(const float* din,
const float* din_grad,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
const float* din_grad,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
const float* din_ptr = din + lod[i] * width;
......@@ -88,10 +88,10 @@ void seq_pool_average_grad<float>(const float* din,
template <>
void seq_pool_sqrt_grad<float>(const float* din,
const float* din_grad,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
const float* din_grad,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
const float* din_ptr = din + lod[i] * width;
......@@ -118,10 +118,10 @@ void seq_pool_sqrt_grad<float>(const float* din,
template <>
void seq_pool_first_grad<float>(const float* din,
const float* din_grad,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
const float* din_grad,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
int64_t height = lod[i + 1] - lod[i];
const float* din_ptr = din + width * lod[i];
......@@ -129,7 +129,7 @@ void seq_pool_first_grad<float>(const float* din,
float* dout_ptr = dout + lod[i] * width;
if (height > 0) {
for (int w = 0; w < width; w++) {
dout_ptr[w] = din_grad_ptr[w];
dout_ptr[w] = din_grad_ptr[w];
}
}
}
......
......@@ -23,31 +23,31 @@ namespace math {
template <typename T>
void seq_pool_sum_grad(const T* din,
const T* din_grad,
T* dout,
const std::vector<uint64_t> lod,
int64_t width);
const T* din_grad,
T* dout,
const std::vector<uint64_t> lod,
int64_t width);
template <typename T>
void seq_pool_average_grad(const T* din,
const T* din_grad,
T* dout,
const std::vector<uint64_t> lod,
int64_t width);
const T* din_grad,
T* dout,
const std::vector<uint64_t> lod,
int64_t width);
template <typename T>
void seq_pool_sqrt_grad(const T* din,
const T* din_grad,
T* dout,
const std::vector<uint64_t> lod,
int64_t width);
const T* din_grad,
T* dout,
const std::vector<uint64_t> lod,
int64_t width);
template <typename T>
void seq_pool_first_grad(const T* din,
const T* din_grad,
T* dout,
const std::vector<uint64_t> lod,
int64_t width);
const T* din_grad,
T* dout,
const std::vector<uint64_t> lod,
int64_t width);
} // namespace math
} // namespace arm
......
......@@ -38,13 +38,17 @@ void SequencePoolGradCompute::Run() {
const auto lod = param.X->lod()[0];
int64_t width = param.X->numel() / param.X->dims()[0];
if (pool_type == "SUM" || pool_type == "MAX" || pool_type == "MIN") {
lite::arm::math::seq_pool_sum_grad(din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
lite::arm::math::seq_pool_sum_grad(
din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
} else if (pool_type == "AVERAGE") {
lite::arm::math::seq_pool_average_grad(din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
lite::arm::math::seq_pool_average_grad(
din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
} else if (pool_type == "SQRT") {
lite::arm::math::seq_pool_sqrt_grad(din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
lite::arm::math::seq_pool_sqrt_grad(
din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
} else if (pool_type == "FIRST" || pool_type == "LAST") {
lite::arm::math::seq_pool_first_grad(din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
lite::arm::math::seq_pool_first_grad(
din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
} else {
LOG(ERROR) << " UNKNOWN sequence pool type";
}
......
......@@ -23,7 +23,8 @@ namespace lite {
namespace kernels {
namespace arm {
class SequencePoolGradCompute : public KernelLite<TARGET(kARM), PRECISION(kFloat)> {
class SequencePoolGradCompute
: public KernelLite<TARGET(kARM), PRECISION(kFloat)> {
public:
void PrepareForRun() override;
......
......@@ -40,7 +40,8 @@ bool SequencePoolGradOp::InferShapeImpl() const {
return true;
}
bool SequencePoolGradOp::AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) {
bool SequencePoolGradOp::AttachImpl(const cpp::OpDesc &opdesc,
lite::Scope *scope) {
param_.X = const_cast<lite::Tensor *>(
&scope->FindVar(opdesc.Input("X").front())->Get<lite::Tensor>());
CHECK(param_.X);
......@@ -63,4 +64,5 @@ bool SequencePoolGradOp::AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scop
} // namespace lite
} // namespace paddle
REGISTER_LITE_OP(sequence_pool_grad, paddle::lite::operators::SequencePoolGradOp);
REGISTER_LITE_OP(sequence_pool_grad,
paddle::lite::operators::SequencePoolGradOp);
......@@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "lite/kernels/arm/sequence_pool_grad_compute.h"
#include <gtest/gtest.h>
#include "lite/core/op_registry.h"
#include "lite/kernels/arm/sequence_pool_compute.h"
#include "lite/kernels/arm/sequence_pool_grad_compute.h"
namespace paddle {
namespace lite {
......@@ -31,10 +31,8 @@ class SequencePoolGradTester {
public:
explicit SequencePoolGradTester(DDim dims,
std::vector<std::vector<uint64_t>> lod,
std::string pool_type)
: dims_(dims),
lod_(lod),
pool_type_(pool_type) {}
std::string pool_type)
: dims_(dims), lod_(lod), pool_type_(pool_type) {}
void prepare_kernel() {
std::unique_ptr<KernelContext> ctx1(new KernelContext);
......@@ -94,7 +92,7 @@ class SequencePoolGradTester {
x_data[i] = in_vec[i];
}
for (int i = 0; i < out_dims_.production(); i++) {
out_grad_data[i] = out_grad_vec[i];
out_grad_data[i] = out_grad_vec[i];
}
param->X = &x;
param->X_Grad = &x_grad;
......@@ -131,8 +129,7 @@ class SequencePoolGradTester {
x_grad[i] = 1.0;
}
LOG(INFO) << "run_backward:";
this->run_backward(
&grad_param_, &grad_kernel_, x, out_grad, x_grad.data());
this->run_backward(&grad_param_, &grad_kernel_, x, out_grad, x_grad.data());
}
private:
......@@ -160,10 +157,12 @@ void generate_lod(int seq_num,
}
}
void TestSequencePoolGrad(DDim dims, std::vector<std::vector<uint64_t>> lod, std::string pool_type) {
void TestSequencePoolGrad(DDim dims,
std::vector<std::vector<uint64_t>> lod,
std::string pool_type) {
LOG(INFO) << "Test SequencePool grad";
std::unique_ptr<SequencePoolGradTester> tester(new SequencePoolGradTester(
dims, lod, pool_type));
std::unique_ptr<SequencePoolGradTester> tester(
new SequencePoolGradTester(dims, lod, pool_type));
tester->prepare_kernel();
float delta = 0.001;
float max_grad_delta = 0.005;
......@@ -184,19 +183,11 @@ TEST(sequence_pool_grad_host, compute) {
generate_lod(seq_num, max_len, lod[0]);
int64_t n = int64_t(lod[0].back());
LOG(INFO) << "sequence_pool_grad parameter: "
<< ", n = "
<< n
<< ", c = "
<< c
<< ", h = "
<< h
<< ", w = "
<< w
<< ", seq_num = "
<< seq_num
<< ", pool_type = "
<< pool_type;
TestSequencePoolGrad(DDim(std::vector<int64_t>({n, c, h, w})), lod, pool_type);
<< ", n = " << n << ", c = " << c << ", h = " << h
<< ", w = " << w << ", seq_num = " << seq_num
<< ", pool_type = " << pool_type;
TestSequencePoolGrad(D
Dim(std::vector<int64_t>({n, c, h, w})), lod, pool_type);
}
}
}
......@@ -204,7 +195,6 @@ TEST(sequence_pool_grad_host, compute) {
}
}
} // namespace arm
} // namespace kernels
} // namespace lite
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册