oarust 0.19.0

GPU-first semantic computing runtime written in Rust
Documentation
//! Mergeable classification counters with deferred device resolution.

use crate::{Matrix, Result};

/// Additive correct/sample counters for categorical accuracy.
///
/// Batch counts recorded through [`AccuracyCount::record`] stay device-resident
/// in bounded groups until [`AccuracyCount::resolve`] or [`AccuracyCount::result`]
/// observes them — the same deferred grouped readback the managed evaluation
/// plane uses, so per-batch host synchronization never happens inside a hot
/// loop. The result is NaN when no samples have been observed.
#[derive(Default)]
pub struct AccuracyCount {
	correct: u64,
	samples: u64,
	pending: Vec<Matrix>,
}

impl AccuracyCount {
	/// Construct empty state.
	#[inline]
	#[must_use]
	pub fn new() -> Self {
		Self::default()
	}

	/// Record one batch's device-resident correct count and sample count.
	///
	/// The count matrix is retained, not read; observation happens in bounded
	/// concatenated groups at resolution. The producer selects the counting
	/// operation — plain or masked — and its geometry contract.
	pub fn record(&mut self, count: Matrix, samples: u64) {
		self.pending.push(count);
		self.samples = self.samples.saturating_add(samples);
	}

	/// Add host-observed counts directly.
	pub fn update(&mut self, correct: u64, samples: u64) {
		self.correct = self.correct.saturating_add(correct);
		self.samples = self.samples.saturating_add(samples);
	}

	/// Observe every pending device count in bounded concatenated groups.
	///
	/// This is the explicit host-observation boundary: concatenation and one
	/// readback per 128 pending counts.
	///
	/// # Errors
	///
	/// Returns concatenation, completion, or readback errors.
	pub fn resolve(&mut self) -> Result<()> {
		if self.pending.is_empty() {
			return Ok(());
		}
		let pending = std::mem::take(&mut self.pending);
		for group in pending.chunks(128) {
			let values = crate::matrix::concat(group, 0)?.read::<u32>()?;
			for value in values {
				self.correct = self.correct.saturating_add(u64::from(value));
			}
		}
		Ok(())
	}

	/// Merge another instance's state, absorbing its unresolved device counts.
	pub fn merge(&mut self, other: &mut Self) {
		self.correct = self.correct.saturating_add(other.correct);
		self.samples = self.samples.saturating_add(other.samples);
		self.pending.append(&mut other.pending);
	}

	/// Resolve pending counts and return correct / samples, or NaN when empty.
	///
	/// # Errors
	///
	/// Returns the resolution error when pending device counts cannot be observed.
	pub fn result(&mut self) -> Result<f64> {
		self.resolve()?;
		Ok(if self.samples == 0 {
			f64::NAN
		} else {
			self.correct as f64 / self.samples as f64
		})
	}

	/// Return resolved correct and sample counts without observing pending state.
	#[must_use]
	pub const fn counts(&self) -> (u64, u64) {
		(self.correct, self.samples)
	}

	/// Return whether unresolved device counts remain.
	#[must_use]
	pub fn has_pending(&self) -> bool {
		!self.pending.is_empty()
	}

	/// Drop all accumulated state, including unresolved device counts.
	pub fn reset(&mut self) {
		self.correct = 0;
		self.samples = 0;
		self.pending.clear();
	}
}