sequence_pooling_test.cc 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/operators/math/sequence_pooling.h"
#include <gtest/gtest.h>
#include <vector>

template <typename DeviceContext, typename Place, typename T>
void TestSequencePoolingSum(const paddle::framework::LoD& lod) {
  paddle::framework::LoDTensor cpu_out_grad;
  paddle::framework::LoDTensor out_grad;
  paddle::framework::LoDTensor in_grad;
  const size_t second_dim = 128u;

  // construct out_grad's tensor in cpu
  const size_t out_first_dim = lod.size() - 1;
  auto out_dims = paddle::framework::make_ddim(
      {static_cast<int64_t>(out_first_dim), static_cast<int64_t>(second_dim)});

  cpu_out_grad.mutable_data<T>(out_dims, paddle::platform::CPUPlace());
  for (int64_t i = 0; i < out_grad.numel(); ++i) {
    cpu_out_grad.data<T>()[i] = static_cast<T>(i);
  }

  // copy to dst out_grad
  auto* place = new Place();
  DeviceContext* context = new DeviceContext(*place);
  if (paddle::platform::is_cpu_place(*place)) {
    out_grad = cpu_out_grad;
  } else {
    TensorCopySync(cpu_out_grad, *place, &out_grad);
  }

  // construct in_grad
  in_grad.set_lod(lod);
  auto in_dims = paddle::framework::make_ddim(
      {static_cast<int64_t>(lod[0].back()), static_cast<int64_t>(second_dim)});
M
minqiyang 已提交
49
  in_grad.mutable_data<T>(in_dims, context->GetPlace());
50 51 52 53 54 55 56 57 58

  // check tensor contruction result
  PADDLE_ENFORCE_EQ(in_grad.dims().size(), out_grad.dims().size());
  for (int64_t i = 1; i < out_grad.dims().size(); ++i) {
    PADDLE_ENFORCE_EQ(in_grad.dims()[i], out_grad.dims()[i]);
  }

  // call functor
  paddle::operators::math::SequencePoolGradFunctor<DeviceContext, T>()(
M
minqiyang 已提交
59
      *context, "SUM", out_grad, &in_grad);
60

M
minqiyang 已提交
61
  EXPECT_EQ(in_grad.numel(), lod[0].back() * second_dim);
62
  EXPECT_EQ(in_grad.lod(), lod);
M
minqiyang 已提交
63 64 65 66 67
  for (int64_t i = 0; i < in_grad.lod()[0].size() - 1; ++i) {
    int64_t begin = in_grad.lod()[0][i];
    int64_t end = in_grad.lod()[0][i + 1];
    paddle::framework::Tensor tmp = in_grad.Slice(begin, end);
    for (int64_t j = 0; j != tmp.numel() / second_dim; ++j) {
68 69 70 71 72 73 74 75 76 77 78 79 80
      for (int64_t m = 0; m != second_dim; ++m) {
        EXPECT_EQ(tmp.data<T>()[m + j * second_dim],
                  out_grad.data<T>()[m + i * second_dim]);
      }
    }
  }

  delete place;
  delete context;
}

TEST(SequencePoolingGrad, CPU_SUM) {
  paddle::framework::LoD lod1;
M
minqiyang 已提交
81
  lod1.push_back(std::vector<size_t>{0, 10});
82
  TestSequencePoolingSum<paddle::platform::CPUDeviceContext,
M
minqiyang 已提交
83
                         paddle::platform::CPUPlace, float>(lod1);
84 85 86 87

  paddle::framework::LoD lod2;
  lod2.push_back(std::vector<size_t>{0, 2, 7, 10});
  TestSequencePoolingSum<paddle::platform::CPUDeviceContext,
M
minqiyang 已提交
88
                         paddle::platform::CPUPlace, float>(lod2);
89 90 91 92 93 94 95
}

#ifdef PADDLE_WITH_CUDA
TEST(SequencePoolingGrad, CUDA_SUM) {
  paddle::framework::LoD lod1;
  lod1.push_back(std::vector<size_t>{0, 10});
  TestSequencePoolingSum<paddle::platform::CUDADeviceContext,
M
minqiyang 已提交
96
                         paddle::platform::CUDAPlace, float>(lod1);
97 98 99 100

  paddle::framework::LoD lod2;
  lod2.push_back(std::vector<size_t>{0, 2, 7, 10});
  TestSequencePoolingSum<paddle::platform::CUDADeviceContext,
M
minqiyang 已提交
101
                         paddle::platform::CUDAPlace, float>(lod2);
102 103
}
#endif