fused_gemm_epilogue_op.cu 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
Copyright (c) 2022 NVIDIA 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/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
18
#include "paddle/fluid/platform/bfloat16.h"
19
#include "paddle/fluid/platform/float16.h"
20
#include "paddle/phi/kernels/funcs/fused_gemm_epilogue.h"
21 22 23 24

namespace paddle {
namespace operators {

25 26
#if CUDA_VERSION >= 11060

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
template <typename T>
phi::funcs::MatmulFusedType GetFwdFusedEpilogueType(
    const phi::GPUContext& ctx,
    const std::string& activation,
    phi::DenseTensor* reserve_space) {
  using FusedType = phi::funcs::MatmulFusedType;

  FusedType fused_type = FusedType::kMatmulBias;
  if (activation != "none") {
    if (activation == "relu") {
      if (reserve_space == nullptr) {
        fused_type = FusedType::kMatmulBiasRelu;
      } else {
        fused_type = FusedType::kMatmulBiasReluWithReservedData;
        int64_t reserve_size =
            SizeOf(phi::DataType::BOOL) * phi::product(reserve_space->dims());
        ctx.Alloc(reserve_space, phi::DataType::BOOL, reserve_size);
      }
    } else if (activation == "gelu") {
      if (reserve_space == nullptr) {
        fused_type = FusedType::kMatmulBiasGelu;
      } else {
        fused_type = FusedType::kMatmulBiasGeluWithReservedData;
        int64_t reserve_size = sizeof(T) * phi::product(reserve_space->dims());
        ctx.Alloc<T>(reserve_space, reserve_size);
      }
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
55 56
          "fused_gemm_epilogue's activate should be one of {none, relu, gelu},"
          " but received %s, please check",
57 58 59 60 61 62
          activation));
    }
  }
  return fused_type;
}

63
template <typename T, typename DeviceContext>
64 65 66
class FusedGemmEpilogueKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
67 68 69 70 71
#if CUDA_VERSION < 11060
    PADDLE_THROW(phi::errors::Unimplemented(
        "The fused_gemm_epilogue operator only support CUDA 11.6 "
        "or higher version."));
#endif
L
Leo Chen 已提交
72
    auto& dev_ctx = ctx.template device_context<phi::GPUContext>();
73

74 75 76 77 78 79
    const phi::DenseTensor* x = ctx.Input<phi::DenseTensor>("X");
    const phi::DenseTensor* y = ctx.Input<phi::DenseTensor>("Y");
    const phi::DenseTensor* bias = ctx.Input<phi::DenseTensor>("Bias");
    phi::DenseTensor* out = ctx.Output<phi::DenseTensor>("Out");
    phi::DenseTensor* reserve_space =
        ctx.Output<phi::DenseTensor>("ReserveSpace");
80 81 82 83 84

    bool trans_x = ctx.Attr<bool>("trans_x");
    bool trans_y = ctx.Attr<bool>("trans_y");

    std::string activation = ctx.Attr<std::string>("activation");
85
    dev_ctx.Alloc<T>(out, out->numel() * sizeof(T));
86
    // (M * K) * (K * N)
87 88 89 90 91 92
    auto x_mat_dims =
        phi::flatten_to_2d(x->dims(), trans_x ? 1 : x->dims().size() - 1);
    int64_t M = trans_x ? x_mat_dims[1] : x_mat_dims[0];
    int64_t K = trans_y ? y->dims()[1] : y->dims()[0];
    int64_t N = trans_y ? y->dims()[0] : y->dims()[1];

93 94 95 96 97 98 99 100 101 102
    void* reserve_data = reserve_space ? reserve_space->data() : nullptr;
    auto fused_type =
        GetFwdFusedEpilogueType<T>(dev_ctx, activation, reserve_space);

    VLOG(6) << "x.shape={" << x->dims() << "}, y.shape={" << y->dims()
            << "}, out.shape={" << out->dims() << "}, M=" << M << ", N=" << N
            << ", K=" << K << ", trans_x=" << trans_x << ", trans_y=" << trans_y
            << ", activation=" << activation << ", fused_type=" << fused_type
            << ", reserve_space=" << reserve_space;

103 104 105 106 107 108 109 110 111 112 113 114 115
    phi::funcs::LinearWithCublasLt<T>::Run(
        dev_ctx,
        x,
        y,
        out,
        static_cast<const void*>(bias->data<T>()),
        reserve_data,
        M,
        N,
        K,
        trans_x,
        trans_y,
        fused_type);
116 117 118
  }
};

119
template <typename T, typename DeviceContext>
120 121 122
class FusedGemmEpilogueGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
123 124 125 126 127
#if CUDA_VERSION < 11060
    PADDLE_THROW(phi::errors::Unimplemented(
        "The fused_gemm_epilogue operator only support CUDA 11.6 "
        "or higher version."));
#endif
L
Leo Chen 已提交
128
    auto& dev_ctx = ctx.template device_context<phi::GPUContext>();
129

130 131 132 133 134 135 136 137
    const phi::DenseTensor* dout = ctx.Input<phi::DenseTensor>("DOut");
    const phi::DenseTensor* x = ctx.Input<phi::DenseTensor>("X");
    const phi::DenseTensor* y = ctx.Input<phi::DenseTensor>("Y");
    const phi::DenseTensor* reserve_space =
        ctx.Input<phi::DenseTensor>("ReserveSpace");
    phi::DenseTensor* dx = ctx.Output<phi::DenseTensor>("DX");
    phi::DenseTensor* dy = ctx.Output<phi::DenseTensor>("DY");
    phi::DenseTensor* dbias = ctx.Output<phi::DenseTensor>("DBias");
138 139

    std::string activation_grad = ctx.Attr<std::string>("activation_grad");
140 141
    bool trans_x = ctx.Attr<bool>("trans_x");
    bool trans_y = ctx.Attr<bool>("trans_y");
142 143

    // (M * K) * (K * N)
144 145 146 147 148
    auto x_mat_dims =
        phi::flatten_to_2d(x->dims(), trans_x ? 1 : x->dims().size() - 1);
    int64_t M = trans_x ? x_mat_dims[1] : x_mat_dims[0];
    int64_t K = trans_y ? y->dims()[1] : y->dims()[0];
    int64_t N = trans_y ? y->dims()[0] : y->dims()[1];
149

150 151 152 153 154 155
    VLOG(6) << "x.shape={" << x->dims() << "}, y.shape={" << y->dims()
            << "}, dout.shape={" << dout->dims() << "}, M=" << M << ", N=" << N
            << ", K=" << K << ", trans_x=" << trans_x << ", trans_y=" << trans_y
            << ", activation=" << activation_grad
            << ", reserve_space=" << reserve_space;

156 157 158 159 160 161 162 163 164 165 166 167 168 169
    phi::funcs::ComputeFusedGemmEpilogueBackward<T>(dev_ctx,
                                                    dout,
                                                    x,
                                                    y,
                                                    reserve_space,
                                                    M,
                                                    N,
                                                    K,
                                                    trans_x,
                                                    trans_y,
                                                    activation_grad,
                                                    dx,
                                                    dy,
                                                    dbias);
170 171
  }
};
172
#endif
173 174 175 176 177

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
namespace plat = paddle::platform;
PD_REGISTER_STRUCT_KERNEL(fused_gemm_epilogue,
                          GPU,
                          ALL_LAYOUT,
                          ops::FusedGemmEpilogueKernel,
                          float,
                          double,
                          plat::float16,
                          plat::bfloat16) {}
PD_REGISTER_STRUCT_KERNEL(fused_gemm_epilogue_grad,
                          GPU,
                          ALL_LAYOUT,
                          ops::FusedGemmEpilogueGradKernel,
                          float,
                          double,
                          plat::float16,
                          plat::bfloat16) {}