mul_op.h 3.8 KB
Newer Older
1 2 3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   Licensed under the Apache License, Version 2.0 (the "License");
F
fengjiayi 已提交
4 5
   You may not use this file except in compliance with the License.
   You may obtain a copy of the License at
6 7 8 9 10

   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,
F
fengjiayi 已提交
11
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 13 14 15
   See the License for the specific language governing permissions and
   limitations under the License. */

#pragma once
Y
Yu Yang 已提交
16

Q
qijun 已提交
17
#include "paddle/operators/math/math_function.h"
Q
qijun 已提交
18

D
dongzhihong 已提交
19 20
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
21 22 23 24

namespace paddle {
namespace operators {

D
dongzhihong 已提交
25 26 27 28 29
using Tensor = framework::Tensor;
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;

Q
qijun 已提交
30
template <typename Place, typename T>
D
dongzhihong 已提交
31
class MulKernel : public framework::OpKernel {
32
 public:
D
dongzhihong 已提交
33
  void Compute(const framework::ExecutionContext& context) const override {
34 35
    const Tensor* x = context.Input<Tensor>("X");
    const Tensor* y = context.Input<Tensor>("Y");
F
fengjiayi 已提交
36
    Tensor* z = context.Output<Tensor>("Out");
37 38
    const Tensor x_matrix =
        x->dims().size() > 2
F
fengjiayi 已提交
39 40
            ? framework::ReshapeToMatrix<T>(
                  *x, context.template GetAttr<int>("x_num_col_dims"))
41 42 43
            : *x;
    const Tensor y_matrix =
        y->dims().size() > 2
F
fengjiayi 已提交
44 45
            ? framework::ReshapeToMatrix<T>(
                  *y, context.template GetAttr<int>("y_num_col_dims"))
46
            : *y;
47

F
fengjiayi 已提交
48
    z->mutable_data<T>(context.GetPlace());
D
dongzhihong 已提交
49 50
    auto* device_context =
        const_cast<platform::DeviceContext*>(context.device_context_);
F
fengjiayi 已提交
51
    math::matmul<Place, T>(x_matrix, false, y_matrix, false, 1, z, 0,
52
                           device_context);
53 54
  }
};
D
dongzhihong 已提交
55

D
dongzhihong 已提交
56 57 58 59
template <typename Place, typename T>
class MulGradKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
F
fengjiayi 已提交
60 61
    int x_num_col_dims = ctx.template GetAttr<int>("x_num_col_dims");
    int y_num_col_dims = ctx.template GetAttr<int>("y_num_col_dims");
62 63 64
    const Tensor* x = ctx.Input<Tensor>("X");
    const Tensor* y = ctx.Input<Tensor>("Y");
    const Tensor x_matrix =
F
fengjiayi 已提交
65
        x->dims().size() > 2 ? framework::ReshapeToMatrix<T>(*x, x_num_col_dims)
66 67
                             : *x;
    const Tensor y_matrix =
F
fengjiayi 已提交
68
        y->dims().size() > 2 ? framework::ReshapeToMatrix<T>(*y, y_num_col_dims)
69 70
                             : *y;
    const Tensor* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
D
dongzhihong 已提交
71

72 73
    Tensor* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    Tensor* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
D
dongzhihong 已提交
74 75
    auto* device_context =
        const_cast<platform::DeviceContext*>(ctx.device_context_);
76 77
    if (dx) {
      dx->mutable_data<T>(ctx.GetPlace());
F
fengjiayi 已提交
78 79
      Tensor dx_matrix = dx->dims().size() > 2 ? framework::ReshapeToMatrix<T>(
                                                     *dx, x_num_col_dims)
80
                                               : *dx;
81
      // dx = dout * y'. dx: M x K, dout : M x N, y : K x N
82 83
      math::matmul<Place, T>(*dout, false, y_matrix, true, 1, &dx_matrix, 0,
                             device_context);
84 85 86
    }
    if (dy) {
      dy->mutable_data<T>(ctx.GetPlace());
F
fengjiayi 已提交
87 88
      Tensor dy_matrix = dy->dims().size() > 2 ? framework::ReshapeToMatrix<T>(
                                                     *dy, y_num_col_dims)
89
                                               : *dy;
90
      // dy = x' * dout. dy K x N, dout : M x N, x : M x K
91 92
      math::matmul<Place, T>(x_matrix, true, *dout, false, 1, &dy_matrix, 0,
                             device_context);
93
    }
D
dongzhihong 已提交
94 95 96
  }
};

97 98
}  // namespace operators
}  // namespace paddle