edit_distance_op.h 4.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yibing Liu 已提交
2

Y
Yibing Liu 已提交
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
Yibing Liu 已提交
6

Y
Yibing Liu 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yibing Liu 已提交
8

Y
Yibing Liu 已提交
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
Yibing Liu 已提交
14 15 16

#pragma once
#include <algorithm>
Y
Yi Wang 已提交
17
#include "paddle/fluid/framework/eigen.h"
18
#include "paddle/fluid/framework/mixed_vector.h"
Y
Yi Wang 已提交
19
#include "paddle/fluid/framework/op_registry.h"
Y
Yibing Liu 已提交
20 21 22 23
namespace paddle {
namespace operators {

template <typename Place, typename T>
24
class EditDistanceKernel : public framework::OpKernel<T> {
Y
Yibing Liu 已提交
25 26 27 28
 public:
  void Compute(const framework::ExecutionContext& ctx) const {
    auto* out_t = ctx.Output<framework::Tensor>("Out");

29 30
    auto* x1_t = ctx.Input<framework::LoDTensor>("Hyps");
    auto* x2_t = ctx.Input<framework::LoDTensor>("Refs");
31 32
    auto* sequence_num = ctx.Output<framework::Tensor>("SequenceNum");
    int64_t* seq_num_data = sequence_num->mutable_data<int64_t>(ctx.GetPlace());
33
    auto batch_size = x1_t->dims()[0];
Y
Yibing Liu 已提交
34

35 36
    auto normalized = ctx.Attr<bool>("normalized");

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    framework::Vector<size_t> hyp_lod(batch_size + 1);
    framework::Vector<size_t> ref_lod(batch_size + 1);

    bool use_length = ctx.HasInput("HypsLength");

    if (use_length) {
      // build lod when using padding
      auto hyp_length_ptr =
          ctx.Input<framework::Tensor>("HypsLength")->data<int64_t>();
      auto ref_length_ptr =
          ctx.Input<framework::Tensor>("RefsLength")->data<int64_t>();

      for (auto i = 0; i < batch_size; i++) {
        hyp_lod[i + 1] = hyp_lod[i] + hyp_length_ptr[i];
        ref_lod[i + 1] = ref_lod[i] + ref_length_ptr[i];
      }

    } else {
      hyp_lod = x1_t->lod()[0];
      ref_lod = x2_t->lod()[0];
    }

    if (normalized) {
      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);
      }
64 65
    }
    auto num_strs = hyp_lod.size() - 1;
66
    *seq_num_data = static_cast<int64_t>(num_strs);
67 68

    out_t->Resize({static_cast<int64_t>(num_strs), 1});
Y
Yibing Liu 已提交
69
    out_t->mutable_data<float>(ctx.GetPlace());
70
    auto out = out_t->data<T>();
Y
Yibing Liu 已提交
71

72
    T distance = 0.0;
73 74 75
    for (size_t num = 0; num < num_strs; ++num) {
      auto m = static_cast<int64_t>(hyp_lod[num + 1] - hyp_lod[num]);
      auto n = static_cast<int64_t>(ref_lod[num + 1] - ref_lod[num]);
Y
Yibing Liu 已提交
76

77
      if (m == 0) {
78
        distance = n;
79
      } else if (n == 0) {
80
        distance = m;
81 82 83 84 85
      } else {
        framework::Tensor dist_t;
        dist_t.Resize({m + 1, n + 1});
        dist_t.mutable_data<T>(ctx.GetPlace());
        auto dist = dist_t.data<T>();
86 87 88 89
        auto hyp_offset = use_length ? num * x1_t->dims()[1] : hyp_lod[num];
        auto ref_offset = use_length ? num * x2_t->dims()[1] : ref_lod[num];
        auto x1 = x1_t->data<int64_t>() + hyp_offset;
        auto x2 = x2_t->data<int64_t>() + ref_offset;
90 91 92 93 94 95 96 97 98 99 100 101 102 103
        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));
          }
Y
Yibing Liu 已提交
104
        }
105
        distance = dist[m * (n + 1) + n];
Y
Yibing Liu 已提交
106 107
      }

108 109 110 111 112
      if (normalized) {
        PADDLE_ENFORCE(n > 0,
                       "The reference string (#%d) cannot be empty "
                       "when Attr(normalized) is enabled.",
                       n);
113
        distance = distance / n;
114
      }
115
      out[num] = distance;
Y
Yibing Liu 已提交
116 117 118 119 120 121
    }
  }
};

}  // namespace operators
}  // namespace paddle