zc2 0.0.53

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! metrics → normalised units. Mirrored by the hub's services/node_scoring.py;
//! changing a constant here means bumping SUITE and the hub's copy.
use super::report::{CpuMetrics, GpuMetrics};

pub const GFLOPS_PER_CPU_UNIT: f64 = 100.0;
pub const GBPS_PER_CPU_UNIT: f64 = 4.0;
pub const TFLOPS_F16_PER_GPU_UNIT: f64 = 10.0;
pub const GBPS_PER_GPU_UNIT: f64 = 100.0;

fn finite(x: f64) -> f64 {
    if x.is_finite() && x > 0.0 {
        x
    } else {
        0.0
    }
}

fn fed(raw: f64, bw: f64, per_unit: f64) -> f64 {
    if raw == 0.0 {
        return 0.0;
    }
    raw * (finite(bw) / (raw * per_unit)).min(1.0)
}

/// A 0 (or non-finite) memory bandwidth means "not measured" (the probe could
/// not run its triad; the report is partial), so no bandwidth factor applies.
/// The hub's copy of the scoring must treat it the same way.
pub fn cpu_units(c: &CpuMetrics) -> f64 {
    if finite(c.mem_bw_gbps) == 0.0 {
        return finite(c.gflops_f32) / GFLOPS_PER_CPU_UNIT;
    }
    fed(
        finite(c.gflops_f32) / GFLOPS_PER_CPU_UNIT,
        c.mem_bw_gbps,
        GBPS_PER_CPU_UNIT,
    )
}

pub fn gpu_units(gpus: &[GpuMetrics]) -> Option<f64> {
    if gpus.is_empty() {
        return None;
    }
    Some(
        gpus.iter()
            .map(|g| {
                fed(
                    finite(g.tflops_f16) / TFLOPS_F16_PER_GPU_UNIT,
                    g.mem_bw_gbps,
                    GBPS_PER_GPU_UNIT,
                )
            })
            .sum(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bench_node::report::{CpuMetrics, GpuMetrics};

    fn cpu(gflops: f64, bw: f64) -> CpuMetrics {
        CpuMetrics {
            gflops_f32: gflops,
            gflops_f64: gflops / 2.0,
            gflops_f32_1t: 50.0,
            mem_bw_gbps: bw,
        }
    }
    fn gpu(model: &str, f16: f64, bw: f64) -> GpuMetrics {
        GpuMetrics {
            vendor: "nvidia".into(),
            model: model.into(),
            uuid: format!("GPU-{model}"),
            tflops_f32: f16 / 2.0,
            tflops_f16: f16,
            mem_bw_gbps: bw,
            vram_bytes: 1 << 34,
        }
    }

    #[test]
    fn a_well_fed_cpu_scores_its_gflops_over_100() {
        assert!((cpu_units(&cpu(812.4, 40.0)) - 8.124).abs() < 1e-9);
    }
    #[test]
    fn a_bandwidth_starved_cpu_is_scored_down() {
        // 8 raw units need 32 GB/s; 16 GB/s halves the score.
        assert!((cpu_units(&cpu(800.0, 16.0)) - 4.0).abs() < 1e-9);
    }
    #[test]
    fn an_unmeasured_bandwidth_is_not_scored_as_zero_bandwidth() {
        assert!((cpu_units(&cpu(800.0, 0.0)) - 8.0).abs() < 1e-9);
        assert!((cpu_units(&cpu(800.0, f64::NAN)) - 8.0).abs() < 1e-9);
        assert_eq!(cpu_units(&cpu(0.0, 0.0)), 0.0);
    }
    #[test]
    fn no_gpu_is_none_not_zero() {
        assert_eq!(gpu_units(&[]), None);
    }
    #[test]
    fn gpus_are_scored_per_device_and_summed() {
        // 4090: 165 TF16 → 16.5 raw, 1000 GB/s → factor 1000/1650. 2080 Ti: 27 TF16 → 2.7 raw, 616 GB/s → factor 1.
        let u = gpu_units(&[
            gpu("RTX 4090", 165.0, 1000.0),
            gpu("RTX 2080 Ti", 27.0, 616.0),
        ])
        .unwrap();
        assert!((u - (16.5 * 1000.0 / 1650.0 + 2.7)).abs() < 1e-9);
    }
    #[test]
    fn nonfinite_metrics_score_zero_not_nan() {
        assert_eq!(cpu_units(&cpu(f64::NAN, 10.0)), 0.0);
        assert_eq!(gpu_units(&[gpu("x", f64::INFINITY, 10.0)]), Some(0.0));
    }
}