attn_gemm.h 7.8 KB
Newer Older
1
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2

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

7
    http://www.apache.org/licenses/LICENSE-2.0
8

9 10 11 12 13 14 15 16
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. */

#pragma once

L
Li Min 已提交
17
#include "paddle/fluid/operators/kernel_primitives/kernel_primitives.h"
18
#include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h"
19 20
#include "paddle/fluid/platform/float16.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
21 22
#include "paddle/phi/kernels/funcs/broadcast_function.h"
#include "paddle/phi/kernels/funcs/elementwise_functor.h"
L
Li Min 已提交
23

24 25
namespace paddle {
namespace operators {
26

27 28 29 30 31
// support gemm-nt and gemm-nn, which is used in fused_attention_op.
template <typename T>
class AttnMatMul {
 public:
  // (m, n, k) = bsz_seq, output_size, input_size
L
Leo Chen 已提交
32
  AttnMatMul(const phi::GPUContext& dev_ctx,
33 34 35 36 37
             bool transA,
             bool transB,
             int bsz_seq,
             int output_size,
             int input_size,
38 39 40 41 42 43 44 45 46 47 48
             bool compute_bias)
      : dev_ctx_(dev_ctx),
        transA_(transA),
        transB_(transB),
        bsz_seq_(bsz_seq),
        output_size_(output_size),
        input_size_(input_size),
        compute_bias_(compute_bias) {}

  ~AttnMatMul() {}

49 50 51 52 53
  void ComputeForward(const phi::DenseTensor* weight,
                      const phi::DenseTensor* input,
                      const phi::DenseTensor* bias,
                      phi::DenseTensor* output,
                      phi::DenseTensor* bias_out) {
54 55
    // Note: for blas.GEMM API in Paddle, it treats all inputs as row-major.
    // here: (transa, transb): nt, input * weight.
56 57
    CBLAS_TRANSPOSE transA = transA_ ? CblasTrans : CblasNoTrans;
    CBLAS_TRANSPOSE transB = transB_ ? CblasTrans : CblasNoTrans;
58 59 60
    T alpha = static_cast<T>(1.0);
    T beta = static_cast<T>(0.0);

61
    // (m, n, k) = bsz_seq, output_size, input_size, (input, weight, out)
L
Leo Chen 已提交
62
    auto blas = phi::funcs::GetBlas<phi::GPUContext, T>(dev_ctx_);
63 64 65 66 67 68 69 70 71 72
    blas.GEMM(transA,
              transB,
              bsz_seq_,
              output_size_,
              input_size_,
              alpha,
              input->data<T>(),
              weight->data<T>(),
              beta,
              output->data<T>());
73
    if (compute_bias_) {
74
      // bias_out = output + bias
75 76
      std::vector<const phi::DenseTensor*> ins = {output, bias};
      std::vector<phi::DenseTensor*> outs = {bias_out};
77
      phi::funcs::BroadcastKernel<phi::ElementwiseType::kBinary, T, T>(
78
          dev_ctx_, ins, &outs, -1, phi::funcs::AddFunctor<T>());
79 80 81
    }
  }

82 83 84 85 86 87
  void ComputeBackward(const phi::DenseTensor* input,
                       const phi::DenseTensor* weight,
                       const phi::DenseTensor* d_output,
                       phi::DenseTensor* d_input,
                       phi::DenseTensor* d_weight,
                       phi::DenseTensor* d_bias,
88
                       bool use_addto = false) {
89
    T alpha = static_cast<T>(1.0);
90 91
    T beta_dA = use_addto ? static_cast<T>(1.0) : static_cast<T>(0.0);
    T beta_dB = static_cast<T>(0.0);
92

L
Leo Chen 已提交
93
    auto blas = phi::funcs::GetBlas<phi::GPUContext, T>(dev_ctx_);
94
    if (!transA_) {
95
      // forward: gemm-nt
96
      if (transB_) {
97 98 99 100 101 102 103
        // backward: gemm-tn, dB = (dC)^T * A
        if (d_weight) {
          int dB_m = output_size_;
          int dB_n = input_size_;
          int dB_k = bsz_seq_;

          T* dB_output_ptr = d_weight->data<T>();
104 105 106 107 108 109 110 111 112
          blas.GEMM(CblasTrans,
                    CblasNoTrans,
                    dB_m,
                    dB_n,
                    dB_k,
                    alpha,
                    d_output->data<T>(),
                    input->data<T>(),
                    beta_dB,
113 114 115 116 117 118 119 120 121 122
                    dB_output_ptr);
        }

        // backward: gemm-nn, dA = dC * B
        if (d_input) {
          int dA_m = bsz_seq_;
          int dA_n = input_size_;
          int dA_k = output_size_;

          T* dA_output_ptr = d_input->data<T>();
123 124 125 126 127 128 129 130 131
          blas.GEMM(CblasNoTrans,
                    CblasNoTrans,
                    dA_m,
                    dA_n,
                    dA_k,
                    alpha,
                    d_output->data<T>(),
                    weight->data<T>(),
                    beta_dA,
132 133
                    dA_output_ptr);
        }
134
      } else {  // fw: gemm-nn
135 136 137 138 139 140 141
        // backward: gemm-tn, dB = A^T * dC
        if (d_weight) {
          int dB_m = input_size_;
          int dB_n = output_size_;
          int dB_k = bsz_seq_;

          T* dB_output_ptr = d_weight->data<T>();
142 143 144 145 146 147 148 149 150
          blas.GEMM(CblasTrans,
                    CblasNoTrans,
                    dB_m,
                    dB_n,
                    dB_k,
                    alpha,
                    input->data<T>(),
                    d_output->data<T>(),
                    beta_dB,
151 152 153 154 155 156 157 158 159 160
                    dB_output_ptr);
        }

        // backward: gemm-nt, dA = dC * B^T
        if (d_input) {
          int dA_m = bsz_seq_;
          int dA_n = input_size_;
          int dA_k = output_size_;

          T* dA_output_ptr = d_input->data<T>();
161 162 163 164 165 166 167 168 169
          blas.GEMM(CblasNoTrans,
                    CblasTrans,
                    dA_m,
                    dA_n,
                    dA_k,
                    alpha,
                    d_output->data<T>(),
                    weight->data<T>(),
                    beta_dA,
170 171
                    dA_output_ptr);
        }
172 173 174
      }
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
175
          "AttnMatMul wrapper do not support (transA=T, transB=T/N)"
176 177
          "parameters."));
    }
178 179 180
    if (compute_bias_ && d_bias) {
      // reduce: {0, 1, 2, 3, 4} -> {2, 3, 4} or {0, 1, 2} -> {2} or {0,1,2,3}
      // -> {3} or {0,1,2,3,4} -> {3,4}
L
Li Min 已提交
181 182 183 184 185 186 187 188 189 190
      const auto input_dims = d_output->dims();
      const auto output_dims = d_bias->dims();
      bool support_case_1 =
          (input_dims.size() == 5 && output_dims.size() == 3 &&
           (input_dims[2] == output_dims[0]) &&
           (input_dims[3] == output_dims[1]) &&
           (input_dims[4] == output_dims[2]));
      bool support_case_2 =
          (input_dims.size() == 3 && output_dims.size() == 1 &&
           (input_dims[2] == output_dims[0]));
191 192 193 194 195 196 197 198
      bool support_case_3 =
          (input_dims.size() == 4 && output_dims.size() == 1 &&
           input_dims[3] == output_dims[0]);
      bool support_case_4 =
          (input_dims.size() == 5 && output_dims.size() == 2 &&
           input_dims[3] == output_dims[0] && input_dims[4] == output_dims[1]);

      gpuStream_t stream = dev_ctx_.stream();
L
Li Min 已提交
199
      if (support_case_1 || support_case_2) {
200
        TensorReduceImpl<T, T, kps::AddFunctor, kps::IdentityFunctor<T>>(
201 202 203 204 205
            dev_ctx_,
            *d_output,
            d_bias,
            kps::IdentityFunctor<T>(),
            {0, 1},
W
Wilber 已提交
206
            stream);
207 208
      } else if (support_case_3 || support_case_4) {
        TensorReduceImpl<T, T, kps::AddFunctor, kps::IdentityFunctor<T>>(
209 210 211 212 213
            dev_ctx_,
            *d_output,
            d_bias,
            kps::IdentityFunctor<T>(),
            {0, 1, 2},
214
            stream);
L
Li Min 已提交
215 216 217 218 219 220
      } else {
        PADDLE_THROW(platform::errors::InvalidArgument(
            "Only support reduce when the input dims are [0,1,2,3,4] and "
            "output is [2,3,4]"
            "or input is [0,1,2] and output is [2]."));
      }
221 222 223 224
    }
  }

 private:
L
Leo Chen 已提交
225
  const phi::GPUContext& dev_ctx_;
226 227 228 229 230 231 232 233 234 235 236 237 238

  bool transA_;
  bool transB_;

  int bsz_seq_;
  int output_size_;
  int input_size_;

  int compute_bias_;
};

}  // namespace operators
}  // namespace paddle