BeamSearchLayer.h 2.4 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
18
/*
 * Copyright (c) 2019-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.
 */

#pragma once

lvhan028's avatar
lvhan028 committed
19
20
#include "src/turbomind/kernels/beam_search_topk_kernels.h"
#include "src/turbomind/layers/beam_search_layers/BaseBeamSearchLayer.h"
Li Zhang's avatar
Li Zhang committed
21
22
#include <float.h>

lvhan028's avatar
lvhan028 committed
23
namespace turbomind {
Li Zhang's avatar
Li Zhang committed
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

template<typename T>
class BeamSearchLayer: public BaseBeamSearchLayer<T> {
private:
    // meta data
    using BaseBeamSearchLayer<T>::vocab_size_;
    using BaseBeamSearchLayer<T>::vocab_size_padded_;

    using BaseBeamSearchLayer<T>::topk_softmax_workspace_size_;
    using BaseBeamSearchLayer<T>::topk_softmax_workspace_;

    void allocateBuffer() override;
    void allocateBuffer(size_t batch_size, size_t beam_width) override;
    void invokeSoftMax(TensorMap* output_tensors, TensorMap* input_tensors) override;

    using BaseBeamSearchLayer<T>::stream_;
    using BaseBeamSearchLayer<T>::is_allocate_buffer_;
    using BaseBeamSearchLayer<T>::allocator_;

    float* float_log_prob_buf_ = nullptr;

protected:
public:
    BeamSearchLayer(size_t           max_batch_size,
                    size_t           head_num,
                    size_t           size_per_head,
                    size_t           beam_width,
                    size_t           vocab_size,
                    size_t           vocab_size_padded,
                    int              end_id,
                    float            diversity_rate,
                    float            temperature,
                    float            len_penalty,
                    float            repetition_penalty,
                    cudaStream_t     stream,
                    cublasMMWrapper* cublas_wrapper,
                    IAllocator*      allocator,
                    bool             is_free_buffer_after_forward);

    BeamSearchLayer(BeamSearchLayer<T> const& beam_search_layer);

    ~BeamSearchLayer();
};

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