-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoken_distribution.h
195 lines (168 loc) · 5.5 KB
/
token_distribution.h
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
/**
* token_distribution.h
*
* Created on: Jan 29, 2017
* Author: asaparov
*/
#ifndef TOKEN_DISTRIBUTION_H_
#define TOKEN_DISTRIBUTION_H_
#include <core/map.h>
using namespace core;
template<typename V>
struct token_distribution
{
typedef V value_type;
hash_map<unsigned int, pair<V, V>> probabilities;
uint64_t atom_count;
V prob;
V dense_prob;
V log_prob;
token_distribution(uint64_t atom_count) :
probabilities(16), atom_count(atom_count), prob(1.0 / atom_count), dense_prob(0.0), log_prob(-log(atom_count)) { }
token_distribution(const token_distribution<V>& src) : probabilities(src.probabilities.table.capacity) {
initialize(src);
}
bool set(unsigned int key, const V& probability) {
if (!probabilities.check_size())
return false;
bool contains; unsigned int index;
pair<V, V>& value = probabilities.get(key, contains, index);
if (!contains) {
probabilities.table.keys[index] = key;
probabilities.table.size++;
value.key = probability;
value.value = log(probability);
} else dense_prob -= value.key;
dense_prob += probability;
update_probabilities();
return true;
}
inline V probability(unsigned int observation) const {
bool contains;
const pair<V, V>& entry = probabilities.get(observation, contains);
if (contains) return entry.key;
else return prob;
}
template<typename SequenceType>
inline V probability(const SequenceType& sequence) const {
if (sequence.length != 1) return 0;
return probability(sequence[0]);
}
inline V log_probability(unsigned int observation) const {
bool contains;
const pair<V, V>& entry = probabilities.get(observation, contains);
if (contains) return entry.value;
else return log_prob;
}
template<typename SequenceType>
inline V log_probability(const SequenceType& sequence) const {
if (sequence.length != 1) return -std::numeric_limits<double>::infinity();
return log_probability(sequence[0]);
}
inline void update_probabilities() {
if (probabilities.table.size >= atom_count) {
if (probabilities.table.size > atom_count || dense_prob == 0.0)
fprintf(stderr, "token_distribution.update_probabilities WARNING: "
"Hashtable contains at least as many elements as atom_count.\n");
prob = 0.0;
log_prob = -std::numeric_limits<V>::infinity();
} else {
prob = (1.0 - dense_prob) / (atom_count - probabilities.table.size);
log_prob = log(prob);
}
}
static inline bool copy(const token_distribution<V>& src, token_distribution<V>& dst) {
return init(dst, src);
}
static inline void free(token_distribution<V>& distribution) {
core::free(distribution.probabilities);
}
private:
inline void initialize(const token_distribution<V>& src) {
atom_count = src.atom_count;
dense_prob = src.dense_prob;
log_prob = src.log_prob;
prob = src.prob;
memcpy(probabilities.values, src.probabilities.values,
sizeof(pair<V, V>) * src.probabilities.table.capacity);
memcpy(probabilities.table.keys, src.probabilities.table.keys,
sizeof(unsigned int) * src.probabilities.table.capacity);
probabilities.table.size = src.probabilities.table.size;
}
template<typename A>
friend bool init(token_distribution<A>&, const token_distribution<A>&);
};
template<typename V>
inline bool init(token_distribution<V>& distribution, uint64_t atom_count)
{
distribution.atom_count = atom_count;
distribution.prob = 1.0 / atom_count;
distribution.dense_prob = 0.0;
distribution.log_prob = -log(atom_count);
return hash_map_init(distribution.probabilities, 16);
}
template<typename V>
inline bool init(token_distribution<V>& distribution, const token_distribution<V>& src)
{
if (!hash_map_init(distribution.probabilities, src.probabilities.table.capacity))
return false;
distribution.initialize(src);
return true;
}
template<typename V, typename Stream>
bool read(token_distribution<V>& distribution, Stream& stream)
{
if (!read(distribution.atom_count, stream)
|| !read(distribution.probabilities, stream))
return false;
distribution.dense_prob = 0.0;
for (const auto& entry : distribution.probabilities)
distribution.dense_prob += entry.value.key;
distribution.update_probabilities();
return true;
}
template<typename V, typename Stream>
inline bool write(const token_distribution<V>& distribution, Stream& stream)
{
return write(distribution.atom_count, stream)
&& write(distribution.probabilities, stream);
}
template<typename V>
inline bool sample(const token_distribution<V>& distribution, unsigned int& output)
{
V random = sample_uniform<V>();
if (random < distribution.dense_prob
|| distribution.probabilities.table.size >= distribution.atom_count)
{
V aggregator = 0.0;
unsigned int last = 0;
for (const auto& entry : distribution.probabilities) {
last = entry.key;
aggregator += entry.value.key;
if (random < aggregator) {
output = entry.key;
return true;
}
}
output = last;
} else {
/* we sample an element not in the hash_map, and we
assume the atoms are in the set {1, ..., atom_count} */
output = sample_uniform(distribution.atom_count) + 1;
while (distribution.probabilities.table.contains(output))
output = sample_uniform(distribution.atom_count) + 1;
}
return true;
}
template<typename V>
inline bool sample(const token_distribution<V>& distribution, sequence& output)
{
output.tokens = (unsigned int*) malloc(sizeof(unsigned int));
if (output.tokens == NULL) {
fprintf(stderr, "sample ERROR: Insufficient memory for token sequence.\n");
return false;
}
output.length = 1;
return sample(distribution, output.tokens[0]);
}
#endif /* TOKEN_DISTRIBUTION_H_ */