/* 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/elementwise_op_function.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template static void ForEachZip(IT1 begin1, IT1 last1, IT2 begin2, Callback callback) { for (; begin1 < last1; ++begin1, ++begin2) { callback(*begin1, *begin2); } } 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 void operator()(T& x_norm, T& y_norm) const { size_t x_offset = &x_norm - x_norm_; size_t y_offset = &y_norm - y_norm_; auto* x = x_ + cols_ * x_offset; T xx = 0, xy = 0; T yy = 0; if (same_row) { auto* y = y_ + cols_ * y_offset; for (size_t i = 0; i < cols_; ++i) { xx += x[i] * x[i]; yy += y[i] * y[i]; xy += x[i] * y[i]; } xx = sqrt(xx); yy = sqrt(yy); x_norm_[x_offset] = xx; y_norm_[y_offset] = yy; z_[x_offset] = xy / (xx * yy); } else { // This can be wrote in a better way. auto* y = y_; for (size_t i = 0; i < cols_; ++i) { xx += x[i] * x[i]; yy += y[i] * y[i]; // only need xy += x[i] * y[i]; } xx = sqrt(xx); yy = sqrt(yy); x_norm_[x_offset] = xx; y_norm_[0] = yy; z_[x_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); ForEachZip(out_x_norm->data(), out_x_norm->data() + rows_x, out_y_norm->data(), functor); } else { CosSimFunctor functor( in_x->data(), in_y->data(), out_x_norm->data(), out_y_norm->data(), out_z->data(), cols); ForEachZip(out_x_norm->data(), out_x_norm->data() + rows_x, out_y_norm->data(), 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 void operator()(const T& x_norm, const T& y_norm) const { size_t x_offset = &x_norm - x_norm_; size_t y_offset = &y_norm - y_norm_; auto x_norm_square = x_norm_[x_offset] * x_norm_[x_offset]; auto xy_norm_prod = x_norm_[x_offset] * y_norm_[y_offset]; auto dz = dz_[x_offset]; auto z = z_[x_offset]; auto* dx = dx_ + cols_ * x_offset; auto* x = x_ + cols_ * x_offset; auto* y = y_ + cols_ * y_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, T* dy, int cols) : x_norm_(x_norm), y_norm_(y_norm), x_(x), y_(y), z_(z), dz_(dz), dx_(dx), dy_(dy), cols_(static_cast(cols)) {} inline void operator()(const T& x_norm, const T& y_norm) const { size_t x_offset = &x_norm - x_norm_; auto xy_norm_prod = x_norm_[x_offset] * y_norm_[0]; auto dz = dz_[x_offset]; auto z = z_[x_offset]; auto* x = x_ + cols_ * x_offset; auto reciprocal_xy_norm_prod = 1 / xy_norm_prod; if (Dx) { auto x_norm_square = x_norm_[x_offset] * x_norm_[x_offset]; auto* dx = dx_ + cols_ * x_offset; auto* x = x_ + cols_ * x_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); } } else { auto y_norm_square = y_norm_[0] * y_norm_[0]; auto reciprocal_y_norm_square = 1 / y_norm_square; for (size_t i = 0; i < cols_; ++i) { dy_[i] += dz * (x[i] * reciprocal_xy_norm_prod - z * y_[i] * reciprocal_y_norm_square); } } } const T* x_norm_; const T* y_norm_; const T* x_; const T* y_; const T* z_; const T* dz_; T* dx_; T* dy_; const size_t cols_; }; 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); ForEachZip(in_x_norm->data(), in_x_norm->data() + rows_x, in_y_norm->data(), 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); ForEachZip(in_y_norm->data(), in_y_norm->data() + rows_x, in_x_norm->data(), 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()), nullptr, cols); ForEachZip(in_x_norm->data(), in_x_norm->data() + rows_x, in_y_norm->data(), functor); } if (out_grad_y) { CosSimDxFunctor functor( in_x_norm->data(), in_y_norm->data(), in_x->data(), in_y->data(), in_z->data(), in_grad_z->data(), nullptr, out_grad_y->mutable_data(context.GetPlace()), cols); ForEachZip(in_x_norm->data(), in_x_norm->data() + rows_x, in_y_norm->data(), functor); } } } }; } // namespace operators } // namespace paddle