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
using Tensor = framework::Tensor;

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

F
fengjiayi 已提交
45
    z->mutable_data<T>(context.GetPlace());
Y
Yu Yang 已提交
46 47 48 49
    auto z_dim = z->dims();
    if (z_dim.size() != 2) {
      z->Resize({x_matrix.dims()[0], y_matrix.dims()[1]});
    }
50 51
    math::matmul<Place, T>(context.device_context(), x_matrix, false, y_matrix,
                           false, 1, z, 0);
Y
Yu Yang 已提交
52 53 54
    if (z_dim.size() != 2) {
      z->Resize(z_dim);
    }
55 56
  }
};
D
dongzhihong 已提交
57

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

Y
Yu Yang 已提交
74 75 76 77 78
    Tensor dout_mat;
    dout_mat.ShareDataWith(*dout);
    dout_mat.Resize({framework::flatten_to_2d(x->dims(), x_num_col_dims)[0],
                     framework::flatten_to_2d(y->dims(), y_num_col_dims)[1]});

79 80
    Tensor* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    Tensor* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
81 82
    if (dx) {
      dx->mutable_data<T>(ctx.GetPlace());
83 84 85
      Tensor dx_matrix = dx->dims().size() > 2
                             ? framework::ReshapeToMatrix(*dx, x_num_col_dims)
                             : *dx;
Y
Yu Yang 已提交
86

87
      // dx = dout * y'. dx: M x K, dout : M x N, y : K x N
Y
Yu Yang 已提交
88 89
      math::matmul<Place, T>(ctx.device_context(), dout_mat, false, y_matrix,
                             true, 1, &dx_matrix, 0);
90 91 92
    }
    if (dy) {
      dy->mutable_data<T>(ctx.GetPlace());
93 94 95
      Tensor dy_matrix = dy->dims().size() > 2
                             ? framework::ReshapeToMatrix(*dy, y_num_col_dims)
                             : *dy;
96
      // dy = x' * dout. dy K x N, dout : M x N, x : M x K
Y
Yu Yang 已提交
97 98
      math::matmul<Place, T>(ctx.device_context(), x_matrix, true, dout_mat,
                             false, 1, &dy_matrix, 0);
99
    }
D
dongzhihong 已提交
100 101 102
  }
};

103 104
}  // namespace operators
}  // namespace paddle