sequence_padding_test.cc 4.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yiqun Liu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/math/sequence_padding.h"
W
wanghuancoder 已提交
16

Y
Yiqun Liu 已提交
17 18
#include <gtest/gtest.h>

19 20 21
template <typename DeviceContext, typename T>
void TestSequencePadding(const DeviceContext &context,
                         const paddle::framework::LoD &lod,
Y
Yiqun Liu 已提交
22 23 24 25 26
                         const size_t sequence_width) {
  paddle::framework::LoDTensor cpu_seq;
  paddle::framework::LoDTensor cpu_seq_back;
  paddle::framework::LoDTensor seq;
  paddle::framework::LoDTensor seq_back;
F
fengjiayi 已提交
27
  paddle::framework::LoDTensor padding;
F
fengjiayi 已提交
28 29
  paddle::framework::LoDTensor cpu_pad_value;
  paddle::framework::LoDTensor pad_value;
Y
Yiqun Liu 已提交
30 31 32 33 34 35 36 37

  const size_t level = lod.size() - 1;
  auto seq_dims =
      paddle::framework::make_ddim({static_cast<int64_t>(lod[level].back()),
                                    static_cast<int64_t>(sequence_width)});

  cpu_seq.set_lod(lod);
  cpu_seq.mutable_data<T>(seq_dims, paddle::platform::CPUPlace());
C
chengduoZH 已提交
38
  for (int64_t i = 0; i < cpu_seq.numel(); ++i) {
Y
Yiqun Liu 已提交
39 40 41
    cpu_seq.data<T>()[i] = static_cast<T>(i);
  }

42 43
  auto place = context.GetPlace();
  if (paddle::platform::is_cpu_place(place)) {
Y
Yiqun Liu 已提交
44 45
    seq = cpu_seq;
  } else {
46
    TensorCopySync(cpu_seq, place, &seq);
Y
Yiqun Liu 已提交
47 48 49 50
    seq.set_lod(lod);
  }

  const size_t max_sequence_length =
Y
yangyaming 已提交
51
      paddle::operators::math::MaximumSequenceLength(lod[level]);
Y
Yiqun Liu 已提交
52 53 54 55 56
  const size_t num_sequences = lod[level].size() - 1;
  auto padding_dims =
      paddle::framework::make_ddim({static_cast<int64_t>(max_sequence_length),
                                    static_cast<int64_t>(num_sequences),
                                    static_cast<int64_t>(sequence_width)});
Y
yangyaming 已提交
57

58
  padding.mutable_data<T>(padding_dims, place);
Y
yangyaming 已提交
59

60
  T *pad_value_data =
F
fengjiayi 已提交
61 62
      cpu_pad_value.mutable_data<T>({1}, paddle::platform::CPUPlace());
  *pad_value_data = static_cast<T>(0);
63
  if (paddle::platform::is_cpu_place(place)) {
F
fengjiayi 已提交
64 65
    pad_value = cpu_pad_value;
  } else {
66
    TensorCopySync(cpu_pad_value, place, &pad_value);
F
fengjiayi 已提交
67 68
  }

Y
Yiqun Liu 已提交
69
  paddle::operators::math::PaddingLoDTensorFunctor<DeviceContext, T>()(
70
      context, seq, &padding, pad_value, -1, 0, false,
Y
yangyaming 已提交
71
      paddle::operators::math::kLengthBatchWidth);
Y
Yiqun Liu 已提交
72 73

  seq_back.set_lod(lod);
74
  seq_back.mutable_data<T>(seq_dims, place);
Y
Yiqun Liu 已提交
75
  paddle::operators::math::UnpaddingLoDTensorFunctor<DeviceContext, T>()(
76
      context, padding, &seq_back, -1, 0, false,
Y
yangyaming 已提交
77
      paddle::operators::math::kLengthBatchWidth);
Y
Yiqun Liu 已提交
78

79
  if (paddle::platform::is_cpu_place(place)) {
Y
Yiqun Liu 已提交
80 81
    cpu_seq_back = seq_back;
  } else {
F
fengjiayi 已提交
82
    TensorCopySync(seq_back, paddle::platform::CPUPlace(), &cpu_seq_back);
Y
Yiqun Liu 已提交
83 84 85 86 87
    cpu_seq_back.set_lod(lod);
  }

  EXPECT_EQ(cpu_seq.numel(), cpu_seq_back.numel());
  EXPECT_EQ(cpu_seq.dims(), cpu_seq_back.dims());
C
chengduoZH 已提交
88
  for (int64_t i = 0; i < cpu_seq.numel(); ++i) {
Y
Yiqun Liu 已提交
89 90
    EXPECT_EQ(cpu_seq.data<T>()[i], cpu_seq_back.data<T>()[i]);
  }
91
}
Y
Yiqun Liu 已提交
92 93

TEST(Seq2BatchPadding, CPU) {
94 95 96 97
  auto place = paddle::platform::CPUPlace();
  auto *context = static_cast<paddle::platform::CPUDeviceContext *>(
      paddle::platform::DeviceContextPool::Instance().Get(place));

Y
Yiqun Liu 已提交
98 99
  paddle::framework::LoD lod1;
  lod1.push_back(std::vector<size_t>{0, 10});
100 101
  TestSequencePadding<paddle::platform::CPUDeviceContext, float>(*context, lod1,
                                                                 16);
Y
Yiqun Liu 已提交
102 103 104

  paddle::framework::LoD lod2;
  lod2.push_back(std::vector<size_t>{0, 2, 7, 10});
105 106
  TestSequencePadding<paddle::platform::CPUDeviceContext, float>(*context, lod2,
                                                                 128);
Y
Yiqun Liu 已提交
107 108 109 110
}

#ifdef PADDLE_WITH_CUDA
TEST(SequencePadding, CUDA) {
111 112 113 114
  auto place = paddle::platform::CUDAPlace(0);
  auto *context = static_cast<paddle::platform::CUDADeviceContext *>(
      paddle::platform::DeviceContextPool::Instance().Get(place));

Y
Yiqun Liu 已提交
115 116
  paddle::framework::LoD lod1;
  lod1.push_back(std::vector<size_t>{0, 10});
117 118
  TestSequencePadding<paddle::platform::CUDADeviceContext, float>(*context,
                                                                  lod1, 16);
Y
Yiqun Liu 已提交
119 120 121

  paddle::framework::LoD lod2;
  lod2.push_back(std::vector<size_t>{0, 2, 7, 10});
122 123
  TestSequencePadding<paddle::platform::CUDADeviceContext, float>(*context,
                                                                  lod2, 128);
Y
Yiqun Liu 已提交
124 125
}
#endif