oxide-conservation
Conservation law verification for GPU computations.
A Rust library that checks whether energy, mass, and information quantities are conserved across GPU kernel boundaries — catching numerical drift, rounding errors, and algorithmic bugs before they cascade through your compute pipeline.
Why Conservation Verification?
GPU kernels manipulate massive arrays of floating-point values in parallel. Between kernel launches, data flows through global memory, shared memory, registers, and back again. At every boundary, subtle bugs can introduce drift:
- Floating-point rounding in reductions, scans, and matrix operations
- Race conditions in atomic operations that lose updates
- Algorithmic errors where a kernel inadvertently drops or duplicates data
- Numerical instability where error compounds across thousands of iterations
In scientific computing, physics simulation, and financial modeling, these bugs are catastrophic. A mass conservation violation of 0.01% in a fluid dynamics simulation might be invisible in a single frame but render a 10,000-step integration completely wrong.
Conservation verification is the assertion layer for numerical correctness. Just as you assert invariants in software, you should assert conservation invariants in compute pipelines.
The Math
Ternary Verification
For any conserved quantity Q measured before and after a kernel execution:
Δ = |Q_after - Q_before|
The ternary verdict is:
| Condition | Verdict | Value |
|---|---|---|
| Δ = 0 | Conserved | +1 |
| 0 < Δ ≤ ε | Approximate | 0 |
| Δ > ε | Violated | −1 |
Where ε (epsilon) is a configurable tolerance that reflects the expected numerical precision of your computation.
Cumulative Drift
Across N kernel executions, cumulative drift is:
D_total = Σᵢ |Δᵢ|
When D_total > threshold, an alert is raised. This catches slow leaks that would pass any single-kernel check but accumulate over a full simulation run.
Conservation Laws
Three built-in laws are provided:
- Energy — Total energy before = total energy after. Critical for physics simulations, Hamiltonian systems, and N-body integrators.
- Mass — Total mass/particle count is preserved. Essential for fluid dynamics, particle transport, and chemical reaction modeling.
- Information — Entropy bounds, data integrity, bit-level preservation. Relevant for compression, encryption, and lossless transform pipelines.
Custom laws can be defined for domain-specific invariants (angular momentum, charge, baryon number, etc.).
Key Types
ConservationLaw
The law being verified. Custom variants let you define domain-specific invariants like "angular_momentum" or "electric_charge".
Ternary
Three-valued logic that distinguishes "perfectly conserved" from "close enough" from "broken." This is more informative than a simple boolean pass/fail.
ConservationMonitor
Takes snapshots of quantities before kernel execution and verifies them after:
let mut monitor = new; // epsilon
monitor.snapshot;
// ... launch GPU kernel ...
let result = monitor.verify;
assert!;
VerificationResult
Holds the full picture: law, before/after values, delta, epsilon, and the ternary verdict.
ConservationBudget
Tracks cumulative drift across an entire compute pipeline:
let mut budget = new; // alert threshold
for step in 0..10000
println!;
println!;
Alert
Raised when cumulative drift exceeds the budget threshold. Includes the total drift, the threshold, and the worst-offending conservation law.
Usage
Add to Cargo.toml:
[]
= "0.1"
Basic Verification
use ;
let result = check;
assert_eq!;
println!; // [ 0 (approximate)] 1000 → 999.9998 (Δ=0.0002, ε=0.001)
Multi-Kernel Pipeline
use ;
let mut monitor = new;
let mut budget = new;
// Snapshot before kernel chain
monitor.snapshot;
monitor.snapshot;
// ... run FFT kernel on GPU ...
// Verify after
let energy_result = monitor.verify;
let info_result = monitor.verify;
budget.record;
budget.record;
if let Some = budget.check_alert
Batch Verification
monitor.snapshot;
monitor.snapshot;
monitor.snapshot;
// Provide after-values for all pending snapshots
let results = monitor.verify_all;
for r in &results
Statistics
ConservationBudget provides aggregate statistics across your entire workload:
| Method | Description |
|---|---|
verification_count() |
Total number of verifications recorded |
violation_count() |
Number of verifications that returned Violated |
violation_rate() |
Fraction of verifications that were violations |
average_drift() |
Mean absolute drift across all verifications |
drift_for(law) |
Cumulative drift for a specific conservation law |
total_drift() |
Sum of drift across all laws |
Connection to the Oxide Stack
oxide-conservation is part of the Oxide family of crates for robust GPU compute in Rust:
- oxide-conservation — Conservation law verification (this crate)
- Works alongside any GPU compute framework (wgpu, CUDA, OpenCL)
- Designed to integrate into CI pipelines for numerical regression testing
- Zero dependencies — add verification without bloating your build
The Oxide philosophy: numerical correctness is not optional. Every GPU compute pipeline should verify its invariants. This crate makes it easy.
License
MIT