beam_search_op_test.cc 2.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yan Chunwei 已提交
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/beam_search_op.h"
Y
Yan Chunwei 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

#include <gtest/gtest.h>
#include <vector>

namespace paddle {
namespace test {

using std::vector;
using framework::LoDTensor;
using framework::LoD;
using operators::BeamSearch;
using paddle::platform::CPUPlace;
using std::cout;
using std::endl;

void CreateInput(LoDTensor* ids, LoDTensor* scores) {
  LoD lod;
P
peizhilin 已提交
33 34
  vector<size_t> level0({0, 2, 4});
  vector<size_t> level1({0, 1, 2, 3, 4});
Y
Yan Chunwei 已提交
35 36 37 38 39
  lod.push_back(level0);
  lod.push_back(level1);
  ids->set_lod(lod);
  scores->set_lod(lod);

P
peizhilin 已提交
40
  auto dims = framework::make_ddim(vector<int64_t>({4, 3}));
Y
Yan Chunwei 已提交
41 42 43 44 45 46
  ids->Resize(dims);
  scores->Resize(dims);
  CPUPlace place;

  auto* ids_data = ids->mutable_data<int64_t>(place);
  auto* scores_data = scores->mutable_data<float>(place);
P
peizhilin 已提交
47 48 49
  vector<int64_t> _ids({4, 2, 5, 2, 1, 3, 3, 5, 2, 8, 2, 1});
  vector<float> _scores({0.5f, 0.3f, 0.2f, 0.6f, 0.3f, 0.1f,
                        0.9f, 0.5f, 0.1f, 0.7f, 0.5f, 0.1f});
Y
Yan Chunwei 已提交
50 51 52 53 54 55 56

  for (int i = 0; i < 12; i++) {
    ids_data[i] = _ids[i];
    scores_data[i] = _scores[i];
  }
}

Y
Refine  
Yu Yang 已提交
57 58
// It seems that beam_search_op has bugs.
TEST(DISABLED_beam_search_op, run) {
Y
Yan Chunwei 已提交
59 60 61 62 63 64 65 66 67
  CPUPlace place;
  LoDTensor ids, scores;
  CreateInput(&ids, &scores);

  LoDTensor pre_ids;
  pre_ids.Resize(framework::make_ddim(vector<int64_t>(4, 1)));
  for (int i = 0; i < 4; i++) {
    pre_ids.mutable_data<int64_t>(place)[i] = i + 1;
  }
68 69 70
  LoDTensor pre_scores;
  pre_scores.Resize(framework::make_ddim(vector<int64_t>(4, 1)));
  for (int i = 0; i < 4; i++) {
71
    pre_scores.mutable_data<float>(place)[i] = 0.1 * (i + 1);
72
  }
Y
Yan Chunwei 已提交
73

74
  BeamSearch beamsearch(ids, scores, (size_t)0, (size_t)2, 0);
Y
Yan Chunwei 已提交
75
  LoDTensor sids, sscores;
76
  beamsearch(pre_ids, pre_scores, &sids, &sscores);
Y
Yan Chunwei 已提交
77 78 79 80 81

  LOG(INFO) << "score: " << sscores << endl;

  ASSERT_EQ(sids.lod(), sscores.lod());

P
peizhilin 已提交
82 83
  vector<int> tids({4, 2, 3, 8});
  vector<float> tscores({0.5f, 0.6f, 0.9f, 0.7f});
Y
Yan Chunwei 已提交
84 85 86 87 88 89 90 91 92

  for (int i = 0; i < 4; i++) {
    ASSERT_EQ(tids[i], sids.data<int64_t>()[i]);
    ASSERT_EQ(tscores[i], sscores.data<float>()[i]);
  }
}

}  // namespace test
}  // namespace paddle