Skip to content

Commit

Permalink
[layer] Added GPU implementation of custom MHA attention layer
Browse files Browse the repository at this point in the history
GPU implementation of custom MHA layer

Signed-off-by: Debadri Samaddar <[email protected]>
  • Loading branch information
s-debadri committed Jul 9, 2024
1 parent 9c8decb commit e52ce37
Show file tree
Hide file tree
Showing 8 changed files with 1,238 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/ccapi/include/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ enum LayerType {
ML_TRAIN_LAYER_TYPE_POSITIONAL_ENCODING, /**< Positional Encoding Layer type
*/
LAYER_IDENTITY = ML_TRAIN_LAYER_TYPE_IDENTITY, /**< Identity Layer type */
LAYER_CUSTOM_MULTI_HEAD_ATTENTION =
ML_TRAIN_LAYER_TYPE_CUSTOM_MULTI_HEAD_ATTENTION, /**< Multi Head Attention
Layer type */
LAYER_PREPROCESS_FLIP =
ML_TRAIN_LAYER_TYPE_PREPROCESS_FLIP, /**< Preprocess flip Layer type */
LAYER_PREPROCESS_TRANSLATE =
Expand Down Expand Up @@ -517,6 +520,16 @@ MultiHeadAttention(const std::vector<std::string> &properties = {}) {
return createLayer(LayerType::LAYER_MULTI_HEAD_ATTENTION, properties);
}

/**
* @brief Helper function to create Custom Multi Head Attention Layer
*/
inline std::unique_ptr<Layer> CustomMultiHeadAttention(
const std::vector<std::string> &properties = {},
const LayerComputeEngine &compute_engine = LayerComputeEngine::CPU) {
return createLayer(LayerType::LAYER_CUSTOM_MULTI_HEAD_ATTENTION, properties,
compute_engine);
}

/**
* @brief Helper function to create Positional Encoding Layer
*/
Expand Down
2 changes: 2 additions & 0 deletions api/nntrainer-api-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ typedef enum {
28, /**< Positional Encoding Layer type (Since 7.0) */
ML_TRAIN_LAYER_TYPE_IDENTITY = 29, /**< Identity Layer type (Since 8.0) */
ML_TRAIN_LAYER_TYPE_SWIGLU = 30, /**< Swiglu Layer type */
ML_TRAIN_LAYER_TYPE_CUSTOM_MULTI_HEAD_ATTENTION =
31, /**< Custom Multi Head Attention Layer type (Since 8.0) */
ML_TRAIN_LAYER_TYPE_PREPROCESS_FLIP =
300, /**< Preprocess flip Layer (Since 6.5) */
ML_TRAIN_LAYER_TYPE_PREPROCESS_TRANSLATE =
Expand Down
5 changes: 5 additions & 0 deletions nntrainer/cl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <addition_layer_cl.h>
#include <cl_context.h>
#include <custom_multi_head_attention_layer_cl.h>
#include <fc_layer_cl.h>
#include <reshape_cl.h>
#include <swiglu_cl.h>
Expand All @@ -40,6 +41,10 @@ static void add_default_object(ClContext &cc) {

cc.registerFactory(nntrainer::createLayer<ReshapeLayerCl>,
ReshapeLayerCl::type, ml::train::LayerType::LAYER_RESHAPE);

cc.registerFactory(nntrainer::createLayer<CustomMultiHeadAttentionLayerCl>,
CustomMultiHeadAttentionLayerCl::type,
ml::train::LayerType::LAYER_CUSTOM_MULTI_HEAD_ATTENTION);
}

static void registerer(ClContext &cc) noexcept {
Expand Down
Loading

0 comments on commit e52ce37

Please sign in to comment.