ctc_align_op.cc 4.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
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/ctc_align_op.h"
W
wanghaoshuang 已提交
16 17 18 19

namespace paddle {
namespace operators {

W
wanghaoshuang 已提交
20
class CTCAlignOp : public framework::OperatorWithKernel {
W
wanghaoshuang 已提交
21 22 23 24
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
25 26 27 28
    PADDLE_ENFORCE_EQ(ctx->HasInput("Input"), true,
                      "Input of CTCAlignOp should not be null.");
    PADDLE_ENFORCE_EQ(ctx->HasOutput("Output"), true,
                      "Output of CTCAlignOp should not be null.");
W
wanghaoshuang 已提交
29 30 31 32

    auto input_dims = ctx->GetInputDim("Input");

    // TODO(wanghaoshuang): it is tricky to set the wrong dimension here.
33
    ctx->SetOutputDim("Output", input_dims);
34 35 36
    if (ctx->HasInput("InputLength")) {
      ctx->SetOutputDim("OutputLength", {input_dims[0], 1});
    }
W
wanghaoshuang 已提交
37 38 39 40 41
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
42 43 44
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Input"),
        ctx.device_context());
W
wanghaoshuang 已提交
45 46 47
  }
};

W
wanghaoshuang 已提交
48
class CTCAlignOpMaker : public framework::OpProtoAndCheckerMaker {
W
wanghaoshuang 已提交
49
 public:
Y
Yu Yang 已提交
50
  void Make() override {
W
wanghaoshuang 已提交
51
    AddInput("Input",
52
             "2-D Tensor or LodTensor with  shape "
53
             "[Lp, 1], where Lp is the sum of all input sequences' length.");
54 55 56 57 58
    AddInput("InputLength",
             "2-D Tensor with shape [batch_size, 1], "
             " When Input is padding mode, InputLength is length of every "
             "sequence in Input.")
        .AsDispensable();
W
wanghaoshuang 已提交
59
    AddOutput("Output", "(Tensor, default: Tensor<int>), The align result.");
60 61 62 63 64
    AddOutput("OutputLength",
              "2-D Tensor with shape [batch_size, 1], "
              "When Input is padding mode, OutputLength is length of every "
              "sequence in Output.")
        .AsDispensable();
W
wanghaoshuang 已提交
65
    AddAttr<int>("blank",
T
tianshuo78520a 已提交
66
                 "(int, default: 0), the blank label set in Connectionist "
67
                 "Temporal Classification (CTC) op.")
W
wanghaoshuang 已提交
68 69 70 71 72
        .SetDefault(0);
    AddAttr<bool>("merge_repeated",
                  "(bool, default: true), whether to "
                  "merge repeated elements between two blanks. ")
        .SetDefault(true);
73
    // add attr padding number for tensor input
74
    AddAttr<int>("padding_value",
75 76 77
                 "(int, default: 0), padding number "
                 "use to padding tensor. ")
        .SetDefault(0);
W
wanghaoshuang 已提交
78
    AddComment(R"DOC(
W
wanghaoshuang 已提交
79
CTCAlign op is used to merge repeated elements between two blanks
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
and then delete all blanks in sequence.

Given:
    Input.data = [0, 1, 2, 2, 0, 4, 0, 4, 5, 0, 6,
                  6, 0, 0, 7, 7, 7, 0]
    Input.dims = {18, 1}
    Input.LoD = [[0, 11, 18]]

And:
    blank = 0
    merge_repeated = True

Then:
    Output.data = [1, 2, 4, 4, 5, 6,
                   6, 7]
    Output.dims = {8, 1}
    Output.LoD = [[0, 6, 8]]
97 98 99
or Given:
    Input.data = [[0, 1, 2, 2, 0, 4], 
                  [0, 4, 5, 0, 6, 0], 
100 101 102 103
                  [0, 7, 7, 7, 0, 0]]
    InputLength.data  = [[6],
                         [5],
                         [4]],   
104 105 106 107 108
    Input.dims = {3, 6},
    Input.Lod = []
And:
    blank = 0
    merge_repeated = True
109
    padding_value = 0
110

111 112 113
Then:
    Output.data = [[1, 2, 4, 0, 0, 0],
                   [4, 5, 6, 0, 0, 0],
114 115 116 117
                   [7, 0, 0, 0, 0, 0]],
    OutputLength.data = [[3],
                         [3],
                         [1]],
118 119
    Output.dims = {3, 6},
    Output.Lod = []
W
wanghaoshuang 已提交
120 121 122 123 124 125 126 127
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
128 129 130 131
REGISTER_OPERATOR(
    ctc_align, ops::CTCAlignOp, ops::CTCAlignOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
W
wanghaoshuang 已提交
132
REGISTER_OP_CPU_KERNEL(
W
wanghaoshuang 已提交
133 134
    ctc_align, ops::CTCAlignKernel<paddle::platform::CPUDeviceContext, int>,
    ops::CTCAlignKernel<paddle::platform::CPUDeviceContext, int64_t>);