fast-nnt 0.4.0

Rust implementation of the NeighborNet phylogenetic network algorithm.
Documentation
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
use anyhow::Result;
use fixedbitset::FixedBitSet;
use log::info;
use ndarray::Array2;
use std::time::{Duration, Instant};

use super::band::{
    BandIndex, BandKernelPerf, avg_band_time_us, band_to_row, calculate_ab_band,
    calculate_atx_band, row_to_band,
};
use crate::splits::asplit::ASplit;

pub const DEFAULT_CUTOFF: f64 = 1e-6;

#[derive(Debug, Clone, serde::Serialize, Default)]
pub struct CGSolveStats {
    pub outer_iterations: usize,
    pub cg_calls: usize,
    pub cg_iters_total: usize,
    pub max_cg_iters_single_call: usize,
    pub stagnation_exits: usize,
    pub inner_loop_activations: usize,
    pub collapse_events: usize,
    pub collapsed_indices_total: usize,
    pub kernel_ab_calls: usize,
    pub kernel_atx_calls: usize,
    pub elapsed_secs: f64,
}

type KernelPerf = BandKernelPerf;

#[derive(Debug)]
struct SolveProgress {
    started: Instant,
    next_log: Instant,
    next_inner_log: Instant,
    outer_iter: usize,
    cg_calls: usize,
    cg_iters_total: usize,
    collapse_events: usize,
    collapsed_indices_total: usize,
    max_cg_iters_single_call: usize,
    inner_loop_activations: usize,
    stagnation_exits: usize,
}

impl SolveProgress {
    fn new(now: Instant) -> Self {
        Self {
            started: now,
            next_log: now + Duration::from_secs(5),
            next_inner_log: now + Duration::from_secs(10),
            outer_iter: 0,
            cg_calls: 0,
            cg_iters_total: 0,
            collapse_events: 0,
            collapsed_indices_total: 0,
            max_cg_iters_single_call: 0,
            inner_loop_activations: 0,
            stagnation_exits: 0,
        }
    }
}

#[derive(Clone, Copy, Debug)]
struct CGOptions {
    cutoff: f64,
}

impl Default for CGOptions {
    fn default() -> Self {
        Self {
            cutoff: DEFAULT_CUTOFF,
        }
    }
}

/// Split weights via active-set NNLS with a conjugate-gradient inner solver.
/// `cycle` is 1-based with a leading 0 sentinel.
pub fn compute_splits(
    cycle: &[usize],
    distances: &Array2<f64>,
) -> Result<(Vec<ASplit>, CGSolveStats)> {
    compute_weighted_splits(cycle, distances, CGOptions::default())
}

fn compute_weighted_splits(
    cycle: &[usize],
    distances: &Array2<f64>,
    options: CGOptions,
) -> Result<(Vec<ASplit>, CGSolveStats)> {
    let n = cycle.len().saturating_sub(1);
    if n <= 1 {
        return Ok((Vec::new(), CGSolveStats::default()));
    }
    if n == 2 {
        let d_ij = distances[[cycle[1] - 1, cycle[2] - 1]];
        if d_ij > 0.0 {
            let mut a = FixedBitSet::with_capacity(n + 1);
            a.grow(n + 1);
            a.set(cycle[1], true);
            return Ok((
                vec![ASplit::from_a_ntax_with_weight(a, n, d_ij)],
                CGSolveStats::default(),
            ));
        }
        return Ok((Vec::new(), CGSolveStats::default()));
    }

    let npairs = n * (n - 1) / 2;
    let d = setup_d(distances, cycle, n);

    let mut x = vec![0.0; npairs];
    let stats = run_active_conjugate(n, &d, &mut x);

    let mut splits = Vec::new();
    let mut index = 0usize;
    for i in 0..n {
        let mut a = FixedBitSet::with_capacity(n + 1);
        a.grow(n + 1);
        for j in (i + 1)..n {
            a.set(cycle[j + 1], true);
            if x[index] > options.cutoff {
                splits.push(ASplit::from_a_ntax_with_weight(a.clone(), n, x[index]));
            }
            index += 1;
        }
    }
    Ok((splits, stats))
}

fn setup_d(distances: &Array2<f64>, cycle: &[usize], n: usize) -> Vec<f64> {
    let mut d = vec![0.0; n * (n - 1) / 2];
    let mut index = 0usize;
    for i in 0..n {
        for j in (i + 1)..n {
            d[index] = distances[[cycle[i + 1] - 1, cycle[j + 1] - 1]];
            index += 1;
        }
    }
    d
}

fn run_active_conjugate(n: usize, d: &[f64], x: &mut [f64]) -> CGSolveStats {
    let collapse_many_negs = true;
    let npairs = d.len();
    let band = BandIndex::new(n);
    let track_kernel_perf = npairs > 4096;
    let mut kernel_perf = KernelPerf::default();
    let started = Instant::now();
    let mut progress = SolveProgress::new(started);
    debug_assert_eq!(
        x.len(),
        npairs,
        "Vectors d,x have different dimensions: d.len()={}, x.len()={}",
        npairs,
        x.len()
    );

    // Unconstrained LS in row-major (uses row-major recurrence)
    run_unconstrained_ls(n, d, x);
    if x.iter().all(|&val| val >= 0.0) {
        return CGSolveStats {
            elapsed_secs: started.elapsed().as_secs_f64(),
            ..Default::default()
        };
    }

    let mut x_band = vec![0.0; npairs];
    row_to_band(x, &mut x_band, &band);

    let mut buf = vec![0.0; npairs];
    let mut row_sums = vec![0.0; n];
    let mut shifted = vec![0.0; n];
    let mut atwd = vec![0.0; npairs];
    {
        let mut y_band = vec![0.0; npairs];
        row_to_band(d, &mut y_band, &band);
        calculate_atx_band(&y_band, &mut atwd, &band, &mut row_sums, &mut shifted);
    }

    // CG scratch vectors (all band-major)
    let mut r = vec![0.0; npairs];
    let mut wtmp = vec![0.0; npairs];
    let mut p = vec![0.0; npairs];
    let mut old_x = vec![1.0; npairs];
    let mut negative_values = Vec::new();
    let mut selected_negative_indices = Vec::new();

    let mut active = vec![false; npairs];
    let fixed_active = vec![false; npairs];
    let mut free_indices: Vec<usize> = (0..npairs).collect();

    let mut first_pass = true;
    loop {
        progress.outer_iter += 1;
        let tol_factor = 1e-8;

        loop {
            if !first_pass {
                free_indices = (0..npairs).filter(|&i| !active[i]).collect();
                circular_conjugate_grads_band_direct(
                    npairs,
                    &mut r,
                    &mut wtmp,
                    &mut p,
                    &atwd,
                    &active,
                    &mut x_band,
                    &band,
                    &mut buf,
                    &mut row_sums,
                    &mut shifted,
                    &mut progress,
                    &mut kernel_perf,
                    track_kernel_perf,
                    &free_indices,
                    tol_factor,
                );
            }
            first_pass = false;

            if collapse_many_negs
                && worst_indices_reuse(
                    &x_band,
                    0.6,
                    &mut negative_values,
                    &mut selected_negative_indices,
                )
            {
                progress.collapse_events += 1;
                progress.collapsed_indices_total += selected_negative_indices.len();
                for &idx in &selected_negative_indices {
                    x_band[idx] = 0.0;
                    active[idx] = true;
                }
                free_indices = (0..npairs).filter(|&i| !active[i]).collect();
                circular_conjugate_grads_band_direct(
                    npairs,
                    &mut r,
                    &mut wtmp,
                    &mut p,
                    &atwd,
                    &active,
                    &mut x_band,
                    &band,
                    &mut buf,
                    &mut row_sums,
                    &mut shifted,
                    &mut progress,
                    &mut kernel_perf,
                    track_kernel_perf,
                    &free_indices,
                    tol_factor,
                );
            }

            let mut min_i: Option<usize> = None;
            let mut min_xi = -1.0_f64;
            for &i in &free_indices {
                if x_band[i] < 0.0 {
                    let xi = old_x[i] / (old_x[i] - x_band[i]);
                    if min_i.is_none() || xi < min_xi {
                        min_i = Some(i);
                        min_xi = xi;
                    }
                }
            }

            match min_i {
                None => break,
                Some(min_i) => {
                    for &i in &free_indices {
                        old_x[i] += min_xi * (x_band[i] - old_x[i]);
                    }
                    active[min_i] = true;
                    x_band[min_i] = 0.0;
                    progress.inner_loop_activations += 1;
                }
            }
        }

        // Gradient check: r = A^T W A x (direct band-major)
        if track_kernel_perf {
            let t0 = Instant::now();
            calculate_ab_band(&x_band, &mut buf, &band, &mut row_sums, &mut shifted);
            kernel_perf.calc_ab_calls += 1;
            kernel_perf.calc_ab_time += t0.elapsed();
        } else {
            calculate_ab_band(&x_band, &mut buf, &band, &mut row_sums, &mut shifted);
        }
        if track_kernel_perf {
            let t0 = Instant::now();
            calculate_atx_band(&buf, &mut r, &band, &mut row_sums, &mut shifted);
            kernel_perf.calc_atx_calls += 1;
            kernel_perf.calc_atx_time += t0.elapsed();
        } else {
            calculate_atx_band(&buf, &mut r, &band, &mut row_sums, &mut shifted);
        }

        if track_kernel_perf && Instant::now() >= progress.next_log {
            let active_count = active.iter().filter(|&&f| f).count();
            info!(
                "CG inference progress: outer_iter={} elapsed={:.1}s cg_calls={} cg_iters_total={} max_cg_single={} active={} inner_activations={} stagnation_exits={} collapse_events={} collapsed_total={} ab_calls={} atx_calls={} ab_avg_us={:.1} atx_avg_us={:.1}",
                progress.outer_iter,
                progress.started.elapsed().as_secs_f64(),
                progress.cg_calls,
                progress.cg_iters_total,
                progress.max_cg_iters_single_call,
                active_count,
                progress.inner_loop_activations,
                progress.stagnation_exits,
                progress.collapse_events,
                progress.collapsed_indices_total,
                kernel_perf.calc_ab_calls,
                kernel_perf.calc_atx_calls,
                avg_time_us(kernel_perf.calc_ab_time, kernel_perf.calc_ab_calls),
                avg_time_us(kernel_perf.calc_atx_time, kernel_perf.calc_atx_calls),
            );
            progress.next_log = Instant::now() + Duration::from_secs(5);
        }

        let mut min_i: Option<usize> = None;
        let mut min_grad = 1.0_f64;
        for i in 0..npairs {
            r[i] -= atwd[i];
            if active[i] && !fixed_active[i] {
                let grad_ij = r[i];
                if min_i.is_none() || grad_ij < min_grad {
                    min_i = Some(i);
                    min_grad = grad_ij;
                }
            }
        }

        let release = if min_grad > -0.0001 { None } else { min_i };
        if release.is_none() {
            if track_kernel_perf {
                info!(
                    "CG inference complete: outer_iters={} elapsed={:.1}s cg_calls={} cg_iters_total={} max_cg_single={} inner_activations={} stagnation_exits={} collapse_events={} collapsed_total={}",
                    progress.outer_iter,
                    progress.started.elapsed().as_secs_f64(),
                    progress.cg_calls,
                    progress.cg_iters_total,
                    progress.max_cg_iters_single_call,
                    progress.inner_loop_activations,
                    progress.stagnation_exits,
                    progress.collapse_events,
                    progress.collapsed_indices_total,
                );
            }
            // Convert x_band back to row-major
            band_to_row(&x_band, x, &band);
            return CGSolveStats {
                outer_iterations: progress.outer_iter,
                cg_calls: progress.cg_calls,
                cg_iters_total: progress.cg_iters_total,
                max_cg_iters_single_call: progress.max_cg_iters_single_call,
                stagnation_exits: progress.stagnation_exits,
                inner_loop_activations: progress.inner_loop_activations,
                collapse_events: progress.collapse_events,
                collapsed_indices_total: progress.collapsed_indices_total,
                kernel_ab_calls: kernel_perf.calc_ab_calls,
                kernel_atx_calls: kernel_perf.calc_atx_calls,
                elapsed_secs: progress.started.elapsed().as_secs_f64(),
            };
        } else if let Some(idx) = release {
            active[idx] = false;
        }
    }
}

fn worst_indices_reuse(
    x: &[f64],
    prop_kept: f64,
    negative_values: &mut Vec<f64>,
    result: &mut Vec<usize>,
) -> bool {
    if prop_kept == 0.0 {
        return false;
    }

    negative_values.clear();
    for &val in x {
        if val < 0.0 {
            negative_values.push(val);
        }
    }
    let num_neg = negative_values.len();
    if num_neg == 0 {
        result.clear();
        return false;
    }

    let nkept = ((prop_kept * num_neg as f64).ceil() as usize).max(1);
    let cutoff = {
        let kth = nkept - 1;
        let (_, pivot, _) = negative_values.select_nth_unstable_by(kth, |a, b| a.total_cmp(b));
        *pivot
    };

    result.clear();
    result.resize(nkept, 0usize);
    let mut front = 0usize;
    let mut back = nkept.saturating_sub(1);
    for (i, &val) in x.iter().enumerate() {
        if val < cutoff {
            result[front] = i;
            front += 1;
        } else if val == cutoff && back >= front {
            result[back] = i;
            if back == 0 {
                break;
            }
            back -= 1;
        }
    }
    true
}

fn run_unconstrained_ls(n: usize, d: &[f64], x: &mut [f64]) {
    let mut index = 0usize;
    for i in 0..=n - 3 {
        x[index] = (d[index] + d[index + (n - i - 2) + 1] - d[index + 1]) / 2.0;
        index += 1;
        for _j in (i + 2)..=n - 2 {
            x[index] =
                (d[index] + d[index + (n - i - 2) + 1] - d[index + 1] - d[index + (n - i - 2)])
                    / 2.0;
            index += 1;
        }
        if i == 0 {
            x[index] = (d[0] + d[n - 2] - d[2 * n - 4]) / 2.0;
        } else {
            x[index] = (d[index] + d[i] - d[i - 1] - d[index + (n - i - 2)]) / 2.0;
        }
        index += 1;
    }
    x[index] = (d[index] + d[n - 2] - d[n - 3]) / 2.0;
}

fn avg_time_us(total: Duration, calls: usize) -> f64 {
    avg_band_time_us(total, calls)
}

/// CG solver operating entirely in band-major layout; all vectors and
/// free_indices use band-major indexing.
#[allow(clippy::too_many_arguments)] // threads shared solver state through a hot path
fn circular_conjugate_grads_band_direct(
    npairs: usize,
    r: &mut [f64],
    w: &mut [f64],
    p: &mut [f64],
    b: &[f64],
    active: &[bool],
    x: &mut [f64],
    band: &BandIndex,
    buf: &mut [f64],
    row_sums: &mut [f64],
    shifted: &mut [f64],
    progress: &mut SolveProgress,
    kernel_perf: &mut KernelPerf,
    track_kernel_perf: bool,
    free_indices: &[usize],
    tol_factor: f64,
) {
    let kmax = (2 * free_indices.len()).min(band.npairs);
    progress.cg_calls += 1;
    let cg_call = progress.cg_calls;

    // r = A^T * A * x (direct band-major: AB then ATX)
    if track_kernel_perf {
        let t0 = Instant::now();
        calculate_ab_band(x, buf, band, row_sums, shifted);
        kernel_perf.calc_ab_calls += 1;
        kernel_perf.calc_ab_time += t0.elapsed();
        let t0 = Instant::now();
        calculate_atx_band(buf, r, band, row_sums, shifted);
        kernel_perf.calc_atx_calls += 1;
        kernel_perf.calc_atx_time += t0.elapsed();
    } else {
        calculate_ab_band(x, buf, band, row_sums, shifted);
        calculate_atx_band(buf, r, band, row_sums, shifted);
    }

    for k in 0..npairs {
        if !active[k] {
            r[k] = b[k] - r[k];
        } else {
            r[k] = 0.0;
        }
    }

    let mut rho: f64 = r.iter().map(|v| v * v).sum();
    let mut r_norm_sq: f64 = rho;
    let mut rho_old = 0.0;

    let b_norm_sq: f64 = b.iter().map(|v| v * v).sum();
    let e0_sq = tol_factor * b_norm_sq;
    let mut k = 0usize;
    let mut checkpoint_norm = r_norm_sq;

    while r_norm_sq > e0_sq && k < kmax {
        k += 1;
        if k == 1 {
            p.copy_from_slice(r);
        } else {
            let beta = rho / rho_old;
            for &i in free_indices {
                p[i] = r[i] + beta * p[i];
            }
        }

        // w = A^T * A * p (direct band-major: AB then ATX)
        if track_kernel_perf {
            let t0 = Instant::now();
            calculate_ab_band(p, buf, band, row_sums, shifted);
            kernel_perf.calc_ab_calls += 1;
            kernel_perf.calc_ab_time += t0.elapsed();
            let t0 = Instant::now();
            calculate_atx_band(buf, w, band, row_sums, shifted);
            kernel_perf.calc_atx_calls += 1;
            kernel_perf.calc_atx_time += t0.elapsed();
        } else {
            calculate_ab_band(p, buf, band, row_sums, shifted);
            calculate_atx_band(buf, w, band, row_sums, shifted);
        }

        let mut alpha_den = 0.0;
        for &i in free_indices {
            alpha_den += p[i] * w[i];
        }
        if alpha_den <= 0.0 || !alpha_den.is_finite() {
            break;
        }
        let alpha = rho / alpha_den;

        let mut rho_new = 0.0;
        for &i in free_indices {
            x[i] += alpha * p[i];
            r[i] -= alpha * w[i];
            rho_new += r[i] * r[i];
        }
        rho_old = rho;
        rho = rho_new;
        r_norm_sq = rho_new;

        // Stagnation detection
        if k > 0 && k.is_multiple_of(200) {
            if r_norm_sq > 0.5 * checkpoint_norm {
                if track_kernel_perf {
                    progress.stagnation_exits += 1;
                }
                break;
            }
            checkpoint_norm = r_norm_sq;
        }

        if track_kernel_perf && (k & 255) == 0 {
            let now = Instant::now();
            if now >= progress.next_inner_log {
                info!(
                    "CG solver progress: outer_iter={} cg_call={} local_iter={}/{} global_cg_iters~{} elapsed={:.1}s r_norm_sq={:.6e} threshold={:.6e}",
                    progress.outer_iter,
                    cg_call,
                    k,
                    kmax,
                    progress.cg_iters_total + k,
                    progress.started.elapsed().as_secs_f64(),
                    r_norm_sq,
                    e0_sq
                );
                progress.next_inner_log = now + Duration::from_secs(10);
            }
        }
    }
    progress.cg_iters_total += k;
    if k > progress.max_cg_iters_single_call {
        progress.max_cg_iters_single_call = k;
    }
}