attention_layer_norm.h 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright (c) 2021 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. */

#pragma once

#include "paddle/fluid/operators/layer_norm_kernel.cu.h"

namespace paddle {
namespace operators {

template <typename T>
class AttnLayerNorm {
 public:
L
Leo Chen 已提交
25
  AttnLayerNorm(const phi::GPUContext& dev_ctx,
26 27 28
                float epsilon,
                int64_t batch_size,
                int64_t feature_size)
29 30 31 32 33 34 35
      : dev_ctx_(dev_ctx),
        epsilon_(epsilon),
        batch_size_(batch_size),
        feature_size_(feature_size) {}

  ~AttnLayerNorm() {}

36 37 38 39
  void ComputeForward(const T* x_data,
                      const LayerNormParamType<T>* scale_data,
                      const LayerNormParamType<T>* bias_data,
                      T* y_data,
40 41 42 43 44
                      LayerNormParamType<T>* mean_data,
                      LayerNormParamType<T>* var_data) {
    auto stream = dev_ctx_.stream();

    switch (GetDesiredBlockDim(feature_size_)) {
45 46 47 48 49 50 51 52 53 54
      FIXED_BLOCK_DIM_CASE(
          LayerNormForward<T, LayerNormParamType<T>, kBlockDim>
          <<<batch_size_, kBlockDim, 0, stream>>>(x_data,
                                                  scale_data,
                                                  bias_data,
                                                  y_data,
                                                  mean_data,
                                                  var_data,
                                                  epsilon_,
                                                  feature_size_));
55 56 57 58 59 60 61
      default:
        PADDLE_THROW(platform::errors::InvalidArgument(
            "Feature_size must be larger than 1"));
        break;
    }
  }

62 63
  void ComputeBackward(const T* x_data,
                       const T* d_y_data,
64 65
                       const LayerNormParamType<T>* scale_data,
                       const LayerNormParamType<T>* mean_data,
66 67
                       const LayerNormParamType<T>* var_data,
                       T* d_x_data,
68 69
                       LayerNormParamType<T>* d_scale_data,
                       LayerNormParamType<T>* d_bias_data) {
70 71 72 73 74 75 76 77 78 79 80 81
    LayerNormBackward<T, LayerNormParamType<T>>(x_data,
                                                d_y_data,
                                                scale_data,
                                                mean_data,
                                                var_data,
                                                d_x_data,
                                                d_scale_data,
                                                d_bias_data,
                                                epsilon_,
                                                batch_size_,
                                                feature_size_,
                                                dev_ctx_);
82 83 84
  }

 private:
L
Leo Chen 已提交
85
  const phi::GPUContext& dev_ctx_;
86 87 88 89 90 91 92 93 94

  int64_t batch_size_;
  int64_t feature_size_;

  float epsilon_;
};

}  // namespace operators
}  // namespace paddle