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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use crate::bounds::Bounds1;
use crate::constellation::Constellation;
use crate::star_node::StarNodeType;
use crate::NNVFloat;
use log::{debug, info};
use ndarray::concatenate;
use ndarray::s;
use ndarray::Array1;
use ndarray::Axis;
use ndarray::Dimension;
use ndarray::Ix2;
use rand::distributions::{Bernoulli, Distribution};
use rand::Rng;
use std::iter;
use std::time::{Duration, Instant};
pub struct Asterism<'a, T: NNVFloat, D: Dimension> {
constellation: &'a mut Constellation<T, D>,
safe_value: T,
is_feasible: Vec<bool>,
}
impl<'a, T: NNVFloat, D: Dimension> Asterism<'a, T, D> {
pub fn new(constellation: &'a mut Constellation<T, D>, safe_value: T) -> Self {
Self {
constellation,
safe_value,
is_feasible: vec![true],
}
}
fn get_feasible(&self, id: usize) -> bool {
*self.is_feasible.get(id).or(Some(&true)).unwrap()
}
fn set_feasible(&mut self, id: usize, val: bool) {
if id >= self.is_feasible.len() {
self.is_feasible.resize(id + 1, true);
}
self.is_feasible[id] = val;
}
}
impl<'a, T: NNVFloat> Asterism<'a, T, Ix2> {
/// Sample from a Gaussian distribution with an upper bound on the output value
///
/// This function uses a two step sampling process. It first walks the Constellation's binary
/// tree, which is defined by the parameters of the underlying neural network. This walk
/// continues until a safe star has been reached, and then it's sampled in the second step to
/// produce the final set of samples.
///
/// # Arguments
///
/// * `safe_value` - Maximum value an output can have to be considered safe
/// * `rng` - A random number generator
/// * `cdf_samples` - Number of samples to use in estimating a polytope's CDF under the Gaussian
/// * `num_samples` - Number of samples to return from the final star that's sampled
///
/// # Panics
///
/// # Returns
///
/// (Vec of samples, log path probability, total invalid cdf proportion)
///
/// Sample probability can be calculated (in a network without dropouts) by calculating
/// gaussian probability and dividing by (1 - invalid cdf proportion)
#[allow(clippy::too_many_lines)]
pub fn sample_safe_star<R: Rng>(
&mut self,
num_samples: usize,
rng: &mut R,
cdf_samples: usize,
max_iters: usize,
time_limit_opt: Option<Duration>,
) -> Option<(Vec<Array1<T>>, T, T)> {
let start_time = Instant::now();
let mut best_sample = None;
let mut best_sample_val = T::infinity();
let mut current_node = 0;
let mut path = vec![];
let mut path_logp = T::zero();
let mut total_infeasible_cdf = T::zero();
let infeasible_reset = |me: &mut Self,
x: usize,
path: &mut Vec<usize>,
path_logp: &mut T,
total_infeasible_cdf: &mut T,
rng: &mut R|
-> usize {
info!("Infeasible reset!");
me.set_feasible(x, false);
let mut infeas_cdf = me
.constellation
.get_node_cdf(x, cdf_samples, max_iters, rng);
path.drain(..).rev().for_each(|x| {
// check if all chilren are infeasible
if me
.constellation
.get_node_child_ids(x)
.iter()
.any(|&x| me.get_feasible(x))
{
// if not infeasible, update CDF
me.constellation
.add_node_cdf(x, T::neg(T::one()) * infeas_cdf);
} else {
me.set_feasible(x, false);
infeas_cdf = me
.constellation
.get_node_cdf(x, cdf_samples, max_iters, rng)
}
});
*total_infeasible_cdf += infeas_cdf;
*path_logp = T::zero();
0
};
loop {
debug!("Current node: {:?}", current_node);
// base case for feasability
if current_node == 0 && !self.get_feasible(current_node) {
info!("No feasible value exists!");
return None;
}
// Try to sample and return if a valid sample is found
let safe_sample_opt = self.try_safe_sample(current_node, rng, num_samples, max_iters);
debug_assert!(self
.constellation
.try_get_gaussian_distribution(current_node)
.is_some());
match safe_sample_opt {
Ok((safe_sample, est_cost)) => {
info!(
"Safe sample with value {} less than {} at depth {:?}",
est_cost,
self.safe_value,
path.len()
);
return Some((vec![safe_sample], path_logp, total_infeasible_cdf));
}
Err((unsafe_sample, val)) => {
best_sample = Some(unsafe_sample);
best_sample_val = val;
}
};
// check feasibility of current node
{
// makes the assumption that bounds are on 0th dimension of output
let output_bounds = if current_node != 0 {
self.constellation.get_node_output_bounds(current_node)
} else {
(T::neg_infinity(), T::infinity())
};
debug!("Output bounds: {:?}", output_bounds);
debug_assert!(self
.constellation
.try_get_gaussian_distribution(current_node)
.is_some());
if output_bounds.1 <= self.safe_value {
// handle case where star is safe
info!(
"Safe sample with value at most {} at depth {} (bounds {:?})",
self.safe_value,
path.len(),
output_bounds
);
let safe_sample = self.constellation.sample_gaussian_node(
current_node,
rng,
num_samples,
max_iters,
);
return Some((safe_sample, path_logp, total_infeasible_cdf));
} else if output_bounds.0 > self.safe_value {
// handle case where star is infeasible
self.set_feasible(current_node, false);
current_node = infeasible_reset(
self,
current_node,
&mut path,
&mut path_logp,
&mut total_infeasible_cdf,
rng,
);
continue;
} else if let StarNodeType::Leaf = self.constellation.get_node_type(current_node) {
// do procedure to select safe part
info!(
"Safe sample with value at most {} at leaf (bounds {:?})",
self.safe_value, output_bounds
);
let safe_sample = self.constellation.sample_gaussian_node_safe(
current_node,
rng,
num_samples,
max_iters,
self.safe_value,
);
return Some((safe_sample, path_logp, total_infeasible_cdf));
}
// otherwise, push to path and continue expanding
path.push(current_node);
}
// check timeout
if let Some(time_limit) = time_limit_opt {
if start_time.elapsed() >= time_limit {
info!(
"Unsafe sample after timeout with value with value {:?} at depth {}",
best_sample_val,
path.len()
);
return best_sample.map(|x| (vec![x], path_logp, total_infeasible_cdf));
}
}
debug_assert!(self
.constellation
.try_get_gaussian_distribution(current_node)
.is_some());
// expand node
{
let result =
self.select_safe_child(current_node, path_logp, cdf_samples, max_iters, rng);
match result {
Some((node, logp)) => {
debug_assert!(node != current_node);
current_node = node;
path_logp += logp;
}
None => {
current_node = infeasible_reset(
self,
current_node,
&mut path,
&mut path_logp,
&mut total_infeasible_cdf,
rng,
);
}
}
}
}
}
fn try_safe_sample<R: Rng>(
&mut self,
current_node: usize,
rng: &mut R,
num_samples: usize,
max_iters: usize,
) -> Result<(Array1<T>, T), (Array1<T>, T)> {
let unsafe_sample =
self.constellation
.sample_gaussian_node(current_node, rng, num_samples, max_iters);
let unsafe_len = unsafe_sample[0].len();
let fixed_input_part: Option<Array1<T>> = {
self.constellation
.get_input_bounds()
.as_ref()
.map(|bounds| {
let fixed_bounds: Bounds1<T> = bounds.split_at(unsafe_len).1;
let fixed_array: Array1<T> = fixed_bounds.lower().to_owned();
fixed_array
})
};
let dnn_idx = self.constellation.get_node_dnn_index(current_node);
let mut best_sample = Array1::zeros(1);
let mut best_val = T::infinity();
let sample_opt: Option<(Array1<T>, T)> = if let Some(fixed_input_part) = fixed_input_part {
let mut input_iter = unsafe_sample
.into_iter()
.zip(iter::repeat(fixed_input_part))
.map(|(unfix, fix)| concatenate(Axis(0), &[unfix.view(), fix.view()]).unwrap());
input_iter.find_map(|x| {
let output = self.constellation.get_dnn().forward1(x.clone());
debug_assert_eq!(output.len(), 1);
if output[[0]] < self.safe_value {
Some((x, output[[0]]))
} else {
if output[[0]] < best_val {
best_sample = x;
best_val = output[[0]]
}
None
}
})
} else {
let mut input_iter = unsafe_sample.into_iter();
input_iter.find_map(|x| {
let output = self.constellation.get_dnn().forward1(x.clone());
debug_assert_eq!(output.len(), 1);
if output[[0]] < self.safe_value {
Some((x, output[[0]]))
} else {
if output[[0]] < best_val {
best_sample = x;
best_val = output[[0]]
}
None
}
})
};
sample_opt
.map(|(sample, est_cost)| {
let unfixed = sample.slice(s![..unsafe_len]).to_owned();
(unfixed, est_cost)
})
.ok_or_else(|| (best_sample.slice(s![..unsafe_len]).to_owned(), best_val))
}
/// Given a node, samples one of its children and returns the log probability
///
/// Returns:
/// child_idx (usize): The child that was reached in the sampling procedure
/// path_logp (T): The log probability of reaching the child from the root node
///
/// # Panics
fn select_safe_child<R: Rng>(
&mut self,
current_node: usize,
mut path_logp: T,
cdf_samples: usize,
max_iters: usize,
rng: &mut R,
) -> Option<(usize, T)> {
let input_bounds = self.constellation.get_input_bounds();
debug_assert!(self
.constellation
.try_get_gaussian_distribution(current_node)
.is_some());
let current_node_type = self.constellation.get_node_type(current_node);
match *current_node_type {
// leaf node, which must be partially safe and partially unsafe
StarNodeType::Leaf => {
panic!();
}
StarNodeType::Affine { child_idx } => Some((child_idx, path_logp)),
StarNodeType::StepRelu {
dim,
fst_child_idx,
snd_child_idx,
} => {
let mut children = vec![fst_child_idx];
if let Some(snd_child) = snd_child_idx {
children.push(snd_child);
}
children = children
.into_iter()
.filter(|&child| self.get_feasible(child))
.collect();
match children.len() {
0 => None,
1 => Some((children[0], path_logp)),
2 => {
let fst_cdf = self.constellation.get_node_cdf(
children[0],
cdf_samples,
max_iters,
rng,
);
let snd_cdf = self.constellation.get_node_cdf(
children[1],
cdf_samples,
max_iters,
rng,
);
debug!(
"Selecting between 2 children with CDFs: {} and {}",
fst_cdf, snd_cdf
);
debug!(
"Selecting between 2 children with CDFs: {} and {}",
fst_cdf, snd_cdf
);
// Handle the case where a CDF gives a non-normal value
debug_assert!(fst_cdf.is_sign_positive());
debug_assert!(snd_cdf.is_sign_positive());
let fst_prob = match (fst_cdf.is_normal(), snd_cdf.is_normal()) {
(true, true) => fst_cdf / (fst_cdf + snd_cdf),
(false, true) => {
let parent_cdf = self.constellation.get_node_cdf(
current_node,
cdf_samples,
max_iters,
rng,
);
let mut derived_fst_cdf = parent_cdf - snd_cdf;
// CDFs are estimates and it is
// possible for the parent to have
// smaller CDF than the child.
if derived_fst_cdf.is_sign_negative() {
derived_fst_cdf = T::epsilon();
}
self.constellation
.set_node_cdf(children[0], derived_fst_cdf);
derived_fst_cdf / (derived_fst_cdf + snd_cdf)
}
(true, false) => {
let parent_cdf = self.constellation.get_node_cdf(
current_node,
cdf_samples,
max_iters,
rng,
);
let mut derived_snd_cdf = parent_cdf - fst_cdf;
if derived_snd_cdf.is_sign_negative() {
derived_snd_cdf = T::epsilon();
}
self.constellation
.set_node_cdf(children[1], derived_snd_cdf);
fst_cdf / (fst_cdf + derived_snd_cdf)
}
// If both CDFs are non-normal, we'll mark the current node as infeasible
(false, false) => {
return None;
}
};
// Safe unwrap due to above handling
let dist = Bernoulli::new(fst_prob.into()).unwrap_or_else(|_| panic!("Bad fst_prob: {:?} fst_cdf: {:?} snd_cdf: {:?}, fst_n {:?}, snd_n {:?}", fst_prob, fst_cdf, snd_cdf, fst_cdf.is_normal(), snd_cdf.is_normal()));
if dist.sample(rng) {
path_logp += fst_prob.ln();
Some((children[0], path_logp))
} else {
path_logp += (T::one() - fst_prob).ln();
Some((children[1], path_logp))
}
}
_ => {
panic!();
}
}
}
StarNodeType::StepReluDropOut {
dim,
dropout_prob,
fst_child_idx,
snd_child_idx,
trd_child_idx,
} => {
let dropout_dist = Bernoulli::new(dropout_prob.into()).unwrap();
if dropout_dist.sample(rng) {
path_logp += dropout_prob.ln();
Some((fst_child_idx, path_logp))
} else {
path_logp += (T::one() - dropout_prob).ln();
let mut children = vec![];
if let Some(snd_child) = snd_child_idx {
children.push(snd_child);
}
if let Some(trd_child) = trd_child_idx {
children.push(trd_child);
}
children = children
.into_iter()
.filter(|&child| self.get_feasible(child))
.collect();
match children.len() {
0 => None,
1 => Some((children[0], path_logp)),
_ => {
let fst_cdf = self.constellation.get_node_cdf(
children[0],
cdf_samples,
max_iters,
rng,
);
let snd_cdf = self.constellation.get_node_cdf(
children[1],
cdf_samples,
max_iters,
rng,
);
// Handle the case where a CDF gives a non-normal value
let fst_prob = match (fst_cdf.is_normal(), snd_cdf.is_normal()) {
(true, true) => fst_cdf / (fst_cdf + snd_cdf),
(false, true) => {
let parent_cdf = self.constellation.get_node_cdf(
current_node,
cdf_samples,
max_iters,
rng,
);
let derived_fst_cdf = parent_cdf - snd_cdf;
self.constellation
.set_node_cdf(children[0], derived_fst_cdf);
derived_fst_cdf / (derived_fst_cdf + snd_cdf)
}
(true, false) => {
let parent_cdf = self.constellation.get_node_cdf(
current_node,
cdf_samples,
max_iters,
rng,
);
let derived_snd_cdf = parent_cdf - fst_cdf;
self.constellation
.set_node_cdf(children[1], derived_snd_cdf);
fst_cdf / (fst_cdf + derived_snd_cdf)
}
// If both CDFs are non-normal, we'll mark the current node as infeasible
(false, false) => {
return None;
}
};
// Safe unwrap due to above handling
let dist = Bernoulli::new(fst_prob.into()).unwrap();
if dist.sample(rng) {
path_logp += fst_prob.ln();
Some((children[0], path_logp))
} else {
path_logp += (T::one() - fst_prob).ln();
Some((children[1], path_logp))
}
}
}
}
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::test_util::*;
use proptest::*;
proptest! {
#[test]
fn test_sample_safe_star(mut constellation in generic_constellation(2, 2, 2, 2)) {
let mut rng = rand::thread_rng();
let mut asterism = Asterism::new(&mut constellation, 1.);
let default = Array1::zeros(asterism.constellation.get_dnn().input_shape()[0].unwrap());
let sample = asterism.sample_safe_star(1, &mut rng, 1, 1).unwrap_or((vec![(default, 0.69)], 0.69));
assert_eq!(sample.0[0].0.len(), asterism.constellation.get_dnn().input_shape()[0].unwrap(), "expected sample shape: {:?}", asterism.constellation.get_dnn().input_shape()[0].unwrap())
}
}
}