/* 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 "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/clip_op.h" #include "paddle/fluid/operators/math/math_function.h" #include "paddle/fluid/operators/math/matrix_bit_code.h" #include "paddle/fluid/platform/transform.h" namespace paddle { namespace operators { template using EigenMatrix = framework::EigenMatrix; using platform::Transform; template class HierarchicalSigmoidOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* in = ctx.Input("X"); auto* w = ctx.Input("W"); auto* label = ctx.Input("Label"); auto* bias = ctx.Input("Bias"); auto* out = ctx.Output("Out"); auto* pre_out = ctx.Output("PreOut"); size_t num_classes = static_cast(ctx.Attr("num_classes")); int64_t code_length = math::FindLastSet(num_classes - 1); int64_t batch_size = in->dims()[0]; framework::Tensor sum; math::SetConstant zero; auto& dev_ctx = ctx.template device_context(); auto pre_out_data = pre_out->mutable_data( framework::make_ddim({batch_size, code_length}), ctx.GetPlace()); auto pre_out_mat = EigenMatrix::From(*pre_out); zero(dev_ctx, pre_out, static_cast(0.0)); auto& place = *ctx.template device_context().eigen_device(); math::RowwiseSum row_sum; math::MatrixBitCodeFunctor bit_code(num_classes, label->data()); std::vector sum_dims({batch_size, 1UL}); sum.mutable_data(framework::make_ddim(sum_dims), ctx.GetPlace()); auto sum_mat = EigenMatrix::From(sum); out->mutable_data(ctx.GetPlace()); auto out_mat = framework::EigenVector::Flatten(*out); if (bias) { bit_code.Add(pre_out, *bias); } bit_code.Mul(pre_out, *w, *in); // clip the matrix with (-40, 40) Transform trans; trans(ctx.template device_context(), pre_out_data, pre_out_data + pre_out->numel(), pre_out_data, ClipFunctor(static_cast(-40.0), static_cast(40.0))); bit_code.Sum(*pre_out, out, static_cast(-1)); // softrelu with threshold is 40.0 trans(ctx.template device_context(), pre_out_data, pre_out_data + pre_out->numel(), pre_out_data, ClipFunctor(static_cast(-40.0), static_cast(40.0))); pre_out_mat.device(place) = (static_cast(1.0) + pre_out_mat.exp()).log(); row_sum(dev_ctx, *pre_out, &sum); out_mat.device(place) = sum_mat + out_mat; } }; template class HierarchicalSigmoidGradOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* in = ctx.Input("X"); auto* w = ctx.Input("W"); auto* in_grad = ctx.Output(framework::GradVarName("X")); auto* w_grad = ctx.Output(framework::GradVarName("W")); auto* bias_grad = ctx.Output(framework::GradVarName("Bias")); auto* label = ctx.Input("Label"); auto* pre_out = ctx.Input("PreOut"); auto* out_grad = ctx.Input(framework::GradVarName("Out")); size_t num_classes = static_cast(ctx.Attr("num_classes")); int64_t code_length = math::FindLastSet(num_classes - 1); int64_t batch_size = in->dims()[0]; framework::Tensor pre_out_grad; pre_out_grad.mutable_data( framework::make_ddim({batch_size, code_length}), ctx.GetPlace()); auto& place = *ctx.template device_context().eigen_device(); auto pre_out_mat = EigenMatrix::From(*pre_out); auto pre_out_grad_mat = EigenMatrix::From(pre_out_grad); math::MatrixBitCodeFunctor bit_code(num_classes, label->data()); // softrelu derivative Eigen::array bcast({1, static_cast(pre_out_grad.dims()[1])}); auto out_grad_mat = EigenMatrix::From(*out_grad); pre_out_grad_mat = out_grad_mat.broadcast(bcast); pre_out_grad_mat.device(place) = pre_out_grad_mat * (static_cast(1.0) - static_cast(1.0) / pre_out_mat.exp()); bit_code.Sub(&pre_out_grad); if (bias_grad) { bias_grad->mutable_data(ctx.GetPlace()); bit_code.AddGrad(pre_out_grad, bias_grad); } in_grad->mutable_data(ctx.GetPlace()); w_grad->mutable_data(ctx.GetPlace()); bit_code.MulGradWeight(pre_out_grad, w_grad, *in); bit_code.MulGradError(pre_out_grad, *w, in_grad); } }; } // namespace operators } // namespace paddle