nnv-rs 0.6.1

Verification and Statistics on Deep Neural Networks
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::ops::Neg;
use std::time::Duration;
use std::time::Instant;

use crate::bounds::Bounds1;
use crate::num::Float;
use crate::num::One;
use crate::num::Zero;
use crate::rand::distributions::Distribution;
use crate::star_node::StarNodeType;
use crate::starsets::ProbStarSet;
use crate::starsets::ProbStarSet2;
use crate::util::FstOrdTuple;
use crate::NNVFloat;
use log::debug;
use log::info;
use ndarray::concatenate;
use ndarray::Array1;
use ndarray::Array2;
use ndarray::Axis;
use ndarray::Dimension;
use ndarray::Ix2;
use ndarray::Slice;
use ordered_float::OrderedFloat;
use rand::distributions::Bernoulli;
use rand::Rng;
use std::iter;

pub trait CensoredProbStarSet<D: 'static + Dimension>: ProbStarSet<D> {
    type I<'a>: Iterator<Item = &'a Option<bool>>
    where
        Self: 'a,
        D: 'a;

    fn get_safe_value(&self) -> NNVFloat;
    fn set_safe_value(&mut self, val: NNVFloat);
    fn get_feasible_iter(&self) -> Self::I<'_>;
    fn is_node_infeasible(&self, id: usize) -> bool;
    fn get_node_feasibility(&self, id: usize) -> Option<bool>;
    fn set_node_feasibility(&mut self, id: usize, val: bool);
}

pub trait CensoredProbStarSet2: CensoredProbStarSet<Ix2> + ProbStarSet2 {
    fn get_node_output_bounds(&mut self, node_id: usize) -> (NNVFloat, NNVFloat) {
        let outer_bounds: Bounds1 = self.get_input_bounds().as_ref().cloned().unwrap();
        let (node_mut, _loc, _scale, dnn) = self.get_node_mut_with_borrows(node_id);
        node_mut.get_output_bounds(dnn, &|x| (x.lower()[[0]], x.upper()[[0]]), &outer_bounds)
    }

    /// # Panics
    fn dfs_samples<R: Rng>(
        &mut self,
        num_samples: usize,
        rng: &mut R,
        time_limit_opt: Option<Duration>,
    ) -> NNVFloat {
        let start_time = Instant::now();
        info!("dfs_samples START");
        let samples = {
            let without_fixed = self.sample_root_node(num_samples, rng);
            let almost_samples = if let Some(bounds) = self.get_input_bounds() {
                let unfixed_dims = without_fixed.shape()[0]; // Assuming rank 2 here
                let fixed_opt = bounds
                    .fixed_vals_or_none()
                    .slice_axis(Axis(0), Slice::from(..-(unfixed_dims as isize)))
                    .insert_axis(Axis(1))
                    .to_owned();
                if fixed_opt.iter().any(Option::is_some) {
                    let zeros: Array2<f64> = Array2::zeros((1, num_samples));
                    let mut fixed = fixed_opt.mapv(|x| x.map_or(0., |val| val)) + zeros;
                    fixed
                        .append(Axis(0), without_fixed.view())
                        .expect("Could not append!");
                    fixed
                } else {
                    without_fixed
                }
            } else {
                without_fixed
            };
            almost_samples
        };
        let activation_patterns = self.get_dnn().calculate_activation_pattern2(samples);
        // currently, tilting is propagated, so we need to initialize it for the root node
        self.get_node_gaussian_distribution(self.get_root_id());
        let safe_value = self.get_safe_value();
        let mut total_infeasible_cdf = 0.;
        // For each datum
        for i in 0..num_samples {
            if let Some(time_limit) = time_limit_opt {
                if start_time.elapsed() >= time_limit {
                    info!("dfs_samples STOP: time out after {} samples", i);
                    return total_infeasible_cdf;
                }
            }
            let mut path = vec![];
            // Start at the root
            let mut current_node_id = self.get_root_id();
            path.push(current_node_id);
            let mut current_node_type = self.get_node_type(current_node_id).clone();
            let mut sample_finished = false;
            // For each ReLU layer activation pattern
            for layer_activations in &activation_patterns {
                if sample_finished {
                    break;
                }
                // Go through the Affine
                if let StarNodeType::Affine { child_idx } = current_node_type {
                    current_node_id = child_idx;
                    path.push(current_node_id);
                    current_node_type = self.get_node_type(current_node_id).clone();
                }
                // For each activation
                for activation in layer_activations.column(i) {
                    if let Some(feasible) = self.get_node_feasibility(current_node_id) {
                        if !feasible {
                            sample_finished = true;
                            break;
                        }
                    }
                    let current_output_bounds = self.get_node_output_bounds(current_node_id);
                    if current_output_bounds.1 < safe_value {
                        self.set_node_feasibility(current_node_id, true);
                        sample_finished = true;
                        break;
                    } else if current_output_bounds.0 > safe_value {
                        current_node_id = self.infeasible_reset(
                            current_node_id,
                            &mut path,
                            &mut 0.,
                            &mut total_infeasible_cdf,
                            rng,
                        );
                        sample_finished = true;
                        break;
                    }
                    // Select a child node based on the activation
                    if let StarNodeType::StepRelu {
                        dim: _,
                        fst_child_idx,
                        snd_child_idx,
                    } = current_node_type
                    {
                        if *activation {
                            current_node_id = fst_child_idx;
                        } else {
                            current_node_id =
                                snd_child_idx.expect("Error selecting a second child!");
                        }
                        current_node_type = self.get_node_type(current_node_id).clone();
                        path.push(current_node_id);
                    } else {
                        panic!("Expected a ReLU layer! Found {:?}", current_node_type);
                    }
                }
            }
            // Leaf reached only if sample didn't exit early
            if !sample_finished {
                let mut current_node_type = self.get_node_type(current_node_id).clone();
                // Go through the last Affine
                if let StarNodeType::Affine { child_idx } = current_node_type {
                    current_node_id = child_idx;
                    current_node_type = self.get_node_type(current_node_id).clone();
                }
                match current_node_type {
                    StarNodeType::Leaf {
                        unsafe_idx: Some(_),
                        ..
                    } => continue,
                    StarNodeType::Leaf {
                        unsafe_idx: None,
                        safe_idx: safe_idx_val,
                    } => {
                        let safe_value = self.get_safe_value();
                        let unsafe_node =
                            self.get_node(current_node_id).get_unsafe_star(safe_value);
                        let unsafe_node_idx = self.add_node(unsafe_node, current_node_id);
                        self.set_node_type(
                            current_node_id,
                            StarNodeType::Leaf {
                                safe_idx: safe_idx_val,
                                unsafe_idx: Some(unsafe_node_idx),
                            },
                        );
                        path.push(unsafe_node_idx);
                        self.infeasible_reset(
                            current_node_id,
                            &mut path,
                            &mut 0.,
                            &mut total_infeasible_cdf,
                            rng,
                        );
                    }
                    _ => panic!(
                        "This should always be a leaf, instead it's a {:?} in {:?}",
                        current_node_type,
                        self.get_node_type_iter().collect::<Vec<_>>()
                    ),
                }
            }
        }
        info!("dfs_samples STOP: complete");
        total_infeasible_cdf
    }

    fn get_overapproximated_infeasible_input_regions(&self) -> Vec<Bounds1> {
        let unsafe_nodes: Vec<usize> = self
            .get_feasible_iter()
            .enumerate()
            .filter(|(_pos, x)| matches!(x, Some(false)))
            .map(|(pos, _x)| pos)
            .collect();
        let deduped_unsafe_nodes = unsafe_nodes.iter().filter(|idx| {
            self.get_node_ancestors(**idx)
                .iter()
                .all(|x| *x == **idx || !unsafe_nodes.contains(x))
        });
        deduped_unsafe_nodes
            .filter_map(|idx| {
                self.get_node(*idx)
                    .try_get_axis_aligned_input_bounds()
                    .as_ref()
            })
            .cloned()
            .collect()
    }

    fn infeasible_reset<R: Rng>(
        &mut self,
        x: usize,
        path: &mut Vec<usize>,
        path_logp: &mut NNVFloat,
        total_infeasible_cdf: &mut NNVFloat,
        rng: &mut R,
    ) -> usize {
        self.set_node_feasibility(x, false);
        let mut infeas_cdf = self.get_node_cdf(x, rng);
        path.drain(..).rev().for_each(|x| {
            // check if all chilren are infeasible
            if self
                .get_node_child_ids(x)
                .iter()
                .any(|&x| !self.is_node_infeasible(x))
            {
                // if not all infeasible, update CDF
                self.add_node_cdf(x, NNVFloat::neg(NNVFloat::one()) * infeas_cdf);
            } else {
                // if all infeasible, set this to infeasible and continue
                self.set_node_feasibility(x, false);
                infeas_cdf = self.get_node_cdf(x, rng);
            }
        });
        *total_infeasible_cdf += infeas_cdf;
        *path_logp = NNVFloat::zero();
        0
    }

    /// 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)]
    fn sample_safe_star<R: Rng>(
        &mut self,
        num_samples: usize,
        rng: &mut R,
        time_limit_opt: Option<Duration>,
    ) -> Option<(Vec<Array1<NNVFloat>>, NNVFloat, NNVFloat)> {
        let start_time = Instant::now();
        info!("sample_safe_star START");
        let max_iters = self.get_max_accept_reject_iters();
        let stability_eps = self.get_stability_eps();
        let mut sample_heap: BinaryHeap<FstOrdTuple<Reverse<OrderedFloat<f64>>, _>> =
            BinaryHeap::new();
        let mut current_node = 0;
        let mut path = vec![];
        let mut path_logp = 0.;
        let mut total_infeasible_cdf = 0.;
        loop {
            // base case for feasability
            if current_node == 0 && self.is_node_infeasible(current_node) {
                info!("sample_safe_star STOP: 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);
            debug_assert!(self
                .try_get_node_gaussian_distribution(current_node)
                .is_some());
            match safe_sample_opt {
                Ok(safe_samples) => {
                    if safe_samples.iter().any(|x| x.iter().any(|v| v.abs() > 10.)) {
                        panic!("Random safe sample big!");
                    }
                    info!(
                        "sample_safe_star STOP: Safe samples found less than {} at depth {}",
                        self.get_safe_value(),
                        path.len()
                    );
                    return Some((safe_samples, path_logp, total_infeasible_cdf));
                }
                Err(sample_val_pairs) => sample_val_pairs
                    .into_iter()
                    .map(|(sample, val)| (Reverse(OrderedFloat(val)), sample.mapv(OrderedFloat)))
                    .for_each(|pair| sample_heap.push(FstOrdTuple(pair))),
            };
            // check feasibility of current node
            {
                if let Some(feasible) = self.get_node_feasibility(current_node) {
                    if !feasible {
                        current_node = self.get_root_id();
                        continue;
                    }
                }
                // makes the assumption that bounds are on 0th dimension of output
                let output_bounds = if current_node == 0 {
                    (NNVFloat::neg_infinity(), NNVFloat::infinity())
                } else {
                    self.get_node_output_bounds(current_node)
                };
                debug!("Output bounds: {:?}", output_bounds);
                debug_assert!(self
                    .try_get_node_gaussian_distribution(current_node)
                    .is_some());
                if output_bounds.1 <= self.get_safe_value() {
                    // handle case where star is safe
                    let safe_sample = self.sample_gaussian_node(current_node, rng, num_samples);
                    if !safe_sample.is_empty() {
                        if safe_sample.iter().any(|x| x.iter().any(|v| v.abs() > 10.)) {
                            panic!("Safe node sample big!");
                        }
                        info!(
                            "sample_safe_star STOP: Safe sample with value at most {} at depth {} (bounds {:?})",
                            self.get_safe_value(),
                            path.len(),
                            output_bounds
                        );
                        return Some((safe_sample, path_logp, total_infeasible_cdf));
                    } else {
                        // Best thing to do here would be to figure out how to sample despite poor conditioning
                        current_node = self.infeasible_reset(
                            current_node,
                            &mut path,
                            &mut path_logp,
                            &mut total_infeasible_cdf,
                            rng,
                        );
                    }
                } else if output_bounds.0 > self.get_safe_value() {
                    // handle case where star is infeasible
                    self.set_node_feasibility(current_node, false);
                    current_node = self.infeasible_reset(
                        current_node,
                        &mut path,
                        &mut path_logp,
                        &mut total_infeasible_cdf,
                        rng,
                    );
                    continue;
                } else {
                    match self.get_node_type(current_node) {
                        StarNodeType::Leaf { .. } => {
                            // do procedure to select safe part
                            let safe_sample = self.sample_gaussian_node_safe(
                                current_node,
                                rng,
                                num_samples,
                                max_iters,
                                self.get_safe_value(),
                                stability_eps,
                            );
                            if !safe_sample.is_empty() {
                                if safe_sample.iter().any(|x| x.iter().any(|v| v.abs() > 10.)) {
                                    panic!("Leaf safe sample big!");
                                }
                                info!(
                                    "sample_safe_star STOP: Safe sample with value at most {} at leaf (bounds {:?})",
                                    self.get_safe_value(),
                                    output_bounds
                                );
                                return Some((safe_sample, path_logp, total_infeasible_cdf));
                            } else {
                                // Best thing to do here would be to figure out how to sample despite poor conditioning
                                current_node = self.infeasible_reset(
                                    current_node,
                                    &mut path,
                                    &mut path_logp,
                                    &mut total_infeasible_cdf,
                                    rng,
                                );
                            }
                        }
                        _ => (),
                    }
                }
                // 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 {
                    let samples: Vec<_> = sample_heap
                        .into_iter_sorted()
                        .take(num_samples)
                        .map(|pair| pair.0 .1.mapv(|x| x.0))
                        .collect();
                    if samples.iter().any(|x| x.iter().any(|v| v.abs() > 10.)) {
                        panic!("Timeout safe sample big!");
                    }
                    info!(
                        "sample_safe_star STOP: Unsafe sample after timeout at depth {}",
                        path.len()
                    );
                    return Some((samples, path_logp, total_infeasible_cdf));
                }
            }
            debug_assert!(self
                .try_get_node_gaussian_distribution(current_node)
                .is_some());
            // expand node
            {
                let result = self.select_safe_child(current_node, path_logp, rng);
                match result {
                    Some((node, logp)) => {
                        debug_assert!(node != current_node);
                        current_node = node;
                        path_logp += logp;
                    }
                    None => {
                        current_node = self.infeasible_reset(
                            current_node,
                            &mut path,
                            &mut path_logp,
                            &mut total_infeasible_cdf,
                            rng,
                        );
                    }
                }
            }
        }
    }

    /// # Errors
    fn try_safe_sample<R: Rng>(
        &mut self,
        current_node: usize,
        rng: &mut R,
        num_samples: usize,
    ) -> Result<Vec<Array1<NNVFloat>>, Vec<(Array1<NNVFloat>, NNVFloat)>> {
        let unsafe_sample: Vec<_> = self.sample_gaussian_node(current_node, rng, num_samples);
        let unsafe_sample_and_vals: Vec<(&Array1<NNVFloat>, f64)> = {
            let sample_iter = unsafe_sample.iter();
            let fixed_input_part: Option<Array1<NNVFloat>> = {
                let unsafe_len = unsafe_sample.get(0).map(|x| x.len());
                if unsafe_len.is_none() {
                    return Err(Vec::new());
                }
                let unsafe_len = unsafe_len.unwrap();
                self.get_input_bounds().as_ref().map(|bounds| {
                    let fixed_bounds: Bounds1 = bounds.split_at(bounds.ndim() - unsafe_len).0;
                    let fixed_array: Array1<NNVFloat> = fixed_bounds.lower().to_owned();
                    fixed_array
                })
            };
            let _node = self.get_node(current_node);
            let _bounds = self.get_input_bounds().as_ref();
            if let Some(fix_part) = fixed_input_part {
                sample_iter
                    .zip(iter::repeat(fix_part))
                    .map(|(unfix, fix)| {
                        (
                            unfix,
                            concatenate(Axis(0), &[fix.view(), unfix.view()]).unwrap(),
                        )
                    })
                    //.filter(|(_sample, x)| bounds.map(|b| b.is_member(&x.view())).unwrap_or(true))
                    //.filter(|(_sample, x)| node.is_input_member(&x.view()))
                    .map(|(sample, x)| (sample, self.get_dnn().forward1(x)[[0]]))
                    .collect()
            } else {
                sample_iter
                    .map(|x| (x, self.get_dnn().forward1(x.clone())[[0]]))
                    //.filter(|(sample, _out)| {
                    //    bounds.map(|b| b.is_member(&sample.view())).unwrap_or(true)
                    //})
                    //.filter(|(sample, _out)| node.is_input_member(&sample.view()))
                    .collect()
            }
        };

        let safe_subset: Vec<_> = unsafe_sample_and_vals
            .iter()
            .filter(|(_sample, out)| *out < self.get_safe_value())
            .map(|(sample, _val)| sample.to_owned())
            .collect();
        if safe_subset.is_empty() {
            Err(unsafe_sample_and_vals
                .into_iter()
                .map(|(x, out)| (x.to_owned(), out))
                .collect())
        } else {
            Ok(safe_subset
                .into_iter()
                .map(|sample| sample.clone())
                .collect())
        }
    }

    /// 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` (`NNVFloat`): 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: NNVFloat,
        rng: &mut R,
    ) -> Option<(usize, NNVFloat)> {
        debug_assert!(self
            .try_get_node_gaussian_distribution(current_node)
            .is_some());
        let current_node_type = self.get_node_type(current_node);
        match *current_node_type {
            // leaf node, which must be partially safe and partially unsafe
            StarNodeType::Leaf {
                safe_idx: Some(safe_idx_val),
                ..
            } => Some((safe_idx_val, 1.)),
            StarNodeType::Leaf {
                safe_idx: None,
                unsafe_idx: unsafe_idx_val,
            } => {
                let safe_value = self.get_safe_value();
                let safe_node = self.get_node(current_node).get_safe_star(safe_value);
                let safe_node_idx = self.add_node(safe_node, current_node);
                self.set_node_type(
                    current_node,
                    StarNodeType::Leaf {
                        safe_idx: Some(safe_node_idx),
                        unsafe_idx: unsafe_idx_val,
                    },
                );
                Some((safe_node_idx, 1.))
            }
            StarNodeType::Affine { child_idx }
            | StarNodeType::Conv { child_idx }
            | StarNodeType::Interpolate { child_idx } => Some((child_idx, path_logp)),
            StarNodeType::StepRelu {
                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.is_node_infeasible(child))
                    .collect();
                match children.len() {
                    0 => None,
                    1 => Some((children[0], path_logp)),
                    2 => {
                        let fst_cdf = self.get_node_cdf(children[0], rng);
                        let snd_cdf = self.get_node_cdf(children[1], 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.get_node_cdf(current_node, 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 = NNVFloat::epsilon();
                                }
                                self.set_node_cdf(children[0], derived_fst_cdf);
                                derived_fst_cdf / (derived_fst_cdf + snd_cdf)
                            }
                            (true, false) => {
                                let parent_cdf = self.get_node_cdf(current_node, rng);
                                let mut derived_snd_cdf = parent_cdf - fst_cdf;
                                if derived_snd_cdf.is_sign_negative() {
                                    derived_snd_cdf = NNVFloat::epsilon();
                                }
                                self.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).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 += (NNVFloat::one() - fst_prob).ln();
                            Some((children[1], path_logp))
                        }
                    }
                    _ => {
                        panic!();
                    }
                }
            }
            StarNodeType::StepReluDropOut {
                dropout_prob,
                fst_child_idx,
                snd_child_idx,
                trd_child_idx,
                ..
            } => {
                let dropout_dist = Bernoulli::new(dropout_prob).unwrap();
                if dropout_dist.sample(rng) {
                    path_logp += dropout_prob.ln();
                    Some((fst_child_idx, path_logp))
                } else {
                    path_logp += (NNVFloat::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.is_node_infeasible(child))
                        .collect();
                    match children.len() {
                        0 => None,
                        1 => Some((children[0], path_logp)),
                        _ => {
                            let fst_cdf = self.get_node_cdf(children[0], rng);
                            let snd_cdf = self.get_node_cdf(children[1], 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.get_node_cdf(current_node, rng);
                                    let derived_fst_cdf = parent_cdf - snd_cdf;
                                    self.set_node_cdf(children[0], derived_fst_cdf);
                                    derived_fst_cdf / (derived_fst_cdf + snd_cdf)
                                }
                                (true, false) => {
                                    let parent_cdf = self.get_node_cdf(current_node, rng);
                                    let derived_snd_cdf = parent_cdf - fst_cdf;
                                    self.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).unwrap();
                            if dist.sample(rng) {
                                path_logp += fst_prob.ln();
                                Some((children[0], path_logp))
                            } else {
                                path_logp += (NNVFloat::one() - fst_prob).ln();
                                Some((children[1], path_logp))
                            }
                        }
                    }
                }
            }
        }
    }
}