/* 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/platform/for_range.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template struct CosSimFunctor { CosSimFunctor(const T* x, const T* y, T* x_norm, T* y_norm, T* z, int cols) : x_norm_(x_norm), y_norm_(y_norm), x_(x), y_(y), z_(z), cols_(static_cast(cols)) {} inline HOSTDEVICE void operator()(size_t offset) const { auto* x = x_ + cols_ * offset; T xx = 0, xy = 0, yy = 0; if (same_row) { auto* y = y_ + cols_ * offset; T tep_x, tep_y; for (size_t i = 0; i < cols_; ++i) { tep_x = x[i]; tep_y = y[i]; xx += tep_x * tep_x; yy += tep_y * tep_y; xy += tep_x * tep_y; } xx = sqrt(xx); yy = sqrt(yy); y_norm_[offset] = yy; x_norm_[offset] = xx; z_[offset] = xy / (xx * yy); } else { // This can be wrote in a better way. T tep_x, tep_y; for (size_t i = 0; i < cols_; ++i) { tep_x = x[i]; tep_y = y_[i]; xx += tep_x * tep_x; yy += tep_y * tep_y; // only need xy += tep_x * tep_y; } xx = sqrt(xx); yy = sqrt(yy); if (offset == 0) y_norm_[0] = yy; x_norm_[offset] = xx; z_[offset] = xy / (xx * yy); } } T* x_norm_; T* y_norm_; const T* x_; const T* y_; T* z_; const size_t cols_; }; template class CosSimKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { // get Tensor auto* in_x = context.Input("X"); auto* in_y = context.Input("Y"); auto* out_z = context.Output("Out"); auto* out_x_norm = context.Output("XNorm"); auto* out_y_norm = context.Output("YNorm"); out_z->mutable_data(context.GetPlace()); out_x_norm->mutable_data(context.GetPlace()); out_y_norm->mutable_data(context.GetPlace()); int rows_x = in_x->dims()[0]; int rows_y = in_y->dims()[0]; int cols = framework::product(in_x->dims()) / rows_x; if (rows_x == rows_y) { CosSimFunctor functor( in_x->data(), in_y->data(), out_x_norm->data(), out_y_norm->data(), out_z->data(), cols); platform::ForRange for_range( static_cast(context.device_context()), rows_x); for_range(functor); } else { CosSimFunctor functor( in_x->data(), in_y->data(), out_x_norm->data(), out_y_norm->data(), out_z->data(), cols); platform::ForRange for_range( static_cast(context.device_context()), rows_x); for_range(functor); } } }; template struct CosSimGradFunctor { CosSimGradFunctor(const T* x_norm, const T* y_norm, const T* x, const T* y, const T* z, const T* dz, T* dx, int cols) : x_norm_(x_norm), y_norm_(y_norm), x_(x), y_(y), z_(z), dz_(dz), dx_(dx), cols_(static_cast(cols)) {} inline HOSTDEVICE void operator()(size_t offset) const { auto x_norm_square = x_norm_[offset] * x_norm_[offset]; auto xy_norm_prod = x_norm_[offset] * y_norm_[offset]; auto dz = dz_[offset]; auto z = z_[offset]; auto* dx = dx_ + cols_ * offset; auto* x = x_ + cols_ * offset; auto* y = y_ + cols_ * offset; auto reciprocal_xy_norm_prod = 1 / xy_norm_prod; auto reciprocal_x_norm_square = 1 / x_norm_square; for (size_t i = 0; i < cols_; ++i) { dx[i] = dz * (y[i] * reciprocal_xy_norm_prod - z * x[i] * reciprocal_x_norm_square); } } const T* x_norm_; const T* y_norm_; const T* x_; const T* y_; const T* z_; const T* dz_; T* dx_; const size_t cols_; }; template struct CosSimDxFunctor { CosSimDxFunctor(const T* x_norm, const T* y_norm, const T* x, const T* y, const T* z, const T* dz, T* dx, int cols) : x_norm_(x_norm), y_norm_(y_norm), x_(x), y_(y), z_(z), dz_(dz), dx_(dx), cols_(static_cast(cols)) {} inline HOSTDEVICE void operator()(size_t offset) const { auto xy_norm_prod = x_norm_[offset] * y_norm_[0]; auto dz = dz_[offset]; auto z = z_[offset]; auto* x = x_ + cols_ * offset; auto reciprocal_xy_norm_prod = 1 / xy_norm_prod; auto x_norm_square = x_norm_[offset] * x_norm_[offset]; auto* dx = dx_ + cols_ * offset; auto reciprocal_x_norm_square = 1 / x_norm_square; for (size_t i = 0; i < cols_; ++i) { dx[i] = dz * (y_[i] * reciprocal_xy_norm_prod - z * x[i] * reciprocal_x_norm_square); } } const T* x_norm_; const T* y_norm_; const T* x_; const T* y_; const T* z_; const T* dz_; T* dx_; const size_t cols_; }; template struct CosSimDyFunctor { inline void operator()(const DeviceContext& ctx, const T* x_norm, const T* y_norm, const T* x, const T* y, const T* z, const T* dz, const size_t rows, const size_t cols, T* dy) const; }; template class CosSimGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { // get Tensor auto* in_x = context.Input("X"); auto* in_y = context.Input("Y"); auto* in_z = context.Input("Out"); auto* in_x_norm = context.Input("XNorm"); auto* in_y_norm = context.Input("YNorm"); auto* out_grad_x = context.Output(framework::GradVarName("X")); auto* out_grad_y = context.Output(framework::GradVarName("Y")); auto* in_grad_z = context.Input(framework::GradVarName("Out")); // compute gradident int rows_x = in_x->dims()[0]; int rows_y = in_y->dims()[0]; int cols = framework::product(in_x->dims()) / rows_x; if (rows_x == rows_y) { if (out_grad_x) { CosSimGradFunctor functor( in_x_norm->data(), in_y_norm->data(), in_x->data(), in_y->data(), in_z->data(), in_grad_z->data(), out_grad_x->mutable_data(context.GetPlace()), cols); platform::ForRange for_range( static_cast(context.device_context()), rows_x); for_range(functor); } if (out_grad_y) { CosSimGradFunctor functor( in_y_norm->data(), in_x_norm->data(), in_y->data(), in_x->data(), in_z->data(), in_grad_z->data(), out_grad_y->mutable_data(context.GetPlace()), cols); platform::ForRange for_range( static_cast(context.device_context()), rows_x); for_range(functor); } } else { if (out_grad_x) { CosSimDxFunctor functor( in_x_norm->data(), in_y_norm->data(), in_x->data(), in_y->data(), in_z->data(), in_grad_z->data(), out_grad_x->mutable_data(context.GetPlace()), cols); platform::ForRange for_range( static_cast(context.device_context()), rows_x); for_range(functor); } if (out_grad_y) { out_grad_y->mutable_data(context.GetPlace()); math::SetConstant set_zero; auto& dev_ctx = context.template device_context(); set_zero(dev_ctx, out_grad_y, static_cast(0)); CosSimDyFunctor functor; functor(dev_ctx, in_x_norm->data(), in_y_norm->data(), in_x->data(), in_y->data(), in_z->data(), in_grad_z->data(), static_cast(rows_x), static_cast(cols), out_grad_y->data()); } } } }; } // namespace operators } // namespace paddle