/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. 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 #include #include #include #include #include #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" #include "paddle/framework/operator.h" #define EXPAND_TEMPLATE(z, n, data) \ case n + 1: { \ Expand(context); \ break; \ } #define REP_EXPAND_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_TEMPLATE, ~) #define COND(n) BOOST_PP_GREATER_EQUAL(BOOST_PP_DIV(n, 6), BOOST_PP_MOD(n, 6)) #define EXPAND_GRAD_CASE(n) \ case n: { \ ExpandBackward(context, reshape_dims_vec, reduce_dims_vec); \ break; \ } #define EXPAND_TEMPLATE_GRAD(z, n, data) \ BOOST_PP_IF(COND(n), EXPAND_GRAD_CASE(n), ) #define REP_EXPAND_GRAD_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_TEMPLATE_GRAD, ~) namespace paddle { namespace operators { using Tensor = framework::Tensor; template using EigenVector = framework::EigenVector; template using EigenTensor = framework::EigenTensor; template class ExpandKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto rank = framework::arity(context.Input("X")->dims()); switch (rank) { REP_EXPAND_TEMPLATE(6) default: PADDLE_ENFORCE(false, "Only support tensor whose rank in [1, 6]."); }; } protected: template void Expand(const framework::ExecutionContext& context) const { auto* in0 = context.Input("X"); auto expand_times = context.Attr>("expandTimes"); auto* out0 = context.Output("Out"); Eigen::DSizes bcast_dims; auto x_dims = in0->dims(); for (size_t i = 0; i < expand_times.size(); ++i) { bcast_dims[i] = expand_times[i]; } auto x = EigenTensor::From(*in0); out0->mutable_data(context.GetPlace()); auto y = EigenTensor::From(*out0); auto place = context.GetEigenDevice(); y.device(place) = x.broadcast(bcast_dims); } }; template class ExpandGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* in0 = context.Input("X"); auto expand_times = context.Attr>("expandTimes"); auto x_dims = in0->dims(); std::vector reshape_dims_vec; std::vector reduce_dims_vec; for (size_t i = 0; i < expand_times.size(); ++i) { if (expand_times[i] == 1) { reshape_dims_vec.push_back(x_dims[i]); } else { if (x_dims[i] == 1) { reduce_dims_vec.push_back(reshape_dims_vec.size()); reshape_dims_vec.push_back(expand_times[i]); } else { reduce_dims_vec.push_back(reshape_dims_vec.size()); reshape_dims_vec.push_back(expand_times[i]); reshape_dims_vec.push_back(x_dims[i]); } } } int dims = reshape_dims_vec.size() * 6 + reduce_dims_vec.size() - 7; switch (dims) { REP_EXPAND_GRAD_TEMPLATE(72) default: PADDLE_ENFORCE(false, "Only support tensor whose rank in [1, 6]."); }; } protected: template void ExpandBackward(const framework::ExecutionContext& context, const std::vector& reshape_dims_vec, const std::vector& reduce_dims_vec) const { size_t reshape_size = Dims / 6 + 1; size_t reduce_size = Dims % 6 + 1; PADDLE_ENFORCE_EQ(reshape_size, reshape_dims_vec.size(), "Inconsistent size between Dims and " "reshape dimensions."); PADDLE_ENFORCE_EQ(reduce_size, reduce_dims_vec.size(), "Inconsistent size between Dims and " "reduce dimensions."); auto* in0 = context.Input(framework::GradVarName("Out")); auto* out0 = context.Output(framework::GradVarName("X")); auto x = EigenVector::Flatten(*(context.Input("X"))); out0->mutable_data(context.GetPlace()); auto x_grad = EigenVector::Flatten(*out0); Eigen::DSizes reshape_dims; for (size_t i = 0; i < reshape_size; ++i) { reshape_dims[i] = reshape_dims_vec[i]; } Eigen::DSizes reduce_dims; for (size_t i = 0; i < reduce_size; ++i) { reduce_dims[i] = reduce_dims_vec[i]; } auto out_grad = EigenVector::Flatten(*in0); x_grad.device(context.GetEigenDevice()) = out_grad.reshape(reshape_dims).sum(reduce_dims).reshape(x.dimensions()); } }; } // operators } // paddle