/* Copyright (c) 2018 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 #include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" namespace paddle { namespace operators { template class CumKernel : public framework::OpKernel { public: using T = typename Functor::ELEMENT_TYPE; void Compute(const framework::ExecutionContext& context) const override { auto& X = GET_DATA_SAFELY(context.Input("X"), "Input", "X", "Cum"); auto& Out = GET_DATA_SAFELY(context.Output("Out"), "Output", "Out", "Cum"); int axis = context.Attr("axis"); bool exclusive = context.Attr("exclusive"); bool reverse = context.Attr("reverse"); auto out_dims = Out.dims(); PADDLE_ENFORCE_EQ( axis < out_dims.size() && axis >= (0 - out_dims.size()), true, platform::errors::OutOfRange( "Attr(axis) is out of range, It's expected " "to be in range of [-%d, %d]. But received Attr(axis) = %d.", out_dims.size(), out_dims.size() - 1, axis)); if (axis < 0) { axis += out_dims.size(); } Out.template mutable_data(context.GetPlace()); int pre = 1; int post = 1; int mid = out_dims[axis]; for (int i = 0; i < axis; ++i) { pre *= out_dims[i]; } for (int i = axis + 1; i < out_dims.size(); ++i) { post *= out_dims[i]; } auto x = framework::EigenVector::Flatten(X); auto out = framework::EigenVector::Flatten(Out); auto* place = context.template device_context().eigen_device(); using IndexT = Eigen::DenseIndex; if (pre == 1) { if (post == 1) { ComputeImp(*place, Eigen::DSizes(mid), x, out, /* axis= */ 0, reverse, exclusive); } else { ComputeImp(*place, Eigen::DSizes(mid, post), x, out, /* axis= */ 0, reverse, exclusive); } } else { if (post == 1) { ComputeImp(*place, Eigen::DSizes(pre, mid), x, out, /* axis= */ 1, reverse, exclusive); } else { ComputeImp(*place, Eigen::DSizes(pre, mid, post), x, out, /* axis= */ 1, reverse, exclusive); } } } private: template void ComputeImp(Device d, const Dim& dims, X x, Out out, int axis, bool reverse, bool exclusive) const { if (!reverse) { out.reshape(dims).device(d) = Functor()(x.reshape(dims), axis, exclusive); } else { std::array rev; rev.fill(false); rev[axis] = reverse; out.reshape(dims).device(d) = Functor()(x.reshape(dims).reverse(rev), axis, exclusive).reverse(rev); } } }; template struct CumsumFunctor { using ELEMENT_TYPE = T; template const typename X::TensorScanSumOp operator()(X x, int axis, bool exclusive) const { return x.cumsum(axis, exclusive); } }; } // namespace operators } // namespace paddle