torsh-data 0.1.2

Data loading and preprocessing utilities for ToRSh
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
# Troubleshooting Guide for ToRSh Data Loading

This guide helps diagnose and resolve common issues when using the ToRSh data loading framework.

## Table of Contents

1. [Common Error Messages]#common-error-messages
2. [Performance Issues]#performance-issues
3. [Memory Problems]#memory-problems
4. [Threading and Concurrency Issues]#threading-and-concurrency-issues
5. [GPU-Related Problems]#gpu-related-problems
6. [Data Corruption and Validation]#data-corruption-and-validation
7. [Integration Issues]#integration-issues
8. [Debugging Tools and Techniques]#debugging-tools-and-techniques
9. [Configuration Problems]#configuration-problems
10. [Platform-Specific Issues]#platform-specific-issues

## Common Error Messages

### IndexError: Index out of bounds

**Error Message:**
```
IndexError: Index 150 out of bounds (size: 100)
```

**Cause:** Accessing dataset with invalid index

**Solutions:**
```rust
// Problem: Incorrect dataset length calculation
impl Dataset for MyDataset {
    fn len(&self) -> usize {
        self.data.len() + 10  // Wrong! Adding extra length
    }
}

// Solution: Return correct length
impl Dataset for MyDataset {
    fn len(&self) -> usize {
        self.data.len()  // Correct
    }
    
    fn get(&self, index: usize) -> Result<Self::Item> {
        // Always validate bounds
        if index >= self.len() {
            return Err(DataError::IndexOutOfBounds {
                index,
                size: self.len(),
            }.into());
        }
        // ... rest of implementation
    }
}

// Additional validation for edge cases
fn create_safe_indices(dataset_len: usize, batch_size: usize) -> Vec<usize> {
    (0..dataset_len)
        .step_by(batch_size)
        .map(|i| i.min(dataset_len - 1))  // Clamp to valid range
        .collect()
}
```

### DataError: Shape mismatch in batch collation

**Error Message:**
```
DataError: Cannot collate tensors with shapes [224, 224, 3] and [256, 256, 3]
```

**Cause:** Inconsistent tensor shapes in batch

**Solutions:**
```rust
// Problem: Mixed image sizes in dataset
let dataset = ImageDataset::new(image_paths); // Images have different sizes

// Solution 1: Consistent preprocessing
let transform = Compose::new(vec![
    Box::new(Resize::new((224, 224))),  // Ensure consistent size
    Box::new(ToTensor),
]);
let dataset = TransformedDataset::new(dataset, transform);

// Solution 2: Custom collate function for variable sizes
struct VariableSizeCollate;

impl Collate<Tensor<f32>> for VariableSizeCollate {
    type Output = Vec<Tensor<f32>>;  // Return list instead of batched tensor
    
    fn collate(&self, batch: Vec<Tensor<f32>>) -> Result<Self::Output> {
        // Don't stack - return as is for variable sizes
        Ok(batch)
    }
}

// Solution 3: Padding for NLP sequences
struct PaddedCollate {
    pad_token: i32,
}

impl Collate<Vec<i32>> for PaddedCollate {
    type Output = Tensor<i32>;
    
    fn collate(&self, batch: Vec<Vec<i32>>) -> Result<Self::Output> {
        let max_len = batch.iter().map(|seq| seq.len()).max().unwrap_or(0);
        
        let mut padded_batch = Vec::new();
        for mut seq in batch {
            seq.resize(max_len, self.pad_token);
            padded_batch.extend(seq);
        }
        
        Tensor::from_slice(&padded_batch, &[batch.len(), max_len])
    }
}
```

### MemoryError: Out of memory

**Error Message:**
```
MemoryError: Failed to allocate 4GB for tensor
```

**Cause:** Insufficient memory for large batches or datasets

**Solutions:**
```rust
// Problem: Batch size too large
let dataloader = DataLoader::builder(dataset)
    .batch_size(1024)  // Too large!
    .build();

// Solution 1: Reduce batch size
let dataloader = DataLoader::builder(dataset)
    .batch_size(32)    // More reasonable
    .build();

// Solution 2: Implement memory monitoring
struct MemoryAwareDataLoader<D> {
    inner: DataLoader<D>,
    max_memory_mb: usize,
    current_batch_size: usize,
    min_batch_size: usize,
    max_batch_size: usize,
}

impl<D: Dataset> MemoryAwareDataLoader<D> {
    pub fn new(dataset: D, initial_batch_size: usize, max_memory_mb: usize) -> Self {
        let dataloader = DataLoader::builder(dataset)
            .batch_size(initial_batch_size)
            .build();
        
        Self {
            inner: dataloader,
            max_memory_mb,
            current_batch_size: initial_batch_size,
            min_batch_size: 1,
            max_batch_size: initial_batch_size * 4,
        }
    }
    
    fn adapt_batch_size(&mut self) -> bool {
        let current_memory = get_memory_usage_mb();
        
        if current_memory > self.max_memory_mb * 80 / 100 {  // 80% threshold
            // Reduce batch size
            self.current_batch_size = (self.current_batch_size * 3 / 4).max(self.min_batch_size);
            true
        } else if current_memory < self.max_memory_mb * 50 / 100 {  // 50% threshold
            // Increase batch size
            self.current_batch_size = (self.current_batch_size * 5 / 4).min(self.max_batch_size);
            true
        } else {
            false
        }
    }
}

// Solution 3: Use streaming for large datasets
struct StreamingDataset<T> {
    data_source: Box<dyn Iterator<Item = Result<T>> + Send>,
}

impl<T> IterableDataset for StreamingDataset<T> {
    type Item = T;
    type Iter = Box<dyn Iterator<Item = Result<T>> + Send>;
    
    fn iter(&self) -> Self::Iter {
        // Stream data instead of loading all at once
        Box::new(self.data_source.by_ref())
    }
}
```

### WorkerError: Worker thread panicked

**Error Message:**
```
WorkerError: Worker thread 2 panicked: called `Result::unwrap()` on an `Err` value
```

**Cause:** Exception in worker thread, often due to unhandled errors

**Solutions:**
```rust
// Problem: Unhandled errors in worker threads
fn worker_function(dataset: &Dataset, indices: Vec<usize>) {
    for index in indices {
        let item = dataset.get(index).unwrap();  // This can panic!
        process_item(item);
    }
}

// Solution: Proper error handling
fn robust_worker_function(dataset: &Dataset, indices: Vec<usize>) -> Result<Vec<ProcessedItem>> {
    let mut results = Vec::new();
    
    for index in indices {
        match dataset.get(index) {
            Ok(item) => {
                match process_item(item) {
                    Ok(processed) => results.push(processed),
                    Err(e) => {
                        eprintln!("Warning: Failed to process item {}: {}", index, e);
                        // Continue with next item instead of panicking
                        continue;
                    }
                }
            }
            Err(e) => {
                eprintln!("Warning: Failed to load item {}: {}", index, e);
                // Option 1: Skip item
                continue;
                // Option 2: Return error (depends on use case)
                // return Err(e);
            }
        }
    }
    
    Ok(results)
}

// Enhanced worker with panic recovery
struct RobustWorker {
    worker_id: usize,
    panic_count: AtomicUsize,
    max_panics: usize,
}

impl RobustWorker {
    fn run_with_recovery<F, R>(&self, work: F) -> Result<R>
    where
        F: FnOnce() -> Result<R> + UnwindSafe,
    {
        match std::panic::catch_unwind(|| work()) {
            Ok(result) => result,
            Err(panic_info) => {
                let panic_count = self.panic_count.fetch_add(1, Ordering::Relaxed);
                
                eprintln!("Worker {} panicked (count: {}): {:?}", 
                         self.worker_id, panic_count + 1, panic_info);
                
                if panic_count >= self.max_panics {
                    return Err(WorkerError::TooManyPanics {
                        worker_id: self.worker_id,
                        panic_count: panic_count + 1,
                    }.into());
                }
                
                // Return a recoverable error instead of propagating panic
                Err(WorkerError::WorkerPanic {
                    worker_id: self.worker_id,
                }.into())
            }
        }
    }
}
```

## Performance Issues

### Slow Data Loading

**Symptoms:**
- High CPU wait time
- Low GPU utilization
- Training stalls waiting for data

**Diagnosis:**
```rust
// Add performance monitoring
struct PerformanceDiagnostic {
    load_times: Vec<Duration>,
    batch_sizes: Vec<usize>,
    start_time: Instant,
}

impl PerformanceDiagnostic {
    fn diagnose_bottlenecks(&self) -> Vec<BottleneckType> {
        let mut bottlenecks = Vec::new();
        
        let avg_load_time = self.load_times.iter().sum::<Duration>() / self.load_times.len() as u32;
        let total_samples: usize = self.batch_sizes.iter().sum();
        let elapsed = self.start_time.elapsed();
        let throughput = total_samples as f64 / elapsed.as_secs_f64();
        
        if avg_load_time > Duration::from_millis(100) {
            bottlenecks.push(BottleneckType::SlowDataLoading);
        }
        
        if throughput < 100.0 {  // samples per second
            bottlenecks.push(BottleneckType::LowThroughput);
        }
        
        let cpu_usage = get_cpu_utilization();
        if cpu_usage > 90.0 {
            bottlenecks.push(BottleneckType::CPUBound);
        }
        
        let io_wait = get_io_wait_percentage();
        if io_wait > 30.0 {
            bottlenecks.push(BottleneckType::IOBound);
        }
        
        bottlenecks
    }
}

#[derive(Debug)]
enum BottleneckType {
    SlowDataLoading,
    LowThroughput,
    CPUBound,
    IOBound,
    MemoryBound,
}
```

**Solutions:**

1. **Optimize I/O:**
```rust
// Use memory mapping for large files
let dataset = MmapDataset::new("large_file.bin", item_size)?;

// Implement prefetching
let prefetch_dataset = PrefetchDataset::new(dataset, prefetch_size: 100);

// Use SSD storage
// Consider network-attached storage for distributed training
```

2. **Increase Parallelism:**
```rust
// Optimize worker count
let optimal_workers = std::cmp::min(
    num_cpus::get(),
    available_memory_gb * 2,  // Rule of thumb
);

let dataloader = DataLoader::builder(dataset)
    .num_workers(optimal_workers)
    .pin_memory(true)  // For GPU training
    .build();
```

3. **Cache Frequently Used Data:**
```rust
struct CachedDataset<D> {
    inner: D,
    cache: Arc<RwLock<LruCache<usize, D::Item>>>,
    cache_hit_rate: AtomicUsize,
    total_accesses: AtomicUsize,
}

impl<D: Dataset> CachedDataset<D>
where
    D::Item: Clone,
{
    fn get_cache_stats(&self) -> (f64, usize) {
        let hits = self.cache_hit_rate.load(Ordering::Relaxed);
        let total = self.total_accesses.load(Ordering::Relaxed);
        let hit_rate = if total > 0 { hits as f64 / total as f64 } else { 0.0 };
        (hit_rate, total)
    }
}
```

### Memory Leaks

**Symptoms:**
- Gradually increasing memory usage
- Out of memory errors after long runs
- Poor performance due to memory pressure

**Diagnosis:**
```rust
// Memory leak detector
struct MemoryLeakDetector {
    snapshots: Vec<MemorySnapshot>,
    check_interval: Duration,
    leak_threshold_mb: f64,
}

#[derive(Debug, Clone)]
struct MemorySnapshot {
    timestamp: Instant,
    rss_mb: f64,
    heap_mb: f64,
    active_objects: usize,
}

impl MemoryLeakDetector {
    fn check_for_leaks(&mut self) -> Option<MemoryLeak> {
        let current_snapshot = self.take_snapshot();
        self.snapshots.push(current_snapshot.clone());
        
        if self.snapshots.len() < 3 {
            return None;
        }
        
        // Check trend over last few snapshots
        let recent_snapshots = &self.snapshots[self.snapshots.len()-3..];
        let memory_trend = self.calculate_trend(recent_snapshots);
        
        if memory_trend > self.leak_threshold_mb {
            Some(MemoryLeak {
                trend_mb_per_sec: memory_trend,
                current_usage_mb: current_snapshot.rss_mb,
                snapshots: recent_snapshots.to_vec(),
            })
        } else {
            None
        }
    }
    
    fn calculate_trend(&self, snapshots: &[MemorySnapshot]) -> f64 {
        if snapshots.len() < 2 {
            return 0.0;
        }
        
        let time_diff = snapshots.last().unwrap().timestamp
            .duration_since(snapshots.first().unwrap().timestamp);
        let memory_diff = snapshots.last().unwrap().rss_mb 
            - snapshots.first().unwrap().rss_mb;
        
        memory_diff / time_diff.as_secs_f64()
    }
}
```

**Solutions:**

1. **Use RAII and Smart Pointers:**
```rust
// Problem: Manual memory management
struct BadDataset {
    raw_data: *mut u8,
    size: usize,
}

// Solution: Use smart pointers
struct GoodDataset {
    data: Arc<Vec<u8>>,  // Automatic cleanup
    metadata: Box<Metadata>,
}

// Use memory pools for frequent allocations
struct PoolAllocatedDataset {
    pool: Arc<MemoryPool<DataItem>>,
    items: Vec<PoolPtr<DataItem>>,
}
```

2. **Implement Proper Drop:**
```rust
struct ResourceDataset {
    file_handles: Vec<File>,
    gpu_buffers: Vec<CudaBuffer>,
}

impl Drop for ResourceDataset {
    fn drop(&mut self) {
        // Explicit cleanup of resources
        for buffer in &mut self.gpu_buffers {
            buffer.free();
        }
        // Files are automatically closed by their Drop impl
    }
}
```

## Threading and Concurrency Issues

### Race Conditions

**Symptoms:**
- Inconsistent results between runs
- Occasional crashes or panics
- Data corruption

**Solutions:**
```rust
// Problem: Unsynchronized access to shared state
struct UnsafeDataset {
    cache: HashMap<usize, DataItem>,  // Not thread-safe!
    access_count: usize,
}

// Solution: Proper synchronization
struct SafeDataset {
    cache: Arc<RwLock<HashMap<usize, DataItem>>>,
    access_count: AtomicUsize,
}

impl Dataset for SafeDataset {
    fn get(&self, index: usize) -> Result<Self::Item> {
        self.access_count.fetch_add(1, Ordering::Relaxed);
        
        // Read-only access
        {
            let cache = self.cache.read().unwrap();
            if let Some(item) = cache.get(&index) {
                return Ok(item.clone());
            }
        }
        
        // Write access (separate scope to avoid deadlock)
        let item = self.load_item(index)?;
        {
            let mut cache = self.cache.write().unwrap();
            cache.insert(index, item.clone());
        }
        
        Ok(item)
    }
}

// Lock-free alternatives for high-performance scenarios
use crossbeam::queue::SegQueue;

struct LockFreeQueue<T> {
    queue: SegQueue<T>,
}

impl<T> LockFreeQueue<T> {
    fn push(&self, item: T) {
        self.queue.push(item);
    }
    
    fn pop(&self) -> Option<T> {
        self.queue.pop()
    }
}
```

### Deadlocks

**Symptoms:**
- Application hangs
- No progress in data loading
- High CPU usage with no work being done

**Solutions:**
```rust
// Problem: Lock ordering issues
struct DeadlockProneDataset {
    cache1: Mutex<HashMap<usize, DataItem>>,
    cache2: Mutex<HashMap<String, MetaData>>,
}

impl DeadlockProneDataset {
    fn bad_method(&self, index: usize, key: &str) {
        let _c1 = self.cache1.lock().unwrap();  // Lock A
        let _c2 = self.cache2.lock().unwrap();  // Lock B
    }
    
    fn another_bad_method(&self, key: &str, index: usize) {
        let _c2 = self.cache2.lock().unwrap();  // Lock B first
        let _c1 = self.cache1.lock().unwrap();  // Lock A second - DEADLOCK!
    }
}

// Solution: Consistent lock ordering
struct DeadlockFreeDataset {
    cache1: Mutex<HashMap<usize, DataItem>>,
    cache2: Mutex<HashMap<String, MetaData>>,
}

impl DeadlockFreeDataset {
    fn safe_method(&self, index: usize, key: &str) {
        // Always acquire locks in the same order
        let _c1 = self.cache1.lock().unwrap();  // Always lock A first
        let _c2 = self.cache2.lock().unwrap();  // Always lock B second
    }
    
    // Even better: Avoid multiple locks
    fn better_method(&self, index: usize, key: &str) -> Result<CombinedResult> {
        let cache1_data = {
            let cache = self.cache1.lock().unwrap();
            cache.get(&index).cloned()
        };  // Lock released here
        
        let cache2_data = {
            let cache = self.cache2.lock().unwrap();
            cache.get(key).cloned()
        };  // Lock released here
        
        Ok(CombinedResult { cache1_data, cache2_data })
    }
}

// Timeout-based locking for deadlock detection
use std::time::Duration;

fn try_with_timeout<T, F>(mutex: &Mutex<T>, timeout: Duration, f: F) -> Result<R>
where
    F: FnOnce(&mut T) -> R,
{
    match mutex.try_lock_for(timeout) {
        Some(mut guard) => Ok(f(&mut *guard)),
        None => Err(DataError::LockTimeout { timeout }.into()),
    }
}
```

## GPU-Related Problems

### CUDA Out of Memory

**Error Message:**
```
CUDA Error: out of memory (error code: 2)
```

**Solutions:**
```rust
// Monitor GPU memory usage
struct GPUMemoryMonitor {
    peak_usage: AtomicUsize,
    current_usage: AtomicUsize,
    allocation_count: AtomicUsize,
}

impl GPUMemoryMonitor {
    fn allocate(&self, size: usize) -> Result<GPUPtr> {
        let available = get_gpu_memory_available()?;
        
        if size > available {
            // Try to free some memory first
            self.run_garbage_collection();
            
            let available_after_gc = get_gpu_memory_available()?;
            if size > available_after_gc {
                return Err(GPUError::OutOfMemory {
                    requested: size,
                    available: available_after_gc,
                }.into());
            }
        }
        
        let ptr = cuda_malloc(size)?;
        self.current_usage.fetch_add(size, Ordering::Relaxed);
        self.allocation_count.fetch_add(1, Ordering::Relaxed);
        
        Ok(ptr)
    }
    
    fn run_garbage_collection(&self) {
        // Force collection of unused GPU memory
        cuda_empty_cache();
        
        // Optional: Move some data back to CPU if needed
        self.offload_to_cpu_if_needed();
    }
}

// Implement GPU memory pools
struct GPUMemoryPool {
    free_blocks: Vec<Vec<GPUBlock>>,
    size_classes: Vec<usize>,
}

impl GPUMemoryPool {
    fn get_block(&mut self, size: usize) -> Result<GPUBlock> {
        let size_class = self.find_size_class(size);
        
        if let Some(block) = self.free_blocks[size_class].pop() {
            Ok(block)
        } else {
            // Allocate new block
            self.allocate_new_block(self.size_classes[size_class])
        }
    }
}
```

### GPU Transfer Issues

**Symptoms:**
- Slow training despite fast loading
- High CPU-GPU transfer overhead

**Solutions:**
```rust
// Asynchronous GPU transfers
struct AsyncGPUDataLoader<D> {
    cpu_loader: DataLoader<D>,
    transfer_queue: VecDeque<TransferJob>,
    gpu_streams: Vec<CudaStream>,
}

struct TransferJob {
    cpu_data: Tensor<f32>,
    gpu_promise: Promise<Tensor<f32>>,
    stream_id: usize,
}

impl<D: Dataset> AsyncGPUDataLoader<D> {
    fn start_async_transfer(&mut self, cpu_batch: Tensor<f32>) -> Future<Tensor<f32>> {
        let stream_id = self.get_next_stream();
        let (promise, future) = create_promise();
        
        let job = TransferJob {
            cpu_data: cpu_batch,
            gpu_promise: promise,
            stream_id,
        };
        
        self.transfer_queue.push_back(job);
        self.process_transfer_queue();
        
        future
    }
    
    fn process_transfer_queue(&mut self) {
        while let Some(job) = self.transfer_queue.pop_front() {
            let stream = &self.gpu_streams[job.stream_id];
            
            // Start async transfer
            let gpu_tensor = job.cpu_data.to_device_async(&self.device, stream)?;
            
            // Set callback for completion
            stream.add_callback(move || {
                job.gpu_promise.set(gpu_tensor);
            });
        }
    }
}

// Pinned memory for faster transfers
struct PinnedMemoryDataLoader<D> {
    inner: DataLoader<D>,
    pinned_buffers: Vec<PinnedBuffer>,
    buffer_index: AtomicUsize,
}

impl<D: Dataset> PinnedMemoryDataLoader<D> {
    fn new(inner: DataLoader<D>, num_buffers: usize) -> Self {
        let pinned_buffers = (0..num_buffers)
            .map(|_| PinnedBuffer::new(estimate_buffer_size(&inner)))
            .collect();
        
        Self {
            inner,
            pinned_buffers,
            buffer_index: AtomicUsize::new(0),
        }
    }
    
    fn get_next_batch(&mut self) -> Result<Tensor<f32>> {
        let buffer_idx = self.buffer_index.fetch_add(1, Ordering::Relaxed) 
            % self.pinned_buffers.len();
        let buffer = &mut self.pinned_buffers[buffer_idx];
        
        // Load data into pinned memory
        let cpu_batch = self.inner.next().unwrap()?;
        buffer.copy_from_tensor(&cpu_batch);
        
        // Fast transfer from pinned memory
        buffer.to_gpu_async()
    }
}
```

## Data Corruption and Validation

### Detecting Corrupted Data

**Symptoms:**
- Training loss becomes NaN
- Inconsistent model performance
- Unexpected tensor values

**Solutions:**
```rust
// Data validation layer
struct ValidatedDataset<D> {
    inner: D,
    validator: DataValidator,
    corruption_count: AtomicUsize,
    total_samples: AtomicUsize,
}

struct DataValidator {
    validate_range: bool,
    min_value: f32,
    max_value: f32,
    validate_shape: bool,
    expected_shape: Vec<usize>,
    validate_checksum: bool,
}

impl<D: Dataset> ValidatedDataset<D>
where
    D::Item: Validateable,
{
    fn get(&self, index: usize) -> Result<D::Item> {
        let item = self.inner.get(index)?;
        self.total_samples.fetch_add(1, Ordering::Relaxed);
        
        match self.validator.validate(&item) {
            Ok(()) => Ok(item),
            Err(validation_error) => {
                self.corruption_count.fetch_add(1, Ordering::Relaxed);
                
                eprintln!("Data corruption detected at index {}: {:?}", 
                         index, validation_error);
                
                // Decide whether to return error or try to recover
                if self.should_attempt_recovery(&validation_error) {
                    self.attempt_recovery(index, item, validation_error)
                } else {
                    Err(validation_error.into())
                }
            }
        }
    }
    
    fn attempt_recovery(
        &self, 
        index: usize, 
        mut item: D::Item, 
        error: ValidationError
    ) -> Result<D::Item> {
        match error {
            ValidationError::OutOfRange { .. } => {
                // Clamp values to valid range
                item.clamp(self.validator.min_value, self.validator.max_value);
                Ok(item)
            }
            ValidationError::InvalidShape { .. } => {
                // Try to reshape or pad
                item.reshape_or_pad(&self.validator.expected_shape)
            }
            ValidationError::NaNDetected { .. } => {
                // Replace NaN with zeros
                item.replace_nan_with_zero();
                Ok(item)
            }
            _ => Err(error.into()),
        }
    }
    
    pub fn get_corruption_rate(&self) -> f64 {
        let corrupted = self.corruption_count.load(Ordering::Relaxed);
        let total = self.total_samples.load(Ordering::Relaxed);
        
        if total > 0 {
            corrupted as f64 / total as f64
        } else {
            0.0
        }
    }
}

trait Validateable {
    fn validate(&self, validator: &DataValidator) -> Result<(), ValidationError>;
    fn clamp(&mut self, min: f32, max: f32);
    fn reshape_or_pad(&mut self, target_shape: &[usize]) -> Result<Self, ValidationError>;
    fn replace_nan_with_zero(&mut self);
}

#[derive(Debug)]
enum ValidationError {
    OutOfRange { value: f32, min: f32, max: f32 },
    InvalidShape { actual: Vec<usize>, expected: Vec<usize> },
    NaNDetected { location: String },
    ChecksumMismatch { expected: u64, actual: u64 },
}
```

### Checksum Validation

```rust
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

struct ChecksumDataset<D> {
    inner: D,
    checksums: HashMap<usize, u64>,
    validation_mode: ChecksumMode,
}

enum ChecksumMode {
    Strict,    // Fail on mismatch
    Warning,   // Log warning but continue
    Disabled,  // No validation
}

impl<D: Dataset> ChecksumDataset<D>
where
    D::Item: Hash,
{
    fn compute_checksum(item: &D::Item) -> u64 {
        let mut hasher = DefaultHasher::new();
        item.hash(&mut hasher);
        hasher.finish()
    }
    
    fn validate_checksum(&self, index: usize, item: &D::Item) -> Result<()> {
        let computed = Self::compute_checksum(item);
        
        if let Some(&expected) = self.checksums.get(&index) {
            match self.validation_mode {
                ChecksumMode::Strict => {
                    if computed != expected {
                        return Err(ValidationError::ChecksumMismatch {
                            expected,
                            actual: computed,
                        }.into());
                    }
                }
                ChecksumMode::Warning => {
                    if computed != expected {
                        eprintln!("Warning: Checksum mismatch at index {}: expected {}, got {}", 
                                index, expected, computed);
                    }
                }
                ChecksumMode::Disabled => {}
            }
        }
        
        Ok(())
    }
}
```

## Debugging Tools and Techniques

### Logging and Tracing

```rust
use tracing::{debug, error, info, instrument, span, warn, Level};

#[instrument(level = "debug")]
impl<D: Dataset> Dataset for DebugDataset<D> {
    type Item = D::Item;
    
    #[instrument(skip(self), fields(index = %index))]
    fn get(&self, index: usize) -> Result<Self::Item> {
        let span = span!(Level::DEBUG, "dataset_get", index = %index);
        let _enter = span.enter();
        
        debug!("Loading item at index {}", index);
        
        let start_time = Instant::now();
        let result = self.inner.get(index);
        let elapsed = start_time.elapsed();
        
        match &result {
            Ok(_) => {
                debug!("Successfully loaded item {} in {:?}", index, elapsed);
                if elapsed > Duration::from_millis(100) {
                    warn!("Slow data loading: {}ms for index {}", elapsed.as_millis(), index);
                }
            }
            Err(e) => {
                error!("Failed to load item {}: {:?}", index, e);
            }
        }
        
        result
    }
}

// Performance tracing
struct TracingDataLoader<D> {
    inner: DataLoader<D>,
    tracer: Arc<Mutex<Tracer>>,
}

struct Tracer {
    events: Vec<TraceEvent>,
    start_time: Instant,
}

#[derive(Debug, Clone)]
struct TraceEvent {
    timestamp: Duration,
    event_type: String,
    metadata: HashMap<String, String>,
    duration: Option<Duration>,
}

impl<D: Dataset> TracingDataLoader<D> {
    fn trace_batch_load<F, R>(&self, batch_id: usize, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let start = Instant::now();
        let result = f();
        let duration = start.elapsed();
        
        let event = TraceEvent {
            timestamp: start.duration_since(self.tracer.lock().unwrap().start_time),
            event_type: "batch_load".to_string(),
            metadata: {
                let mut map = HashMap::new();
                map.insert("batch_id".to_string(), batch_id.to_string());
                map
            },
            duration: Some(duration),
        };
        
        self.tracer.lock().unwrap().events.push(event);
        result
    }
    
    pub fn export_trace(&self, path: &Path) -> Result<()> {
        let tracer = self.tracer.lock().unwrap();
        let json = serde_json::to_string_pretty(&tracer.events)?;
        std::fs::write(path, json)?;
        Ok(())
    }
}
```

### Memory Debugging

```rust
// Memory usage tracker
struct MemoryDebugger {
    allocations: HashMap<*const u8, AllocationInfo>,
    total_allocated: usize,
    peak_allocated: usize,
}

#[derive(Debug)]
struct AllocationInfo {
    size: usize,
    stack_trace: String,
    timestamp: Instant,
}

impl MemoryDebugger {
    fn track_allocation(&mut self, ptr: *const u8, size: usize) {
        let stack_trace = get_stack_trace();
        
        self.allocations.insert(ptr, AllocationInfo {
            size,
            stack_trace,
            timestamp: Instant::now(),
        });
        
        self.total_allocated += size;
        self.peak_allocated = self.peak_allocated.max(self.total_allocated);
    }
    
    fn track_deallocation(&mut self, ptr: *const u8) {
        if let Some(info) = self.allocations.remove(&ptr) {
            self.total_allocated -= info.size;
        }
    }
    
    fn find_leaks(&self) -> Vec<LeakReport> {
        let now = Instant::now();
        
        self.allocations.iter()
            .filter(|(_, info)| now.duration_since(info.timestamp) > Duration::from_secs(300))
            .map(|(ptr, info)| LeakReport {
                ptr: *ptr,
                size: info.size,
                age: now.duration_since(info.timestamp),
                stack_trace: info.stack_trace.clone(),
            })
            .collect()
    }
}

#[derive(Debug)]
struct LeakReport {
    ptr: *const u8,
    size: usize,
    age: Duration,
    stack_trace: String,
}

fn get_stack_trace() -> String {
    // Implementation would use backtrace crate
    "stack trace placeholder".to_string()
}
```

### Integration Testing

```rust
// Comprehensive integration test
#[cfg(test)]
mod integration_tests {
    use super::*;
    
    #[test]
    fn test_end_to_end_data_pipeline() {
        // Create test dataset
        let dataset = create_test_dataset(1000);
        
        // Test various configurations
        let configs = vec![
            DataLoaderConfig { batch_size: 32, num_workers: 1, shuffle: false },
            DataLoaderConfig { batch_size: 64, num_workers: 4, shuffle: true },
            DataLoaderConfig { batch_size: 16, num_workers: 2, shuffle: true },
        ];
        
        for config in configs {
            test_dataloader_config(dataset.clone(), config);
        }
    }
    
    fn test_dataloader_config(dataset: TestDataset, config: DataLoaderConfig) {
        let dataloader = DataLoader::builder(dataset.clone())
            .batch_size(config.batch_size)
            .num_workers(config.num_workers)
            .shuffle(config.shuffle)
            .build();
        
        let mut total_samples = 0;
        let mut batch_count = 0;
        let mut load_times = Vec::new();
        
        for batch_result in dataloader {
            let start = Instant::now();
            let batch = batch_result.expect("Batch loading failed");
            let load_time = start.elapsed();
            
            // Validate batch
            assert!(!batch.is_empty(), "Empty batch returned");
            assert!(batch.len() <= config.batch_size, "Batch size exceeded");
            
            total_samples += batch.len();
            batch_count += 1;
            load_times.push(load_time);
            
            // Test some batches then break to avoid long test times
            if batch_count >= 10 {
                break;
            }
        }
        
        // Performance assertions
        let avg_load_time = load_times.iter().sum::<Duration>() / load_times.len() as u32;
        assert!(avg_load_time < Duration::from_millis(1000), 
               "Average load time too high: {:?}", avg_load_time);
        
        println!("Config {:?}: {} batches, {} samples, avg load time: {:?}", 
                config, batch_count, total_samples, avg_load_time);
    }
}
```

This comprehensive troubleshooting guide covers the most common issues encountered in data loading and provides practical solutions for each problem category. Use this guide to quickly diagnose and resolve issues in your ToRSh data loading pipelines.