|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/framework/mixed_vector.h" |
| 16 | +#include "paddle/fluid/operators/math/softmax.h" |
| 17 | +#include "paddle/fluid/operators/warpctc_op.h" |
| 18 | +#include "paddle/fluid/platform/cudnn_helper.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace operators { |
| 22 | + |
| 23 | +#if CUDNN_VERSION >= 7001 |
| 24 | +using ScopedTensorDescriptor = platform::ScopedTensorDescriptor; |
| 25 | +using ScopedCTCLossDescriptor = platform::ScopedCTCLossDescriptor; |
| 26 | +using DataLayout = platform::DataLayout; |
| 27 | + |
| 28 | +template <typename DeviceContext, typename T> |
| 29 | +class CudnnCTCKernel : public framework::OpKernel<T> { |
| 30 | + public: |
| 31 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 32 | + // =====================Copied code from warpctc=========================== |
| 33 | + auto* logits = ctx.Input<LoDTensor>("Logits"); |
| 34 | + auto* label = ctx.Input<LoDTensor>("Label"); |
| 35 | + auto* warpctc_grad = ctx.Output<LoDTensor>("WarpCTCGrad"); |
| 36 | + auto* loss = ctx.Output<LoDTensor>("Loss"); |
| 37 | + |
| 38 | + const size_t level = 0; |
| 39 | + |
| 40 | + auto logits_lod = framework::ToAbsOffset(logits->lod()); |
| 41 | + auto logits_dims = logits->dims(); |
| 42 | + PADDLE_ENFORCE_EQ(logits_dims[0], |
| 43 | + static_cast<int64_t>(logits_lod[level].back()), |
| 44 | + "The first dimension of Input(Logits) should be equal to " |
| 45 | + "the sum of all sequences' lengths."); |
| 46 | + |
| 47 | + auto label_lod = framework::ToAbsOffset(label->lod()); |
| 48 | + auto label_dims = label->dims(); |
| 49 | + PADDLE_ENFORCE_EQ( |
| 50 | + label_dims[0], label->numel(), |
| 51 | + "The width of each timestep in Input(Label) should be 1."); |
| 52 | + |
| 53 | + const size_t num_sequences = logits_lod[level].size() - 1; |
| 54 | + PADDLE_ENFORCE_EQ(num_sequences, label_lod[level].size() - 1, |
| 55 | + "The number of sequences of Input(Logits) should be " |
| 56 | + "equal to that of Input(Label)."); |
| 57 | + PADDLE_ENFORCE_LE(num_sequences, 256, |
| 58 | + "The labelLengths must less than 256 for cudnn call."); |
| 59 | + |
| 60 | + const size_t sequence_width = logits->numel() / logits_dims[0]; |
| 61 | + auto loss_dims = |
| 62 | + framework::make_ddim({static_cast<int64_t>(num_sequences), 1}); |
| 63 | + |
| 64 | + // NOTE: cudnn takes softmax input, calculate softmax first, then do padding |
| 65 | + auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>(); |
| 66 | + LoDTensor softmax_logits; |
| 67 | + softmax_logits.mutable_data<T>(logits->dims(), ctx.GetPlace()); |
| 68 | + softmax_logits.set_lod(logits_lod); |
| 69 | + int rank = logits->dims().size(); |
| 70 | + Tensor in_2d = framework::ReshapeToMatrix(*logits, rank - 1); |
| 71 | + Tensor out_2d = framework::ReshapeToMatrix(softmax_logits, rank - 1); |
| 72 | + math::SoftmaxFunctor<DeviceContext, T, false>()(dev_ctx, &in_2d, &out_2d); |
| 73 | + |
| 74 | + // ctc needs sequences data stored in transposed padding format |
| 75 | + // logits and grad using padding data of layout 'TNC' |
| 76 | + // T: max_sequence_length |
| 77 | + // N: batch_size (num_sequences) |
| 78 | + // C: width |
| 79 | + LoDTensor warpctc_logits; |
| 80 | + const size_t max_sequence_length = |
| 81 | + math::MaximumSequenceLength(logits_lod[level]); |
| 82 | + auto warpctc_logits_dims = |
| 83 | + framework::make_ddim({static_cast<int64_t>(max_sequence_length), |
| 84 | + static_cast<int64_t>(num_sequences), |
| 85 | + static_cast<int64_t>(sequence_width)}); |
| 86 | + warpctc_logits.mutable_data<T>(warpctc_logits_dims, ctx.GetPlace()); |
| 87 | + |
| 88 | + LoDTensor cpu_pad_value; |
| 89 | + T* pad_value_data = |
| 90 | + cpu_pad_value.mutable_data<T>({1}, platform::CPUPlace()); |
| 91 | + *pad_value_data = static_cast<T>(0); |
| 92 | + LoDTensor pad_value; |
| 93 | + if (platform::is_cpu_place(ctx.GetPlace())) { |
| 94 | + pad_value = cpu_pad_value; |
| 95 | + } else { |
| 96 | + TensorCopySync(cpu_pad_value, ctx.GetPlace(), &pad_value); |
| 97 | + } |
| 98 | + |
| 99 | + math::PaddingLoDTensorFunctor<DeviceContext, T>()( |
| 100 | + ctx.template device_context<DeviceContext>(), softmax_logits, |
| 101 | + &warpctc_logits, pad_value, -1, 0, false /* norm_by_times */, |
| 102 | + math::kLengthBatchWidth); |
| 103 | + const T* warpctc_logits_data = warpctc_logits.data<T>(); |
| 104 | + |
| 105 | + std::vector<int> warpctc_label_lengths(num_sequences); |
| 106 | + std::vector<int> warpctc_logits_lengths(num_sequences); |
| 107 | + |
| 108 | + for (size_t i = 0; i < num_sequences; ++i) { |
| 109 | + warpctc_label_lengths[i] = label_lod[level][i + 1] - label_lod[level][i]; |
| 110 | + warpctc_logits_lengths[i] = |
| 111 | + logits_lod[level][i + 1] - logits_lod[level][i]; |
| 112 | + } |
| 113 | + |
| 114 | + T* warpctc_grad_data = |
| 115 | + warpctc_grad->mutable_data<T>(warpctc_logits.dims(), ctx.GetPlace()); |
| 116 | + |
| 117 | + math::SetConstant<DeviceContext, T>()( |
| 118 | + ctx.template device_context<DeviceContext>(), warpctc_grad, |
| 119 | + static_cast<T>(0)); |
| 120 | + |
| 121 | + Tensor warpctc_label; |
| 122 | + TensorCopySync(*label, platform::CPUPlace(), &warpctc_label); |
| 123 | + const int* warpctc_label_data = warpctc_label.data<int>(); |
| 124 | + // ======================================================================== |
| 125 | + |
| 126 | + ScopedTensorDescriptor logits_desc; |
| 127 | + ScopedTensorDescriptor grad_desc; |
| 128 | + ScopedCTCLossDescriptor ctcloss_desc; |
| 129 | + // layout here doesn't have effect. |
| 130 | + DataLayout layout = DataLayout::kNCHW; |
| 131 | + |
| 132 | + auto cu_logits_desc = logits_desc.descriptor<T>( |
| 133 | + layout, framework::vectorize2int(warpctc_logits.dims())); |
| 134 | + auto cu_grad_desc = grad_desc.descriptor<T>( |
| 135 | + layout, framework::vectorize2int(warpctc_grad->dims())); |
| 136 | + auto cu_ctcloss_desc = ctcloss_desc.descriptor<T>(); |
| 137 | + |
| 138 | + auto handle = dev_ctx.cudnn_handle(); |
| 139 | + size_t workspace_size; |
| 140 | + |
| 141 | + CUDNN_ENFORCE(platform::dynload::cudnnGetCTCLossWorkspaceSize( |
| 142 | + handle, cu_logits_desc, cu_grad_desc, warpctc_label_data, |
| 143 | + warpctc_label_lengths.data(), warpctc_logits_lengths.data(), |
| 144 | + CUDNN_CTC_LOSS_ALGO_DETERMINISTIC, cu_ctcloss_desc, &workspace_size)); |
| 145 | + |
| 146 | + T* loss_data = loss->mutable_data<T>(loss_dims, ctx.GetPlace()); |
| 147 | + |
| 148 | + auto workspace_handle = dev_ctx.cudnn_workspace_handle(); |
| 149 | + auto cudnn_func = [&](void* cudnn_workspace) { |
| 150 | + CUDNN_ENFORCE(platform::dynload::cudnnCTCLoss( |
| 151 | + handle, cu_logits_desc, warpctc_logits_data, warpctc_label_data, |
| 152 | + warpctc_label_lengths.data(), warpctc_logits_lengths.data(), |
| 153 | + loss_data, cu_grad_desc, warpctc_grad_data, |
| 154 | + CUDNN_CTC_LOSS_ALGO_DETERMINISTIC, cu_ctcloss_desc, cudnn_workspace, |
| 155 | + workspace_size)); |
| 156 | + }; |
| 157 | + workspace_handle.RunFunc(cudnn_func, workspace_size); |
| 158 | + } |
| 159 | +}; |
| 160 | + |
| 161 | +template <typename DeviceContext, typename T> |
| 162 | +class CudnnCTCGradKernel : public framework::OpKernel<T> { |
| 163 | + public: |
| 164 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 165 | + auto* warpctc_grad = ctx.Input<LoDTensor>("WarpCTCGrad"); |
| 166 | + auto* logits_grad = ctx.Output<LoDTensor>(framework::GradVarName("Logits")); |
| 167 | + const Tensor* loss_grad = ctx.Input<Tensor>(framework::GradVarName("Loss")); |
| 168 | + |
| 169 | + logits_grad->mutable_data<T>(ctx.GetPlace()); |
| 170 | + bool norm_by_times = ctx.Attr<bool>("norm_by_times"); |
| 171 | + math::UnpaddingLoDTensorFunctor<DeviceContext, T>()( |
| 172 | + ctx.template device_context<DeviceContext>(), *warpctc_grad, |
| 173 | + logits_grad, -1, 0, norm_by_times, math::kLengthBatchWidth); |
| 174 | + |
| 175 | + const T* loss_grad_data = loss_grad->data<T>(); |
| 176 | + math::ScaleLoDTensorFunctor<DeviceContext, T>()( |
| 177 | + ctx.template device_context<DeviceContext>(), loss_grad_data, |
| 178 | + logits_grad); |
| 179 | + } |
| 180 | +}; |
| 181 | + |
| 182 | +#endif |
| 183 | +} // namespace operators |
| 184 | +} // namespace paddle |
| 185 | + |
| 186 | +namespace ops = paddle::operators; |
| 187 | +namespace plat = paddle::platform; |
| 188 | +#if CUDNN_VERSION >= 7001 |
| 189 | +REGISTER_OP_KERNEL( |
| 190 | + warpctc, CUDNN, plat::CUDAPlace, |
| 191 | + ops::CudnnCTCKernel<paddle::platform::CUDADeviceContext, float>); |
| 192 | +REGISTER_OP_KERNEL( |
| 193 | + warpctc_grad, CUDNN, plat::CUDAPlace, |
| 194 | + ops::CudnnCTCGradKernel<paddle::platform::CUDADeviceContext, float>); |
| 195 | +#endif |
0 commit comments