/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. 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. */ #pragma once #include #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" namespace paddle { namespace operators { template class EditDistanceKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const { auto* out_t = ctx.Output("Out"); auto* x1_t = ctx.Input("Hyps"); auto* x2_t = ctx.Input("Refs"); auto normalized = ctx.Attr("normalized"); auto hyp_lod = x1_t->lod()[0]; auto ref_lod = x2_t->lod()[0]; PADDLE_ENFORCE( hyp_lod.size() == ref_lod.size(), "Input(Hyps) and Input(Refs) must have the same batch size."); for (size_t i = 1; i < ref_lod.size(); ++i) { PADDLE_ENFORCE(ref_lod[i] > ref_lod[i - 1], "Reference string %d is empty.", i); } auto num_strs = hyp_lod.size() - 1; out_t->Resize({static_cast(num_strs), 1}); out_t->mutable_data(ctx.GetPlace()); auto out = out_t->data(); std::vector distance(num_strs, 0.0); for (size_t num = 0; num < num_strs; ++num) { auto m = static_cast(hyp_lod[num + 1] - hyp_lod[num]); auto n = static_cast(ref_lod[num + 1] - ref_lod[num]); if (m == 0) { distance[num] = n; } else if (n == 0) { distance[num] = m; } else { framework::Tensor dist_t; dist_t.Resize({m + 1, n + 1}); dist_t.mutable_data(ctx.GetPlace()); auto dist = dist_t.data(); auto x1 = x1_t->data() + hyp_lod[num]; auto x2 = x2_t->data() + ref_lod[num]; for (int64_t i = 0; i < m + 1; ++i) { dist[i * (n + 1)] = i; } for (int64_t j = 0; j < n + 1; ++j) { dist[j] = j; } for (int64_t i = 1; i < m + 1; ++i) { for (int64_t j = 1; j < n + 1; ++j) { int cost = x1[i - 1] == x2[j - 1] ? 0 : 1; int dels = dist[(i - 1) * (n + 1) + j] + 1; int ins = dist[i * (n + 1) + (j - 1)] + 1; int subs = dist[(i - 1) * (n + 1) + (j - 1)] + cost; dist[i * (n + 1) + j] = std::min(dels, std::min(ins, subs)); } } distance[num] = dist[m * (n + 1) + n]; } if (normalized) { PADDLE_ENFORCE(n > 0, "The reference string (#%d) cannot be empty " "when Attr(normalized) is enabled.", n); distance[num] = distance[num] / n; } out[num] = distance[num]; } } }; } // namespace operators } // namespace paddle