fused_transformer_op.h 3.0 KB
Newer Older
F
Feng Xing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/* 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.
Indicesou 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 <string>

namespace paddle {
namespace operators {

template <typename T>
class FusedMHA {
  FusedMHA(int, int, int, int, float, bool, uint64_t, uint64_t*, uint64_t*);
  ~FusedMHA();

  void ComputeForward(T*, T*);
  void ComputeBackward(const T*, T*, T*);

 private:
  int batch_size;
  int max_seq_len;
  int dim_embed;

  int num_head;
  int head_size;

  float dropout;

  bool is_test;
  uint64_t seed;

  int32_t seqlen;
  int32_t* cu_seqlen;
};

template <typename T>
class FusedAttention {
 public:
  FusedAttention(int, int, int, int, float, float, bool);
  ~FusedAttention();

  void ComputeForward(T*, T*);
  void ComputeBackward();

 private:
  FusedMHA<T>* fmha;  // fused multihead attention

  int batch_size;
  int max_seq_len;
  int dim_embed;

  int num_head;
  int head_size;

  float dropout;
  T attn_dropout;

  bool normalize_pre_or_post;

  // weights and bias used in attention
  T* fattn_qkv_w;
  T* fattn_qkv_b;
  T* fattn_o_w;
  T* fattn_o_b;
  T* fattn_n_w;
  T* fattn_n_b;
  T* fattn_norm_w;
  T* fattn_norm_b;

  T* fattn_grad_qkv_w;
  T* fattn_grad_qkv_b;
  T* fattn_grad_o_w;
  T* fattn_grad_o_b;
  T* fattn_grad_n_w;
  T* fattn_grad_n_b;
  T* fattn_grad_norm_w;
  T* fattn_grad_norm_b;
};

template <typename T>
class FusedFFN {
  FusedFFN(int, int, int, int, float, std::string, bool);
  ~FusedFFN();

  void ComputeForward(T*, T*);
  void ComputeBackward();

 private:
  int batch_size;
  int max_seq_len;
  int dim_embed;
  int dim_feedforward;

  float attn_dropout;
  float act_dropout;

  bool normalize_pre_or_post;

  std::string act_method;

  // weights and bias used in ffn
  T* fffn_inter_w;
  T* fffn_inter_b;
  T* fffn_output_w;
  T* fffn_output_b;

  T* fffn_grad_inter_w;
  T* fffn_grad_inter_b;
  T* fffn_grad_output_w;
  T* fffn_grad_output_b;
};

template <typename T>
class FusedTransformerEncoderLayer {
 public:
  FusedTransformerEncoderLayer(int, int, int, int, int, float, float, float,
                               std::string, bool);
  ~FusedTransformerEncoderLayer();

  void ComputeForward(T* src, T* output);
  void ComputeBackward();

 private:
  FusedAttention<T>* fused_attn;
  FusedFFN<T>* fused_ffn;

  int batch_size;
  int max_seq_len;
  int dim_embed;
  int dim_feedforward;

  int num_head;
  int head_size;

  float dropout;
  float attn_dropout;
  float act_dropout;

  bool normalize_pre_or_post;

  std::string act_method;
};
}
}