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
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
//! Core configuration types and structures
//!
//! This module defines the main configuration structures used throughout
//! the TenfloweRS dataset library. All configurations support serialization
//! and deserialization for YAML, TOML, and JSON formats.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Global configuration containing all subsystem configurations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalConfig {
    /// Dataset-related configuration
    pub dataset: DatasetConfig,
    /// Data loader configuration
    pub dataloader: DataLoaderConfig,
    /// Transform pipeline configuration
    pub transforms: TransformConfig,
    /// Performance and optimization settings
    pub performance: PerformanceConfig,
    /// Caching configuration
    pub cache: CacheConfig,
    /// GPU acceleration settings
    pub gpu: GpuConfig,
    /// Audio processing configuration
    pub audio: AudioConfig,
    /// Format-specific configurations
    pub formats: FormatConfig,
    /// Logging and monitoring configuration
    pub logging: LoggingConfig,
}

impl Default for GlobalConfig {
    fn default() -> Self {
        Self {
            dataset: DatasetConfig::default(),
            dataloader: DataLoaderConfig::default(),
            transforms: TransformConfig::default(),
            performance: PerformanceConfig::default(),
            cache: CacheConfig::default(),
            gpu: GpuConfig::default(),
            audio: AudioConfig::default(),
            formats: FormatConfig::default(),
            logging: LoggingConfig::default(),
        }
    }
}

impl GlobalConfig {
    /// Merge another configuration into this one
    pub fn merge(&mut self, other: GlobalConfig) -> crate::Result<()> {
        self.dataset.merge(other.dataset);
        self.dataloader.merge(other.dataloader);
        self.transforms.merge(other.transforms);
        self.performance.merge(other.performance);
        self.cache.merge(other.cache);
        self.gpu.merge(other.gpu);
        self.audio.merge(other.audio);
        self.formats.merge(other.formats);
        self.logging.merge(other.logging);
        Ok(())
    }
}

/// Dataset configuration settings
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DatasetConfig {
    /// Default batch size for data loading
    pub batch_size: usize,
    /// Whether to shuffle data by default
    pub shuffle: bool,
    /// Random seed for reproducibility
    pub seed: Option<u64>,
    /// Default data root directory
    pub data_root: Option<PathBuf>,
    /// Maximum dataset size (for limiting large datasets)
    pub max_size: Option<usize>,
    /// Whether to pin memory for GPU transfers
    pub pin_memory: bool,
    /// Drop last incomplete batch
    pub drop_last: bool,
}

impl Default for DatasetConfig {
    fn default() -> Self {
        Self {
            batch_size: 32,
            shuffle: true,
            seed: None,
            data_root: None,
            max_size: None,
            pin_memory: false,
            drop_last: false,
        }
    }
}

impl DatasetConfig {
    fn merge(&mut self, other: DatasetConfig) {
        if other.batch_size != Self::default().batch_size {
            self.batch_size = other.batch_size;
        }
        if other.shuffle != Self::default().shuffle {
            self.shuffle = other.shuffle;
        }
        if other.seed.is_some() {
            self.seed = other.seed;
        }
        if other.data_root.is_some() {
            self.data_root = other.data_root;
        }
        if other.max_size.is_some() {
            self.max_size = other.max_size;
        }
        if other.pin_memory != Self::default().pin_memory {
            self.pin_memory = other.pin_memory;
        }
        if other.drop_last != Self::default().drop_last {
            self.drop_last = other.drop_last;
        }
    }
}

/// Data loader configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DataLoaderConfig {
    /// Number of worker threads
    pub num_workers: usize,
    /// Prefetch factor for async loading
    pub prefetch_factor: usize,
    /// Enable distributed loading
    pub distributed: bool,
    /// NUMA node preferences
    pub numa_nodes: Option<Vec<usize>>,
    /// Timeout for data loading operations (seconds)
    pub timeout: Option<u64>,
    /// Enable work stealing between workers
    pub work_stealing: bool,
}

impl Default for DataLoaderConfig {
    fn default() -> Self {
        Self {
            num_workers: num_cpus::get(),
            prefetch_factor: 2,
            distributed: false,
            numa_nodes: None,
            timeout: Some(300), // 5 minutes
            work_stealing: true,
        }
    }
}

impl DataLoaderConfig {
    fn merge(&mut self, other: DataLoaderConfig) {
        if other.num_workers != Self::default().num_workers {
            self.num_workers = other.num_workers;
        }
        if other.prefetch_factor != Self::default().prefetch_factor {
            self.prefetch_factor = other.prefetch_factor;
        }
        if other.distributed != Self::default().distributed {
            self.distributed = other.distributed;
        }
        if other.numa_nodes.is_some() {
            self.numa_nodes = other.numa_nodes;
        }
        if other.timeout.is_some() {
            self.timeout = other.timeout;
        }
        if other.work_stealing != Self::default().work_stealing {
            self.work_stealing = other.work_stealing;
        }
    }
}

/// Transform pipeline configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct TransformConfig {
    /// Enable CPU SIMD acceleration
    pub enable_simd: bool,
    /// Enable GPU acceleration
    pub enable_gpu: bool,
    /// Default image resize strategy
    pub default_resize_strategy: String,
    /// Augmentation probability
    pub augmentation_probability: f32,
    /// Random transform parameters
    pub random_params: HashMap<String, f32>,
    /// Transform pipeline stages
    pub pipeline_stages: Vec<String>,
}

impl Default for TransformConfig {
    fn default() -> Self {
        Self {
            enable_simd: true,
            enable_gpu: false,
            default_resize_strategy: "bilinear".to_string(),
            augmentation_probability: 0.5,
            random_params: HashMap::new(),
            pipeline_stages: vec!["normalize".to_string(), "resize".to_string()],
        }
    }
}

impl TransformConfig {
    fn merge(&mut self, other: TransformConfig) {
        if other.enable_simd != Self::default().enable_simd {
            self.enable_simd = other.enable_simd;
        }
        if other.enable_gpu != Self::default().enable_gpu {
            self.enable_gpu = other.enable_gpu;
        }
        if other.default_resize_strategy != Self::default().default_resize_strategy {
            self.default_resize_strategy = other.default_resize_strategy;
        }
        if other.augmentation_probability != Self::default().augmentation_probability {
            self.augmentation_probability = other.augmentation_probability;
        }
        self.random_params.extend(other.random_params);
        if !other.pipeline_stages.is_empty() {
            self.pipeline_stages = other.pipeline_stages;
        }
    }
}

/// Performance and optimization settings
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PerformanceConfig {
    /// Number of threads for parallel operations
    pub num_threads: usize,
    /// Enable memory mapping for large files
    pub enable_mmap: bool,
    /// Memory pool size (MB)
    pub memory_pool_size: usize,
    /// Enable zero-copy operations
    pub enable_zero_copy: bool,
    /// Async I/O configuration
    pub async_io: AsyncIoConfig,
    /// Performance monitoring settings
    pub monitoring: MonitoringConfig,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            num_threads: num_cpus::get(),
            enable_mmap: true,
            memory_pool_size: 1024, // 1GB
            enable_zero_copy: true,
            async_io: AsyncIoConfig::default(),
            monitoring: MonitoringConfig::default(),
        }
    }
}

impl PerformanceConfig {
    fn merge(&mut self, other: PerformanceConfig) {
        if other.num_threads != Self::default().num_threads {
            self.num_threads = other.num_threads;
        }
        if other.enable_mmap != Self::default().enable_mmap {
            self.enable_mmap = other.enable_mmap;
        }
        if other.memory_pool_size != Self::default().memory_pool_size {
            self.memory_pool_size = other.memory_pool_size;
        }
        if other.enable_zero_copy != Self::default().enable_zero_copy {
            self.enable_zero_copy = other.enable_zero_copy;
        }
        self.async_io.merge(other.async_io);
        self.monitoring.merge(other.monitoring);
    }
}

/// Async I/O configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AsyncIoConfig {
    /// Enable async I/O operations
    pub enabled: bool,
    /// Number of async I/O threads
    pub io_threads: usize,
    /// Buffer size for async operations
    pub buffer_size: usize,
    /// Queue depth for async operations
    pub queue_depth: usize,
}

impl Default for AsyncIoConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            io_threads: 4,
            buffer_size: 64 * 1024, // 64KB
            queue_depth: 32,
        }
    }
}

impl AsyncIoConfig {
    fn merge(&mut self, other: AsyncIoConfig) {
        if other.enabled != Self::default().enabled {
            self.enabled = other.enabled;
        }
        if other.io_threads != Self::default().io_threads {
            self.io_threads = other.io_threads;
        }
        if other.buffer_size != Self::default().buffer_size {
            self.buffer_size = other.buffer_size;
        }
        if other.queue_depth != Self::default().queue_depth {
            self.queue_depth = other.queue_depth;
        }
    }
}

/// Performance monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct MonitoringConfig {
    /// Enable performance monitoring
    pub enabled: bool,
    /// Monitoring interval (seconds)
    pub interval: u64,
    /// Enable memory usage tracking
    pub track_memory: bool,
    /// Enable throughput tracking
    pub track_throughput: bool,
    /// Enable latency tracking
    pub track_latency: bool,
}

impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            interval: 30,
            track_memory: true,
            track_throughput: true,
            track_latency: true,
        }
    }
}

impl MonitoringConfig {
    fn merge(&mut self, other: MonitoringConfig) {
        if other.enabled != Self::default().enabled {
            self.enabled = other.enabled;
        }
        if other.interval != Self::default().interval {
            self.interval = other.interval;
        }
        if other.track_memory != Self::default().track_memory {
            self.track_memory = other.track_memory;
        }
        if other.track_throughput != Self::default().track_throughput {
            self.track_throughput = other.track_throughput;
        }
        if other.track_latency != Self::default().track_latency {
            self.track_latency = other.track_latency;
        }
    }
}

/// Caching configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CacheConfig {
    /// Enable caching
    pub enabled: bool,
    /// Cache size in MB
    pub size_mb: usize,
    /// Cache eviction policy
    pub eviction_policy: String,
    /// Enable persistent cache
    pub persistent: bool,
    /// Cache directory
    pub cache_dir: Option<PathBuf>,
    /// Enable predictive prefetching
    pub predictive_prefetch: bool,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            size_mb: 512,
            eviction_policy: "lru".to_string(),
            persistent: false,
            cache_dir: None,
            predictive_prefetch: false,
        }
    }
}

impl CacheConfig {
    fn merge(&mut self, other: CacheConfig) {
        if other.enabled != Self::default().enabled {
            self.enabled = other.enabled;
        }
        if other.size_mb != Self::default().size_mb {
            self.size_mb = other.size_mb;
        }
        if other.eviction_policy != Self::default().eviction_policy {
            self.eviction_policy = other.eviction_policy;
        }
        if other.persistent != Self::default().persistent {
            self.persistent = other.persistent;
        }
        if other.cache_dir.is_some() {
            self.cache_dir = other.cache_dir;
        }
        if other.predictive_prefetch != Self::default().predictive_prefetch {
            self.predictive_prefetch = other.predictive_prefetch;
        }
    }
}

/// GPU configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct GpuConfig {
    /// Enable GPU acceleration
    pub enabled: bool,
    /// Preferred GPU device ID
    pub device_id: Option<usize>,
    /// GPU memory pool size (MB)
    pub memory_pool_mb: usize,
    /// Enable GPU-CPU memory pinning
    pub enable_pinned_memory: bool,
    /// GPU computation precision
    pub precision: String,
}

impl Default for GpuConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            device_id: None,
            memory_pool_mb: 1024,
            enable_pinned_memory: true,
            precision: "fp32".to_string(),
        }
    }
}

impl GpuConfig {
    fn merge(&mut self, other: GpuConfig) {
        if other.enabled != Self::default().enabled {
            self.enabled = other.enabled;
        }
        if other.device_id.is_some() {
            self.device_id = other.device_id;
        }
        if other.memory_pool_mb != Self::default().memory_pool_mb {
            self.memory_pool_mb = other.memory_pool_mb;
        }
        if other.enable_pinned_memory != Self::default().enable_pinned_memory {
            self.enable_pinned_memory = other.enable_pinned_memory;
        }
        if other.precision != Self::default().precision {
            self.precision = other.precision;
        }
    }
}

/// Audio processing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AudioConfig {
    /// Default sample rate
    pub sample_rate: u32,
    /// Default number of channels
    pub channels: u16,
    /// Audio buffer size
    pub buffer_size: usize,
    /// Enable audio augmentation
    pub enable_augmentation: bool,
    /// Audio format preferences
    pub preferred_format: String,
}

impl Default for AudioConfig {
    fn default() -> Self {
        Self {
            sample_rate: 44100,
            channels: 2,
            buffer_size: 1024,
            enable_augmentation: true,
            preferred_format: "wav".to_string(),
        }
    }
}

impl AudioConfig {
    fn merge(&mut self, other: AudioConfig) {
        if other.sample_rate != Self::default().sample_rate {
            self.sample_rate = other.sample_rate;
        }
        if other.channels != Self::default().channels {
            self.channels = other.channels;
        }
        if other.buffer_size != Self::default().buffer_size {
            self.buffer_size = other.buffer_size;
        }
        if other.enable_augmentation != Self::default().enable_augmentation {
            self.enable_augmentation = other.enable_augmentation;
        }
        if other.preferred_format != Self::default().preferred_format {
            self.preferred_format = other.preferred_format;
        }
    }
}

/// Format-specific configurations
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct FormatConfig {
    /// Image format configuration
    pub image: ImageFormatConfig,
    /// Text format configuration
    pub text: TextFormatConfig,
    /// Parquet format configuration
    pub parquet: ParquetFormatConfig,
    /// HDF5 format configuration
    pub hdf5: Hdf5FormatConfig,
}

impl Default for FormatConfig {
    fn default() -> Self {
        Self {
            image: ImageFormatConfig::default(),
            text: TextFormatConfig::default(),
            parquet: ParquetFormatConfig::default(),
            hdf5: Hdf5FormatConfig::default(),
        }
    }
}

impl FormatConfig {
    fn merge(&mut self, other: FormatConfig) {
        self.image.merge(other.image);
        self.text.merge(other.text);
        self.parquet.merge(other.parquet);
        self.hdf5.merge(other.hdf5);
    }
}

/// Image format configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageFormatConfig {
    /// Default image size for resizing
    pub default_size: (u32, u32),
    /// Supported image formats
    pub supported_formats: Vec<String>,
    /// Enable lazy loading
    pub lazy_loading: bool,
}

impl Default for ImageFormatConfig {
    fn default() -> Self {
        Self {
            default_size: (224, 224),
            supported_formats: vec!["jpg".to_string(), "png".to_string(), "webp".to_string()],
            lazy_loading: true,
        }
    }
}

impl ImageFormatConfig {
    fn merge(&mut self, other: ImageFormatConfig) {
        if other.default_size != Self::default().default_size {
            self.default_size = other.default_size;
        }
        if !other.supported_formats.is_empty() {
            self.supported_formats = other.supported_formats;
        }
        if other.lazy_loading != Self::default().lazy_loading {
            self.lazy_loading = other.lazy_loading;
        }
    }
}

/// Text format configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextFormatConfig {
    /// Default encoding
    pub encoding: String,
    /// Maximum line length
    pub max_line_length: Option<usize>,
    /// Enable tokenization caching
    pub cache_tokenization: bool,
}

impl Default for TextFormatConfig {
    fn default() -> Self {
        Self {
            encoding: "utf-8".to_string(),
            max_line_length: Some(1024),
            cache_tokenization: true,
        }
    }
}

impl TextFormatConfig {
    fn merge(&mut self, other: TextFormatConfig) {
        if other.encoding != Self::default().encoding {
            self.encoding = other.encoding;
        }
        if other.max_line_length.is_some() {
            self.max_line_length = other.max_line_length;
        }
        if other.cache_tokenization != Self::default().cache_tokenization {
            self.cache_tokenization = other.cache_tokenization;
        }
    }
}

/// Parquet format configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParquetFormatConfig {
    /// Batch size for reading
    pub batch_size: usize,
    /// Enable predicate pushdown
    pub predicate_pushdown: bool,
    /// Enable column pruning
    pub column_pruning: bool,
}

impl Default for ParquetFormatConfig {
    fn default() -> Self {
        Self {
            batch_size: 1024,
            predicate_pushdown: true,
            column_pruning: true,
        }
    }
}

impl ParquetFormatConfig {
    fn merge(&mut self, other: ParquetFormatConfig) {
        if other.batch_size != Self::default().batch_size {
            self.batch_size = other.batch_size;
        }
        if other.predicate_pushdown != Self::default().predicate_pushdown {
            self.predicate_pushdown = other.predicate_pushdown;
        }
        if other.column_pruning != Self::default().column_pruning {
            self.column_pruning = other.column_pruning;
        }
    }
}

/// HDF5 format configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Hdf5FormatConfig {
    /// Chunk cache size
    pub chunk_cache_size: usize,
    /// Enable parallel I/O
    pub parallel_io: bool,
    /// Compression level
    pub compression_level: Option<u32>,
}

impl Default for Hdf5FormatConfig {
    fn default() -> Self {
        Self {
            chunk_cache_size: 1024 * 1024, // 1MB
            parallel_io: true,
            compression_level: Some(6),
        }
    }
}

impl Hdf5FormatConfig {
    fn merge(&mut self, other: Hdf5FormatConfig) {
        if other.chunk_cache_size != Self::default().chunk_cache_size {
            self.chunk_cache_size = other.chunk_cache_size;
        }
        if other.parallel_io != Self::default().parallel_io {
            self.parallel_io = other.parallel_io;
        }
        if other.compression_level.is_some() {
            self.compression_level = other.compression_level;
        }
    }
}

/// Logging and monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct LoggingConfig {
    /// Log level
    pub level: String,
    /// Log format
    pub format: String,
    /// Enable file logging
    pub file_logging: bool,
    /// Log file path
    pub log_file: Option<PathBuf>,
    /// Enable metrics collection
    pub collect_metrics: bool,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
            format: "json".to_string(),
            file_logging: false,
            log_file: None,
            collect_metrics: false,
        }
    }
}

impl LoggingConfig {
    fn merge(&mut self, other: LoggingConfig) {
        if other.level != Self::default().level {
            self.level = other.level;
        }
        if other.format != Self::default().format {
            self.format = other.format;
        }
        if other.file_logging != Self::default().file_logging {
            self.file_logging = other.file_logging;
        }
        if other.log_file.is_some() {
            self.log_file = other.log_file;
        }
        if other.collect_metrics != Self::default().collect_metrics {
            self.collect_metrics = other.collect_metrics;
        }
    }
}