/* Copyright (c) 2016 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/beam_search_op.h" #include #include #include #include #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/op_registry.h" namespace paddle { namespace operators { void BeamSearch::operator()(const framework::LoDTensor &pre_ids, framework::LoDTensor *selected_ids, framework::LoDTensor *selected_scores) { auto abs_lod = framework::ToAbsOffset(ids_->lod()); auto &high_level = abs_lod[lod_level_]; auto items = SelectTopBeamSizeItems(); auto selected_items = ToMap(items, high_level.back()); VLOG(3) << "selected_items:"; for (size_t i = 0; i < selected_items.size(); ++i) { VLOG(3) << "offset:" << i; for (auto &item : selected_items[i]) { VLOG(3) << ItemToString(item); } } PruneEndidCandidates(pre_ids, &selected_items); // calculate the output tensor's height size_t num_instances = std::accumulate( std::begin(selected_items), std::end(selected_items), 0, [](size_t a, std::vector &b) { return a + b.size(); }); // the output tensor shape should be [num_instances, 1] auto dims = framework::make_ddim( std::vector({static_cast(num_instances), 1})); selected_ids->Resize(dims); selected_scores->Resize(dims); std::map> hash; framework::LoD new_lod; auto *ids_data = selected_ids->mutable_data(platform::CPUPlace()); auto *scores_data = selected_scores->mutable_data(platform::CPUPlace()); // fill in data std::vector low_level; size_t low_offset = 0; for (auto &items : selected_items) { low_level.push_back(low_offset); sort(items.begin(), items.end(), [](const Item &a, const Item &b) { if (a.offset < b.offset) { return true; } return a.id < b.id; }); for (auto &item : items) { ids_data[low_offset] = item.id; scores_data[low_offset] = item.score; low_offset++; } } low_level.push_back(low_offset); // fill lod framework::LoD lod(2); lod[0].assign(high_level.begin(), high_level.end()); lod[1].assign(low_level.begin(), low_level.end()); if (!framework::CheckLoD(lod)) { PADDLE_THROW("lod %s is not right", framework::LoDToString(lod)); } selected_ids->set_lod(lod); selected_scores->set_lod(lod); } int BeamSearch::PruneEndidCandidates(const framework::LoDTensor &pre_ids, std::vector> *items) { auto *pre_ids_data = pre_ids.data(); int res = 0; for (size_t offset = 0; offset < items->size(); offset++) { auto prefix_id = pre_ids_data[offset]; if (prefix_id == end_id_) { items->at(offset).clear(); } else { res++; } } return res; } std::vector> BeamSearch::ToMap( const std::vector> &items, size_t element_num) { std::vector> result; result.resize(element_num); for (auto &entries : items) { for (const auto &item : entries) { result[item.offset].push_back(item); } } return result; } std::vector> BeamSearch::SelectTopBeamSizeItems() { std::vector> result; std::vector items; // for each source sentence, select the top beam_size items across all // candidate sets. while (NextItemSet(&items)) { std::nth_element(std::begin(items), std::begin(items) + beam_size_, std::end(items), [](const Item &a, const Item &b) { // TODO(superjom) make score's comparation customizable. // partial sort in descending order return a.score > b.score; }); // prune the top beam_size items. if (items.size() > beam_size_) { items.resize(beam_size_); } result.emplace_back(items); } VLOG(3) << "SelectTopBeamSizeItems result size " << result.size(); for (auto &items : result) { VLOG(3) << "item set:"; for (auto &item : items) { VLOG(3) << ItemToString(item); } } return result; } // the candidates of a source bool BeamSearch::NextItemSet(std::vector *items) { if (sent_offset_ >= ids_->NumElements(lod_level_)) { return false; } // find the current candidates auto ids = *ids_; auto scores = *scores_; auto abs_lod = framework::ToAbsOffset(ids.lod()); auto *ids_data = ids.data(); auto *scores_data = scores.data(); size_t instance_dim = 1; for (int i = 1; i < ids.dims().size(); i++) { instance_dim *= ids.dims()[i]; } items->clear(); items->reserve(framework::product(ids.dims())); for (size_t offset = abs_lod[lod_level_][sent_offset_]; offset < abs_lod[lod_level_][sent_offset_ + 1]; offset++) { for (size_t d = 0; d < instance_dim; d++) { const size_t dim_offset = offset * instance_dim + d; items->emplace_back(offset, ids_data[dim_offset], scores_data[dim_offset]); } } sent_offset_++; return true; } std::ostream &operator<<(std::ostream &os, const BeamSearch::Item &item) { os << "{"; os << "offset: " << item.offset << ", "; os << "id: " << item.id << ", "; os << "score: " << item.score << ""; os << "}"; return os; } std::string ItemToString(const BeamSearch::Item &item) { std::ostringstream stream; stream << item; return stream.str(); } class BeamSearchOpMaker : public framework::OpProtoAndCheckerMaker { public: BeamSearchOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { // inputs and outputs stored in proto AddInput("pre_ids", "ids in previous step"); AddInput("ids", "a LoDTensor of shape of [None,k]"); AddInput("scores", "a LoDTensor that has the same shape and LoD with `ids`"); AddOutput("selected_ids", "a LoDTensor that stores the IDs selected by beam search"); AddOutput( "selected_scores", "a LoDTensor that has the same shape and LoD with `selected_ids`"); // Attributes stored in AttributeMap AddAttr("level", "the level of LoDTensor"); AddAttr("beam_size", "beam size for beam search"); AddAttr("end_id", "the token id which indicates the end of a sequence"); AddComment( "This is a beam search operator that help to generate sequences."); } }; class BeamSearchOp : public framework::OperatorWithKernel { public: BeamSearchOp(const std::string& type, const framework::VariableNameMap& inputs, const framework::VariableNameMap& outputs, const framework::AttributeMap& attrs) : OperatorBase(type, inputs, outputs, attrs) {} BeamSearchOp(const BeamSearchOp& o) : framework::OperatorBase( static_cast(o)) { PADDLE_THROW("Not Implemented"); } protected: void InferShape(const framework::InferShapeContext &ctx) const override { for (const std::string &arg : std::vector({"pre_ids", "ids", "scores"})) { PADDLE_ENFORCE(context->HasInput(arg), "BeamSearch need input argument '%s'", arg); } for (const std::string &arg : std::vector({"selected_ids", "selected_scores"})) { PADDLE_ENFORCE(context->HasOutput(arg), "BeamSearch need output argument '%s'", arg); } } private: void RunImpl(const framework::Scope& scope, const platform::Place& dev_place) const override { auto ids_var = scope.FindVar(Input("ids")); auto scores_var = scope.FindVar(Input("scores")); auto pre_ids_var = scope.FindVar(Input("pre_ids")); PADDLE_ENFORCE_NOT_NULL(ids_var); PADDLE_ENFORCE_NOT_NULL(scores_var); PADDLE_ENFORCE_NOT_NULL(pre_ids_var); auto& ids = ids_var->Get(); auto& scores = scores_var->Get(); auto& pre_ids = pre_ids_var->Get(); size_t level = Attr("level"); size_t beam_size = Attr("beam_size"); int end_id = Attr("end_id"); BeamSearch alg(ids, scores, level, beam_size, end_id); auto selected_ids_var = scope.FindVar(Output("selected_ids")); auto selected_scores_var = scope.FindVar(Output("selected_scores")); PADDLE_ENFORCE_NOT_NULL(selected_ids_var); PADDLE_ENFORCE_NOT_NULL(selected_scores_var); auto& selected_ids_tensor = *selected_ids_var->GetMutable(); auto& selected_scores_tensor = *selected_scores_var->GetMutable(); alg(pre_ids, &selected_ids_tensor, &selected_scores_tensor); } public: using framework::OperatorWithKernel::OperatorWithKernel; }; /* class BeamSearchInferShape : public framework::InferShapeBase { public: void operator()(framework::InferShapeContext *context) const override { for (const std::string &arg : std::vector({"pre_ids", "ids", "scores"})) { PADDLE_ENFORCE(context->HasInput(arg), "BeamSearch need input argument '%s'", arg); } for (const std::string &arg : std::vector({"selected_ids", "selected_scores"})) { PADDLE_ENFORCE(context->HasOutput(arg), "BeamSearch need output argument '%s'", arg); } } }; class BeamSearchInferVarType : public framework::VarTypeInference { public: void operator()(const framework::OpDesc &op_desc, framework::BlockDesc *block) const override { for (auto &o : op_desc.Output("selected_ids")) { block->Var(o)->SetType(framework::proto::VarType::LOD_TENSOR); } for (auto &o : op_desc.Output("selected_scores")) { block->Var(o)->SetType(framework::proto::VarType::LOD_TENSOR); } } }; */ } // namespace operators } // namespace paddle REGISTER_OPERATOR(beam_search, paddle::operators::BeamSearchOp, paddle::operators::BeamSearchProtoAndCheckerMaker, paddle::operators::BeamSearchInferShape, paddle::operators::BeamSearchInferVarType, paddle::framework::EmptyGradOpMaker);