beam_search_op.cc 6.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yan Chunwei 已提交
2

L
Luo Tao 已提交
3 4 5
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
Y
Yan Chunwei 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yan Chunwei 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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
Yan Chunwei 已提交
14

15 16
#include "paddle/fluid/operators/beam_search_op.h"

17 18
#include <string>
#include <vector>
19

Y
Yi Wang 已提交
20
#include "paddle/fluid/framework/op_registry.h"
Y
Yan Chunwei 已提交
21 22 23 24

namespace paddle {
namespace operators {

K
ktlichkid 已提交
25
class BeamSearchOpMaker : public framework::OpProtoAndCheckerMaker {
Y
Yan Chunwei 已提交
26
 public:
Y
Yu Yang 已提交
27
  void Make() override {
Y
Yan Chunwei 已提交
28
    // inputs and outputs stored in proto
29 30 31 32
    AddInput("pre_ids",
             "(LoDTensor) The LoDTensor containing the selected ids at the "
             "previous step. It should be a tensor with shape (batch_size, 1) "
             "and lod `[[0, 1, ... , batch_size], [0, 1, ..., batch_size]]` at "
33
             "the first step.");
34 35 36 37 38
    AddInput("pre_scores",
             "(LoDTensor) The LoDTensor containing the accumulated "
             "scores corresponding to the selected ids at the previous step.");
    AddInput("ids",
             "(LoDTensor) The LoDTensor containing the candidates ids. Its "
39 40 41
             "shape should be (batch_size * beam_size, W). If not set, it will "
             "be calculated out according to Input(scores) in this operator.")
        .AsDispensable();
Y
Yan Chunwei 已提交
42
    AddInput("scores",
43 44 45 46 47 48 49
             "(LoDTensor) The LoDTensor containing the current scores "
             "corresponding to Input(ids). If Input(ids) is not nullptr, its "
             "shape is the same as that of Input(ids)."
             "If is_accumulated is true, Input(scores) is accumulated scores "
             "and will be used derectedly. Else, each score will be "
             "transformed to the log field and accumulate Input(pre_sores) "
             "first.");
Y
Yan Chunwei 已提交
50
    AddOutput("selected_ids",
51 52 53 54
              "A LodTensor that stores the IDs selected by beam search.");
    AddOutput("selected_scores",
              "A LoDTensor containing the accumulated scores corresponding to "
              "Output(selected_ids).");
55
    AddOutput("parent_idx",
T
tianshuo78520a 已提交
56
              "A Tensor preserving the selected_ids' parent index in pre_ids.")
57
        .AsDispensable();
Y
Yan Chunwei 已提交
58 59 60 61 62 63

    // Attributes stored in AttributeMap
    AddAttr<int>("level", "the level of LoDTensor");
    AddAttr<int>("beam_size", "beam size for beam search");
    AddAttr<int>("end_id",
                 "the token id which indicates the end of a sequence");
64 65 66
    AddAttr<bool>("is_accumulated",
                  "Whether the Input(scores) is accumulated scores.")
        .SetDefault(true);
Y
Yan Chunwei 已提交
67

68
    AddComment(R"DOC(
M
minqiyang 已提交
69
This operator does the search in beams for one time step.
70 71 72 73 74 75 76 77 78 79 80 81 82
Specifically, it selects the top-K candidate word ids of current step from
Input(ids) according to their Input(scores) for all source sentences,
where K is Attr(beam_size) and Input(ids), Input(scores) are predicted results
from the computation cell. Additionally, Input(pre_ids) and Input(pre_scores)
are the output of beam_search at previous step, they are needed for special use
to handle ended candidate translations. The paths linking prefixes and selected
candidates are organized and reserved in lod.

Note that the Input(scores) passed in should be accumulated scores, and
length penalty should be done with extra operators before calculating the
accumulated scores if needed, also suggest finding top-K before it and
using the top-K candidates following.
)DOC");
Y
Yan Chunwei 已提交
83 84 85
  }
};

K
ktlichkid 已提交
86
class BeamSearchOp : public framework::OperatorWithKernel {
K
ktlichkid 已提交
87 88
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
K
ktlichkid 已提交
89

K
ktlichkid 已提交
90
  void InferShape(framework::InferShapeContext *ctx) const override {
K
ktlichkid 已提交
91
    for (const std::string &arg :
92
         std::vector<std::string>({"pre_ids", "scores"})) {
93
      OP_INOUT_CHECK(ctx->HasInput(arg), "Input", arg, "BeamSeach");
K
ktlichkid 已提交
94 95 96
    }
    for (const std::string &arg :
         std::vector<std::string>({"selected_ids", "selected_scores"})) {
97
      OP_INOUT_CHECK(ctx->HasOutput(arg), "Output", arg, "BeamSeach");
K
ktlichkid 已提交
98
    }
99 100 101 102
    auto id_dims = ctx->GetInputDim("pre_ids");
    ctx->SetOutputDim("selected_scores", ctx->GetInputDim("pre_scores"));
    ctx->SetOutputDim("selected_ids", id_dims);
    ctx->SetOutputDim("parent_idx", {id_dims[0]});
103 104
  }

105
 protected:
106 107
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
108 109 110 111 112 113 114
    auto *scores = ctx.Input<framework::LoDTensor>("scores");
    size_t level = ctx.Attr<int>("level");
    size_t batch_size = scores->lod()[level].size() - 1;
    // The current CUDA kernel only support cases with batch_size < 4.
    // Compute on CPU for cases with batch_size > 4.
    if (batch_size <= 4) {
      return framework::OpKernelType(
115 116
          OperatorWithKernel::IndicateVarDataType(ctx, "pre_ids"),
          ctx.GetPlace());
117 118
    } else {
      return framework::OpKernelType(
119
          OperatorWithKernel::IndicateVarDataType(ctx, "pre_ids"),
120 121
          platform::CPUPlace());
    }
K
ktlichkid 已提交
122 123 124
  }
};

Q
Qiao Longfei 已提交
125 126
class BeamSearchInferVarType : public framework::VarTypeInference {
 public:
M
minqiyang 已提交
127
  void operator()(framework::InferVarTypeContext *ctx) const override {
128 129
    ctx->SetOutputType("selected_ids",
                       framework::proto::VarType::LOD_TENSOR,
130
                       framework::ALL_ELEMENTS);
131 132
    ctx->SetOutputType("selected_scores",
                       framework::proto::VarType::LOD_TENSOR,
133
                       framework::ALL_ELEMENTS);
Q
Qiao Longfei 已提交
134 135
  }
};
K
ktlichkid 已提交
136

Y
Yan Chunwei 已提交
137 138
}  // namespace operators
}  // namespace paddle
K
ktlichkid 已提交
139

K
ktlichkid 已提交
140
namespace ops = paddle::operators;
K
ktlichkid 已提交
141

142 143 144
REGISTER_OPERATOR(beam_search,
                  ops::BeamSearchOp,
                  ops::BeamSearchOpMaker,
K
ktlichkid 已提交
145
                  ops::BeamSearchInferVarType);
L
Leo Chen 已提交
146 147 148 149 150
REGISTER_OP_CPU_KERNEL(beam_search,
                       ops::BeamSearchOpKernel<phi::CPUContext, float>,
                       ops::BeamSearchOpKernel<phi::CPUContext, double>,
                       ops::BeamSearchOpKernel<phi::CPUContext, int>,
                       ops::BeamSearchOpKernel<phi::CPUContext, int64_t>);