/* 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/operators/math/math_function.h" #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template using EigenMatrix = framework::EigenMatrix; template class MulKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { const Tensor* X = context.Input("X"); const Tensor* Y = context.Input("Y"); Tensor* Z = context.Output("Out"); const Tensor X_matrix = X->dims().size() > 2 ? framework::FlattenToMatrix( *X, context.template GetAttr("x_num_row_dims")) : *X; const Tensor Y_matrix = Y->dims().size() > 2 ? framework::FlattenToMatrix( *Y, context.template GetAttr("y_num_row_dims")) : *Y; Z->mutable_data(context.GetPlace()); auto* device_context = const_cast(context.device_context_); math::matmul(X_matrix, false, Y_matrix, false, 1, Z, 0, device_context); } }; template class MulGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { int x_num_row_dims = ctx.template GetAttr("x_num_row_dims"); int y_num_row_dims = ctx.template GetAttr("y_num_row_dims"); const Tensor* X = ctx.Input("X"); const Tensor* Y = ctx.Input("Y"); const Tensor X_matrix = X->dims().size() > 2 ? framework::FlattenToMatrix(*X, x_num_row_dims) : *X; const Tensor Y_matrix = Y->dims().size() > 2 ? framework::FlattenToMatrix(*Y, y_num_row_dims) : *Y; const Tensor* dOut = ctx.Input(framework::GradVarName("Out")); Tensor* dX = ctx.Output(framework::GradVarName("X")); Tensor* dY = ctx.Output(framework::GradVarName("Y")); dX->mutable_data(ctx.GetPlace()); dY->mutable_data(ctx.GetPlace()); Tensor dX_matrix = dX->dims().size() > 2 ? framework::FlattenToMatrix(*dX, x_num_row_dims) : *dX; Tensor dY_matrix = dY->dims().size() > 2 ? framework::FlattenToMatrix(*dY, y_num_row_dims) : *dY; auto* device_context = const_cast(ctx.device_context_); // dX = dOut * Y'. dX: M x K, dOut : M x N, Y : K x N math::matmul(*dOut, false, Y_matrix, true, 1, &dX_matrix, 0, device_context); // dY = X' * dOut. dY: K x N, dOut : M x N, X : M x K math::matmul(X_matrix, true, *dOut, false, 1, &dY_matrix, 0, device_context); } }; } // namespace operators } // namespace paddle