331 lines
11 KiB
Rust
331 lines
11 KiB
Rust
//! Basic functionality tests for edge-aware training system
|
|
//!
|
|
//! Tests core functionality and compilation without complex integration
|
|
|
|
use rustytorch::revolutionary::*;
|
|
use std::collections::HashMap;
|
|
|
|
/// Test basic edge target optimizer creation
|
|
#[test]
|
|
fn test_edge_target_optimizer_basic() {
|
|
let optimizer = EdgeTargetOptimizer::new();
|
|
let all_metrics = optimizer.get_all_metrics();
|
|
assert_eq!(all_metrics.len(), 0); // No optimizations run yet
|
|
}
|
|
|
|
/// Test ARM optimization configuration
|
|
#[test]
|
|
fn test_arm_optimization_basic() {
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
let arm_opts = ArmOptimizations::default();
|
|
|
|
optimizer.configure_arm(arm_opts);
|
|
let metrics = optimizer.optimize_for_target(EdgeTarget::ARM).unwrap();
|
|
|
|
// Verify basic metrics
|
|
assert!(metrics.performance_improvement > 1.0);
|
|
assert!(!metrics.target_specific.is_empty());
|
|
}
|
|
|
|
/// Test RISC-V optimization with vector extensions
|
|
#[test]
|
|
fn test_riscv_optimization_basic() {
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
let riscv_opts = RiscVOptimizations::default();
|
|
|
|
optimizer.configure_riscv(riscv_opts);
|
|
let metrics = optimizer.optimize_for_target(EdgeTarget::RISCV).unwrap();
|
|
|
|
// RISC-V should provide good performance improvement
|
|
assert!(metrics.performance_improvement > 2.0);
|
|
}
|
|
|
|
/// Test WASM optimization
|
|
#[test]
|
|
fn test_wasm_optimization_basic() {
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
let wasm_opts = WasmOptimizations::default();
|
|
|
|
optimizer.configure_wasm(wasm_opts);
|
|
let metrics = optimizer.optimize_for_target(EdgeTarget::WASM).unwrap();
|
|
|
|
// WASM should show improvement
|
|
assert!(metrics.performance_improvement > 1.2);
|
|
}
|
|
|
|
/// Test Mobile GPU optimization
|
|
#[test]
|
|
fn test_mobile_gpu_optimization_basic() {
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
let mobile_gpu_opts = MobileGpuOptimizations::default();
|
|
|
|
optimizer.configure_mobile_gpu(mobile_gpu_opts);
|
|
let metrics = optimizer.optimize_for_target(EdgeTarget::MobileGPU).unwrap();
|
|
|
|
// Mobile GPU should provide significant acceleration
|
|
assert!(metrics.performance_improvement > 3.0);
|
|
assert!(metrics.memory_reduction_ratio > 0.1);
|
|
}
|
|
|
|
/// Test IoT optimization for ultra-low power
|
|
#[test]
|
|
fn test_iot_optimization_basic() {
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
let iot_opts = IoTOptimizations::default();
|
|
|
|
optimizer.configure_iot(iot_opts);
|
|
let metrics = optimizer.optimize_for_target(EdgeTarget::IoT).unwrap();
|
|
|
|
// IoT should prioritize power efficiency
|
|
assert!(metrics.power_efficiency_gain > 5.0);
|
|
assert!(metrics.memory_reduction_ratio > 0.5);
|
|
}
|
|
|
|
/// Test federated coordinator creation
|
|
#[tokio::test]
|
|
async fn test_federated_coordinator_basic() {
|
|
let (coordinator, _sender) = FederatedCoordinator::new(
|
|
"test-coordinator".to_string(),
|
|
SelectionStrategy::Random,
|
|
GradientCompression {
|
|
algorithm: CompressionAlgorithm::TopK,
|
|
compression_ratio: 0.1,
|
|
error_correction: true,
|
|
adaptive_compression: true,
|
|
},
|
|
AggregationStrategy {
|
|
algorithm: AggregationAlgorithm::FedAvg,
|
|
weighting: WeightingScheme::SampleBased,
|
|
byzantine_tolerance: ByzantineTolerance {
|
|
enabled: false,
|
|
max_byzantine_fraction: 0.0,
|
|
detection_algorithm: ByzantineDetection::None,
|
|
},
|
|
differential_privacy: None,
|
|
},
|
|
);
|
|
|
|
// Test basic metrics
|
|
let metrics = coordinator.get_metrics();
|
|
assert_eq!(metrics.total_devices, 0);
|
|
assert_eq!(metrics.active_devices, 0);
|
|
}
|
|
|
|
/// Test device registration in federated system
|
|
#[tokio::test]
|
|
async fn test_device_registration_basic() {
|
|
let (coordinator, _sender) = FederatedCoordinator::new(
|
|
"test-coordinator".to_string(),
|
|
SelectionStrategy::Random,
|
|
GradientCompression {
|
|
algorithm: CompressionAlgorithm::None,
|
|
compression_ratio: 1.0,
|
|
error_correction: false,
|
|
adaptive_compression: false,
|
|
},
|
|
AggregationStrategy {
|
|
algorithm: AggregationAlgorithm::FedAvg,
|
|
weighting: WeightingScheme::Equal,
|
|
byzantine_tolerance: ByzantineTolerance {
|
|
enabled: false,
|
|
max_byzantine_fraction: 0.0,
|
|
detection_algorithm: ByzantineDetection::None,
|
|
},
|
|
differential_privacy: None,
|
|
},
|
|
);
|
|
|
|
let device = create_test_device("device-1", EdgeDeviceType::StandardMobile);
|
|
let result = coordinator.register_device(device).await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let metrics = coordinator.get_metrics();
|
|
assert_eq!(metrics.total_devices, 1);
|
|
assert_eq!(metrics.active_devices, 1);
|
|
}
|
|
|
|
/// Test device selection at small scale
|
|
#[tokio::test]
|
|
async fn test_device_selection_basic() {
|
|
let (coordinator, _sender) = FederatedCoordinator::new(
|
|
"test-coordinator".to_string(),
|
|
SelectionStrategy::BatteryAware,
|
|
GradientCompression {
|
|
algorithm: CompressionAlgorithm::TopK,
|
|
compression_ratio: 0.1,
|
|
error_correction: false,
|
|
adaptive_compression: false,
|
|
},
|
|
AggregationStrategy {
|
|
algorithm: AggregationAlgorithm::FedAvg,
|
|
weighting: WeightingScheme::SampleBased,
|
|
byzantine_tolerance: ByzantineTolerance {
|
|
enabled: false,
|
|
max_byzantine_fraction: 0.0,
|
|
detection_algorithm: ByzantineDetection::None,
|
|
},
|
|
differential_privacy: None,
|
|
},
|
|
);
|
|
|
|
// Register 5 test devices
|
|
for i in 0..5 {
|
|
let device = create_test_device(&format!("device-{}", i), EdgeDeviceType::StandardMobile);
|
|
coordinator.register_device(device).await.unwrap();
|
|
}
|
|
|
|
let selection_criteria = SelectionCriteria {
|
|
min_battery_level: 0.1,
|
|
min_bandwidth_mbps: 1.0,
|
|
max_latency_ms: 1000,
|
|
required_availability_minutes: 10,
|
|
min_data_quality: 0.1,
|
|
};
|
|
|
|
let result = coordinator.select_devices(3, selection_criteria).await;
|
|
assert!(result.is_ok());
|
|
|
|
let selection_result = result.unwrap();
|
|
assert_eq!(selection_result.selected_devices.len(), 3);
|
|
assert!(selection_result.total_available >= 3);
|
|
}
|
|
|
|
/// Test multiple target platform optimization
|
|
#[test]
|
|
fn test_multiple_platform_optimization() {
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
|
|
// Configure all target platforms
|
|
optimizer.configure_arm(ArmOptimizations::default());
|
|
optimizer.configure_riscv(RiscVOptimizations::default());
|
|
optimizer.configure_wasm(WasmOptimizations::default());
|
|
optimizer.configure_mobile_gpu(MobileGpuOptimizations::default());
|
|
optimizer.configure_iot(IoTOptimizations::default());
|
|
|
|
let targets = [
|
|
EdgeTarget::ARM,
|
|
EdgeTarget::RISCV,
|
|
EdgeTarget::WASM,
|
|
EdgeTarget::MobileGPU,
|
|
EdgeTarget::IoT,
|
|
];
|
|
|
|
for target in &targets {
|
|
let metrics = optimizer.optimize_for_target(*target).unwrap();
|
|
assert!(metrics.performance_improvement > 1.0);
|
|
assert!(!metrics.target_specific.is_empty());
|
|
}
|
|
|
|
// Verify all metrics are stored
|
|
let all_metrics = optimizer.get_all_metrics();
|
|
assert_eq!(all_metrics.len(), 5);
|
|
}
|
|
|
|
/// Test performance targets validation
|
|
#[test]
|
|
fn test_performance_targets_validation() {
|
|
// Test ARM NEON target (>2.5x speedup)
|
|
{
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
let arm_opts = ArmOptimizations {
|
|
enable_neon: true,
|
|
target_arch: ArmArchitecture::CortexA,
|
|
..Default::default()
|
|
};
|
|
optimizer.configure_arm(arm_opts);
|
|
let metrics = optimizer.optimize_for_target(EdgeTarget::ARM).unwrap();
|
|
assert!(metrics.performance_improvement >= 2.5, "ARM NEON target not met");
|
|
}
|
|
|
|
// Test Mobile GPU target (>4.0x speedup)
|
|
{
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
let mobile_gpu_opts = MobileGpuOptimizations {
|
|
gpu_vendor: MobileGpuVendor::Apple,
|
|
compute_shaders: true,
|
|
tile_based_rendering: true,
|
|
bandwidth_optimization: true,
|
|
power_efficiency: true,
|
|
};
|
|
optimizer.configure_mobile_gpu(mobile_gpu_opts);
|
|
let metrics = optimizer.optimize_for_target(EdgeTarget::MobileGPU).unwrap();
|
|
assert!(metrics.performance_improvement >= 4.0, "Mobile GPU target not met");
|
|
}
|
|
|
|
// Test IoT power efficiency target (>10x)
|
|
{
|
|
let mut optimizer = EdgeTargetOptimizer::new();
|
|
let iot_opts = IoTOptimizations {
|
|
ultra_low_power: true,
|
|
minimal_memory: true,
|
|
wake_on_inference: true,
|
|
target_platform: IoTPlatform::ESP32,
|
|
..Default::default()
|
|
};
|
|
optimizer.configure_iot(iot_opts);
|
|
let metrics = optimizer.optimize_for_target(EdgeTarget::IoT).unwrap();
|
|
assert!(metrics.power_efficiency_gain >= 10.0, "IoT power efficiency target not met");
|
|
}
|
|
}
|
|
|
|
// Helper function to create test devices
|
|
fn create_test_device(device_id: &str, device_type: EdgeDeviceType) -> FederatedDevice {
|
|
use std::collections::HashSet;
|
|
|
|
FederatedDevice {
|
|
device_id: device_id.to_string(),
|
|
device_type,
|
|
status: DeviceStatus::Available,
|
|
network_info: NetworkInfo {
|
|
connection_type: ConnectionType::WiFi,
|
|
bandwidth_mbps: 50.0,
|
|
latency_ms: 20,
|
|
reliability: 0.95,
|
|
data_plan: DataPlan {
|
|
unlimited: true,
|
|
monthly_allowance_gb: None,
|
|
current_usage_gb: 0.0,
|
|
cost_per_gb: None,
|
|
},
|
|
},
|
|
power_status: PowerStatus {
|
|
battery_level: 0.8,
|
|
is_charging: false,
|
|
power_source: PowerSource::Battery,
|
|
estimated_battery_life_minutes: Some(240),
|
|
},
|
|
compute_capabilities: ComputeCapabilities {
|
|
cpu_cores: 4,
|
|
ram_mb: 4096,
|
|
has_gpu: false,
|
|
simd_support: true,
|
|
estimated_flops: 1e9,
|
|
memory_bandwidth_gbps: 10.0,
|
|
},
|
|
data_info: DataInfo {
|
|
sample_count: 1000,
|
|
quality_score: 0.9,
|
|
privacy_level: PrivacyLevel::Personal,
|
|
distribution: DataDistribution {
|
|
distribution_type: "normal".to_string(),
|
|
parameters: HashMap::new(),
|
|
},
|
|
},
|
|
availability: AvailabilitySchedule {
|
|
timezone_offset_hours: 0,
|
|
available_hours: (0..24).collect(),
|
|
preferred_duration_minutes: 30,
|
|
blackout_periods: vec![],
|
|
},
|
|
performance_metrics: PerformanceMetrics {
|
|
avg_training_time_seconds: 300.0,
|
|
avg_upload_time_seconds: 10.0,
|
|
accuracy_contribution: 0.85,
|
|
reliability_score: 0.9,
|
|
communication_efficiency: 0.8,
|
|
},
|
|
last_seen: std::time::SystemTime::now(),
|
|
}
|
|
}
|