warpctc_op.cc 8.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yiqun Liu 已提交
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/warpctc_op.h"
Y
Yiqun Liu 已提交
16

H
Huihuang Zheng 已提交
17 18
#include <memory>

19 20 21
#ifdef PADDLE_WITH_HIP
#include "paddle/fluid/platform/miopen_helper.h"
#endif
W
Wu Yi 已提交
22 23 24 25
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/platform/cudnn_helper.h"
#endif

Y
Yiqun Liu 已提交
26 27 28 29 30 31 32 33
namespace paddle {
namespace operators {

class WarpCTCOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
34 35 36 37 38
    OP_INOUT_CHECK(ctx->HasInput("Logits"), "Input", "Logits", "WarpCTC");
    OP_INOUT_CHECK(ctx->HasInput("Label"), "Input", "Label", "WarpCTC");
    OP_INOUT_CHECK(ctx->HasOutput("WarpCTCGrad"), "Output", "WarpCTCGrad",
                   "WarpCTC");
    OP_INOUT_CHECK(ctx->HasOutput("Loss"), "Output", "Loss", "WarpCTC");
Y
Yiqun Liu 已提交
39 40 41

    auto logits_dims = ctx->GetInputDim("Logits");
    int blank = ctx->Attrs().Get<int>("blank");
42 43 44 45 46 47 48 49
    int sequence_width = 0;

    if (ctx->HasInput("LogitsLength")) {
      sequence_width = logits_dims[2];
    } else {
      sequence_width =
          static_cast<int>(framework::product(logits_dims) / logits_dims[0]);
    }
50 51 52 53 54 55 56 57 58 59 60 61

    PADDLE_ENFORCE_GE(
        blank, 0, platform::errors::InvalidArgument(
                      "The value of Attr(blank) should be in interval [0, %d), "
                      "but received %d",
                      blank));
    PADDLE_ENFORCE_LT(
        blank, sequence_width,
        platform::errors::InvalidArgument(
            "The value of Attr(blank) should be in interval [0, %d), "
            "but received %d",
            blank));
62

Y
Yiqun Liu 已提交
63
    // TODO(liuyiqun): it is tricky to set the wrong dimension here.
W
whs 已提交
64
    ctx->SetOutputDim("Loss", {-1, 1});
Y
Yiqun Liu 已提交
65 66 67 68 69
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
W
Wu Yi 已提交
70 71
    framework::LibraryType library_{framework::LibraryType::kPlain};
    framework::DataLayout layout_ = framework::DataLayout::kAnyLayout;
72
    return framework::OpKernelType(
73 74
        OperatorWithKernel::IndicateVarDataType(ctx, "Logits"), ctx.GetPlace(),
        layout_, library_);
Y
Yiqun Liu 已提交
75 76 77 78 79
  }
};

class WarpCTCOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
80
  void Make() override {
Y
Yiqun Liu 已提交
81
    AddInput("Logits",
82 83 84 85 86 87 88 89 90 91 92
             "(2-D LoDTensor<float>) or (3-D Tensor<float>), the "
             "unscaled probabilities of variable-length sequences."
             "When is a 2-D Tensor with LoD information, "
             "it's shape is [Lp, num_classes + 1], "
             "where Lp is the sum of all input sequences' length "
             "and num_classes is the true number of classes "
             "(not including the blank label)."
             "When it is 3-D Tensor, it's shape is "
             "[max_logit_length, batch_size, num_classes + 1], "
             "where max_logit_length is the length of the longest "
             "logit sequence.");
Y
Yiqun Liu 已提交
93
    AddInput("Label",
94 95 96 97 98 99 100 101 102 103 104 105 106 107
             "(2-D LoDTensor<int>) or (2-D Tensor<int>), the "
             "ground truth of variable-length sequence. "
             "When it is a 2-D Tensor with LoD information, "
             "it is of the shape [Lg, 1], where Lg is th sum of "
             "all labels' length."
             "When it is a 2-D Tensor<int>, it's shape is also [Lg, 1].");
    AddInput("LogitsLength",
             "1-D Tensor<int64_t>. "
             "Input sequence length for Logits when Logits is a 3-D tensor.")
        .AsDispensable();
    AddInput("LabelLength",
             "1-D Tensor<int64_t>. "
             "Target sequence length for Label when Label is a 2-D tensor.")
        .AsDispensable();
Y
Yiqun Liu 已提交
108
    AddOutput("WarpCTCGrad",
109
              "(Tensor), a temporary "
Y
Yiqun Liu 已提交
110 111 112 113 114
              "output Tensor to store the gradients of warp-ctc, which is "
              "computed with loss together in one call. It is a 3-D Tensor of "
              "the shape [max_sequence_length, batch_size, num_classes + 1].")
        .AsIntermediate();
    AddOutput("Loss",
115
              "(Tensor), the Connectionist "
Y
Yiqun Liu 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
              "Temporal Classification (CTC) loss, which is a 2-D Tensor of "
              "the shape [batch_size, 1]");
    AddAttr<int>("blank",
                 "(int, default: 0), the blank label of Connectionist "
                 "Temporal Classification (CTC) loss, which is in the "
                 "half-opened interval [0, num_classes + 1).")
        .SetDefault(0);
    AddAttr<bool>("norm_by_times",
                  "(bool, default: false), whether to "
                  "normalize the gradients by the number of time-step, "
                  "which is also the sequence's length.")
        .SetDefault(false);
    AddComment(R"DOC(
An operator integrating the open-source
[warp-ctc](https://github.com/baidu-research/warp-ctc) library, which is used in
[Deep Speech 2: End-toEnd Speech Recognition in English and Mandarin](
https://arxiv.org/pdf/1512.02595v1.pdf),
to compute Connectionist Temporal Classification (CTC) loss.
It can be aliased as softmax with ctc, since a native softmax activation is
T
tianshuo78520a 已提交
135
interated to the warp-ctc library, to to normalize values for each row of the
Y
Yiqun Liu 已提交
136 137
input tensor.

T
tianshuo78520a 已提交
138
More detail of CTC loss can be found by referring to
Y
Yiqun Liu 已提交
139 140 141 142 143 144 145
[Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with
Recurrent Neural Networks](
http://machinelearning.wustl.edu/mlpapers/paper_files/icml2006_GravesFGS06.pdf).
)DOC");
  }
};

H
hong 已提交
146 147
template <typename T>
class WarpCTCGradOpMaker : public framework::SingleGradOpMaker<T> {
H
Huihuang Zheng 已提交
148
 public:
H
hong 已提交
149
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
H
Huihuang Zheng 已提交
150 151

 protected:
152
  void Apply(GradOpPtr<T> op) const override {
H
Huihuang Zheng 已提交
153 154
    op->SetType("warpctc_grad");

H
hong 已提交
155 156 157
    op->SetInput("WarpCTCGrad", this->Output("WarpCTCGrad"));
    op->SetInput("Logits", this->Input("Logits"));
    op->SetInput(framework::GradVarName("Loss"), this->OutputGrad("Loss"));
H
Huihuang Zheng 已提交
158

H
hong 已提交
159
    op->SetInput("LogitsLength", this->Input("LogitsLength"));
160

H
hong 已提交
161
    op->SetOutput(framework::GradVarName("Logits"), this->InputGrad("Logits"));
H
Huihuang Zheng 已提交
162

H
hong 已提交
163
    op->SetAttrMap(this->Attrs());
H
Huihuang Zheng 已提交
164 165 166
  }
};

Y
Yiqun Liu 已提交
167 168 169 170 171
class WarpCTCGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
172 173 174
    OP_INOUT_CHECK(ctx->HasInput("WarpCTCGrad"), "Input", "WarpCTCGrad",
                   "WarpCTCGrad");
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("Logits")), "Output",
175
                   framework::GradVarName("Logits"), "WarpCTCGrad");
Y
Yiqun Liu 已提交
176 177 178 179 180 181 182 183
    ctx->SetOutputDim(framework::GradVarName("Logits"),
                      ctx->GetInputDim("Logits"));
    ctx->ShareLoD("Logits", /*->*/ framework::GradVarName("Logits"));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
184 185
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Loss")),
186
                                   ctx.GetPlace());
Y
Yiqun Liu 已提交
187 188 189
  }
};

190
DECLARE_NO_NEED_BUFFER_VARS_INFERER(WarpCTCGradOpNoNeedBufferVarInferer,
Z
Zeng Jinle 已提交
191
                                    "Logits");
192

Y
Yiqun Liu 已提交
193 194 195 196
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
197
REGISTER_OPERATOR(warpctc, ops::WarpCTCOp, ops::WarpCTCOpMaker,
H
hong 已提交
198 199
                  ops::WarpCTCGradOpMaker<paddle::framework::OpDesc>,
                  ops::WarpCTCGradOpMaker<paddle::imperative::OpBase>);
200
REGISTER_OPERATOR(warpctc_grad, ops::WarpCTCGradOp,
201
                  ops::WarpCTCGradOpNoNeedBufferVarInferer);
Y
Yiqun Liu 已提交
202
REGISTER_OP_CPU_KERNEL(
203 204
    warpctc, ops::WarpCTCKernel<paddle::platform::CPUDeviceContext, float>,
    ops::WarpCTCKernel<paddle::platform::CPUDeviceContext, double>);
Y
Yiqun Liu 已提交
205 206
REGISTER_OP_CPU_KERNEL(
    warpctc_grad,
207 208
    ops::WarpCTCGradKernel<paddle::platform::CPUDeviceContext, float>,
    ops::WarpCTCGradKernel<paddle::platform::CPUDeviceContext, double>);