/* Copyright (c) 2022 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/phi/core/dense_tensor.h" #include "paddle/phi/kernels/funcs/blas/blas.h" #include "paddle/phi/kernels/funcs/matrix_inverse.h" namespace phi { template void MatrixPowerGradFunction(const DenseTensor* X, const DenseTensor* Out, const DenseTensor* dOut, const int n, DenseTensor* dX, const Context& ctx) { ctx.template Alloc(dX); const auto& x_dims = X->dims(); auto blas = phi::funcs::GetBlas(ctx); if (n == 0) { // \nabla X = O phi::funcs::SetConstant zero; zero(ctx, dX, static_cast(0)); return; } else if (n == 1) { // \nabla X = \nabla Out paddle::framework::TensorCopy(*dOut, ctx.GetPlace(), ctx, dX); return; } auto trans_desc = phi::funcs::CreateMatrixDescriptor(x_dims, 0, true); auto no_trans_desc = phi::funcs::CreateMatrixDescriptor(x_dims, 0, false); if (n == -1) { // \nabla X = Out^{T} * \nabla Out * Out^{T} DenseTensor temp_dx; temp_dx.Resize(X->dims()); ctx.template Alloc(&temp_dx); blas.MatMul(*Out, trans_desc, *dOut, no_trans_desc, static_cast(-1), &temp_dx, static_cast(0)); blas.MatMul(temp_dx, no_trans_desc, *Out, trans_desc, static_cast(1), dX, static_cast(0)); return; } DenseTensor new_x; new_x.Resize(X->dims()); ctx.template Alloc(&new_x); int new_n = n; if (n > 0) { // newX = X paddle::framework::TensorCopy(*X, ctx.GetPlace(), ctx, &new_x); } else { // newX = X^{-1}, n = -n phi::funcs::MatrixInverseFunctor mat_inv; mat_inv(ctx, *X, &new_x); new_n = -n; } // Use chain rule blow to compute \nabla newX^{n} // First, Get newX^{0}, newX^{1}, ..., newX^{n - 1}, // Note that newX^{0} can be omitted std::vector> tensor_list(new_n - 1); tensor_list[0] = std::make_shared(new_x); int index = 1; while (index < new_n - 1) { DenseTensor tensor_list_index; tensor_list_index.Resize(X->dims()); ctx.template Alloc(&tensor_list_index); tensor_list[index] = std::make_shared(tensor_list_index); blas.MatMul(*tensor_list[index - 1], no_trans_desc, new_x, no_trans_desc, static_cast(1), tensor_list[index].get(), static_cast(0)); index++; } // Second, \nabla newX = \sum_{i = 0}^{n - 1} (newX^{T}^{i} // * \nabla Out // * (newX^{T}^{n - i - 1}) DenseTensor dx_new; dx_new.Resize(X->dims()); ctx.template Alloc(&dx_new); blas.MatMul(*tensor_list[new_n - 2], trans_desc, *dOut, no_trans_desc, static_cast(1), &dx_new, static_cast(0)); DenseTensor da_an_minus1; da_an_minus1.Resize(X->dims()); ctx.template Alloc(&da_an_minus1); blas.MatMul(*dOut, no_trans_desc, *tensor_list[new_n - 2], trans_desc, static_cast(1), &da_an_minus1, static_cast(0)); blas.AXPY( X->numel(), static_cast(1), da_an_minus1.data(), dx_new.data()); int start = 0; while (start < new_n - 2) { DenseTensor a_da; a_da.Resize(X->dims()); ctx.template Alloc(&a_da); DenseTensor a_da_a; a_da_a.Resize(X->dims()); ctx.template Alloc(&a_da_a); blas.MatMul(*tensor_list[start], trans_desc, *dOut, no_trans_desc, static_cast(1), &a_da, static_cast(0)); blas.MatMul(a_da, no_trans_desc, *tensor_list[new_n - 3 - start], trans_desc, static_cast(1), &a_da_a, static_cast(0)); blas.AXPY( X->numel(), static_cast(1), a_da_a.data(), dx_new.data()); start++; } if (n > 0) { // \nabla X = \nabla newX paddle::framework::TensorCopy(dx_new, ctx.GetPlace(), ctx, dX); } else { // \nabla X = newX^{T} * \nabla newX * newX^{T} DenseTensor temp_dx; temp_dx.Resize(X->dims()); ctx.template Alloc(&temp_dx); blas.MatMul(new_x, trans_desc, dx_new, no_trans_desc, static_cast(-1), &temp_dx, static_cast(0)); blas.MatMul(temp_dx, no_trans_desc, new_x, trans_desc, static_cast(1), dX, static_cast(0)); } return; } template void MatrixPowerGradKernel(const Context& ctx, const DenseTensor& x, const DenseTensor& out, const DenseTensor& out_grad, int n, DenseTensor* x_grad) { auto X = &x; auto Out = &out; auto dOut = &out_grad; auto dX = x_grad; MatrixPowerGradFunction(X, Out, dOut, n, dX, ctx); } } // namespace phi