|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include "paddle/framework/eigen.h" |
| 18 | +#include "paddle/framework/op_registry.h" |
| 19 | +#include "paddle/operators/math/math_function.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace operators { |
| 23 | + |
| 24 | +using framework::Tensor; |
| 25 | + |
| 26 | +template <typename T, int MajorType = Eigen::RowMajor, |
| 27 | + typename IndexType = Eigen::DenseIndex> |
| 28 | +using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>; |
| 29 | + |
| 30 | +template <typename Place, typename T> |
| 31 | +class BilinearTensorProductKernel : public framework::OpKernel<T> { |
| 32 | + public: |
| 33 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 34 | + auto* x = ctx.Input<Tensor>("X"); |
| 35 | + auto* y = ctx.Input<Tensor>("Y"); |
| 36 | + auto* weight = ctx.Input<Tensor>("Weight"); |
| 37 | + auto* bias = ctx.Input<Tensor>("Bias"); |
| 38 | + auto* out = ctx.Output<Tensor>("Out"); |
| 39 | + out->mutable_data<T>(ctx.GetPlace()); |
| 40 | + |
| 41 | + auto y_mat = EigenMatrix<T>::From(*y); |
| 42 | + auto output_mat = EigenMatrix<T>::From(*out); |
| 43 | + |
| 44 | + auto batch_size = x->dims()[0]; |
| 45 | + auto weight_dims = weight->dims(); |
| 46 | + int out_dim = weight_dims[0]; |
| 47 | + auto x_dim = weight_dims[1]; |
| 48 | + auto y_dim = weight_dims[2]; |
| 49 | + auto place = ctx.GetEigenDevice<Place>(); |
| 50 | + |
| 51 | + // Create the intermediate variable to caculate the result of |
| 52 | + // Input(X) multiplied by Input(Weight_i), the formula is: |
| 53 | + // left_mul = X Weight_i. |
| 54 | + Tensor left_mul; |
| 55 | + left_mul.mutable_data<T>(framework::make_ddim({batch_size, y_dim}), |
| 56 | + ctx.GetPlace()); |
| 57 | + auto left_mul_mat = EigenMatrix<T>::From(left_mul); |
| 58 | + |
| 59 | + for (int i = 0; i < out_dim; ++i) { |
| 60 | + auto output_col_vec = output_mat.chip(i, 1); |
| 61 | + Tensor weight_mat = |
| 62 | + weight->Slice(i, i + 1).Resize(framework::make_ddim({x_dim, y_dim})); |
| 63 | + math::gemm<Place, T>(ctx.device_context(), CblasNoTrans, CblasNoTrans, |
| 64 | + batch_size, y_dim, x_dim, 1, x->data<T>(), |
| 65 | + weight_mat.data<T>(), 0, left_mul.data<T>()); |
| 66 | + output_col_vec.device(place) = |
| 67 | + (left_mul_mat * y_mat).sum(Eigen::DSizes<int, 1>(1)); |
| 68 | + } |
| 69 | + if (bias) { |
| 70 | + auto bias_vec = EigenMatrix<T>::From(*bias); |
| 71 | + Eigen::DSizes<int, 2> bcast(batch_size, 1); |
| 72 | + output_mat.device(place) = bias_vec.broadcast(bcast) + output_mat; |
| 73 | + } |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +template <typename Place, typename T> |
| 78 | +class BilinearTensorProductGradKernel : public framework::OpKernel<T> { |
| 79 | + public: |
| 80 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 81 | + const Tensor* x = ctx.Input<Tensor>("X"); |
| 82 | + const Tensor* y = ctx.Input<Tensor>("Y"); |
| 83 | + const Tensor* weight = ctx.Input<Tensor>("Weight"); |
| 84 | + Tensor* d_x = ctx.Output<Tensor>(framework::GradVarName("X")); |
| 85 | + Tensor* d_y = ctx.Output<Tensor>(framework::GradVarName("Y")); |
| 86 | + Tensor* d_weight = ctx.Output<Tensor>(framework::GradVarName("Weight")); |
| 87 | + Tensor* d_bias = ctx.Output<Tensor>(framework::GradVarName("Bias")); |
| 88 | + const Tensor* d_out = ctx.Input<Tensor>(framework::GradVarName("Out")); |
| 89 | + |
| 90 | + auto batch_size = x->dims()[0]; |
| 91 | + auto weight_dims = weight->dims(); |
| 92 | + int out_dim = weight_dims[0]; |
| 93 | + auto x_dim = weight_dims[1]; |
| 94 | + auto y_dim = weight_dims[2]; |
| 95 | + |
| 96 | + auto x_mat = EigenMatrix<T>::From(*x); |
| 97 | + auto y_mat = EigenMatrix<T>::From(*y); |
| 98 | + auto d_out_mat = EigenMatrix<T>::From(*d_out); |
| 99 | + auto place = ctx.GetEigenDevice<Place>(); |
| 100 | + |
| 101 | + // Create the intermediate variable to caculate the Output(Y@Grad). |
| 102 | + Tensor x_scale; |
| 103 | + x_scale.mutable_data<T>(framework::make_ddim({batch_size, x_dim}), |
| 104 | + ctx.GetPlace()); |
| 105 | + auto x_scale_mat = EigenMatrix<T>::From(x_scale); |
| 106 | + |
| 107 | + // Create the intermediate variable to caculate the Output(X@Grad). |
| 108 | + Tensor y_scale; |
| 109 | + y_scale.mutable_data<T>(framework::make_ddim({batch_size, y_dim}), |
| 110 | + ctx.GetPlace()); |
| 111 | + auto y_scale_mat = EigenMatrix<T>::From(y_scale); |
| 112 | + |
| 113 | + math::SetConstant<Place, T> set_zero; |
| 114 | + |
| 115 | + // Set Output(X@Grad) be zero. |
| 116 | + if (d_x) { |
| 117 | + d_x->mutable_data<T>(ctx.GetPlace()); |
| 118 | + set_zero(ctx.device_context(), d_x, static_cast<T>(0)); |
| 119 | + } |
| 120 | + |
| 121 | + // Set Output(Y@Grad) be zero. |
| 122 | + if (d_y) { |
| 123 | + d_y->mutable_data<T>(ctx.GetPlace()); |
| 124 | + set_zero(ctx.device_context(), d_y, static_cast<T>(0)); |
| 125 | + } |
| 126 | + |
| 127 | + // Caculate the Output(X@Grad) and Output(Y@Grad). |
| 128 | + if (d_x || d_y) { |
| 129 | + Eigen::DSizes<int, 2> bcast_for_x(1, y_dim); |
| 130 | + Eigen::DSizes<int, 2> bcast_for_y(1, x_dim); |
| 131 | + for (int i = 0; i < out_dim; ++i) { |
| 132 | + Tensor weight_i = weight->Slice(i, i + 1).Resize( |
| 133 | + framework::make_ddim({x_dim, y_dim})); |
| 134 | + auto output_vec = d_out_mat.chip(i, 1); |
| 135 | + if (d_x) { |
| 136 | + y_scale_mat.device(place) = |
| 137 | + output_vec.reshape(Eigen::DSizes<int, 2>(batch_size, 1)) |
| 138 | + .broadcast(bcast_for_x) * |
| 139 | + y_mat; |
| 140 | + math::gemm<Place, T>(ctx.device_context(), CblasNoTrans, CblasTrans, |
| 141 | + batch_size, x_dim, y_dim, 1, y_scale.data<T>(), |
| 142 | + weight_i.data<T>(), 1, d_x->data<T>()); |
| 143 | + } |
| 144 | + if (d_y) { |
| 145 | + x_scale_mat.device(place) = |
| 146 | + output_vec.reshape(Eigen::DSizes<int, 2>(batch_size, 1)) |
| 147 | + .broadcast(bcast_for_y) * |
| 148 | + x_mat; |
| 149 | + math::gemm<Place, T>(ctx.device_context(), CblasNoTrans, CblasNoTrans, |
| 150 | + batch_size, y_dim, x_dim, 1, x_scale.data<T>(), |
| 151 | + weight_i.data<T>(), 1, d_y->data<T>()); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Caculate the gradient of Input(Weight). |
| 157 | + if (d_weight) { |
| 158 | + d_weight->mutable_data<T>(ctx.GetPlace()); |
| 159 | + Eigen::DSizes<int, 2> bcast_for_weight(1, x_dim); |
| 160 | + for (int i = 0; i < out_dim; ++i) { |
| 161 | + Tensor d_weight_i = d_weight->Slice(i, i + 1).Resize( |
| 162 | + framework::make_ddim({x_dim, y_dim})); |
| 163 | + auto output_vec = d_out_mat.chip(i, 1); |
| 164 | + x_scale_mat.device(place) = |
| 165 | + output_vec.reshape(Eigen::DSizes<int, 2>(batch_size, 1)) |
| 166 | + .broadcast(bcast_for_weight) * |
| 167 | + x_mat; |
| 168 | + math::gemm<Place, T>(ctx.device_context(), CblasTrans, CblasNoTrans, |
| 169 | + x_dim, y_dim, batch_size, 1, x_scale.data<T>(), |
| 170 | + y->data<T>(), 0, d_weight_i.data<T>()); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // Caculate the gradient of Input(Bias). |
| 175 | + if (d_bias) { |
| 176 | + d_bias->mutable_data<T>(ctx.GetPlace()); |
| 177 | + auto d_bias_mat = EigenMatrix<T>::From(*d_bias); |
| 178 | + d_bias_mat.device(place) = d_out_mat.sum(Eigen::DSizes<int, 1>(0)); |
| 179 | + } |
| 180 | + } |
| 181 | +}; |
| 182 | + |
| 183 | +} // namespace operators |
| 184 | +} // namespace paddle |
0 commit comments