|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 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 | +#pragma once |
| 16 | +#include "paddle/framework/op_registry.h" |
| 17 | +#include "paddle/operators/math/math_function.h" |
| 18 | +#include "paddle/operators/strided_memcpy.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace operators { |
| 22 | + |
| 23 | +using Tensor = framework::Tensor; |
| 24 | +using LoDTensor = framework::LoDTensor; |
| 25 | +using LoD = framework::LoD; |
| 26 | + |
| 27 | +template <typename T> |
| 28 | +inline LoD SequenceSliceLoD(const T& in, const int64_t* offset_data, |
| 29 | + const int64_t* length_data) { |
| 30 | + auto out_lod = in.lod(); |
| 31 | + size_t lod_offset = 0; |
| 32 | + |
| 33 | + auto n = in.lod()[0].size() - 1; |
| 34 | + out_lod[0][0] = 0; |
| 35 | + for (size_t i = 0; i < n; ++i) { |
| 36 | + lod_offset += length_data[i]; |
| 37 | + out_lod[0][i+1] = lod_offset; |
| 38 | + } |
| 39 | + return out_lod; |
| 40 | +} |
| 41 | + |
| 42 | +template <typename Place, typename T> |
| 43 | +class SequenceSliceOpKernel : public framework::OpKernel<T> { |
| 44 | + public: |
| 45 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 46 | + auto* in = ctx.Input<LoDTensor>("X"); |
| 47 | + auto* offset = ctx.Input<Tensor>("Offset"); |
| 48 | + auto* length = ctx.Input<Tensor>("Length"); |
| 49 | + auto* out = ctx.Output<LoDTensor>("Out"); |
| 50 | + |
| 51 | + auto lod = in->lod(); |
| 52 | + auto n = lod[0].size() - 1; |
| 53 | + |
| 54 | + PADDLE_ENFORCE_EQ(lod.size(), 1UL, |
| 55 | + "Only support one level sequence now."); |
| 56 | + PADDLE_ENFORCE_EQ( |
| 57 | + n, static_cast<size_t>(length->dims()[0]), |
| 58 | + "The size of input-sequence and length-array should be the same") |
| 59 | + PADDLE_ENFORCE_EQ( |
| 60 | + n, static_cast<size_t>(offset->dims()[0]), |
| 61 | + "The size of input-sequence and offset-array should be the same") |
| 62 | + |
| 63 | + const int64_t* offset_data = offset->data<int64_t>(); |
| 64 | + const int64_t* length_data = length->data<int64_t>(); |
| 65 | + framework::Tensor offset_cpu; |
| 66 | + framework::Tensor length_cpu; |
| 67 | + |
| 68 | + if (platform::is_gpu_place(ctx.GetPlace())) { |
| 69 | + offset_cpu.mutable_data<T>(offset->dims(), platform::CPUPlace()); |
| 70 | + offset_cpu.CopyFrom(*offset, platform::CPUPlace(), ctx.device_context()); |
| 71 | + offset_data = offset_cpu.data<int64_t>(); |
| 72 | + |
| 73 | + length_cpu.mutable_data<T>(length->dims(), platform::CPUPlace()); |
| 74 | + length_cpu.CopyFrom(*length, platform::CPUPlace(), ctx.device_context()); |
| 75 | + length_data = length_cpu.data<int64_t>(); |
| 76 | + } |
| 77 | + |
| 78 | + for (size_t i = 0; i < n; ++i) { |
| 79 | + PADDLE_ENFORCE_LT(0, offset_data[i], |
| 80 | + "The offset[%d] must greater than zero.", i) |
| 81 | + PADDLE_ENFORCE_LT(0, length_data[i], |
| 82 | + "The length[%d] must greater than zero.", i) |
| 83 | + PADDLE_ENFORCE_LT( |
| 84 | + lod[0][i] + offset_data[i] + length_data[i], |
| 85 | + lod[0][i + 1], |
| 86 | + "The target tensor's length overflow.") |
| 87 | + } |
| 88 | + |
| 89 | + out->mutable_data<T>(ctx.GetPlace()); |
| 90 | + auto out_lod = SequenceSliceLoD(*in, offset_data, length_data); |
| 91 | + auto out_dims = in->dims(); |
| 92 | + out_dims[0] = out_lod[0][out_lod[0].size() - 1]; |
| 93 | + out->Resize(out_dims); |
| 94 | + out->set_lod(out_lod); |
| 95 | + |
| 96 | + auto in_stride = framework::stride(in->dims()); |
| 97 | + auto out_stride = framework::stride(out->dims()); |
| 98 | + |
| 99 | + size_t out_offset = 0; |
| 100 | + for (size_t i = 0; i < n; ++i) { |
| 101 | + Tensor in_t = |
| 102 | + in->Slice(static_cast<int>(lod[0][i] + offset_data[i]), |
| 103 | + static_cast<int>(lod[0][i] + offset_data[i] + |
| 104 | + length_data[i])); |
| 105 | + |
| 106 | + StridedMemcpy<T>(ctx.device_context(), in_t.data<T>(), |
| 107 | + in_stride, in_t.dims(), out_stride, |
| 108 | + out->data<T>() + out_offset); |
| 109 | + out_offset += length_data[i] * in_stride[0]; |
| 110 | + } |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +template <typename Place, typename T> |
| 115 | +class SequenceSliceGradOpKernel : public framework::OpKernel<T> { |
| 116 | + public: |
| 117 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 118 | + auto* in = ctx.Input<LoDTensor>("X"); |
| 119 | + auto* offset = ctx.Input<Tensor>("Offset"); |
| 120 | + auto* length = ctx.Input<Tensor>("Length"); |
| 121 | + auto* out_grad = |
| 122 | + ctx.Input<framework::LoDTensor>(framework::GradVarName("Out")); |
| 123 | + auto* x_grad = |
| 124 | + ctx.Output<framework::LoDTensor>(framework::GradVarName("X")); |
| 125 | + |
| 126 | + const int64_t* offset_data = offset->data<int64_t>(); |
| 127 | + const int64_t* length_data = length->data<int64_t>(); |
| 128 | + framework::Tensor offset_cpu; |
| 129 | + framework::Tensor length_cpu; |
| 130 | + |
| 131 | + if (platform::is_gpu_place(ctx.GetPlace())) { |
| 132 | + offset_cpu.mutable_data<T>(offset->dims(), platform::CPUPlace()); |
| 133 | + offset_cpu.CopyFrom(*offset, platform::CPUPlace(), ctx.device_context()); |
| 134 | + offset_data = offset_cpu.data<int64_t>(); |
| 135 | + |
| 136 | + length_cpu.mutable_data<T>(length->dims(), platform::CPUPlace()); |
| 137 | + length_cpu.CopyFrom(*length, platform::CPUPlace(), ctx.device_context()); |
| 138 | + length_data = length_cpu.data<int64_t>(); |
| 139 | + } |
| 140 | + |
| 141 | + auto lod = in->lod(); |
| 142 | + auto out_lod = out_grad->lod(); |
| 143 | + |
| 144 | + if (x_grad) { |
| 145 | + x_grad->mutable_data<T>(ctx.GetPlace()); |
| 146 | + x_grad->set_lod(in->lod()); |
| 147 | + math::SetConstant<Place, T> set_zero; |
| 148 | + set_zero(ctx.device_context(), x_grad, static_cast<T>(0)); |
| 149 | + |
| 150 | + auto out_grad_stride = framework::stride(out_grad->dims()); |
| 151 | + |
| 152 | + for (size_t i = 0; i < out_lod[0].size() - 1; ++i) { |
| 153 | + Tensor out_grad_t = |
| 154 | + out_grad->Slice(static_cast<int>(out_lod[0][i]), |
| 155 | + static_cast<int>(out_lod[0][i + 1])); |
| 156 | + auto out_grad_stride = framework::stride(out_grad_t.dims()); |
| 157 | + |
| 158 | + auto x_grad_stride = framework::stride(x_grad->dims()); |
| 159 | + |
| 160 | + Tensor x_grad_t = x_grad->Slice( |
| 161 | + static_cast<int>(lod[0][i] + offset_data[i]), |
| 162 | + static_cast<int>(lod[0][i] + offset_data[i] + length_data[i])); |
| 163 | + |
| 164 | + StridedMemcpy<T>(ctx.device_context(), out_grad_t.data<T>(), |
| 165 | + out_grad_stride, out_grad_t.dims(), x_grad_stride, |
| 166 | + x_grad_t.data<T>()); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +}; |
| 171 | + |
| 172 | +} // namespace operators |
| 173 | +} // namespace paddle |
0 commit comments