torsh-data 0.1.3

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
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# Best Practices for Data Loading in ToRSh

This guide provides comprehensive best practices for efficient and effective data loading in the ToRSh deep learning framework.

## Table of Contents

1. [General Principles]#general-principles
2. [Dataset Design]#dataset-design
3. [DataLoader Configuration]#dataloader-configuration
4. [Transform Optimization]#transform-optimization
5. [Memory Management]#memory-management
6. [Performance Optimization]#performance-optimization
7. [Error Handling and Debugging]#error-handling-and-debugging
8. [Security Considerations]#security-considerations
9. [Testing and Validation]#testing-and-validation
10. [Production Deployment]#production-deployment

## General Principles

### 1. Data Pipeline Design

**Design for Scalability**
```rust
// Good: Separates data loading, transformation, and batching concerns
let dataset = MyDataset::new(data_path)?;
let transform = MyTransformPipeline::new();
let dataloader = DataLoader::builder(dataset)
    .batch_size(32)
    .shuffle(true)
    .num_workers(4)
    .transform(transform)
    .build();

// Avoid: Tightly coupled data loading and processing
let dataloader = MonolithicDataLoader::new(data_path, batch_size, transform_config);
```

**Lazy Loading Principle**
```rust
// Good: Load data only when needed
#[derive(Clone)]
pub struct LazyImageDataset {
    image_paths: Vec<PathBuf>,
    transform: Option<ImageTransform>,
}

impl Dataset for LazyImageDataset {
    type Item = Tensor<f32>;
    
    fn get(&self, index: usize) -> Result<Self::Item> {
        // Load image only when requested
        let image = load_image(&self.image_paths[index])?;
        let tensor = image_to_tensor(image)?;
        
        if let Some(ref transform) = self.transform {
            transform.transform(tensor)
        } else {
            Ok(tensor)
        }
    }
}

// Avoid: Loading all data upfront
pub struct EagerImageDataset {
    images: Vec<Tensor<f32>>, // Memory intensive!
}
```

### 2. Thread Safety

**Always Implement Send + Sync**
```rust
// Good: Thread-safe dataset
#[derive(Clone)]
pub struct ThreadSafeDataset {
    data: Arc<Vec<DataItem>>,
    metadata: Arc<Metadata>,
}

unsafe impl Send for ThreadSafeDataset {}
unsafe impl Sync for ThreadSafeDataset {}

// Good: Use Arc for shared immutable data
#[derive(Clone)]
pub struct SharedDataset {
    config: Arc<DatasetConfig>,
    cache: Arc<RwLock<HashMap<usize, CachedItem>>>,
}
```

**Avoid Shared Mutable State**
```rust
// Avoid: Shared mutable state without proper synchronization
pub struct BadDataset {
    counter: usize, // Not thread-safe!
}

// Better: Use atomic operations for shared counters
use std::sync::atomic::{AtomicUsize, Ordering};

pub struct GoodDataset {
    access_counter: Arc<AtomicUsize>,
}

impl Dataset for GoodDataset {
    fn get(&self, index: usize) -> Result<Self::Item> {
        self.access_counter.fetch_add(1, Ordering::Relaxed);
        // ... rest of implementation
    }
}
```

## Dataset Design

### 1. Efficient Data Storage

**Use Memory Mapping for Large Files**
```rust
use memmap2::MmapOptions;

#[derive(Clone)]
pub struct MmapDataset {
    mmap: Arc<memmap2::Mmap>,
    item_size: usize,
    num_items: usize,
}

impl MmapDataset {
    pub fn new<P: AsRef<Path>>(path: P, item_size: usize) -> Result<Self> {
        let file = File::open(path)?;
        let mmap = unsafe { MmapOptions::new().map(&file)? };
        let num_items = mmap.len() / item_size;
        
        Ok(Self {
            mmap: Arc::new(mmap),
            item_size,
            num_items,
        })
    }
}

impl Dataset for MmapDataset {
    type Item = &[u8];
    
    fn len(&self) -> usize {
        self.num_items
    }
    
    fn get(&self, index: usize) -> Result<Self::Item> {
        let start = index * self.item_size;
        let end = start + self.item_size;
        Ok(&self.mmap[start..end])
    }
}
```

**Implement Hierarchical Data Access**
```rust
// Good: Hierarchical dataset structure
pub struct HierarchicalDataset {
    metadata_index: HashMap<String, Vec<usize>>,
    data_chunks: Vec<DataChunk>,
}

impl HierarchicalDataset {
    pub fn get_by_category(&self, category: &str) -> Result<CategoryDataset> {
        let indices = self.metadata_index.get(category)
            .ok_or_else(|| DataError::CategoryNotFound(category.to_string()))?;
        
        Ok(CategoryDataset {
            parent: self,
            indices: indices.clone(),
        })
    }
}
```

### 2. Robust Data Validation

**Validate Data at Creation Time**
```rust
impl TensorDataset<f32> {
    pub fn new(tensors: Vec<Tensor<f32>>) -> Result<Self> {
        // Validate all tensors have compatible shapes
        if tensors.is_empty() {
            return Err(DataError::EmptyDataset);
        }
        
        let first_shape = tensors[0].shape();
        let expected_batch_dim = first_shape.dims()[0];
        
        for (i, tensor) in tensors.iter().enumerate() {
            let shape = tensor.shape();
            if shape.dims()[0] != expected_batch_dim {
                return Err(DataError::InvalidShape {
                    tensor_index: i,
                    expected: expected_batch_dim,
                    actual: shape.dims()[0],
                });
            }
        }
        
        Ok(Self { tensors })
    }
}
```

**Implement Data Integrity Checks**
```rust
pub trait DataIntegrity {
    fn verify_integrity(&self) -> Result<()>;
    fn repair_if_possible(&mut self) -> Result<bool>;
}

impl DataIntegrity for FileDataset {
    fn verify_integrity(&self) -> Result<()> {
        for (i, path) in self.file_paths.iter().enumerate() {
            if !path.exists() {
                return Err(DataError::MissingFile {
                    index: i,
                    path: path.clone(),
                });
            }
            
            // Check file size, format, etc.
            let metadata = std::fs::metadata(path)?;
            if metadata.len() == 0 {
                return Err(DataError::CorruptedFile {
                    index: i,
                    path: path.clone(),
                    reason: "Empty file".to_string(),
                });
            }
        }
        Ok(())
    }
    
    fn repair_if_possible(&mut self) -> Result<bool> {
        let mut repaired = false;
        self.file_paths.retain(|path| {
            if !path.exists() {
                eprintln!("Warning: Removing missing file: {:?}", path);
                repaired = true;
                false
            } else {
                true
            }
        });
        Ok(repaired)
    }
}
```

### 3. Flexible Dataset Composition

**Use Composition over Inheritance**
```rust
// Good: Compositional design
pub struct CompositeDataset<D1, D2> {
    dataset1: D1,
    dataset2: D2,
    combine_fn: fn(D1::Item, D2::Item) -> CombinedItem,
}

impl<D1: Dataset, D2: Dataset> Dataset for CompositeDataset<D1, D2> {
    type Item = CombinedItem;
    
    fn len(&self) -> usize {
        std::cmp::min(self.dataset1.len(), self.dataset2.len())
    }
    
    fn get(&self, index: usize) -> Result<Self::Item> {
        let item1 = self.dataset1.get(index)?;
        let item2 = self.dataset2.get(index)?;
        Ok((self.combine_fn)(item1, item2))
    }
}

// Usage
let features = FeatureDataset::new(feature_data);
let labels = LabelDataset::new(label_data);
let combined = CompositeDataset::new(features, labels, |f, l| (f, l));
```

## DataLoader Configuration

### 1. Optimal Batch Size Selection

**Consider Memory and Computation Trade-offs**
```rust
pub struct BatchSizeOptimizer {
    available_memory: usize,
    item_size_estimate: usize,
    target_memory_usage: f32, // 0.8 = 80% of available memory
}

impl BatchSizeOptimizer {
    pub fn recommend_batch_size(&self, dataset_len: usize) -> usize {
        let memory_based = (self.available_memory as f32 * self.target_memory_usage) 
            / self.item_size_estimate as f32;
        
        let power_of_2 = (memory_based.log2().floor() as u32).min(10); // Cap at 1024
        let recommended = 2_usize.pow(power_of_2);
        
        // Ensure we don't exceed dataset size
        std::cmp::min(recommended, dataset_len)
    }
}

// Usage
let optimizer = BatchSizeOptimizer {
    available_memory: get_available_memory(),
    item_size_estimate: estimate_item_size(&dataset),
    target_memory_usage: 0.7,
};

let batch_size = optimizer.recommend_batch_size(dataset.len());
```

### 2. Worker Configuration

**Determine Optimal Worker Count**
```rust
pub fn optimal_worker_count() -> usize {
    let cpu_count = num_cpus::get();
    let available_memory = get_available_memory();
    
    // Rule of thumb: 1-2 workers per CPU core, limited by memory
    let cpu_based = (cpu_count as f32 * 1.5) as usize;
    let memory_based = available_memory / (512 * 1024 * 1024); // 512MB per worker
    
    std::cmp::max(1, std::cmp::min(cpu_based, memory_based))
}

// Configure DataLoader with optimal settings
let dataloader = DataLoader::builder(dataset)
    .batch_size(32)
    .num_workers(optimal_worker_count())
    .pin_memory(true) // If using GPU
    .drop_last(true)  // For consistent batch sizes
    .build();
```

### 3. Sampling Strategies

**Choose Appropriate Sampling for Your Use Case**
```rust
// For balanced training
let balanced_sampler = WeightedRandomSampler::new(
    compute_class_weights(&dataset)?,
    dataset.len(),
    true, // replacement
)?;

// For distributed training
let distributed_sampler = sampler.into_distributed(
    world_size,
    rank,
);

// For curriculum learning
let curriculum_sampler = CurriculumSampler::new(
    dataset.len(),
    difficulty_scores,
    initial_easy_ratio: 0.8,
);
```

## Transform Optimization

### 1. Transform Pipeline Design

**Order Transforms by Computational Cost**
```rust
// Good: Cheap operations first, expensive operations last
let transform_pipeline = TransformPipeline::new()
    .add(ValidateInput)        // Fast validation
    .add(Normalize::new(mean, std))  // Simple arithmetic
    .add(Resize::new(224, 224))      // Moderate cost
    .add(RandomAugmentation::new())  // Most expensive
    .build();

// Avoid: Expensive operations early in pipeline
let bad_pipeline = TransformPipeline::new()
    .add(ExpensiveAugmentation::new())  // Wasteful if validation fails
    .add(ValidateInput)
    .add(SimpleNormalize)
    .build();
```

**Use Conditional Transforms Wisely**
```rust
// Good: Skip expensive operations when not needed
let conditional_augmentation = ExpensiveAugmentation::new()
    .when(|item| item.requires_augmentation())
    .with_probability(0.5); // Only apply 50% of the time

// Cache expensive computations
let cached_transform = CacheTransform::new(
    ExpensiveTransform::new(),
    |item| item.compute_cache_key(), // Key function
);
```

### 2. Memory-Efficient Transforms

**Prefer In-Place Operations**
```rust
// Good: In-place normalization
pub struct InPlaceNormalize {
    mean: f32,
    std: f32,
}

impl Transform<&mut Tensor<f32>> for InPlaceNormalize {
    type Output = ();
    
    fn transform(&self, tensor: &mut Tensor<f32>) -> Result<()> {
        // Modify tensor in-place to avoid allocation
        tensor.sub_scalar_(self.mean)?;
        tensor.div_scalar_(self.std)?;
        Ok(())
    }
}

// Use copy-on-write semantics when appropriate
pub struct CowTransform<T> {
    inner: T,
}

impl<T: Transform<Tensor<f32>, Output = Tensor<f32>>> Transform<Tensor<f32>> for CowTransform<T> {
    type Output = Tensor<f32>;
    
    fn transform(&self, input: Tensor<f32>) -> Result<Self::Output> {
        // Try in-place operation first
        if input.ref_count() == 1 {
            // Safe to modify in-place
            let mut tensor = input;
            self.inner.transform_inplace(&mut tensor)?;
            Ok(tensor)
        } else {
            // Need to copy
            let copied = input.clone();
            self.inner.transform(copied)
        }
    }
}
```

## Memory Management

### 1. Memory Pool Usage

**Implement Memory Pools for Frequent Allocations**
```rust
use std::sync::Mutex;

pub struct TensorPool {
    pools: Vec<Mutex<Vec<Tensor<f32>>>>,
    max_pool_size: usize,
}

impl TensorPool {
    pub fn new(max_pool_size: usize) -> Self {
        let num_sizes = 10; // Support different tensor sizes
        let pools = (0..num_sizes)
            .map(|_| Mutex::new(Vec::new()))
            .collect();
        
        Self { pools, max_pool_size }
    }
    
    pub fn get_tensor(&self, shape: &[usize]) -> Tensor<f32> {
        let pool_idx = self.shape_to_pool_index(shape);
        
        if let Ok(mut pool) = self.pools[pool_idx].try_lock() {
            if let Some(tensor) = pool.pop() {
                // Reuse existing tensor
                tensor.resize_(shape).unwrap();
                return tensor;
            }
        }
        
        // Create new tensor
        Tensor::zeros(shape).unwrap()
    }
    
    pub fn return_tensor(&self, tensor: Tensor<f32>) {
        let shape = tensor.shape().dims();
        let pool_idx = self.shape_to_pool_index(shape);
        
        if let Ok(mut pool) = self.pools[pool_idx].try_lock() {
            if pool.len() < self.max_pool_size {
                pool.push(tensor);
            }
        }
        // Otherwise, let tensor drop naturally
    }
}

// Global tensor pool
lazy_static::lazy_static! {
    static ref TENSOR_POOL: TensorPool = TensorPool::new(100);
}
```

### 2. Memory Usage Monitoring

**Track Memory Usage**
```rust
pub struct MemoryTracker {
    peak_usage: AtomicUsize,
    current_usage: AtomicUsize,
    allocation_count: AtomicUsize,
}

impl MemoryTracker {
    pub fn track_allocation(&self, size: usize) {
        let new_usage = self.current_usage.fetch_add(size, Ordering::Relaxed) + size;
        
        // Update peak usage
        let mut peak = self.peak_usage.load(Ordering::Relaxed);
        while new_usage > peak {
            match self.peak_usage.compare_exchange_weak(
                peak,
                new_usage,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(x) => peak = x,
            }
        }
        
        self.allocation_count.fetch_add(1, Ordering::Relaxed);
    }
    
    pub fn track_deallocation(&self, size: usize) {
        self.current_usage.fetch_sub(size, Ordering::Relaxed);
    }
    
    pub fn get_stats(&self) -> MemoryStats {
        MemoryStats {
            current_usage: self.current_usage.load(Ordering::Relaxed),
            peak_usage: self.peak_usage.load(Ordering::Relaxed),
            allocation_count: self.allocation_count.load(Ordering::Relaxed),
        }
    }
}
```

## Performance Optimization

### 1. CPU Optimization

**Use SIMD When Possible**
```rust
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

pub fn vectorized_normalize(data: &mut [f32], mean: f32, std: f32) {
    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("avx2") {
            unsafe {
                vectorized_normalize_avx2(data, mean, std);
                return;
            }
        }
    }
    
    // Fallback scalar implementation
    for value in data.iter_mut() {
        *value = (*value - mean) / std;
    }
}

#[cfg(target_arch = "x86_64")]
unsafe fn vectorized_normalize_avx2(data: &mut [f32], mean: f32, std: f32) {
    let mean_vec = _mm256_set1_ps(mean);
    let std_vec = _mm256_set1_ps(std);
    
    let chunks = data.chunks_exact_mut(8);
    let remainder = chunks.remainder();
    
    for chunk in chunks {
        let values = _mm256_loadu_ps(chunk.as_ptr());
        let normalized = _mm256_div_ps(
            _mm256_sub_ps(values, mean_vec),
            std_vec
        );
        _mm256_storeu_ps(chunk.as_mut_ptr(), normalized);
    }
    
    // Handle remainder with scalar code
    for value in remainder {
        *value = (*value - mean) / std;
    }
}
```

**Leverage Parallelism**
```rust
use rayon::prelude::*;

impl Transform<Vec<f32>> for ParallelNormalize {
    type Output = Vec<f32>;
    
    fn transform(&self, input: Vec<f32>) -> Result<Self::Output> {
        Ok(input
            .into_par_iter()
            .map(|x| (x - self.mean) / self.std)
            .collect())
    }
    
    fn transform_batch(&self, inputs: Vec<Vec<f32>>) -> Result<Vec<Self::Output>> {
        Ok(inputs
            .into_par_iter()
            .map(|input| self.transform(input).unwrap())
            .collect())
    }
}
```

### 2. I/O Optimization

**Use Asynchronous I/O**
```rust
use tokio::fs::File;
use tokio::io::AsyncReadExt;

#[derive(Clone)]
pub struct AsyncFileDataset {
    file_paths: Arc<Vec<PathBuf>>,
    chunk_size: usize,
}

impl AsyncFileDataset {
    pub async fn get_async(&self, index: usize) -> Result<Vec<u8>> {
        let path = &self.file_paths[index];
        let mut file = File::open(path).await?;
        
        let metadata = file.metadata().await?;
        let file_size = metadata.len() as usize;
        
        let mut buffer = Vec::with_capacity(file_size);
        file.read_to_end(&mut buffer).await?;
        
        Ok(buffer)
    }
}

// Prefetch data asynchronously
pub struct PrefetchingDataset<D> {
    inner: D,
    prefetch_buffer: Arc<Mutex<VecDeque<(usize, D::Item)>>>,
    buffer_size: usize,
}

impl<D: Dataset + Send + Sync + 'static> PrefetchingDataset<D>
where
    D::Item: Send + 'static,
{
    pub fn new(inner: D, buffer_size: usize) -> Self {
        let dataset = Self {
            inner,
            prefetch_buffer: Arc::new(Mutex::new(VecDeque::new())),
            buffer_size,
        };
        
        // Start prefetching thread
        dataset.start_prefetching();
        dataset
    }
    
    fn start_prefetching(&self) {
        let inner = Arc::new(self.inner.clone());
        let buffer = self.prefetch_buffer.clone();
        let buffer_size = self.buffer_size;
        
        tokio::spawn(async move {
            for i in 0..inner.len() {
                // Check if buffer is full
                {
                    let buf = buffer.lock().unwrap();
                    if buf.len() >= buffer_size {
                        // Wait for space
                        drop(buf);
                        tokio::time::sleep(Duration::from_millis(10)).await;
                        continue;
                    }
                }
                
                // Load item asynchronously
                if let Ok(item) = inner.get(i) {
                    let mut buf = buffer.lock().unwrap();
                    buf.push_back((i, item));
                }
            }
        });
    }
}
```

### 3. GPU Optimization

**Efficient GPU Memory Transfer**
```rust
use torsh_core::device::DeviceType;

pub struct GPUOptimizedDataLoader<D> {
    cpu_dataloader: DataLoader<D>,
    device: DeviceType,
    pin_memory: bool,
    prefetch_factor: usize,
}

impl<D: Dataset> GPUOptimizedDataLoader<D>
where
    D::Item: Send + 'static,
{
    pub fn new(dataloader: DataLoader<D>, device: DeviceType) -> Self {
        Self {
            cpu_dataloader: dataloader,
            device,
            pin_memory: true,
            prefetch_factor: 2,
        }
    }
    
    pub fn iter_gpu(&self) -> GPUDataLoaderIterator<D> {
        GPUDataLoaderIterator::new(
            self.cpu_dataloader.iter(),
            self.device.clone(),
            self.prefetch_factor,
        )
    }
}

pub struct GPUDataLoaderIterator<D: Dataset> {
    cpu_iter: DataLoaderIterator<D>,
    device: DeviceType,
    transfer_queue: VecDeque<Tensor<f32>>,
    prefetch_factor: usize,
}

impl<D: Dataset> Iterator for GPUDataLoaderIterator<D> {
    type Item = Result<Tensor<f32>>;
    
    fn next(&mut self) -> Option<Self::Item> {
        // Start async GPU transfer for future batches
        while self.transfer_queue.len() < self.prefetch_factor {
            if let Some(cpu_batch) = self.cpu_iter.next() {
                match cpu_batch {
                    Ok(batch) => {
                        // Asynchronously transfer to GPU
                        let gpu_batch = batch.to_device(&self.device);
                        self.transfer_queue.push_back(gpu_batch);
                    }
                    Err(e) => return Some(Err(e)),
                }
            } else {
                break;
            }
        }
        
        self.transfer_queue.pop_front().map(Ok)
    }
}
```

## Error Handling and Debugging

### 1. Comprehensive Error Types

**Define Rich Error Types**
```rust
#[derive(Debug, thiserror::Error)]
pub enum DataError {
    #[error("Dataset is empty")]
    EmptyDataset,
    
    #[error("Index {index} out of bounds (size: {size})")]
    IndexOutOfBounds { index: usize, size: usize },
    
    #[error("Invalid tensor shape at index {tensor_index}: expected {expected}, got {actual}")]
    InvalidShape {
        tensor_index: usize,
        expected: usize,
        actual: usize,
    },
    
    #[error("File not found: {path}")]
    FileNotFound { path: PathBuf },
    
    #[error("Corrupted data at index {index}: {reason}")]
    CorruptedData { index: usize, reason: String },
    
    #[error("Transform failed: {transform_name} at step {step}: {reason}")]
    TransformFailed {
        transform_name: String,
        step: usize,
        reason: String,
    },
    
    #[error("Memory allocation failed: requested {size} bytes")]
    MemoryAllocationFailed { size: usize },
    
    #[error("Timeout while loading data: {timeout_ms}ms")]
    Timeout { timeout_ms: u64 },
    
    #[error("Worker thread panicked: {worker_id}")]
    WorkerPanic { worker_id: usize },
}

impl DataError {
    pub fn severity(&self) -> ErrorSeverity {
        match self {
            DataError::EmptyDataset => ErrorSeverity::Fatal,
            DataError::IndexOutOfBounds { .. } => ErrorSeverity::Error,
            DataError::CorruptedData { .. } => ErrorSeverity::Warning,
            DataError::Timeout { .. } => ErrorSeverity::Warning,
            _ => ErrorSeverity::Error,
        }
    }
    
    pub fn is_recoverable(&self) -> bool {
        matches!(
            self,
            DataError::Timeout { .. } | 
            DataError::CorruptedData { .. }
        )
    }
}
```

### 2. Debugging Utilities

**Add Debugging and Profiling Support**
```rust
pub struct DebugDataLoader<D> {
    inner: DataLoader<D>,
    debug_config: DebugConfig,
    stats: Arc<Mutex<LoadingStats>>,
}

#[derive(Debug, Clone)]
pub struct DebugConfig {
    pub log_slow_loads: bool,
    pub slow_threshold_ms: u64,
    pub sample_data: bool,
    pub track_memory: bool,
}

#[derive(Debug, Default)]
pub struct LoadingStats {
    pub total_loads: usize,
    pub slow_loads: usize,
    pub total_time_ms: u64,
    pub avg_time_ms: f64,
    pub memory_peak_mb: f64,
}

impl<D: Dataset> DebugDataLoader<D> {
    pub fn new(dataloader: DataLoader<D>, config: DebugConfig) -> Self {
        Self {
            inner: dataloader,
            debug_config: config,
            stats: Arc::new(Mutex::new(LoadingStats::default())),
        }
    }
    
    pub fn get_stats(&self) -> LoadingStats {
        self.stats.lock().unwrap().clone()
    }
}

impl<D: Dataset> Iterator for DebugDataLoader<D> {
    type Item = Result<D::Item>;
    
    fn next(&mut self) -> Option<Self::Item> {
        let start_time = std::time::Instant::now();
        let start_memory = if self.debug_config.track_memory {
            get_current_memory_usage()
        } else {
            0.0
        };
        
        let result = self.inner.next();
        
        let elapsed_ms = start_time.elapsed().as_millis() as u64;
        
        // Update statistics
        {
            let mut stats = self.stats.lock().unwrap();
            stats.total_loads += 1;
            stats.total_time_ms += elapsed_ms;
            stats.avg_time_ms = stats.total_time_ms as f64 / stats.total_loads as f64;
            
            if elapsed_ms > self.debug_config.slow_threshold_ms {
                stats.slow_loads += 1;
                if self.debug_config.log_slow_loads {
                    eprintln!("Slow data load: {}ms", elapsed_ms);
                }
            }
            
            if self.debug_config.track_memory {
                let current_memory = get_current_memory_usage();
                stats.memory_peak_mb = stats.memory_peak_mb.max(current_memory);
            }
        }
        
        result
    }
}
```

## Security Considerations

### 1. Input Validation

**Validate All External Data**
```rust
pub struct SecureDataset {
    inner: Box<dyn Dataset<Item = Vec<u8>>>,
    validator: DataValidator,
}

pub struct DataValidator {
    max_file_size: usize,
    allowed_extensions: HashSet<String>,
    virus_scanner: Option<Box<dyn VirusScanner>>,
}

impl DataValidator {
    pub fn validate_file(&self, path: &Path) -> Result<()> {
        // Check file extension
        if let Some(ext) = path.extension() {
            let ext_str = ext.to_string_lossy().to_lowercase();
            if !self.allowed_extensions.contains(&ext_str) {
                return Err(SecurityError::InvalidFileType(ext_str));
            }
        }
        
        // Check file size
        let metadata = std::fs::metadata(path)?;
        if metadata.len() as usize > self.max_file_size {
            return Err(SecurityError::FileTooLarge {
                size: metadata.len() as usize,
                max_allowed: self.max_file_size,
            });
        }
        
        // Virus scan if available
        if let Some(ref scanner) = self.virus_scanner {
            scanner.scan_file(path)?;
        }
        
        Ok(())
    }
}

impl Dataset for SecureDataset {
    type Item = Vec<u8>;
    
    fn get(&self, index: usize) -> Result<Self::Item> {
        let data = self.inner.get(index)?;
        
        // Additional runtime validation
        if data.len() > self.validator.max_file_size {
            return Err(SecurityError::DataTooLarge {
                size: data.len(),
                max_allowed: self.validator.max_file_size,
            }.into());
        }
        
        Ok(data)
    }
}
```

### 2. Sandboxing and Resource Limits

**Implement Resource Limits**
```rust
use std::time::{Duration, Instant};

pub struct ResourceLimitedDataLoader<D> {
    inner: DataLoader<D>,
    memory_limit: usize,
    time_limit: Duration,
    current_memory: Arc<AtomicUsize>,
}

impl<D: Dataset> ResourceLimitedDataLoader<D> {
    pub fn with_limits(
        dataloader: DataLoader<D>,
        memory_limit: usize,
        time_limit: Duration,
    ) -> Self {
        Self {
            inner: dataloader,
            memory_limit,
            time_limit,
            current_memory: Arc::new(AtomicUsize::new(0)),
        }
    }
}

impl<D: Dataset> Iterator for ResourceLimitedDataLoader<D> {
    type Item = Result<D::Item>;
    
    fn next(&mut self) -> Option<Self::Item> {
        let start_time = Instant::now();
        
        // Check memory limit
        let current_memory = self.current_memory.load(Ordering::Relaxed);
        if current_memory > self.memory_limit {
            return Some(Err(ResourceError::MemoryLimitExceeded {
                current: current_memory,
                limit: self.memory_limit,
            }.into()));
        }
        
        // Load with timeout
        let result = match timeout(self.time_limit, self.inner.next()) {
            Ok(Some(result)) => result,
            Ok(None) => return None,
            Err(_) => return Some(Err(ResourceError::TimeoutExceeded {
                timeout: self.time_limit,
            }.into())),
        };
        
        Some(result)
    }
}
```

## Testing and Validation

### 1. Comprehensive Test Coverage

**Test All Components**
```rust
#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    
    #[test]
    fn test_dataset_bounds_checking() {
        let dataset = create_test_dataset(100);
        
        // Valid access
        assert!(dataset.get(0).is_ok());
        assert!(dataset.get(99).is_ok());
        
        // Invalid access
        assert!(dataset.get(100).is_err());
        assert!(dataset.get(1000).is_err());
    }
    
    #[test]
    fn test_dataloader_deterministic_with_seed() {
        let dataset = create_test_dataset(100);
        
        let loader1 = DataLoader::builder(dataset.clone())
            .shuffle(true)
            .random_seed(42)
            .build();
            
        let loader2 = DataLoader::builder(dataset)
            .shuffle(true)
            .random_seed(42)
            .build();
        
        let batches1: Vec<_> = loader1.collect();
        let batches2: Vec<_> = loader2.collect();
        
        assert_eq!(batches1.len(), batches2.len());
        for (b1, b2) in batches1.iter().zip(batches2.iter()) {
            assert_tensors_equal(b1, b2);
        }
    }
    
    proptest! {
        #[test]
        fn test_transform_properties(
            data in prop::collection::vec(-1000.0f32..1000.0, 1..1000)
        ) {
            let transform = NormalizeTransform::new(0.0, 1.0);
            
            // Test that transform preserves vector length
            let original_len = data.len();
            let transformed = transform.transform(data.clone()).unwrap();
            prop_assert_eq!(transformed.len(), original_len);
            
            // Test that inverse transform works
            let denormalize = NormalizeTransform::new(0.0, 1.0).inverse();
            let roundtrip = denormalize.transform(transformed).unwrap();
            
            for (orig, rt) in data.iter().zip(roundtrip.iter()) {
                prop_assert!((orig - rt).abs() < 1e-5);
            }
        }
    }
}
```

### 2. Performance Testing

**Benchmark Critical Paths**
```rust
#[cfg(test)]
mod benchmarks {
    use super::*;
    use criterion::{criterion_group, criterion_main, Criterion};
    
    fn benchmark_dataloader_throughput(c: &mut Criterion) {
        let dataset = create_large_test_dataset(10000);
        let dataloader = DataLoader::builder(dataset)
            .batch_size(32)
            .num_workers(4)
            .build();
        
        c.bench_function("dataloader_throughput", |b| {
            b.iter(|| {
                let mut count = 0;
                for batch in dataloader.iter() {
                    count += batch.unwrap().len();
                    if count >= 1000 { break; } // Benchmark first 1000 items
                }
            });
        });
    }
    
    criterion_group!(benches, benchmark_dataloader_throughput);
    criterion_main!(benches);
}
```

## Production Deployment

### 1. Configuration Management

**Use Configuration Files**
```rust
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct DataLoaderConfig {
    pub batch_size: usize,
    pub num_workers: usize,
    pub shuffle: bool,
    pub drop_last: bool,
    pub pin_memory: bool,
    pub timeout_ms: Option<u64>,
    pub prefetch_factor: usize,
    pub memory_limit_mb: Option<usize>,
    pub cache_size_mb: Option<usize>,
}

impl Default for DataLoaderConfig {
    fn default() -> Self {
        Self {
            batch_size: 32,
            num_workers: num_cpus::get(),
            shuffle: true,
            drop_last: false,
            pin_memory: cfg!(feature = "cuda"),
            timeout_ms: Some(30000),
            prefetch_factor: 2,
            memory_limit_mb: None,
            cache_size_mb: Some(512),
        }
    }
}

pub fn create_dataloader_from_config<D: Dataset>(
    dataset: D,
    config: &DataLoaderConfig,
) -> Result<DataLoader<D>> {
    let mut builder = DataLoader::builder(dataset)
        .batch_size(config.batch_size)
        .num_workers(config.num_workers)
        .shuffle(config.shuffle)
        .drop_last(config.drop_last)
        .pin_memory(config.pin_memory);
    
    if let Some(timeout) = config.timeout_ms {
        builder = builder.timeout(Duration::from_millis(timeout));
    }
    
    Ok(builder.build())
}
```

### 2. Monitoring and Metrics

**Implement Comprehensive Monitoring**
```rust
use prometheus::{Counter, Histogram, Gauge, register_counter, register_histogram, register_gauge};

pub struct DataLoaderMetrics {
    batches_loaded: Counter,
    load_duration: Histogram,
    memory_usage: Gauge,
    error_count: Counter,
}

impl DataLoaderMetrics {
    pub fn new() -> Result<Self> {
        Ok(Self {
            batches_loaded: register_counter!(
                "dataloader_batches_total",
                "Total number of batches loaded"
            )?,
            load_duration: register_histogram!(
                "dataloader_load_duration_seconds",
                "Time spent loading batches"
            )?,
            memory_usage: register_gauge!(
                "dataloader_memory_usage_bytes",
                "Current memory usage"
            )?,
            error_count: register_counter!(
                "dataloader_errors_total",
                "Total number of loading errors"
            )?,
        })
    }
    
    pub fn record_batch_loaded(&self, duration: Duration) {
        self.batches_loaded.inc();
        self.load_duration.observe(duration.as_secs_f64());
    }
    
    pub fn record_error(&self) {
        self.error_count.inc();
    }
    
    pub fn update_memory_usage(&self, bytes: usize) {
        self.memory_usage.set(bytes as f64);
    }
}
```

This comprehensive best practices guide covers all aspects of efficient and robust data loading in the ToRSh framework. Follow these patterns to build high-performance, reliable data pipelines for your machine learning applications.