-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathdecoding_utils.cc
More file actions
196 lines (154 loc) · 6.57 KB
/
Copy pathdecoding_utils.cc
File metadata and controls
196 lines (154 loc) · 6.57 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "ctranslate2/decoding_utils.h"
#include <set>
#include "ctranslate2/ops/ops.h"
#include "dispatch.h"
namespace ctranslate2 {
DisableTokens::DisableTokens(StorageView& logits, const float disable_value)
: _logits(logits)
, _logits_data(logits.device() == Device::CPU ? logits.data<float>() : nullptr)
, _disable_value(disable_value)
, _batch_size(logits.dim(0))
, _vocabulary_size(logits.dim(1))
{
}
void DisableTokens::apply() {
const dim_t num_indices = _flat_indices.size();
if (num_indices == 0)
return;
const Device device = _logits.device();
const DataType dtype = _logits.dtype();
const StorageView flat_indices({num_indices}, _flat_indices, device);
DEVICE_AND_TYPE_DISPATCH(device, dtype,
primitives<D>::indexed_fill(_logits.data<T>(),
static_cast<T>(_disable_value),
flat_indices.data<int32_t>(),
num_indices));
_flat_indices.clear();
}
RepetitionPenalty::RepetitionPenalty(const float penalty)
: _penalty(penalty)
{
}
void RepetitionPenalty::apply(dim_t,
StorageView& logits,
DisableTokens&,
const StorageView& sequences,
const std::vector<dim_t>&,
const std::vector<std::vector<size_t>>*) {
if (!sequences)
return;
const Device device = logits.device();
const DataType dtype = logits.dtype();
StorageView previous_ids = sequences.to(device);
StorageView previous_scores(device, dtype);
ops::Gather(/*axis=*/-1, /*batch_dims=*/1)(logits, previous_ids, previous_scores);
DEVICE_AND_TYPE_DISPATCH(device, dtype,
primitives<D>::penalize_previous_tokens(logits.data<T>(),
previous_scores.data<T>(),
previous_ids.data<int32_t>(),
static_cast<T>(_penalty),
logits.dim(0),
previous_ids.dim(-1),
logits.dim(-1)));
}
NoRepeatNgram::NoRepeatNgram(const size_t ngram_size)
: _ngram_size(ngram_size)
{
}
void NoRepeatNgram::apply(dim_t,
StorageView&,
DisableTokens& disable_tokens,
const StorageView& sequences,
const std::vector<dim_t>&,
const std::vector<std::vector<size_t>>*) {
if (!sequences || sequences.dim(-1) < _ngram_size)
return;
const dim_t batch_size = sequences.dim(0);
const dim_t length = sequences.dim(1);
for (dim_t batch_id = 0; batch_id < batch_size; ++batch_id) {
const auto* begin = sequences.index<int32_t>({batch_id, 0});
const auto* end = begin + length;
const auto* current_ngram_begin = end - _ngram_size + 1;
std::set<size_t> ngram_final_tokens;
while (true) {
begin = std::search(begin, end, current_ngram_begin, end);
if (begin + _ngram_size > end)
break;
ngram_final_tokens.emplace(begin[_ngram_size - 1]);
begin += 1;
}
for (const auto token_id : ngram_final_tokens)
disable_tokens.add(batch_id, token_id);
}
}
SuppressSequences::SuppressSequences(std::vector<std::vector<size_t>> sequences) {
for (auto& sequence : sequences) {
if (sequence.empty())
continue;
if (sequence.size() == 1) // Single tokens are always suppressed.
_ids.emplace_back(sequence[0]);
else
_sequences.emplace_back(std::move(sequence));
}
}
void SuppressSequences::apply(dim_t,
StorageView&,
DisableTokens& disable_tokens,
const StorageView& sequences,
const std::vector<dim_t>&,
const std::vector<std::vector<size_t>>*) {
for (const auto token_id : _ids)
disable_tokens.add(token_id);
if (!sequences)
return;
const dim_t batch_size = sequences.dim(0);
const dim_t length = sequences.dim(1);
for (dim_t batch_id = 0; batch_id < batch_size; ++batch_id) {
const auto* begin = sequences.index<int32_t>({batch_id, 0});
const auto* end = begin + length;
for (const auto& banned_sequence : _sequences) {
const dim_t compare_length = banned_sequence.size() - 1;
if (length < compare_length)
continue;
const bool disable_last = std::equal(end - compare_length,
end,
banned_sequence.begin(),
banned_sequence.begin() + compare_length);
if (disable_last)
disable_tokens.add(batch_id, banned_sequence.back());
}
}
}
SuppressTokens::SuppressTokens(std::vector<size_t> ids)
: _ids(std::move(ids))
{
}
void SuppressTokens::apply(dim_t,
StorageView&,
DisableTokens& disable_tokens,
const StorageView&,
const std::vector<dim_t>&,
const std::vector<std::vector<size_t>>*) {
for (const auto token_id : _ids)
disable_tokens.add(token_id);
}
SuppressTokensBegin::SuppressTokensBegin(std::vector<size_t> ids)
: _ids(std::move(ids))
{
}
void SuppressTokensBegin::apply(dim_t step,
StorageView& logits,
DisableTokens& disable_tokens,
const StorageView&,
const std::vector<dim_t>& batch_offset,
const std::vector<std::vector<size_t>>* prefix) {
const dim_t batch_size = logits.dim(0);
for (dim_t batch_id = 0; batch_id < batch_size; ++batch_id) {
const dim_t sample_begin = get_sample_begin(batch_size, batch_id, batch_offset, prefix);
if (step != sample_begin)
continue;
for (const auto token_id : _ids)
disable_tokens.add(batch_id, token_id);
}
}
}