forked from Luce-Org/lucebox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddtree.cpp
More file actions
226 lines (200 loc) · 7.23 KB
/
Copy pathddtree.cpp
File metadata and controls
226 lines (200 loc) · 7.23 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// DDTree implementation.
// See ddtree.h for public interface.
#include "ddtree.h"
#include <algorithm>
#include <cmath>
#include <vector>
namespace dflash::common {
void extract_draft_topk(const float * logits,
int n_positions, int vocab, int K,
float * out_log_probs,
int32_t * out_token_ids,
float temperature) {
struct Entry { float logit; int32_t id; };
auto cmp_greater = [](const Entry & a, const Entry & b) {
return a.logit > b.logit;
};
const float inv_t = 1.0f / std::max(1e-3f, temperature);
#pragma omp parallel for schedule(static)
for (int i = 0; i < n_positions; i++) {
const float * li = logits + (size_t)i * vocab;
std::vector<Entry> heap;
heap.reserve(K);
float running_max = -INFINITY;
float running_sum_exp = 0.0f;
for (int j = 0; j < vocab; j++) {
const float l = li[j] * inv_t;
if (l > running_max) {
if (running_max > -INFINITY) {
running_sum_exp = running_sum_exp * std::exp(running_max - l);
}
running_sum_exp += 1.0f;
running_max = l;
} else {
running_sum_exp += std::exp(l - running_max);
}
if ((int)heap.size() < K) {
heap.push_back({l, (int32_t)j});
std::push_heap(heap.begin(), heap.end(), cmp_greater);
} else if (l > heap.front().logit) {
std::pop_heap(heap.begin(), heap.end(), cmp_greater);
heap.back() = {l, (int32_t)j};
std::push_heap(heap.begin(), heap.end(), cmp_greater);
}
}
const float log_z = running_max + std::log(running_sum_exp);
std::sort_heap(heap.begin(), heap.end(), cmp_greater);
for (int k = 0; k < K; k++) {
out_log_probs[(size_t)i * K + k] = heap[k].logit - log_z;
out_token_ids[(size_t)i * K + k] = heap[k].id;
}
}
}
DDTree build_ddtree(const float * top_log_probs,
const int32_t * top_token_ids,
int L, int K, int budget,
bool chain_seed) {
DDTree tree;
if (budget <= 0 || L <= 0) {
tree.parents.push_back(-1);
tree.child_maps.emplace_back();
tree.visibility.assign(1, 1);
return tree;
}
struct HeapEntry {
float neg_logw;
std::vector<int> ranks;
int parent_index;
int depth;
int rank;
float logw;
};
struct HeapCmp {
bool operator()(const HeapEntry & a, const HeapEntry & b) const {
return a.neg_logw > b.neg_logw;
}
};
std::priority_queue<HeapEntry, std::vector<HeapEntry>, HeapCmp> heap;
tree.token_ids.reserve(budget);
tree.depths.reserve(budget);
tree.parents.reserve(budget + 1);
tree.parents.push_back(-1);
tree.child_maps.emplace_back();
if (chain_seed) {
const int chain_depth = std::min(L, budget);
float cum_logw = 0.0f;
int prev_idx = 0;
for (int d = 1; d <= chain_depth; d++) {
const int32_t tok_id = top_token_ids[(size_t)(d - 1) * K + 0];
cum_logw += top_log_probs[(size_t)(d - 1) * K + 0];
const int cur_idx = tree.n_nodes + 1;
tree.token_ids.push_back(tok_id);
tree.depths.push_back(d);
tree.parents.push_back(prev_idx);
tree.child_maps.emplace_back();
tree.child_maps[prev_idx][tok_id] = cur_idx;
tree.n_nodes++;
if (K > 1) {
const float sibling_logw = cum_logw
- top_log_probs[(size_t)(d - 1) * K + 0]
+ top_log_probs[(size_t)(d - 1) * K + 1];
heap.push({
-sibling_logw,
{1},
prev_idx,
d,
1,
sibling_logw,
});
}
prev_idx = cur_idx;
}
} else {
const float root_logw = top_log_probs[0 * K + 0];
heap.push({
-root_logw,
{0},
0,
1,
0,
root_logw,
});
}
while (!heap.empty() && tree.n_nodes < budget) {
HeapEntry top = heap.top();
heap.pop();
const int depth_minus_1 = top.depth - 1;
const int rank = top.rank;
const int32_t token_id = top_token_ids[(size_t)depth_minus_1 * K + rank];
const int current_index = tree.n_nodes + 1;
tree.token_ids.push_back(token_id);
tree.depths.push_back(top.depth);
tree.parents.push_back(top.parent_index);
tree.child_maps.emplace_back();
tree.child_maps[top.parent_index][token_id] = current_index;
tree.n_nodes++;
if (rank + 1 < K) {
const float sibling_logw = top.logw
- top_log_probs[(size_t)depth_minus_1 * K + rank]
+ top_log_probs[(size_t)depth_minus_1 * K + rank + 1];
std::vector<int> sibling_ranks = top.ranks;
sibling_ranks.back() = rank + 1;
heap.push({
-sibling_logw,
std::move(sibling_ranks),
top.parent_index,
top.depth,
rank + 1,
sibling_logw,
});
}
if (top.depth < L) {
const float child_logw = top.logw
+ top_log_probs[(size_t)top.depth * K + 0];
std::vector<int> child_ranks = top.ranks;
child_ranks.push_back(0);
heap.push({
-child_logw,
std::move(child_ranks),
current_index,
top.depth + 1,
0,
child_logw,
});
}
}
// Build ancestor-only visibility mask.
const int N = 1 + tree.n_nodes;
tree.visibility.assign((size_t)N * N, 0);
tree.visibility[0 * N + 0] = 1;
for (int i = 1; i < N; i++) {
const int p = tree.parents[i];
for (int j = 0; j < i; j++) {
tree.visibility[(size_t)i * N + j] = tree.visibility[(size_t)p * N + j];
}
tree.visibility[(size_t)i * N + i] = 1;
}
return tree;
}
std::vector<int> follow_verified_tree(const DDTree & tree,
const int32_t * posterior,
int & out_next_token,
int * out_node_idx) {
std::vector<int> accepted;
accepted.reserve(tree.n_nodes + 1);
accepted.push_back(0);
int current_index = 0;
int next_token = posterior[current_index];
while (true) {
const auto & children = tree.child_maps[current_index];
auto it = children.find(next_token);
if (it == children.end()) break;
current_index = it->second;
accepted.push_back(current_index);
next_token = posterior[current_index];
}
out_next_token = next_token;
if (out_node_idx) *out_node_idx = current_index;
return accepted;
}
} // namespace dflash::common