|
| 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/eigen.h" |
| 17 | +#include "paddle/framework/op_registry.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 | +LoD subsequenceLoD(const T* in, const std::vector<int> offsets, |
| 29 | + const std::vector<int> sizes) { |
| 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 += sizes[i]; |
| 37 | + out_lod[0][i+1] = lod_offset; |
| 38 | + } |
| 39 | + return out_lod; |
| 40 | +} |
| 41 | + |
| 42 | +template <typename Place, typename T> |
| 43 | +class SubSequenceOpKernel : public framework::OpKernel<T> { |
| 44 | + public: |
| 45 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 46 | + auto* in = ctx.Input<LoDTensor>("X"); |
| 47 | + std::vector<int> offsets = ctx.Attr<std::vector<int>>("offset"); |
| 48 | + std::vector<int> sizes = ctx.Attr<std::vector<int>>("size"); |
| 49 | + auto* out = ctx.Output<LoDTensor>("Out"); |
| 50 | + |
| 51 | + auto offset_len = offsets.size(); |
| 52 | + auto size_len = sizes.size(); |
| 53 | + |
| 54 | + auto lod = in->lod(); |
| 55 | + auto n = lod[0].size() - 1; |
| 56 | + |
| 57 | + PADDLE_ENFORCE_EQ(lod.size(), 1UL, "Only support one level sequence now."); |
| 58 | + PADDLE_ENFORCE_EQ(n, offset_len, |
| 59 | + "The length of input and offset should be the same") |
| 60 | + PADDLE_ENFORCE_EQ(n, size_len, |
| 61 | + "The length of input and size should be the same") |
| 62 | + |
| 63 | + for (size_t i = 0; i < n; ++i) { |
| 64 | + auto offset = offsets[i]; |
| 65 | + auto size = sizes[i]; |
| 66 | + PADDLE_ENFORCE_LT(lod[0][i] + offset + size, lod[0][i + 1], |
| 67 | + "The target tensor's length overflow") |
| 68 | + } |
| 69 | + |
| 70 | + out->mutable_data<T>(ctx.GetPlace()); |
| 71 | + auto out_lod = subsequenceLoD(in, offsets, sizes); |
| 72 | + out->set_lod(out_lod); |
| 73 | + |
| 74 | + auto in_stride = framework::stride(in->dims()); |
| 75 | + auto out_stride = framework::stride(out->dims()); |
| 76 | + |
| 77 | + size_t out_offset = 0; |
| 78 | + for (size_t i = 0; i < n; ++i) { |
| 79 | + auto offset = offsets[i]; |
| 80 | + auto size = sizes[i]; |
| 81 | + |
| 82 | + Tensor in_t = in->Slice(static_cast<int>(lod[0][i] + offset), |
| 83 | + static_cast<int>(lod[0][i] + offset + size)); |
| 84 | + |
| 85 | + StridedMemcpy<T>(ctx.device_context(), in_t.data<T>(), |
| 86 | + in_stride, in_t.dims(), out_stride, |
| 87 | + out->data<T>() + out_offset); |
| 88 | + out_offset += size * in_stride[0]; |
| 89 | + } |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +template <typename Place, typename T> |
| 94 | +class SubSequenceGradOpKernel : public framework::OpKernel<T> { |
| 95 | + public: |
| 96 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 97 | + auto* in = ctx.Input<LoDTensor>("X"); |
| 98 | + std::vector<int> offsets = ctx.Attr<std::vector<int>>("offset"); |
| 99 | + std::vector<int> sizes = ctx.Attr<std::vector<int>>("size"); |
| 100 | + auto* out_grad = |
| 101 | + ctx.Input<framework::LoDTensor>(framework::GradVarName("Out")); |
| 102 | + auto* x_grad = |
| 103 | + ctx.Output<framework::LoDTensor>(framework::GradVarName("X")); |
| 104 | + |
| 105 | + auto offset_len = offsets.size(); |
| 106 | + auto size_len = sizes.size(); |
| 107 | + |
| 108 | + auto lod = in->lod(); |
| 109 | + auto n = lod[0].size() - 1; |
| 110 | + |
| 111 | + // check input data format |
| 112 | + PADDLE_ENFORCE_EQ(lod.size(), 1UL, "Only support one level sequence now."); |
| 113 | + PADDLE_ENFORCE_EQ(n, offset_len, |
| 114 | + "The length of input and offset should be the same") |
| 115 | + PADDLE_ENFORCE_EQ(n, size_len, |
| 116 | + "The length of input and size should be the same") |
| 117 | + |
| 118 | + for (size_t i = 0; i < n; ++i) { |
| 119 | + auto offset = offsets[i]; |
| 120 | + auto size = sizes[i]; |
| 121 | + PADDLE_ENFORCE_LT(lod[0][i] + offset + size, lod[0][i + 1], |
| 122 | + "The target tensor's length overflow") |
| 123 | + } |
| 124 | + |
| 125 | + auto out_lod = subsequenceLoD(in, offsets, sizes); |
| 126 | + |
| 127 | + x_grad->set_lod(lod); |
| 128 | + x_grad->mutable_data<T>(ctx.GetPlace()); |
| 129 | + auto temp = framework::EigenVector<T>::Flatten(*x_grad); |
| 130 | + temp.device(ctx.GetEigenDevice<Place>()) = temp.constant(static_cast<T>(0)); |
| 131 | + |
| 132 | + auto out_grad_stride = framework::stride(out_grad->dims()); |
| 133 | + |
| 134 | + for (size_t i = 0; i < out_lod[0].size() - 1; ++i) { |
| 135 | + Tensor out_grad_t = |
| 136 | + out_grad->Slice(static_cast<int>(out_lod[0][i]), |
| 137 | + static_cast<int>(out_lod[0][i + 1])); |
| 138 | + auto out_grad_stride = framework::stride(out_grad_t.dims()); |
| 139 | + |
| 140 | + auto x_grad_stride = framework::stride(x_grad->dims()); |
| 141 | + |
| 142 | + auto offset = offsets[i]; |
| 143 | + auto size = sizes[i]; |
| 144 | + |
| 145 | + Tensor x_grad_t = x_grad->Slice(static_cast<int>(lod[0][i] + offset), |
| 146 | + static_cast<int>(lod[0][i] + offset + size)); |
| 147 | + |
| 148 | + StridedMemcpy<T>(ctx.device_context(), out_grad_t.data<T>(), |
| 149 | + out_grad_stride, out_grad_t.dims(), x_grad_stride, |
| 150 | + x_grad_t.data<T>()); |
| 151 | + } |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | +} // namespace operators |
| 156 | +} // namespace paddle |
0 commit comments