mul_op.h 4.0 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"
Y
Yu Yang 已提交
18 19
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/math_function.h"
20 21 22 23

namespace paddle {
namespace operators {

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

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

F
fengjiayi 已提交
44
    z->mutable_data<T>(context.GetPlace());
Y
Yu Yang 已提交
45 46 47 48
    auto z_dim = z->dims();
    if (z_dim.size() != 2) {
      z->Resize({x_matrix.dims()[0], y_matrix.dims()[1]});
    }
Y
Yu Yang 已提交
49 50 51 52

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

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

Q
QI JUN 已提交
59
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
60
class MulGradKernel : public framework::OpKernel<T> {
D
dongzhihong 已提交
61 62
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
F
fengjiayi 已提交
63 64
    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");
65 66 67 68 69 70 71 72 73
    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 已提交
74

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

80 81 82 83 84 85 86 87 88 89
    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 已提交
90
    auto& dev_ctx = ctx.template device_context<DeviceContext>();
Y
Yu Yang 已提交
91
    auto blas = math::GetBlas<DeviceContext, T>(dev_ctx);
92 93
    if (dx) {
      dx->mutable_data<T>(ctx.GetPlace());
94 95 96
      Tensor dx_matrix = dx->dims().size() > 2
                             ? framework::ReshapeToMatrix(*dx, x_num_col_dims)
                             : *dx;
Y
Yu Yang 已提交
97

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

112 113
}  // namespace operators
}  // namespace paddle