Files
foxhunt/crates/ml-core/src/error.rs
jgrusewski 22004a7368 refactor(cuda): eliminate candle from ml-core, ml-ppo, and 4 thin crates
Hard refactor — no shims, no compat layers. Candle removed from Cargo.toml
and all source files in 6 crates:

- ml-core: MlDevice enum, checkpoint.rs (safetensors direct), cudarc imports
  fixed from candle re-export to direct, AdamWConfig lr_decay, cuda_compat
  gutted. Net -7,341 lines.
- ml-ppo: All 16 files rewritten. LSTM→CudaLSTM, VarMap→GpuVarStore,
  PPOAgent 2306→700 lines, checkpoint→binary format.
- ml-ensemble: GPU-resident sigmoid via custom CUDA kernel.
- ml-explainability: Integrated gradients via GPU finite-difference kernels.
- ml-labeling: Device→MlDevice.
- ml-hyperopt: Cargo.toml only.

Remaining: ml-dqn (24 files), ml-supervised (4 files), ml crate (104 files).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 22:27:56 +01:00

68 lines
2.1 KiB
Rust

//! Error types for ML models crate - unified with CommonError
use common::error::CommonError;
/// `Result` type alias for ML models operations - updated to use CommonError
pub type MLResult<T> = Result<T, CommonError>;
/// `Result` type alias for model operations
pub type ModelResult<T> = Result<T, CommonError>;
/// Convert a framework computation error to CommonError
///
/// Accepts any `Display` error type (cudarc, etc.) and wraps it
/// as an ML computation error.
pub fn computation_error_to_common_error<E: std::fmt::Display>(err: E) -> CommonError {
CommonError::ml(
"computation",
format!("ML computation error: {}", err),
)
}
/// Create an ML training error
pub fn ml_training_error(message: &str, model: Option<String>) -> CommonError {
CommonError::ml(
"training",
format!("ML training error: {} (model: {:?})", message, model),
)
}
/// Create an ML inference error
pub fn ml_inference_error(message: &str, model: Option<String>) -> CommonError {
CommonError::ml(
"inference",
format!("ML inference error: {} (model: {:?})", message, model),
)
}
/// Create an ML validation error
pub fn ml_validation_error(field: &str, message: &str) -> CommonError {
CommonError::validation(format!(
"ML validation error in field '{}': {}",
field, message
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ml_error_creation() {
let inference_error =
ml_inference_error("Test inference error", Some("test_model".to_owned()));
let error_str = inference_error.to_string();
assert!(error_str.contains("Test inference error"));
assert!(error_str.contains("test_model"));
let training_error = ml_training_error("Test training error", None);
let error_str = training_error.to_string();
assert!(error_str.contains("Test training error"));
let validation_error = ml_validation_error("input", "Invalid input shape");
let error_str = validation_error.to_string();
assert!(error_str.contains("input"));
assert!(error_str.contains("Invalid input shape"));
}
}