warpctc_grad_kernel_impl.h 3.0 KB
Newer Older
0
0x45f 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright (c) 2022 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.

#pragma once

#include <vector>

#include "paddle/fluid/operators/math/sequence_padding.h"
#include "paddle/phi/backends/dynload/warpctc.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/math_function.h"
25
#include "paddle/phi/kernels/funcs/sequence_scale.h"
0
0x45f 已提交
26 27 28 29 30 31 32
#include "paddle/utils/optional.h"

namespace phi {

template <typename T, typename Context>
void WarpctcGradKernel(const Context& dev_ctx,
                       const DenseTensor& logits,
33
                       const paddle::optional<DenseTensor>& logits_length,
Z
Zhong Hui 已提交
34 35
                       const DenseTensor& warpctcgrad,
                       const DenseTensor& loss_grad,
0
0x45f 已提交
36 37 38 39 40 41
                       int blank,
                       bool norm_by_times,
                       DenseTensor* logits_grad) {
  dev_ctx.template Alloc<T>(logits_grad);

  if (logits_length.is_initialized()) {
Z
Zhong Hui 已提交
42 43 44
    int max_seq_length = warpctcgrad.dims()[0];  // Tmax
    int num_sequences = warpctcgrad.dims()[1];   // B
    int seq_width = warpctcgrad.dims()[2];       // D
0
0x45f 已提交
45 46 47 48 49 50

    // B
    auto logits_len_e = EigenTensor<int64_t, 1>::From(*logits_length);
    // (B, 1)
    auto loss_grad_e = EigenTensor<T, 2>::From(loss_grad);
    // (T, B, D)
Z
Zhong Hui 已提交
51
    auto warpctcgrad_e = EigenTensor<T, 3>::From(warpctcgrad);
0
0x45f 已提交
52 53 54 55 56

    auto logits_grad_e = EigenTensor<T, 3>::From(*logits_grad);

    Eigen::DSizes<int, 3> grad_shape(1, num_sequences, 1);
    Eigen::DSizes<int, 3> bcast(max_seq_length, 1, seq_width);
Z
Zhong Hui 已提交
57 58
    auto logits_g =
        warpctcgrad_e * loss_grad_e.reshape(grad_shape).broadcast(bcast).eval();
0
0x45f 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    auto* place = dev_ctx.eigen_device();
    if (norm_by_times) {
      auto scales = logits_len_e.cast<T>()
                        .inverse()
                        .reshape(grad_shape)
                        .broadcast(bcast)
                        .eval();
      logits_grad_e.device(*place) = logits_g * scales;
    } else {
      logits_grad_e.device(*place) = logits_g;
    }
  } else {
    paddle::operators::math::UnpaddingLoDTensorFunctor<Context, T>()(
        dev_ctx,
Z
Zhong Hui 已提交
74
        warpctcgrad,
0
0x45f 已提交
75 76 77 78 79 80 81
        logits_grad,
        -1,
        0,
        norm_by_times,
        paddle::operators::math::kLengthBatchWidth);

    const T* loss_grad_data = loss_grad.data<T>();
82
    phi::funcs::ScaleLoDTensorFunctor<Context, T>()(
0
0x45f 已提交
83 84 85 86 87
        dev_ctx, loss_grad_data, logits_grad);
  }
}

}  // namespace phi