/* Copyright (c) 2016 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 "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/math/cross_entropy.h" #include "paddle/fluid/operators/math/math_function.h" #include "paddle/fluid/platform/for_range.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template class CrossEntropyOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* x = ctx.Input("X"); auto* labels = ctx.Input("Label"); auto* y = ctx.Output("Y"); y->mutable_data(ctx.GetPlace()); int rank = x->dims().size(); Tensor x_2d = framework::ReshapeToMatrix(*x, rank - 1); Tensor labels_2d = framework::ReshapeToMatrix(*labels, rank - 1); Tensor y_2d = framework::ReshapeToMatrix(*y, rank - 1); math::CrossEntropyFunctor()( ctx.template device_context(), &y_2d, &x_2d, &labels_2d, ctx.Attr("soft_label")); } }; template class XeSoftlabelGradFunctor { public: XeSoftlabelGradFunctor(T* dx, const T* dy, // NOLINT const T* x, // NOLINT const T* label, // NOLINT size_t num_classes) : dx_(dx), dy_(dy), x_(x), label_(label), num_classes_(num_classes) {} HOSTDEVICE void operator()(size_t i) { auto row_ids = i / num_classes_; dx_[i] = -label_[i] * dy_[row_ids] / x_[i]; } private: T* dx_; const T* dy_; const T* x_; const T* label_; size_t num_classes_; }; template class XeGradFunctor { public: XeGradFunctor(T* dx, const T* dy, // NOLINT const T* x, // NOLINT const int64_t* label, // NOLINT size_t num_classes) : dx_(dx), dy_(dy), x_(x), label_(label), num_classes_(num_classes) {} HOSTDEVICE void operator()(size_t sample_id) { auto x_is_true_offset = sample_id * num_classes_ + label_[sample_id]; for (size_t x_offset = sample_id * num_classes_; x_offset < (sample_id + 1) * num_classes_; ++x_offset) { dx_[x_offset] = x_offset != x_is_true_offset ? static_cast(0) : -dy_[sample_id] / x_[x_offset]; } } private: T* dx_; const T* dy_; const T* x_; const int64_t* label_; size_t num_classes_; }; template class CrossEntropyGradientOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* x = ctx.Input("X"); auto* dy = ctx.Input(framework::GradVarName("Y")); auto* label = ctx.Input("Label"); auto* dx = ctx.Output(framework::GradVarName("X")); T* dx_data = dx->mutable_data(ctx.GetPlace()); // Following computation only depends on the last dimension size. So it's // unnecessary to convert tensors to 2-D views. int rank = x->dims().size(); int64_t class_num = x->dims()[rank - 1]; if (ctx.Attr("soft_label")) { XeSoftlabelGradFunctor functor(dx_data, dy->data(), x->data(), label->data(), static_cast(class_num)); platform::ForRange for_range( ctx.template device_context(), static_cast(dx->numel())); for_range(functor); } else { XeGradFunctor functor(dx_data, dy->data(), x->data(), label->data(), static_cast(class_num)); platform::ForRange for_range( ctx.template device_context(), static_cast(dy->numel())); for_range(functor); } } }; } // namespace operators } // namespace paddle