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
use std::cmp::{Ordering, Reverse};
use std::collections::BinaryHeap;
use ndarray::{Array, ArrayView, Axis, Ix2};
use ort::tensor::ndarray_tensor::NdArrayTensor;
use rand::distributions::{Distribution, WeightedIndex};
#[allow(unused_imports)]
use rand::prelude::*;
use rand::thread_rng;
pub trait Sampler {
fn sample(&self, logits: ArrayView<f32, Ix2>) -> Vec<u32>;
}
pub struct TopKSampler {
k: usize,
temperature: f32,
}
pub struct RandomSampler {
temperature: f32,
}
pub struct ArgmaxSampler {}
impl TopKSampler {
pub fn new(k: usize, temperature: f32) -> Self {
Self {
k,
temperature: if temperature == 0.0 {
1e-12
} else {
temperature
},
}
}
}
impl RandomSampler {
pub fn new(temperature: f32) -> Self {
Self {
temperature: if temperature == 0.0 {
1e-12
} else {
temperature
},
}
}
}
impl ArgmaxSampler {
pub fn new() -> Self {
Self {}
}
}
impl Sampler for TopKSampler {
fn sample(&self, logits: ArrayView<f32, Ix2>) -> Vec<u32> {
let top_elements: Vec<Vec<(usize, f32)>> = logits
.axis_iter(Axis(0))
.map(|row| {
let mut h = BinaryHeap::new();
for (id, item) in row.iter().enumerate() {
h.push(Reverse(Elem {
value: *item,
position: id,
}));
if h.len() > self.k {
h.pop();
}
}
h.into_iter()
.map(|rev| (rev.0.position, rev.0.value))
.collect()
})
.collect();
let mut rng = thread_rng();
let mut sampled_ids = Vec::new();
for top_elements in top_elements {
let mut weights = Vec::new();
for (id, value) in top_elements {
weights.push((id, (value / self.temperature).exp()));
}
let dist = WeightedIndex::new(weights.iter().map(|(_, w)| *w));
sampled_ids.push(match dist {
Ok(dist) => dist.sample(&mut rng) as u32,
Err(_) => weights[0].0 as u32,
});
}
sampled_ids.into_iter().map(|id| id as u32).collect()
}
}
impl Sampler for RandomSampler {
fn sample(&self, logits: ArrayView<f32, Ix2>) -> Vec<u32> {
let mut rng = thread_rng();
let mut sampled_ids = Vec::new();
for row in logits.axis_iter(Axis(0)) {
let mut weights = Vec::new();
for (id, value) in row.iter().enumerate() {
weights.push((id, (value / self.temperature).exp()));
}
let dist = WeightedIndex::new(weights.iter().map(|(_, w)| *w));
sampled_ids.push(match dist {
Ok(dist) => dist.sample(&mut rng) as u32,
Err(_) => weights[0].0 as u32,
});
}
sampled_ids.into_iter().map(|id| id as u32).collect()
}
}
impl Sampler for ArgmaxSampler {
fn sample(&self, logits: ArrayView<f32, Ix2>) -> Vec<u32> {
let mut sampled_ids = Vec::new();
for row in logits.axis_iter(Axis(0)) {
let mut max_value = f32::MIN;
let mut max_id = 0;
for (id, value) in row.iter().enumerate() {
if *value > max_value {
max_value = *value;
max_id = id;
}
}
sampled_ids.push(max_id);
}
sampled_ids.into_iter().map(|id| id as u32).collect()
}
}
#[derive(Copy, Clone)]
struct Elem {
value: f32,
position: usize,
}
impl PartialEq<Self> for Elem {
fn eq(&self, other: &Self) -> bool {
self.value.eq(&(other.value))
}
}
impl Eq for Elem {}
impl PartialOrd<Self> for Elem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.value.partial_cmp(&other.value)
}
}
impl Ord for Elem {
fn cmp(&self, other: &Self) -> Ordering {
self.value
.partial_cmp(&other.value)
.unwrap_or(Ordering::Equal)
}
}
pub fn select_k(array: Array<f32, Ix2>, k: usize, axis: Axis) -> Vec<Vec<(usize, f32)>> {
let other_axis = if axis.index() == 0 { Axis(1) } else { Axis(0) };
let top_elements: Vec<Vec<(usize, f32)>> = array
.axis_iter(other_axis)
.map(|row| {
let mut h = BinaryHeap::new();
for (id, item) in row.iter().enumerate() {
h.push(Reverse(Elem {
value: *item,
position: id,
}));
if h.len() > k {
h.pop();
}
}
h.into_iter()
.map(|rev| (rev.0.position, rev.0.value))
.collect()
})
.collect();
top_elements
}
pub fn sample(array: ArrayView<f32, Ix2>, k: usize, temp: f32, axis: Axis) -> Vec<usize> {
let softmax_array = (array.map(|x| x / temp)).softmax(Axis(1));
let top_elems = select_k(softmax_array, k, axis);
let mut rng = thread_rng();
let top_elem_id: Vec<usize> = top_elems
.iter()
.map(|row| {
WeightedIndex::new(row.iter().map(|x| x.1).collect::<Vec<f32>>())
.unwrap()
.sample(&mut rng)
})
.collect();
top_elem_id
.iter()
.zip(top_elems)
.map(|(top_elem_id, top_elems)| top_elems[*top_elem_id].0)
.collect()
}