mul_op.h 7.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
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
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14 15

#pragma once
Y
Yu Yang 已提交
16

Y
Yi Wang 已提交
17
#include "paddle/fluid/framework/op_registry.h"
18
#include "paddle/fluid/framework/operator.h"
Y
Yu Yang 已提交
19 20
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/math_function.h"
21 22 23 24

namespace paddle {
namespace operators {

D
dongzhihong 已提交
25 26
using Tensor = framework::Tensor;

Q
QI JUN 已提交
27
template <typename DeviceContext, 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]});
    }
Y
Yu Yang 已提交
50 51 52 53

    auto blas = math::GetBlas<DeviceContext, T>(context);

    blas.MatMul(x_matrix, y_matrix, z);
Y
Yu Yang 已提交
54 55 56
    if (z_dim.size() != 2) {
      z->Resize(z_dim);
    }
57 58
  }
};
D
dongzhihong 已提交
59

Q
QI JUN 已提交
60
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
61
class MulGradKernel : public framework::OpKernel<T> {
D
dongzhihong 已提交
62 63
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
F
fengjiayi 已提交
64 65
    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");
66 67 68 69 70 71 72 73 74
    auto* x = ctx.Input<framework::LoDTensor>("X");
    auto* y = ctx.Input<framework::LoDTensor>("Y");
    auto x_matrix = x->dims().size() > 2
                        ? framework::ReshapeToMatrix(*x, x_num_col_dims)
                        : static_cast<const Tensor&>(*x);
    auto y_matrix = y->dims().size() > 2
                        ? framework::ReshapeToMatrix(*y, y_num_col_dims)
                        : static_cast<const Tensor&>(*y);
    auto* dout = ctx.Input<framework::LoDTensor>(framework::GradVarName("Out"));
D
dongzhihong 已提交
75

Y
Yu Yang 已提交
76 77 78 79 80
    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]});

81 82 83 84 85 86 87 88 89 90
    auto* dx = ctx.Output<framework::LoDTensor>(framework::GradVarName("X"));
    auto* dy = ctx.Output<framework::LoDTensor>(framework::GradVarName("Y"));

    if (dx != nullptr) {
      dx->set_lod(x->lod());
    }
    if (dy != nullptr) {
      dy->set_lod(y->lod());
    }

Q
QI JUN 已提交
91
    auto& dev_ctx = ctx.template device_context<DeviceContext>();
Y
Yu Yang 已提交
92
    auto blas = math::GetBlas<DeviceContext, T>(dev_ctx);
93 94
    if (dx) {
      dx->mutable_data<T>(ctx.GetPlace());
95 96 97
      Tensor dx_matrix = dx->dims().size() > 2
                             ? framework::ReshapeToMatrix(*dx, x_num_col_dims)
                             : *dx;
Y
Yu Yang 已提交
98

99
      // dx = dout * y'. dx: M x K, dout : M x N, y : K x N
Y
Yu Yang 已提交
100
      blas.MatMul(dout_mat, false, y_matrix, true, &dx_matrix);
101 102 103
    }
    if (dy) {
      dy->mutable_data<T>(ctx.GetPlace());
104 105 106
      Tensor dy_matrix = dy->dims().size() > 2
                             ? framework::ReshapeToMatrix(*dy, y_num_col_dims)
                             : *dy;
107
      // dy = x' * dout. dy K x N, dout : M x N, x : M x K
Y
Yu Yang 已提交
108
      blas.MatMul(x_matrix, true, dout_mat, false, &dy_matrix);
109
    }
D
dongzhihong 已提交
110 111 112
  }
};

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
template <typename DeviceContext, typename T>
class MulDoubleGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    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");
    auto* x = ctx.Input<framework::LoDTensor>("X");
    auto* y = ctx.Input<framework::LoDTensor>("Y");
    auto x_mat = x->dims().size() > 2
                     ? framework::ReshapeToMatrix(*x, x_num_col_dims)
                     : static_cast<const Tensor&>(*x);
    auto y_mat = y->dims().size() > 2
                     ? framework::ReshapeToMatrix(*y, y_num_col_dims)
                     : static_cast<const Tensor&>(*y);

    const int m = framework::flatten_to_2d(x->dims(), x_num_col_dims)[0];
    const int n = framework::flatten_to_2d(y->dims(), y_num_col_dims)[1];

    auto* dout = ctx.Input<framework::LoDTensor>("DOut");
    Tensor dout_mat;
    dout_mat.ShareDataWith(*dout);
    dout_mat.Resize({m, n});

    auto* ddx = ctx.Input<framework::LoDTensor>("DDX");
    auto* ddy = ctx.Input<framework::LoDTensor>("DDY");

    auto* dx = ctx.Output<framework::LoDTensor>("DX");
    auto* dy = ctx.Output<framework::LoDTensor>("DY");
    auto* ddout = ctx.Output<framework::LoDTensor>("DDOut");

    Tensor ddout_mat;
    if (ddout) {
      ddout->set_lod(dout->lod());
      // allocate and reshape ddout
      ddout->mutable_data<T>(ctx.GetPlace());
      ddout_mat.ShareDataWith(*ddout);
      ddout_mat.Resize({m, n});
    }

    auto& dev_ctx = ctx.template device_context<DeviceContext>();
    auto blas = math::GetBlas<DeviceContext, T>(dev_ctx);
    // a flag to specify whether ddout value has been set, if flag
    // is false, MatMul beta should be 0 to set ddout, if flag is
    // true, MatMul beta should be 1 to add result to ddout.
    bool ddout_flag = false;
    if (ddx) {
      auto ddx_mat = ddx->dims().size() > 2
                         ? framework::ReshapeToMatrix(*ddx, x_num_col_dims)
                         : static_cast<const Tensor&>(*ddx);

      // dy = ddx' * dout. dy : K x M, ddx' : K x M, dout : M x N
      if (dy) {
        dy->set_lod(y->lod());
        // allocate and reshape dy
        dy->mutable_data<T>(ctx.GetPlace());
        Tensor dy_mat = dy->dims().size() > 2
                            ? framework::ReshapeToMatrix(*dy, y_num_col_dims)
                            : *dy;
        blas.MatMul(ddx_mat, true, dout_mat, false, &dy_mat);
      }
      // ddout1 = ddx * y. ddx : M x K, y : K x N, ddout1 : M x N
      if (ddout) {
        blas.MatMul(ddx_mat, false, y_mat, false, static_cast<T>(1.0),
                    &ddout_mat, static_cast<T>(ddout_flag));
        ddout_flag = true;
      }
    }
    if (ddy) {
      auto ddy_mat = ddy->dims().size() > 2
                         ? framework::ReshapeToMatrix(*ddy, y_num_col_dims)
                         : static_cast<const Tensor&>(*ddy);
      // dx = dout * ddy'. dout : M x N, ddy' : N x K, dx : M x K
      if (dx) {
        dx->set_lod(x->lod());
        // allocate and reshape dx
        dx->mutable_data<T>(ctx.GetPlace());
        Tensor dx_mat = dx->dims().size() > 2
                            ? framework::ReshapeToMatrix(*dx, x_num_col_dims)
                            : *dx;
        blas.MatMul(dout_mat, false, ddy_mat, true, &dx_mat);
      }
      // ddout2 = x * ddy. x : M x K, ddy : K x N, ddout2 : M x N
      if (ddout) {
        blas.MatMul(x_mat, false, ddy_mat, false, static_cast<T>(1.0),
                    &ddout_mat, static_cast<T>(ddout_flag));
      }
    }
  }
};

203 204
}  // namespace operators
}  // namespace paddle