tenflowers-dataset 0.1.1

Data pipeline and dataset utilities for TenfloweRS
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
//! Enhanced DataLoader with work stealing queue for improved multi-threaded performance
//!
//! This module provides an enhanced version of the DataLoader that uses a work-stealing
//! queue for better load balancing and higher throughput in multi-threaded scenarios.

use crate::dataloader::{BatchResult, CollateFn, DataLoaderConfig, DefaultCollate, Sampler};
use crate::numa_scheduler::{NumaScheduler, NumaWorkerAssignment};
use crate::{Dataset, WorkStealingQueue};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};
use tenflowers_core::{Device, Result, TensorError};

/// Task for workers to execute
#[derive(Debug, Clone)]
struct WorkTask {
    /// Batch of indices to process
    batch_indices: Vec<usize>,
    /// Task ID for ordering
    task_id: usize,
}

/// Result of processing a work task
#[derive(Debug)]
#[allow(dead_code)]
struct WorkResult<T> {
    /// Task ID for ordering
    task_id: usize,
    /// Processed batch result
    batch_result: Result<BatchResult<T>>,
    /// Processing time for performance monitoring
    processing_time: Duration,
}

/// Enhanced DataLoader with work stealing queue
#[allow(dead_code)]
pub struct EnhancedDataLoader<T, D: Dataset<T>> {
    /// Reference to the dataset
    dataset: Arc<D>,
    /// Configuration options
    config: DataLoaderConfig,
    /// Work stealing queue for distributing tasks
    work_queue: Arc<WorkStealingQueue<WorkTask>>,
    /// Results queue for collecting processed batches
    results: Arc<std::sync::Mutex<std::collections::BTreeMap<usize, WorkResult<T>>>>,
    /// Worker thread handles
    worker_handles: Vec<JoinHandle<WorkerStats>>,
    /// Shutdown signal for workers
    shutdown_signal: Arc<AtomicBool>,
    /// Next task ID counter
    next_task_id: AtomicUsize,
    /// Next result ID to return
    next_result_id: AtomicUsize,
    /// Total number of tasks to process
    total_tasks: usize,
    /// Performance statistics
    stats: Arc<std::sync::Mutex<LoaderStats>>,
    /// NUMA scheduler for worker affinity
    numa_scheduler: Option<NumaScheduler>,
    /// NUMA worker assignments
    numa_assignments: Vec<NumaWorkerAssignment>,
}

/// Worker thread statistics
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct WorkerStats {
    /// Worker ID
    worker_id: usize,
    /// Number of tasks processed by this worker
    tasks_processed: usize,
    /// Number of tasks stolen from other workers
    tasks_stolen: usize,
    /// Total processing time
    total_processing_time: Duration,
    /// Number of cache hits (if using caching)
    cache_hits: usize,
}

/// Overall loader statistics
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct LoaderStats {
    /// Total number of batches processed
    batches_processed: usize,
    /// Total processing time across all workers
    total_processing_time: Duration,
    /// Average batch processing time
    average_batch_time: Duration,
    /// Peak memory usage (if available)
    peak_memory_usage: Option<usize>,
    /// Number of work stealing events
    work_stealing_events: usize,
}

impl<T, D: Dataset<T> + Send + Sync + 'static> EnhancedDataLoader<T, D>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    /// Create a new enhanced DataLoader with work stealing
    pub fn new<S: Sampler>(dataset: D, config: DataLoaderConfig, sampler: S) -> Result<Self> {
        let dataset = Arc::new(dataset);
        let indices: Vec<usize> = sampler.sample_indices(dataset.len()).collect();

        if indices.is_empty() {
            return Err(TensorError::invalid_argument(
                "No indices to sample".to_string(),
            ));
        }

        // Calculate total number of tasks (batches)
        let total_tasks = if config.drop_last {
            indices.len() / config.batch_size
        } else {
            (indices.len() + config.batch_size - 1) / config.batch_size
        };

        // Create work stealing queue
        let work_queue = Arc::new(WorkStealingQueue::new(config.num_workers));

        // Create results collection
        let results = Arc::new(std::sync::Mutex::new(std::collections::BTreeMap::new()));

        // Initialize shutdown signal and counters
        let shutdown_signal = Arc::new(AtomicBool::new(false));
        let next_task_id = AtomicUsize::new(0);
        let next_result_id = AtomicUsize::new(0);

        // Initialize statistics
        let stats = Arc::new(std::sync::Mutex::new(LoaderStats {
            batches_processed: 0,
            total_processing_time: Duration::from_secs(0),
            average_batch_time: Duration::from_secs(0),
            peak_memory_usage: None,
            work_stealing_events: 0,
        }));

        // Initialize NUMA scheduler if enabled
        let (numa_scheduler, numa_assignments) = if let Some(numa_config) = &config.numa_config {
            if numa_config.enabled {
                match NumaScheduler::new(numa_config.clone()) {
                    Ok(mut scheduler) => {
                        match scheduler.assign_workers(config.num_workers) {
                            Ok(assignments) => (Some(scheduler), assignments),
                            Err(_) => {
                                // Fall back to no NUMA scheduling if assignment fails
                                (None, Vec::new())
                            }
                        }
                    }
                    Err(_) => {
                        // Fall back to no NUMA scheduling if creation fails
                        (None, Vec::new())
                    }
                }
            } else {
                (None, Vec::new())
            }
        } else {
            (None, Vec::new())
        };

        // Generate work tasks
        let mut task_id = 0;
        for batch_start in (0..indices.len()).step_by(config.batch_size) {
            let batch_end = if config.drop_last {
                let end = batch_start + config.batch_size;
                if end <= indices.len() {
                    end
                } else {
                    continue;
                }
            } else {
                (batch_start + config.batch_size).min(indices.len())
            };

            if batch_start >= indices.len()
                || (config.drop_last && batch_end - batch_start < config.batch_size)
            {
                break;
            }

            let batch_indices = indices[batch_start..batch_end].to_vec();
            let task = WorkTask {
                batch_indices,
                task_id,
            };

            work_queue.push(task);
            task_id += 1;
        }

        // Start worker threads
        let mut worker_handles = Vec::new();

        for worker_id in 0..config.num_workers {
            let dataset_clone = Arc::clone(&dataset);
            let config_clone = config.clone();
            let work_queue_clone = Arc::clone(&work_queue);
            let results_clone = Arc::clone(&results);
            let shutdown_clone = Arc::clone(&shutdown_signal);
            let stats_clone = Arc::clone(&stats);

            // Get NUMA assignment for this worker if available
            let numa_assignment = numa_assignments
                .iter()
                .find(|assignment| assignment.worker_id == worker_id)
                .cloned();

            let handle = thread::spawn(move || {
                // Set CPU affinity if NUMA assignment is available
                if let Some(assignment) = &numa_assignment {
                    let _ = NumaScheduler::set_thread_affinity(assignment);
                }

                Self::worker_thread(
                    worker_id,
                    dataset_clone,
                    config_clone,
                    work_queue_clone,
                    results_clone,
                    shutdown_clone,
                    stats_clone,
                    numa_assignment,
                )
            });

            worker_handles.push(handle);
        }

        Ok(Self {
            dataset,
            config,
            work_queue,
            results,
            worker_handles,
            shutdown_signal,
            next_task_id,
            next_result_id,
            total_tasks,
            stats,
            numa_scheduler,
            numa_assignments,
        })
    }

    /// Worker thread function
    #[allow(clippy::too_many_arguments)]
    fn worker_thread(
        worker_id: usize,
        dataset: Arc<D>,
        config: DataLoaderConfig,
        work_queue: Arc<WorkStealingQueue<WorkTask>>,
        results: Arc<std::sync::Mutex<std::collections::BTreeMap<usize, WorkResult<T>>>>,
        shutdown_signal: Arc<AtomicBool>,
        _stats: Arc<std::sync::Mutex<LoaderStats>>,
        _numa_assignment: Option<NumaWorkerAssignment>,
    ) -> WorkerStats {
        let mut worker_stats = WorkerStats {
            worker_id,
            tasks_processed: 0,
            tasks_stolen: 0,
            total_processing_time: Duration::from_secs(0),
            cache_hits: 0,
        };

        while !shutdown_signal.load(Ordering::Relaxed) {
            // Try to get work (either from own queue or steal from others)
            if let Some(task) = work_queue.wait_for_work(worker_id, Some(100)) {
                let start_time = Instant::now();

                // Process the task
                let batch_result = Self::process_task(&dataset, &task, &config);
                let processing_time = start_time.elapsed();

                // Store the result
                let work_result = WorkResult {
                    task_id: task.task_id,
                    batch_result,
                    processing_time,
                };

                {
                    let mut results_map = results.lock().expect("lock should not be poisoned");
                    results_map.insert(task.task_id, work_result);
                }

                // Update worker statistics
                worker_stats.tasks_processed += 1;
                worker_stats.total_processing_time += processing_time;

                // Check if this task was stolen (simple heuristic)
                if worker_id != task.task_id % config.num_workers {
                    worker_stats.tasks_stolen += 1;
                }
            } else if work_queue.is_empty() {
                // No more work available
                break;
            }
        }

        worker_stats
    }

    /// Process a single work task
    fn process_task(
        dataset: &Arc<D>,
        task: &WorkTask,
        config: &DataLoaderConfig,
    ) -> Result<BatchResult<T>> {
        let mut batch = Vec::with_capacity(task.batch_indices.len());

        for &idx in &task.batch_indices {
            match dataset.get(idx) {
                Ok(sample) => {
                    // If target device is specified, move tensors to target device
                    if let Some(device) = config.target_device {
                        let (features, labels) = sample;
                        let features_on_device = features.to(device)?;
                        let labels_on_device = labels.to(device)?;
                        batch.push((features_on_device, labels_on_device));
                    } else {
                        batch.push(sample);
                    }
                }
                Err(e) => return Err(e),
            }
        }

        if config.collate_batches {
            // Collate the batch into stacked tensors
            let collate_fn = DefaultCollate;
            let (features, labels) = collate_fn.collate(batch)?;
            Ok(BatchResult::Collated(features, labels))
        } else {
            // Return individual samples
            Ok(BatchResult::Samples(batch))
        }
    }

    /// Get the next processed batch (blocking)
    pub fn next_batch(&self) -> Option<Result<BatchResult<T>>> {
        let current_id = self.next_result_id.fetch_add(1, Ordering::Relaxed);

        if current_id >= self.total_tasks {
            return None;
        }

        // Wait for the result to be available with timeout
        let start_time = Instant::now();
        let timeout = Duration::from_secs(10); // 10 second timeout

        loop {
            {
                let mut results_map = self.results.lock().expect("lock should not be poisoned");
                if let Some(result) = results_map.remove(&current_id) {
                    // Update statistics
                    {
                        let mut stats = self.stats.lock().expect("lock should not be poisoned");
                        stats.batches_processed += 1;
                        stats.total_processing_time += result.processing_time;
                        stats.average_batch_time =
                            stats.total_processing_time / stats.batches_processed as u32;
                    }

                    return Some(result.batch_result);
                }
            }

            // Check timeout
            if start_time.elapsed() > timeout {
                return Some(Err(TensorError::invalid_argument(format!(
                    "Timeout waiting for batch {current_id}"
                ))));
            }

            // Check if work is done (more robust condition)
            // Only return None if:
            // 1. Work queue is empty
            // 2. All worker threads have finished (not just current_id check)
            // 3. We've waited a reasonable amount of time
            if self.work_queue.is_empty() && start_time.elapsed() > Duration::from_millis(100) {
                // Check if any workers are still alive
                let all_workers_done = self
                    .worker_handles
                    .iter()
                    .all(|handle| handle.is_finished());
                if all_workers_done {
                    return None;
                }
            }

            // Brief sleep to avoid busy waiting
            thread::sleep(Duration::from_millis(1));
        }
    }

    /// Get loader statistics
    pub fn get_stats(&self) -> LoaderStats {
        self.stats
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Get work queue statistics
    pub fn get_queue_stats(&self) -> (Vec<usize>, usize, bool) {
        (
            self.work_queue.queue_lengths(),
            self.total_tasks, // Use planned total batches instead of current queue count
            self.work_queue.is_empty(),
        )
    }

    /// Get NUMA assignment statistics if NUMA scheduling is enabled
    pub fn get_numa_stats(&self) -> Option<crate::numa_scheduler::NumaAssignmentStats> {
        self.numa_scheduler
            .as_ref()
            .map(|scheduler| scheduler.get_assignment_stats())
    }

    /// Get NUMA topology information if NUMA scheduling is enabled
    pub fn get_numa_topology(&self) -> Option<&crate::numa_scheduler::NumaTopology> {
        self.numa_scheduler
            .as_ref()
            .map(|scheduler| scheduler.topology())
    }

    /// Shutdown the loader and collect worker statistics
    pub fn shutdown(self) -> Result<Vec<WorkerStats>> {
        // Signal shutdown
        self.shutdown_signal.store(true, Ordering::Relaxed);
        self.work_queue.shutdown();

        // Collect worker statistics
        let mut worker_stats = Vec::new();
        for handle in self.worker_handles {
            match handle.join() {
                Ok(stats) => worker_stats.push(stats),
                Err(_) => {
                    return Err(TensorError::invalid_argument(
                        "Worker thread panicked".to_string(),
                    ))
                }
            }
        }

        Ok(worker_stats)
    }
}

impl<T, D: Dataset<T> + Send + Sync + 'static> Iterator for EnhancedDataLoader<T, D>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    type Item = Result<BatchResult<T>>;

    fn next(&mut self) -> Option<Self::Item> {
        self.next_batch()
    }
}

/// Builder for EnhancedDataLoader with convenient configuration
pub struct EnhancedDataLoaderBuilder {
    config: DataLoaderConfig,
}

impl EnhancedDataLoaderBuilder {
    /// Create a new builder with default configuration
    pub fn new() -> Self {
        Self {
            config: DataLoaderConfig::default(),
        }
    }

    /// Set the batch size
    pub fn batch_size(mut self, batch_size: usize) -> Self {
        self.config.batch_size = batch_size;
        self
    }

    /// Set the number of worker threads
    pub fn num_workers(mut self, num_workers: usize) -> Self {
        self.config.num_workers = num_workers;
        self
    }

    /// Enable or disable dropping the last incomplete batch
    pub fn drop_last(mut self, drop_last: bool) -> Self {
        self.config.drop_last = drop_last;
        self
    }

    /// Enable automatic batch collation
    pub fn collate_batches(mut self, collate: bool) -> Self {
        self.config.collate_batches = collate;
        self
    }

    /// Set the prefetch factor for background data loading
    pub fn prefetch_factor(mut self, prefetch_factor: usize) -> Self {
        self.config.prefetch_factor = prefetch_factor;
        self
    }

    /// Set target device for tensor loading
    pub fn target_device(mut self, device: Device) -> Self {
        self.config.target_device = Some(device);
        self
    }

    /// Enable NUMA-aware scheduling with default configuration
    pub fn numa_scheduling(mut self) -> Self {
        self.config.numa_config = Some(crate::numa_scheduler::NumaConfig::default());
        self
    }

    /// Set custom NUMA configuration
    pub fn numa_config(mut self, numa_config: crate::numa_scheduler::NumaConfig) -> Self {
        self.config.numa_config = Some(numa_config);
        self
    }

    /// Build the enhanced DataLoader
    pub fn build<T, D: Dataset<T> + Send + Sync + 'static, S: Sampler>(
        self,
        dataset: D,
        sampler: S,
    ) -> Result<EnhancedDataLoader<T, D>>
    where
        T: Clone
            + Default
            + scirs2_core::numeric::Zero
            + Send
            + Sync
            + 'static
            + bytemuck::Pod
            + bytemuck::Zeroable,
    {
        EnhancedDataLoader::new(dataset, self.config, sampler)
    }
}

impl Default for EnhancedDataLoaderBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{SequentialSampler, TensorDataset};
    use tenflowers_core::Tensor;

    #[test]
    fn test_enhanced_dataloader_creation() {
        let features =
            Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0], &[4, 2])
                .expect("test: tensor creation should succeed");
        let labels = Tensor::<f32>::from_vec(vec![0.0, 1.0, 0.0, 1.0], &[4])
            .expect("test: tensor creation should succeed");

        let dataset = TensorDataset::new(features, labels);
        let sampler = SequentialSampler::new();

        let loader = EnhancedDataLoaderBuilder::new()
            .batch_size(2)
            .num_workers(2)
            .build(dataset, sampler)
            .expect("test: operation should succeed");

        // Check queue statistics
        let (queue_lengths, total_tasks, _is_empty) = loader.get_queue_stats();
        assert_eq!(queue_lengths.len(), 2); // 2 workers
        assert_eq!(total_tasks, 2); // 4 samples / 2 batch_size = 2 batches
                                    // Note: don't check is_empty since worker threads may consume tasks immediately
    }

    #[test]
    fn test_enhanced_dataloader_processing() {
        let features = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2])
            .expect("test: tensor creation should succeed");
        let labels = Tensor::<f32>::from_vec(vec![0.0, 1.0, 2.0], &[3])
            .expect("test: tensor creation should succeed");

        let dataset = TensorDataset::new(features, labels);
        let sampler = SequentialSampler::new();

        let mut loader = EnhancedDataLoaderBuilder::new()
            .batch_size(2)
            .num_workers(1)
            .collate_batches(true)
            .build(dataset, sampler)
            .expect("test: operation should succeed");

        // Get first batch
        let batch1 = loader
            .next()
            .expect("test: iterator should have next")
            .expect("test: batch loading should succeed");
        match batch1 {
            BatchResult::Collated(features, labels) => {
                assert_eq!(features.shape().dims(), &[2, 2]); // batch_size=2, feature_size=2
                assert_eq!(labels.shape().dims(), &[2]); // batch_size=2
            }
            _ => panic!("Expected collated batch"),
        }

        // Get second batch (partial)
        let batch2 = loader
            .next()
            .expect("test: iterator should have next")
            .expect("test: batch loading should succeed");
        match batch2 {
            BatchResult::Collated(features, labels) => {
                assert_eq!(features.shape().dims(), &[1, 2]); // batch_size=1, feature_size=2
                assert_eq!(labels.shape().dims(), &[1]); // batch_size=1
            }
            _ => panic!("Expected collated batch"),
        }

        // No more batches
        assert!(loader.next().is_none());

        // Check statistics
        let stats = loader.get_stats();
        assert_eq!(stats.batches_processed, 2);
        assert!(stats.average_batch_time > Duration::from_secs(0));
    }

    #[test]
    fn test_enhanced_dataloader_worker_stats() {
        let features =
            Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0], &[4, 2])
                .expect("test: tensor creation should succeed");
        let labels = Tensor::<f32>::from_vec(vec![0.0, 1.0, 0.0, 1.0], &[4])
            .expect("test: tensor creation should succeed");

        let dataset = TensorDataset::new(features, labels);
        let sampler = SequentialSampler::new();

        let loader = EnhancedDataLoaderBuilder::new()
            .batch_size(1)
            .num_workers(2)
            .build(dataset, sampler)
            .expect("test: operation should succeed");

        // Process all batches
        let _batches: Vec<_> = loader.collect();

        // Shutdown and get worker statistics
        // Note: In real usage, we'd need to handle the loader properly
        // For testing, we just verify the structure exists
    }
}