LlamaFfnLayer.cc 4.63 KB
Newer Older
Li Zhang's avatar
Li Zhang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (c) OpenMMLab. All rights reserved.
 * Copyright (c) 2022-2023, NVIDIA CORPORATION.  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.
 */

lvhan028's avatar
lvhan028 committed
18
// Modified from https://github.com/NVIDIA/FasterTransformer/blob/main/src/turbomind/layers/FfnLayer.h
Li Zhang's avatar
Li Zhang committed
19

lvhan028's avatar
lvhan028 committed
20
21
22
#include "src/turbomind/models/llama/LlamaFfnLayer.h"
#include "src/turbomind/kernels/activation_kernels.h"
#include "src/turbomind/models/llama/LlamaNcclGuard.h"
Li Zhang's avatar
Li Zhang committed
23
#include "src/turbomind/models/llama/llama_utils.h"
lvhan028's avatar
lvhan028 committed
24
#include "src/turbomind/utils/nvtx_utils.h"
Li Zhang's avatar
Li Zhang committed
25
26
// #include <glog/logging.h>

lvhan028's avatar
lvhan028 committed
27
namespace turbomind {
Li Zhang's avatar
Li Zhang committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

template<typename T>
void LlamaFfnLayer<T>::allocateBuffer(size_t token_num)
{
    inter_buf_          = (T*)allocator_->reMalloc(inter_buf_, sizeof(T) * token_num * inter_size_, false);
    gating_buf_         = (T*)allocator_->reMalloc(gating_buf_, sizeof(T) * token_num * inter_size_, false);
    is_allocate_buffer_ = true;
}

template<typename T>
void LlamaFfnLayer<T>::freeBuffer()
{
    if (is_allocate_buffer_) {
        allocator_->free((void**)&inter_buf_);
        allocator_->free((void**)&gating_buf_);
        is_allocate_buffer_ = false;
    }
}

template<typename T>
void LlamaFfnLayer<T>::activation(int num_token)
{
Li Zhang's avatar
Li Zhang committed
50
    NvtxScope scope("activation");
Li Zhang's avatar
Li Zhang committed
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
    invokeGenericActivation<SiluActivation>(gating_buf_,
                                            (const T*)nullptr,  // bias
                                            inter_buf_,
                                            (const T*)nullptr,  // gated_bias
                                            nullptr,            // ia3_tasks
                                            (const T*)nullptr,  // ia3_weights
                                            num_token,          // m
                                            inter_size_,        // n
                                            0,                  // int8_mode
                                            nullptr,            // activation_in
                                            nullptr,            // activation_out
                                            nullptr,            // padding_offset
                                            0,                  // seq_len
                                            stream_);
    sync_check_cuda_error();
}

template<typename T>
void LlamaFfnLayer<T>::forward(TensorMap*               output_tensors,
                               const TensorMap*         input_tensors,
                               const LlamaFfnWeight<T>* weights)
{
    /**
     * input_tensors:
     *   \param ffn_input [token_num, hidden_dimension]
     *
     * output_tensors:
     *   \param ffn_output [token_num, hidden_dimension]
     */

Li Zhang's avatar
Li Zhang committed
81
82
    NvtxScope scope("ffn");

Li Zhang's avatar
Li Zhang committed
83
84
85
86
87
88
89
90
    const size_t num_token = input_tensors->at("ffn_input").shape[0];
    // LOG(WARNING);

    allocateBuffer(num_token);

    const T* ffn_input_data  = input_tensors->at("ffn_input").getPtr<T>();
    T*       ffn_output_data = output_tensors->at("ffn_output").getPtr<T>();

91
    if (weights->fused_gating_intermediate.kernel) {
Li Zhang's avatar
Li Zhang committed
92
        NvtxScope scope("fused_silu_ffn");
93
94
95
96
        linear_.forward(
            gating_buf_, ffn_input_data, num_token, weights->fused_gating_intermediate, LlamaLinear<T>::kFusedSiluFfn);
    }
    else {
Li Zhang's avatar
Li Zhang committed
97
98
99
100
101
102
103
104
        {  // w1(x)
            NvtxScope scope("w1");
            linear_.forward(gating_buf_, ffn_input_data, num_token, weights->gating);
        }
        {  // w3(x)
            NvtxScope scope("w3");
            linear_.forward(inter_buf_, ffn_input_data, num_token, weights->intermediate);
        }
105
        // silu(w1(x)) * w3(x)
106
107
        activation(num_token);
    }
Li Zhang's avatar
Li Zhang committed
108

Li Zhang's avatar
Li Zhang committed
109
110
111
112
    {  // w2(x)
        NvtxScope scope("w2");
        linear_.forward(ffn_output_data, gating_buf_, num_token, weights->output);
    }
Li Zhang's avatar
Li Zhang committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

    if (tensor_para_.world_size_ > 1) {
        NcclGuard nccl_guard(tensor_para_, stream_);
        ftNcclAllReduceSum(ffn_output_data, ffn_output_data, num_token * hidden_units_, tensor_para_, stream_);
        sync_check_cuda_error();
    }

    if (is_free_buffer_after_forward_) {
        freeBuffer();
    }
    // LOG(WARNING);
}

template class LlamaFfnLayer<float>;
template class LlamaFfnLayer<half>;

lvhan028's avatar
lvhan028 committed
129
}  // namespace turbomind