-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.rs
196 lines (160 loc) · 5.86 KB
/
distance.rs
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
use halo2_base::{
gates::{GateInstructions, RangeInstructions},
utils::ScalarField,
AssignedValue, Context,
};
use std::fmt::Debug;
use super::fixed_point::{FixedPointChip, FixedPointInstructions};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DistanceStrategy {
Vertical,
}
#[derive(Clone, Debug)]
pub struct DistanceChip<'a, F: ScalarField, const PRECISION_BITS: u32> {
strategy: DistanceStrategy,
fixed_point_gate: &'a FixedPointChip<F, PRECISION_BITS>,
}
impl<'a, F: ScalarField, const PRECISION_BITS: u32> DistanceChip<'a, F, PRECISION_BITS> {
pub fn new(
strategy: DistanceStrategy,
fixed_point_gate: &'a FixedPointChip<F, PRECISION_BITS>,
) -> Self {
Self { strategy, fixed_point_gate }
}
pub fn default(fixed_point_gate: &'a FixedPointChip<F, PRECISION_BITS>) -> Self {
Self::new(DistanceStrategy::Vertical, fixed_point_gate)
}
}
pub trait DistanceInstructions<F: ScalarField, const PRECISION_BITS: u32> {
type FixedPointGate: FixedPointInstructions<F, PRECISION_BITS>;
fn fixed_point_gate(&self) -> &Self::FixedPointGate;
fn strategy(&self) -> DistanceStrategy;
/// Computes the hamming distance of two quantized vectors.
/// This is equal to (1 - HammingDistance).
fn hamming_distance(
&self,
ctx: &mut Context<F>,
a: &Vec<AssignedValue<F>>,
b: &Vec<AssignedValue<F>>,
) -> AssignedValue<F>
where
F: ScalarField;
/// Computes the Manhattan distance (L1) of two quantized vectors.
fn manhattan_distance(
&self,
ctx: &mut Context<F>,
a: &Vec<AssignedValue<F>>,
b: &Vec<AssignedValue<F>>,
) -> AssignedValue<F>
where
F: ScalarField;
/// Computes the Euclidean distance (L2) of two quantized vectors.
fn euclidean_distance(
&self,
ctx: &mut Context<F>,
a: &Vec<AssignedValue<F>>,
b: &Vec<AssignedValue<F>>,
) -> AssignedValue<F>
where
F: ScalarField;
/// Computes the Cosine distance of two quantized vectors.
/// This is equal to (1 - CosineDistance).
fn cosine_distance(
&self,
ctx: &mut Context<F>,
a: &Vec<AssignedValue<F>>,
b: &Vec<AssignedValue<F>>,
) -> AssignedValue<F>
where
F: ScalarField;
}
impl<'a, F: ScalarField, const PRECISION_BITS: u32> DistanceInstructions<F, PRECISION_BITS>
for DistanceChip<'a, F, PRECISION_BITS>
{
type FixedPointGate = FixedPointChip<F, PRECISION_BITS>;
fn fixed_point_gate(&self) -> &Self::FixedPointGate {
&self.fixed_point_gate
}
fn strategy(&self) -> DistanceStrategy {
self.strategy
}
fn euclidean_distance(
&self,
ctx: &mut Context<F>,
a: &Vec<AssignedValue<F>>,
b: &Vec<AssignedValue<F>>,
) -> AssignedValue<F>
where
F: ScalarField,
{
assert_eq!(a.len(), b.len());
let ab: Vec<AssignedValue<F>> =
a.iter().zip(b).map(|(a_i, b_i)| self.fixed_point_gate.qsub(ctx, *a_i, *b_i)).collect();
// compute sum of squares of differences via self-inner product
let dist_square = self.fixed_point_gate.inner_product(ctx, ab.clone(), ab);
// take the square root
let dist = self.fixed_point_gate.qsqrt(ctx, dist_square);
// println!("{:?}", dist);
dist
}
fn cosine_distance(
&self,
ctx: &mut Context<F>,
a: &Vec<AssignedValue<F>>,
b: &Vec<AssignedValue<F>>,
) -> AssignedValue<F>
where
F: ScalarField,
{
assert_eq!(a.len(), b.len());
let ab: AssignedValue<F> = self.fixed_point_gate.inner_product(ctx, a.clone(), b.clone()); // sum (a.b)
let aa = self.fixed_point_gate.inner_product(ctx, a.clone(), a.clone()); // sum (a^2)
let bb = self.fixed_point_gate.inner_product(ctx, b.clone(), b.clone()); // sum (b^2)
let aa_sqrt = self.fixed_point_gate.qsqrt(ctx, aa);
let bb_sqrt = self.fixed_point_gate.qsqrt(ctx, bb);
let denom = self.fixed_point_gate.qmul(ctx, aa_sqrt, bb_sqrt);
let sim = self.fixed_point_gate.qdiv(ctx, ab, denom);
let one = ctx.load_constant(self.fixed_point_gate.quantization(1.0));
self.fixed_point_gate.qsub(ctx, one, sim)
}
fn hamming_distance(
&self,
ctx: &mut Context<F>,
a: &Vec<AssignedValue<F>>,
b: &Vec<AssignedValue<F>>,
) -> AssignedValue<F>
where
F: ScalarField,
{
assert_eq!(a.len(), b.len());
let ab: Vec<AssignedValue<F>> = a
.iter()
.zip(b)
.map(|(a_i, b_i)| self.fixed_point_gate.gate().is_equal(ctx, *a_i, *b_i))
.collect();
let ab_sum: AssignedValue<F> = self.fixed_point_gate.range_gate().gate().sum(ctx, ab);
let len: F = self.fixed_point_gate.quantization(a.len() as f64);
let len: AssignedValue<F> = ctx.load_witness(len);
let ab_sum_q: F = self.fixed_point_gate.quantization(ab_sum.value().get_lower_128() as f64);
let ab_sum_q: AssignedValue<F> = ctx.load_witness(ab_sum_q);
let sim = self.fixed_point_gate.qdiv(ctx, ab_sum_q, len);
let one = ctx.load_constant(self.fixed_point_gate.quantization(1.0));
self.fixed_point_gate.qsub(ctx, one, sim)
}
fn manhattan_distance(
&self,
ctx: &mut Context<F>,
a: &Vec<AssignedValue<F>>,
b: &Vec<AssignedValue<F>>,
) -> AssignedValue<F>
where
F: ScalarField,
{
assert_eq!(a.len(), b.len());
let ab_diff: Vec<AssignedValue<F>> =
a.iter().zip(b).map(|(a_i, b_i)| self.fixed_point_gate.qsub(ctx, *a_i, *b_i)).collect();
let ab_diff_abs: Vec<AssignedValue<F>> =
ab_diff.iter().map(|d| self.fixed_point_gate.qabs(ctx, *d)).collect();
self.fixed_point_gate.range_gate().gate().sum(ctx, ab_diff_abs)
}
}