|
| 1 | +use clap::Parser; |
| 2 | +use sketch_core::config::{self, ImplMode}; |
| 3 | + |
| 4 | +#[derive(Clone)] |
| 5 | +struct Lcg64 { |
| 6 | + state: u64, |
| 7 | +} |
| 8 | + |
| 9 | +impl Lcg64 { |
| 10 | + fn new(seed: u64) -> Self { |
| 11 | + Self { state: seed } |
| 12 | + } |
| 13 | + |
| 14 | + fn next_u64(&mut self) -> u64 { |
| 15 | + self.state = self |
| 16 | + .state |
| 17 | + .wrapping_mul(6364136223846793005) |
| 18 | + .wrapping_add(1442695040888963407); |
| 19 | + self.state |
| 20 | + } |
| 21 | + |
| 22 | + fn next_f64_0_1(&mut self) -> f64 { |
| 23 | + let x = self.next_u64() >> 11; |
| 24 | + (x as f64) / ((1u64 << 53) as f64) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +fn pearson_corr(exact: &[f64], est: &[f64]) -> f64 { |
| 29 | + let n = exact.len().min(est.len()); |
| 30 | + if n == 0 { |
| 31 | + return f64::NAN; |
| 32 | + } |
| 33 | + let (mut sum_x, mut sum_y) = (0.0, 0.0); |
| 34 | + for i in 0..n { |
| 35 | + sum_x += exact[i]; |
| 36 | + sum_y += est[i]; |
| 37 | + } |
| 38 | + let mean_x = sum_x / (n as f64); |
| 39 | + let mean_y = sum_y / (n as f64); |
| 40 | + let (mut num, mut den_x, mut den_y) = (0.0, 0.0, 0.0); |
| 41 | + for i in 0..n { |
| 42 | + let dx = exact[i] - mean_x; |
| 43 | + let dy = est[i] - mean_y; |
| 44 | + num += dx * dy; |
| 45 | + den_x += dx * dx; |
| 46 | + den_y += dy * dy; |
| 47 | + } |
| 48 | + if den_x == 0.0 || den_y == 0.0 { |
| 49 | + return f64::NAN; |
| 50 | + } |
| 51 | + num / (den_x.sqrt() * den_y.sqrt()) |
| 52 | +} |
| 53 | + |
| 54 | +fn mape(exact: &[f64], est: &[f64]) -> f64 { |
| 55 | + let n = exact.len().min(est.len()); |
| 56 | + let mut num = 0.0; |
| 57 | + let mut denom = 0.0; |
| 58 | + for i in 0..n { |
| 59 | + if exact[i] == 0.0 { |
| 60 | + continue; |
| 61 | + } |
| 62 | + num += ((exact[i] - est[i]) / exact[i]).abs(); |
| 63 | + denom += 1.0; |
| 64 | + } |
| 65 | + if denom == 0.0 { |
| 66 | + return if exact == est { 0.0 } else { f64::INFINITY }; |
| 67 | + } |
| 68 | + (num / denom) * 100.0 |
| 69 | +} |
| 70 | + |
| 71 | +fn rmse_percentage(exact: &[f64], est: &[f64]) -> f64 { |
| 72 | + let n = exact.len().min(est.len()); |
| 73 | + let mut sum_sq = 0.0; |
| 74 | + let mut denom = 0.0; |
| 75 | + for i in 0..n { |
| 76 | + if exact[i] == 0.0 { |
| 77 | + continue; |
| 78 | + } |
| 79 | + let rel = (exact[i] - est[i]) / exact[i]; |
| 80 | + sum_sq += rel * rel; |
| 81 | + denom += 1.0; |
| 82 | + } |
| 83 | + if denom == 0.0 { |
| 84 | + return if exact == est { 0.0 } else { f64::INFINITY }; |
| 85 | + } |
| 86 | + (sum_sq / denom).sqrt() * 100.0 |
| 87 | +} |
| 88 | + |
| 89 | +fn rank_fraction(sorted: &[f64], x: f64) -> f64 { |
| 90 | + if sorted.is_empty() { |
| 91 | + return 0.0; |
| 92 | + } |
| 93 | + let idx = sorted.partition_point(|v| *v <= x); |
| 94 | + (idx as f64) / (sorted.len() as f64) |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Parser)] |
| 98 | +struct Args { |
| 99 | + #[arg(long, value_enum, default_value = "sketchlib")] |
| 100 | + cms_impl: ImplMode, |
| 101 | + #[arg(long, value_enum, default_value = "sketchlib")] |
| 102 | + kll_impl: ImplMode, |
| 103 | + #[arg(long, value_enum, default_value = "sketchlib")] |
| 104 | + cmwh_impl: ImplMode, |
| 105 | +} |
| 106 | + |
| 107 | +fn main() { |
| 108 | + let args = Args::parse(); |
| 109 | + config::configure(args.cms_impl, args.kll_impl, args.cmwh_impl) |
| 110 | + .expect("sketch backend already initialised"); |
| 111 | + |
| 112 | + let mode = if matches!(args.cms_impl, ImplMode::Legacy) |
| 113 | + || matches!(args.kll_impl, ImplMode::Legacy) |
| 114 | + || matches!(args.cmwh_impl, ImplMode::Legacy) |
| 115 | + { |
| 116 | + "Legacy" |
| 117 | + } else { |
| 118 | + "sketchlib-rust" |
| 119 | + }; |
| 120 | + |
| 121 | + println!("# Sketchlib Fidelity Report ({})", mode); |
| 122 | + println!(); |
| 123 | + println!("Fidelity tests will be added as sketch implementations are integrated."); |
| 124 | +} |
0 commit comments