/* 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 "paddle/framework/op_registry.h" #include "paddle/operators/math/math_function.h" #include "paddle/operators/math/matrix_bit_code.h" namespace paddle { namespace operators { template using EigenMatrix = framework::EigenMatrix; template class HierarchicalSigmoidOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto ins = ctx.MultiInput("X"); auto params = ctx.MultiInput("Parameters"); auto* label = ctx.Input("Label"); auto* bias = ctx.Input("Bias"); auto* out = ctx.Output("Out"); size_t num_classes = static_cast(ctx.Attr("num_classes")); framework::Tensor sum; framework::Tensor pre_out; auto place = ctx.GetEigenDevice(); auto& device_ctx = ctx.device_context(); math::ColwiseSum col_sum; math::RowwiseSum row_sum; auto pre_out_mat = EigenMatrix::From(pre_out); int64_t batch_size = ins[0]->dims()[0]; int64_t code_length = math::FindLastSet(num_classes - 1); std::vector pre_out_dims({batch_size, code_length}); pre_out.mutable_data(framework::make_ddim(pre_out_dims), ctx.GetPlace()); std::vector sum_dims({batch_size, 1UL}); sum.mutable_data(framework::make_ddim(sum_dims), ctx.GetPlace()); out->mutable_data(ctx.GetPlace()); if (bias) { math::AddByBitCode(num_classes, *label, pre_out, *bias); } for (size_t i = 0; i < ins.size(); ++i) { math::MulByBitCode(num_classes, *label, pre_out, *params[i], *ins[i]); } // clip the matrix with (-40, 40) pre_out_mat.device(place) = pre_out_mat.abs().cwiseMax(static_cast(40.0)); math::SumByBitCode(num_classes, *label, *out, pre_out, static_cast(-1)); // softrelu with threshold is 40.0 pre_out_mat.device(place) = pre_out_mat.abs().cwiseMax(static_cast(40.0)); pre_out_mat.device(place) = (static_cast(1.0) + pre_out_mat.exp()).log(); row_sum(device_ctx, pre_out, &sum); col_sum(device_ctx, *out, &sum); } }; template class HierarchicalSigmoidGradOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto ins = ctx.MultiInput("X"); auto ins_grad = ctx.MultiOutput(framework::GradVarName("X")); auto params = ctx.MultiOutput( framework::GradVarName("Parameters")); auto* bias = ctx.Output(framework::GradVarName("Bias")); auto* label = ctx.Output(framework::GradVarName("Label")); size_t num_classes = static_cast(ctx.Attr("num_classes")); framework::Tensor pre_out; auto place = ctx.GetEigenDevice(); auto& dev_ctx = ctx.device_context(); int64_t batch_size = ins_grad.size(); int64_t code_length = math::FindLastSet(num_classes - 1); auto pre_out_mat = EigenMatrix::From(pre_out); // init pre_out matrix with {1.0} std::vector pre_out_dims({batch_size, code_length}); pre_out.mutable_data(framework::make_ddim(pre_out_dims), ctx.GetPlace()); math::SetConstant set; set(dev_ctx, &pre_out, static_cast(1.0)); // softrelu derivative pre_out_mat.device(place) = pre_out_mat * (static_cast(1.0) - static_cast(1.0) / pre_out_mat); math::SubByBitCode(num_classes, *label, pre_out); if (bias) { math::AddByBitCodeGrad(num_classes, *label, pre_out, *bias); } for (size_t i = 0; i < ins_grad.size(); ++i) { math::MulByBitCodeGradWeight(num_classes, *label, pre_out, *params[i], *ins[i]); math::MulByBitCodeGradError(num_classes, *label, pre_out, *params[i], *ins_grad[i]); } } }; } // namespace operators } // namespace paddle