ronn-providers 0.1.0

Execution provider framework for RONN - hardware abstraction layer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! Example NPU (Neural Processing Unit) provider implementation.
//!
//! This module provides a reference implementation for integrating NPU hardware
//! accelerators. It demonstrates the patterns and interfaces needed to create
//! custom hardware providers for specialized AI processors.

use std::any::Any;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use std::time::Instant;

use anyhow::{Result, anyhow};
use ronn_core::{CompiledKernel, DataType, KernelStats, MemoryUsage, SubGraph, Tensor};

use super::traits::{
    CustomHardwareProvider, CustomKernel, DeviceBuffer, DeviceMemory, DeviceMemoryInfo,
    HardwareCapability, HardwareProfiler, KernelInfo, PowerProfile, ProfilingResults,
    ProfilingSession, ProfilingSummary, ProviderStats,
};

/// Configuration for NPU provider.
#[derive(Debug, Clone)]
pub struct NpuConfig {
    /// NPU device identifier.
    pub device_id: String,
    /// Maximum memory pool size in bytes.
    pub max_memory_pool_bytes: u64,
    /// Enable quantization optimizations.
    pub enable_quantization: bool,
    /// Target precision for operations.
    pub target_precision: String,
    /// Power management mode.
    pub power_mode: NpuPowerMode,
    /// Custom compiler flags.
    pub compiler_flags: Vec<String>,
    /// Profiling configuration.
    pub enable_profiling: bool,
}

impl Default for NpuConfig {
    fn default() -> Self {
        Self {
            device_id: "npu0".to_string(),
            max_memory_pool_bytes: 2 * 1024 * 1024 * 1024, // 2GB
            enable_quantization: true,
            target_precision: "int8".to_string(),
            power_mode: NpuPowerMode::Balanced,
            compiler_flags: vec![
                "-O3".to_string(),
                "-ffast-math".to_string(),
                "-march=native".to_string(),
            ],
            enable_profiling: false,
        }
    }
}

/// NPU power management modes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NpuPowerMode {
    /// Maximum performance, higher power consumption.
    Performance,
    /// Balanced performance and power.
    Balanced,
    /// Power-efficient mode, lower performance.
    PowerSaver,
}

/// Example NPU provider implementation.
#[derive(Debug)]
pub struct NpuProvider {
    config: NpuConfig,
    device_memory: Arc<NpuDeviceMemory>,
    profiler: Arc<Mutex<NpuProfiler>>,
    stats: Arc<RwLock<ProviderStats>>,
    initialized: bool,
    hardware_capability: HardwareCapability,
}

impl NpuProvider {
    /// Create a new NPU provider with the given configuration.
    pub fn new(config: NpuConfig) -> Self {
        let device_memory = Arc::new(NpuDeviceMemory::new(config.max_memory_pool_bytes));
        let profiler = Arc::new(Mutex::new(NpuProfiler::new()));
        let stats = Arc::new(RwLock::new(ProviderStats {
            total_operations: 0,
            average_execution_time_us: 0.0,
            memory_usage_bytes: 0,
            peak_memory_bytes: 0,
            hardware_utilization: 0.0,
            current_power_watts: 15.0,
            total_energy_joules: 0.0,
        }));

        // Simulate NPU hardware capabilities
        let hardware_capability = HardwareCapability {
            vendor: "Example NPU Corp".to_string(),
            model: "NPU-X1000".to_string(),
            architecture_version: "2.1".to_string(),
            supported_data_types: vec![
                DataType::I8,
                DataType::U8,
                DataType::I16,
                DataType::F16,
                DataType::F32,
            ],
            max_memory_bytes: config.max_memory_pool_bytes,
            peak_tops: 50.0, // 50 TOPS
            memory_bandwidth_gbps: 400.0,
            supported_operations: vec![
                "Conv2D".to_string(),
                "DepthwiseConv2D".to_string(),
                "MatMul".to_string(),
                "BatchNorm".to_string(),
                "ReLU".to_string(),
                "MaxPool".to_string(),
                "AvgPool".to_string(),
                "Softmax".to_string(),
                "Add".to_string(),
                "Mul".to_string(),
            ],
            features: {
                let mut features = HashMap::new();
                features.insert("quantization".to_string(), "int8,int4".to_string());
                features.insert(
                    "sparsity".to_string(),
                    "structured,unstructured".to_string(),
                );
                features.insert("batch_processing".to_string(), "1-32".to_string());
                features
            },
            power_profile: PowerProfile {
                idle_power_watts: 5.0,
                peak_power_watts: 25.0,
                tdp_watts: 20.0,
                efficiency_tops_per_watt: 2.5,
            },
        };

        Self {
            config,
            device_memory,
            profiler,
            stats,
            initialized: false,
            hardware_capability,
        }
    }

    /// Set the power management mode.
    pub fn set_power_mode(&mut self, mode: NpuPowerMode) -> Result<()> {
        self.config.power_mode = mode;
        // In a real implementation, this would configure the hardware
        tracing::info!("NPU power mode set to {:?}", mode);
        Ok(())
    }

    /// Get the current thermal state of the NPU.
    pub fn get_thermal_state(&self) -> NpuThermalState {
        // Simulate thermal monitoring
        NpuThermalState {
            temperature_celsius: 45.0,
            thermal_throttling_active: false,
            max_safe_temperature: 85.0,
        }
    }
}

impl CustomHardwareProvider for NpuProvider {
    fn provider_name(&self) -> &str {
        "example_npu"
    }

    fn get_hardware_capability(&self) -> HardwareCapability {
        self.hardware_capability.clone()
    }

    fn is_hardware_available(&self) -> bool {
        // In a real implementation, this would check hardware presence
        // For the example, we'll simulate availability based on device_id
        !self.config.device_id.is_empty()
    }

    fn initialize(&mut self) -> Result<()> {
        if self.initialized {
            return Ok(());
        }

        tracing::info!("Initializing NPU provider: {}", self.config.device_id);

        // Simulate NPU initialization
        std::thread::sleep(std::time::Duration::from_millis(100));

        self.initialized = true;
        tracing::info!("NPU provider initialized successfully");
        Ok(())
    }

    fn compile_subgraph(&self, subgraph: &SubGraph) -> Result<Box<dyn CustomKernel>> {
        if !self.initialized {
            return Err(anyhow!("NPU provider not initialized"));
        }

        let kernel = NpuKernel::compile(
            subgraph,
            &self.config,
            Arc::clone(&self.device_memory),
            Arc::clone(&self.profiler),
        )?;

        Ok(Box::new(kernel))
    }

    fn get_device_memory(&self) -> &dyn DeviceMemory {
        self.device_memory.as_ref()
    }

    fn get_performance_stats(&self) -> ProviderStats {
        self.stats.read().unwrap().clone()
    }

    fn shutdown(&mut self) -> Result<()> {
        if !self.initialized {
            return Ok(());
        }

        tracing::info!("Shutting down NPU provider");

        // Cleanup resources
        self.device_memory.clear_all_allocations();
        self.initialized = false;

        tracing::info!("NPU provider shutdown complete");
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// NPU thermal state information.
#[derive(Debug, Clone)]
pub struct NpuThermalState {
    /// Current temperature in Celsius.
    pub temperature_celsius: f64,
    /// Whether thermal throttling is active.
    pub thermal_throttling_active: bool,
    /// Maximum safe operating temperature.
    pub max_safe_temperature: f64,
}

/// NPU-specific compiled kernel.
#[derive(Debug)]
pub struct NpuKernel {
    name: String,
    operations: Vec<String>,
    device_memory: Arc<NpuDeviceMemory>,
    profiler: Arc<Mutex<NpuProfiler>>,
    kernel_info: KernelInfo,
    compiled_code: Vec<u8>, // Simulated compiled kernel
}

impl NpuKernel {
    fn compile(
        subgraph: &SubGraph,
        config: &NpuConfig,
        device_memory: Arc<NpuDeviceMemory>,
        profiler: Arc<Mutex<NpuProfiler>>,
    ) -> Result<Self> {
        let compilation_start = Instant::now();

        // Simulate kernel compilation
        let operations: Vec<String> = subgraph
            .nodes
            .iter()
            .map(|node| node.op_type.clone())
            .collect();

        let name = format!(
            "npu_kernel_{}",
            subgraph.name.as_deref().unwrap_or("unnamed")
        );

        // Estimate memory usage and execution time based on operations
        let estimated_memory_bytes = operations.len() as u64 * 1024 * 1024; // 1MB per op
        let estimated_execution_time_us = operations.len() as f64 * 100.0; // 100us per op

        // Simulate compilation with delay
        let compilation_delay = std::time::Duration::from_millis(50 + operations.len() as u64 * 10);
        std::thread::sleep(compilation_delay);

        let compilation_time_ms = compilation_start.elapsed().as_millis() as f64;

        let kernel_info = KernelInfo {
            name: name.clone(),
            operations: operations.clone(),
            estimated_memory_bytes,
            estimated_execution_time_us,
            hardware_utilization: 0.8, // Assume 80% utilization
            compilation_time_ms,
        };

        // Generate simulated compiled code
        let compiled_code = vec![0u8; 1024]; // Dummy compiled kernel

        Ok(Self {
            name,
            operations,
            device_memory,
            profiler,
            kernel_info,
            compiled_code,
        })
    }
}

impl CustomKernel for NpuKernel {
    fn execute(&self, inputs: &[Tensor]) -> Result<Vec<Tensor>> {
        let start_time = Instant::now();

        // Start profiling
        let mut profiler = self.profiler.lock().unwrap();
        let session = profiler.start_profiling(&self.name)?;
        drop(profiler); // Release lock

        // Simulate kernel execution
        let execution_delay =
            std::time::Duration::from_micros(self.kernel_info.estimated_execution_time_us as u64);
        std::thread::sleep(execution_delay);

        // Create dummy output tensors
        let outputs: Vec<Tensor> = inputs
            .iter()
            .map(|input| {
                // For this example, just pass through the input as output
                input.clone()
            })
            .collect();

        // Stop profiling
        let mut profiler = self.profiler.lock().unwrap();
        let _results = profiler.stop_profiling(session)?;

        tracing::debug!(
            "NPU kernel '{}' executed in {:?}",
            self.name,
            start_time.elapsed()
        );

        Ok(outputs)
    }

    fn get_memory_usage(&self) -> MemoryUsage {
        MemoryUsage {
            device_bytes: self.kernel_info.estimated_memory_bytes,
            host_bytes: 0,
            shared_bytes: 0,
        }
    }

    fn get_performance_stats(&self) -> KernelStats {
        KernelStats {
            total_executions: 1,
            average_execution_time_us: self.kernel_info.estimated_execution_time_us,
            min_execution_time_us: self.kernel_info.estimated_execution_time_us * 0.9,
            max_execution_time_us: self.kernel_info.estimated_execution_time_us * 1.1,
            total_memory_allocated: self.kernel_info.estimated_memory_bytes,
            peak_memory_usage: self.kernel_info.estimated_memory_bytes,
        }
    }

    fn get_kernel_info(&self) -> KernelInfo {
        self.kernel_info.clone()
    }

    fn warmup(&self) -> Result<()> {
        tracing::debug!("Warming up NPU kernel '{}'", self.name);
        // Simulate warmup with a quick execution
        std::thread::sleep(std::time::Duration::from_millis(10));
        Ok(())
    }
}

/// NPU-specific device memory implementation.
#[derive(Debug)]
pub struct NpuDeviceMemory {
    max_pool_size: u64,
    allocated_buffers: Arc<Mutex<HashMap<u64, DeviceBuffer>>>,
    next_handle: Arc<Mutex<u64>>,
    total_allocated: Arc<Mutex<u64>>,
}

impl NpuDeviceMemory {
    fn new(max_pool_size: u64) -> Self {
        Self {
            max_pool_size,
            allocated_buffers: Arc::new(Mutex::new(HashMap::new())),
            next_handle: Arc::new(Mutex::new(1)),
            total_allocated: Arc::new(Mutex::new(0)),
        }
    }

    fn clear_all_allocations(&self) {
        let mut buffers = self.allocated_buffers.lock().unwrap();
        buffers.clear();
        let mut total = self.total_allocated.lock().unwrap();
        *total = 0;
    }
}

impl DeviceMemory for NpuDeviceMemory {
    fn allocate(&self, size: usize, alignment: usize) -> Result<DeviceBuffer> {
        let mut total = self.total_allocated.lock().unwrap();
        if *total + size as u64 > self.max_pool_size {
            return Err(anyhow!("NPU memory pool exhausted"));
        }

        let mut next_handle = self.next_handle.lock().unwrap();
        let handle = *next_handle;
        *next_handle += 1;

        let buffer = DeviceBuffer {
            handle,
            size,
            alignment,
            device_id: 0,
            memory_type: "NPU_HBM".to_string(),
        };

        let mut buffers = self.allocated_buffers.lock().unwrap();
        buffers.insert(handle, buffer.clone());
        *total += size as u64;

        tracing::debug!("NPU allocated {} bytes, handle: {}", size, handle);
        Ok(buffer)
    }

    fn deallocate(&self, buffer: DeviceBuffer) -> Result<()> {
        let mut buffers = self.allocated_buffers.lock().unwrap();
        if buffers.remove(&buffer.handle).is_some() {
            let mut total = self.total_allocated.lock().unwrap();
            *total = total.saturating_sub(buffer.size as u64);
            tracing::debug!(
                "NPU deallocated {} bytes, handle: {}",
                buffer.size,
                buffer.handle
            );
            Ok(())
        } else {
            Err(anyhow!("Invalid NPU buffer handle: {}", buffer.handle))
        }
    }

    fn copy_to_device(&self, host_data: &[u8], device_buffer: &DeviceBuffer) -> Result<()> {
        if host_data.len() > device_buffer.size {
            return Err(anyhow!("Host data larger than device buffer"));
        }

        // Simulate memory copy
        let copy_delay = std::time::Duration::from_nanos(host_data.len() as u64);
        std::thread::sleep(copy_delay);

        tracing::debug!("NPU copied {} bytes to device", host_data.len());
        Ok(())
    }

    fn copy_from_device(&self, device_buffer: &DeviceBuffer, host_data: &mut [u8]) -> Result<()> {
        if host_data.len() > device_buffer.size {
            return Err(anyhow!("Host buffer smaller than device data"));
        }

        // Simulate memory copy
        let copy_delay = std::time::Duration::from_nanos(host_data.len() as u64);
        std::thread::sleep(copy_delay);

        tracing::debug!("NPU copied {} bytes from device", host_data.len());
        Ok(())
    }

    fn get_memory_info(&self) -> DeviceMemoryInfo {
        let total_allocated = *self.total_allocated.lock().unwrap();
        DeviceMemoryInfo {
            total_bytes: self.max_pool_size,
            available_bytes: self.max_pool_size.saturating_sub(total_allocated),
            allocated_bytes: total_allocated,
            bandwidth_gbps: 400.0,
            memory_type: "HBM2E".to_string(),
        }
    }

    fn synchronize(&self) -> Result<()> {
        // Simulate synchronization
        std::thread::sleep(std::time::Duration::from_micros(10));
        Ok(())
    }

    fn can_access(&self, _buffer1: &DeviceBuffer, _buffer2: &DeviceBuffer) -> bool {
        // For NPU, all buffers are in the same address space
        true
    }
}

/// NPU profiler implementation.
#[derive(Debug)]
pub struct NpuProfiler {
    active_sessions: HashMap<u64, ProfilingSession>,
    completed_sessions: Vec<ProfilingResults>,
    next_session_id: u64,
}

impl NpuProfiler {
    fn new() -> Self {
        Self {
            active_sessions: HashMap::new(),
            completed_sessions: Vec::new(),
            next_session_id: 1,
        }
    }
}

impl HardwareProfiler for NpuProfiler {
    fn start_profiling(&mut self, operation_name: &str) -> Result<ProfilingSession> {
        let session_id = self.next_session_id;
        self.next_session_id += 1;

        let session = ProfilingSession {
            session_id,
            operation_name: operation_name.to_string(),
            start_time: Instant::now(),
        };

        self.active_sessions.insert(session_id, session.clone());
        Ok(session)
    }

    fn stop_profiling(&mut self, session: ProfilingSession) -> Result<ProfilingResults> {
        self.active_sessions.remove(&session.session_id);

        let execution_time_us = session.start_time.elapsed().as_micros() as f64;

        let results = ProfilingResults {
            operation_name: session.operation_name,
            execution_time_us,
            memory_usage_bytes: 1024 * 1024, // 1MB
            hardware_utilization: 85.0,
            power_consumption_watts: 20.0,
            energy_consumed_mj: execution_time_us * 20.0 / 1_000_000.0,
            custom_metrics: {
                let mut metrics = HashMap::new();
                metrics.insert(
                    "npu_ops_per_second".to_string(),
                    1_000_000.0 / execution_time_us,
                );
                metrics.insert("npu_cache_hit_rate".to_string(), 0.95);
                metrics
            },
        };

        self.completed_sessions.push(results.clone());
        Ok(results)
    }

    fn get_profiling_summary(&self) -> ProfilingSummary {
        if self.completed_sessions.is_empty() {
            return ProfilingSummary {
                total_operations: 0,
                total_execution_time_us: 0.0,
                average_execution_time_us: 0.0,
                peak_memory_bytes: 0,
                average_utilization: 0.0,
                total_energy_joules: 0.0,
                top_operations_by_time: Vec::new(),
            };
        }

        let total_operations = self.completed_sessions.len() as u64;
        let total_execution_time_us: f64 = self
            .completed_sessions
            .iter()
            .map(|r| r.execution_time_us)
            .sum();
        let average_execution_time_us = total_execution_time_us / total_operations as f64;
        let peak_memory_bytes = self
            .completed_sessions
            .iter()
            .map(|r| r.memory_usage_bytes)
            .max()
            .unwrap_or(0);
        let average_utilization: f64 = self
            .completed_sessions
            .iter()
            .map(|r| r.hardware_utilization)
            .sum::<f64>()
            / total_operations as f64;
        let total_energy_joules: f64 = self
            .completed_sessions
            .iter()
            .map(|r| r.energy_consumed_mj / 1000.0)
            .sum();

        // Get top 5 operations by execution time
        let mut operations_by_time: Vec<(String, f64)> = self
            .completed_sessions
            .iter()
            .map(|r| (r.operation_name.clone(), r.execution_time_us))
            .collect();
        operations_by_time
            .sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        operations_by_time.truncate(5);

        ProfilingSummary {
            total_operations,
            total_execution_time_us,
            average_execution_time_us,
            peak_memory_bytes,
            average_utilization,
            total_energy_joules,
            top_operations_by_time: operations_by_time,
        }
    }

    fn reset_profiling(&mut self) {
        self.active_sessions.clear();
        self.completed_sessions.clear();
        self.next_session_id = 1;
    }
}

/// Create a new NPU provider with default configuration.
pub fn create_npu_provider() -> Result<NpuProvider> {
    Ok(NpuProvider::new(NpuConfig::default()))
}

/// Create a new NPU provider with custom configuration.
pub fn create_npu_provider_with_config(config: NpuConfig) -> Result<NpuProvider> {
    Ok(NpuProvider::new(config))
}

#[cfg(test)]
mod tests {
    use super::*;
    use ronn_core::{Node, OpType};

    #[test]
    fn test_npu_provider_creation() {
        let provider = create_npu_provider().unwrap();
        assert_eq!(provider.provider_name(), "example_npu");
        assert!(provider.is_hardware_available());
    }

    #[test]
    fn test_npu_provider_initialization() {
        let mut provider = create_npu_provider().unwrap();
        assert!(provider.initialize().is_ok());
        assert!(provider.initialize().is_ok()); // Should be idempotent
    }

    #[test]
    fn test_npu_memory_allocation() {
        let provider = create_npu_provider().unwrap();
        let memory = provider.get_device_memory();

        let buffer = memory.allocate(1024, 256).unwrap();
        assert_eq!(buffer.size, 1024);
        assert_eq!(buffer.alignment, 256);
        assert_eq!(buffer.memory_type, "NPU_HBM");

        assert!(memory.deallocate(buffer).is_ok());
    }

    #[test]
    fn test_npu_kernel_compilation() {
        let mut provider = create_npu_provider().unwrap();
        provider.initialize().unwrap();

        let subgraph = SubGraph {
            name: Some("test_subgraph".to_string()),
            nodes: vec![Node {
                name: "conv1".to_string(),
                op_type: "Conv2D".to_string(),
                inputs: vec!["input".to_string()],
                outputs: vec!["conv1_out".to_string()],
                attributes: std::collections::HashMap::new(),
            }],
            inputs: vec!["input".to_string()],
            outputs: vec!["conv1_out".to_string()],
        };

        let kernel = provider.compile_subgraph(&subgraph).unwrap();
        let info = kernel.get_kernel_info();
        assert_eq!(info.name, "npu_kernel_test_subgraph");
        assert_eq!(info.operations, vec!["Conv2D"]);
    }

    #[test]
    fn test_npu_power_mode() {
        let mut provider = create_npu_provider().unwrap();

        assert!(provider.set_power_mode(NpuPowerMode::Performance).is_ok());
        assert_eq!(provider.config.power_mode, NpuPowerMode::Performance);

        assert!(provider.set_power_mode(NpuPowerMode::PowerSaver).is_ok());
        assert_eq!(provider.config.power_mode, NpuPowerMode::PowerSaver);
    }

    #[test]
    fn test_npu_thermal_monitoring() {
        let provider = create_npu_provider().unwrap();
        let thermal_state = provider.get_thermal_state();

        assert!(thermal_state.temperature_celsius > 0.0);
        assert!(thermal_state.max_safe_temperature > thermal_state.temperature_celsius);
    }

    #[test]
    fn test_npu_profiler() {
        let mut profiler = NpuProfiler::new();

        let session = profiler.start_profiling("test_op").unwrap();
        std::thread::sleep(std::time::Duration::from_millis(10));
        let results = profiler.stop_profiling(session).unwrap();

        assert_eq!(results.operation_name, "test_op");
        assert!(results.execution_time_us > 0.0);

        let summary = profiler.get_profiling_summary();
        assert_eq!(summary.total_operations, 1);
    }
}