softmax_cudnn_op.cu.cc 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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

    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,
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. */

#include "paddle/fluid/operators/math/softmax.h"
#include "paddle/fluid/framework/op_registry.h"
D
dengkaipeng 已提交
17
#include "paddle/fluid/operators/softmax_op.h"
18 19 20 21 22 23 24 25 26 27

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

template <typename T>
class SoftmaxCUDNNKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
D
dengkaipeng 已提交
28 29
    auto& dev_ctx =
        context.template device_context<platform::CUDADeviceContext>();
30 31
    auto* X = context.Input<Tensor>("X");
    auto* Out = context.Output<Tensor>("Out");
D
dengkaipeng 已提交
32 33
    const int axis = context.Attr<int>("axis");
    int rank = X->dims().size();
34 35 36 37

    // allocate memory on device.
    Out->mutable_data<T>(context.GetPlace());

D
dengkaipeng 已提交
38 39 40 41 42 43 44
    std::vector<int> perm, shape;
    CalcTransPermAndShapeByAxis(*X, axis, &perm, &shape);

    Tensor X_2d, Out_2d;
    Tensor X_trans, Out_trans;
    if (axis != -1 && axis != rank - 1) {
      X_trans.mutable_data<T>(framework::make_ddim(shape), context.GetPlace());
D
dengkaipeng 已提交
45 46 47 48 49 50
      Out_trans.mutable_data<T>(framework::make_ddim(shape),
                                context.GetPlace());
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, *X, &X_trans,
                                                   perm);
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, *Out,
                                                   &Out_trans, perm);
D
dengkaipeng 已提交
51 52 53 54 55 56
      X_2d = framework::ReshapeToMatrix(X_trans, rank - 1);
      Out_2d = framework::ReshapeToMatrix(Out_trans, rank - 1);
    } else {
      X_2d = framework::ReshapeToMatrix(*X, rank - 1);
      Out_2d = framework::ReshapeToMatrix(*Out, rank - 1);
    }
F
fengjiayi 已提交
57

58
    math::SoftmaxCUDNNFunctor<T>()(
D
dengkaipeng 已提交
59 60
        context.template device_context<platform::CUDADeviceContext>(), &X_2d,
        &Out_2d);
D
dengkaipeng 已提交
61 62

    if (axis != -1 && axis != rank - 1) {
D
dengkaipeng 已提交
63 64
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, Out_trans,
                                                   Out, perm);
D
dengkaipeng 已提交
65
    }
66 67 68 69 70 71 72
  }
};

template <typename T>
class SoftmaxGradCUDNNKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
D
dengkaipeng 已提交
73 74
    auto& dev_ctx =
        context.template device_context<platform::CUDADeviceContext>();
75 76 77
    auto* Out = context.Input<Tensor>("Out");
    auto* dOut = context.Input<Tensor>(framework::GradVarName("Out"));
    auto* dX = context.Output<Tensor>(framework::GradVarName("X"));
D
dengkaipeng 已提交
78 79
    const int axis = context.Attr<int>("axis");
    int rank = Out->dims().size();
80 81 82 83

    // allocate memory on device.
    dX->mutable_data<T>(context.GetPlace());

D
dengkaipeng 已提交
84 85 86 87 88 89 90
    std::vector<int> perm, shape;
    CalcTransPermAndShapeByAxis(*dX, axis, &perm, &shape);

    Tensor dX_2d, Out_2d, dOut_2d;
    Tensor dX_trans, Out_trans, dOut_trans;
    if (axis != -1 && axis != rank - 1) {
      dX_trans.mutable_data<T>(framework::make_ddim(shape), context.GetPlace());
D
dengkaipeng 已提交
91 92 93 94 95 96 97 98 99 100
      Out_trans.mutable_data<T>(framework::make_ddim(shape),
                                context.GetPlace());
      dOut_trans.mutable_data<T>(framework::make_ddim(shape),
                                 context.GetPlace());
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, *dX,
                                                   &dX_trans, perm);
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, *Out,
                                                   &Out_trans, perm);
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, *dOut,
                                                   &dOut_trans, perm);
D
dengkaipeng 已提交
101 102 103 104 105 106 107 108
      dX_2d = framework::ReshapeToMatrix(dX_trans, rank - 1);
      Out_2d = framework::ReshapeToMatrix(Out_trans, rank - 1);
      dOut_2d = framework::ReshapeToMatrix(dOut_trans, rank - 1);
    } else {
      dX_2d = framework::ReshapeToMatrix(*dX, rank - 1);
      Out_2d = framework::ReshapeToMatrix(*Out, rank - 1);
      dOut_2d = framework::ReshapeToMatrix(*dOut, rank - 1);
    }
F
fengjiayi 已提交
109

110
    math::SoftmaxGradCUDNNFunctor<T>()(
D
dengkaipeng 已提交
111 112
        context.template device_context<platform::CUDADeviceContext>(), &Out_2d,
        &dOut_2d, &dX_2d);
D
dengkaipeng 已提交
113 114

    if (axis != -1 && axis != rank - 1) {
D
dengkaipeng 已提交
115 116
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, dX_trans, dX,
                                                   perm);
D
dengkaipeng 已提交
117
    }
118 119 120 121 122 123 124
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
K
Kexin Zhao 已提交
125 126 127
namespace plat = paddle::platform;
REGISTER_OP_KERNEL(softmax, CUDNN, plat::CUDAPlace,
                   ops::SoftmaxCUDNNKernel<float>,
P
phlrain 已提交
128
                   ops::SoftmaxCUDNNKernel<double>,
K
Kexin Zhao 已提交
129 130
                   ops::SoftmaxCUDNNKernel<plat::float16>);
REGISTER_OP_KERNEL(softmax_grad, CUDNN, plat::CUDAPlace,
P
phlrain 已提交
131
                   ops::SoftmaxGradCUDNNKernel<float>,
C
chengduo 已提交
132 133
                   ops::SoftmaxGradCUDNNKernel<double>,
                   ops::SoftmaxGradCUDNNKernel<plat::float16>);