/* Copyright (c) 2020 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. */ #ifdef PADDLE_WITH_XPU #include #include #include #include #include "paddle/fluid/framework/op_registry.h" namespace paddle { namespace operators { using framework::OpKernelType; using framework::Tensor; template class MulXPUKernel : 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::ReshapeToMatrix( *x, context.template Attr("x_num_col_dims")) : *x; const Tensor y_matrix = y->dims().size() > 2 ? framework::ReshapeToMatrix( *y, context.template Attr("y_num_col_dims")) : *y; z->mutable_data(context.GetPlace()); auto z_dim = z->dims(); if (z_dim.size() != 2) { z->Resize({x_matrix.dims()[0], y_matrix.dims()[1]}); } bool trans_a = false; bool trans_b = false; int m = x_matrix.dims()[0]; int k = x_matrix.dims()[1]; int k1 = y_matrix.dims()[0]; int n = y_matrix.dims()[1]; PADDLE_ENFORCE_EQ( k, k1, platform::errors::InvalidArgument("Shape mistake in mul_op")); T alpha = static_cast(1.0); T beta = static_cast(0.0); const T* data_a = x_matrix.data(); const T* data_b = y_matrix.data(); T* data_c = z->data(); auto& dev_ctx = context.template device_context(); int ret = xpu::fc_int16(dev_ctx.x_context(), trans_a, trans_b, m, n, k, alpha, data_a, data_b, beta, data_c); PADDLE_ENFORCE_EQ( ret, XPU_SUCCESS, platform::errors::External( "XPU API return wrong value[%d], please check whether " "Baidu Kunlun Card is properly installed.", ret)); if (z_dim.size() != 2) { z->Resize(z_dim); } } }; template class MulGradXPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { int x_num_col_dims = ctx.template Attr("x_num_col_dims"); int y_num_col_dims = ctx.template Attr("y_num_col_dims"); auto* x = ctx.Input("X"); auto* y = ctx.Input("Y"); auto x_matrix = x->dims().size() > 2 ? framework::ReshapeToMatrix(*x, x_num_col_dims) : static_cast(*x); auto y_matrix = y->dims().size() > 2 ? framework::ReshapeToMatrix(*y, y_num_col_dims) : static_cast(*y); auto* dout = ctx.Input(framework::GradVarName("Out")); Tensor dout_mat; dout_mat.Resize({phi::flatten_to_2d(x->dims(), x_num_col_dims)[0], phi::flatten_to_2d(y->dims(), y_num_col_dims)[1]}); auto* dx = ctx.Output(framework::GradVarName("X")); auto* dy = ctx.Output(framework::GradVarName("Y")); if (dx != nullptr) { dx->set_lod(x->lod()); } if (dy != nullptr) { dy->set_lod(y->lod()); } auto& dev_ctx = ctx.template device_context(); if (dx) { dx->mutable_data(ctx.GetPlace()); Tensor dx_matrix = dx->dims().size() > 2 ? framework::ReshapeToMatrix(*dx, x_num_col_dims) : *dx; // dx = dout * y'. dx: M x K, dout : M x N, y : K x N // blas.MatMul(dout_mat, false, y_matrix, true, &dx_matrix); bool trans_a = false; bool trans_b = true; int m = dout_mat.dims()[0]; int k = dout_mat.dims()[1]; int n = y_matrix.dims()[0]; int k1 = y_matrix.dims()[1]; PADDLE_ENFORCE_EQ( k, k1, platform::errors::InvalidArgument("Shape mistake in mul_op")); int lda = (!trans_a) ? k : m; int ldb = (!trans_b) ? n : k; int ldc = n; T alpha = static_cast(1.0); T beta = static_cast(0.0); const T* data_a = dout->data(); const T* data_b = y_matrix.data(); T* data_c = dx_matrix.data(); int ret = xpu::gemm_int16(dev_ctx.x_context(), trans_a, trans_b, m, n, k, alpha, data_a, lda, data_b, ldb, beta, data_c, ldc); PADDLE_ENFORCE_EQ(ret, XPU_SUCCESS, platform::errors::External( "XPU API return wrong value[%d], please check " "where Baidu Kunlun Card is properly installed.", ret)); } if (dy) { dy->mutable_data(ctx.GetPlace()); Tensor dy_matrix = dy->dims().size() > 2 ? framework::ReshapeToMatrix(*dy, y_num_col_dims) : *dy; // dy = x' * dout. dy K x N, dout : M x N, x : M x K // blas.MatMul(x_matrix, true, dout_mat, false, &dy_matrix); bool trans_a = true; bool trans_b = false; int k = x_matrix.dims()[0]; int m = x_matrix.dims()[1]; int k1 = dout_mat.dims()[0]; int n = dout_mat.dims()[1]; PADDLE_ENFORCE_EQ( k, k1, platform::errors::InvalidArgument("Shape mistake in mul_op")); int lda = (!trans_a) ? k : m; int ldb = (!trans_b) ? n : k; int ldc = n; T alpha = static_cast(1.0); T beta = static_cast(0.0); const T* data_a = x_matrix.data(); const T* data_b = dout->data(); T* data_c = dy_matrix.data(); int ret = xpu::gemm_int16(dev_ctx.x_context(), trans_a, trans_b, m, n, k, alpha, data_a, lda, data_b, ldb, beta, data_c, ldc); PADDLE_ENFORCE_EQ(ret, XPU_SUCCESS, platform::errors::External( "XPU API return wrong value[%d], please check " "where Baidu Kunlun Card is properly installed.", ret)); } } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_XPU_KERNEL( mul, ops::MulXPUKernel); REGISTER_OP_XPU_KERNEL( mul_grad, ops::MulGradXPUKernel) #endif