softmax_cudnn_op.cu.cc 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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"
D
dengkaipeng 已提交
16
#include "paddle/fluid/operators/softmax_op.h"
17 18 19 20 21 22 23 24 25 26 27
#include "paddle/fluid/framework/op_registry.h"

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
    auto& dev_ctx = context.template device_context<platform::CUDADeviceContext>();
29 30
    auto* X = context.Input<Tensor>("X");
    auto* Out = context.Output<Tensor>("Out");
D
dengkaipeng 已提交
31 32
    const int axis = context.Attr<int>("axis");
    int rank = X->dims().size();
33 34 35 36

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

D
dengkaipeng 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    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());
      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);
      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 已提交
53

54
    math::SoftmaxCUDNNFunctor<T>()(
F
fengjiayi 已提交
55
        context.template device_context<platform::CUDADeviceContext>(),
D
dengkaipeng 已提交
56 57 58 59 60
        &X_2d, &Out_2d);

    if (axis != -1 && axis != rank - 1) {
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, Out_trans, Out, perm);
    }
61 62 63 64 65 66 67
  }
};

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

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

D
dengkaipeng 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    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());
      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);
      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 已提交
98

99
    math::SoftmaxGradCUDNNFunctor<T>()(
F
fengjiayi 已提交
100
        context.template device_context<platform::CUDADeviceContext>(),
D
dengkaipeng 已提交
101 102 103 104 105
        &Out_2d, &dOut_2d, &dX_2d);

    if (axis != -1 && axis != rank - 1) {
      TransCompute<platform::CUDADeviceContext, T>(rank, dev_ctx, dX_trans, dX, perm);
    }
106 107 108 109 110 111 112
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
K
Kexin Zhao 已提交
113 114 115
namespace plat = paddle::platform;
REGISTER_OP_KERNEL(softmax, CUDNN, plat::CUDAPlace,
                   ops::SoftmaxCUDNNKernel<float>,
P
phlrain 已提交
116
                   ops::SoftmaxCUDNNKernel<double>,
K
Kexin Zhao 已提交
117 118
                   ops::SoftmaxCUDNNKernel<plat::float16>);
REGISTER_OP_KERNEL(softmax_grad, CUDNN, plat::CUDAPlace,
P
phlrain 已提交
119
                   ops::SoftmaxGradCUDNNKernel<float>,
C
chengduo 已提交
120 121
                   ops::SoftmaxGradCUDNNKernel<double>,
                   ops::SoftmaxGradCUDNNKernel<plat::float16>);