/* 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/clip_op.h" #include "paddle/operators/math/math_function.h" #include "paddle/operators/math/matrix_bit_code.h" #include "paddle/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* params = ctx.Input("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")); int64_t code_length = math::FindLastSet(num_classes - 1); int64_t batch_size = in->dims()[0]; auto* ids = label->data(); framework::Tensor pre_out; framework::Tensor sum; 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); auto& place = *ctx.template device_context().eigen_device(); auto& device_ctx = ctx.template device_context(); math::RowwiseSum row_sum; math::MatrixBitCodeFunctor bit_code; 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(num_classes, ids, pre_out, *bias); } for (int i = 0; i < in->dims()[0]; ++i) { bit_code.Mul(num_classes, ids, pre_out, params->Slice(i, i + 1), in->Slice(i, i + 1)); } // 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(num_classes, ids, 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(device_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* in_grad = ctx.Output(framework::GradVarName("X")); auto* params = ctx.Output(framework::GradVarName("Parameters")); auto* bias = ctx.Output(framework::GradVarName("Bias")); auto* label = ctx.Input("Label"); 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; pre_out.mutable_data(framework::make_ddim({batch_size, code_length}), ctx.GetPlace()); auto& place = *ctx.template device_context().eigen_device(); auto& device_ctx = ctx.template device_context(); auto pre_out_mat = EigenMatrix::From(pre_out); auto* ids = label->data(); // init pre_out matrix with {1.0} math::SetConstant one; math::MatrixBitCodeFunctor bit_code; one(device_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); bit_code.Sub(num_classes, ids, pre_out); if (bias) { bit_code.AddGrad(num_classes, ids, pre_out, *bias); } for (int i = 0; i < in_grad->dims()[0]; ++i) { auto p_sliced = params->Slice(i, i + 1); auto in_sliced = in->Slice(i, i + 1); auto in_grad_sliced = in_grad->Slice(i, i + 1); bit_code.MulGradWeight(num_classes, ids, pre_out, p_sliced, in_sliced); bit_code.MulGradError(num_classes, ids, pre_out, p_sliced, in_grad_sliced); } } }; } // namespace operators } // namespace paddle