trustformers 0.1.1

TrustformeRS - Rust port of Hugging Face Transformers
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
//! # Classification Data Collators
//!
//! This module provides specialized data collators for text classification tasks.
//! It includes implementations for BERT-style models performing sentiment analysis,
//! topic classification, and other classification tasks.
//!
//! ## Overview
//!
//! Text classification involves training models to assign labels to input texts.
//! This module provides collators that prepare data appropriately for classification
//! tasks, handling label formatting and ensuring proper input representation.
//!
//! ## Architecture
//!
//! ```text
//! Classification Data Collator
//!      ├─ Handles text input sequences
//!      ├─ Manages single or multi-label classifications
//!      ├─ Creates appropriate attention masks
//!      ├─ Formats labels for loss computation
//!      └─ Supports various classification architectures
//! ```
//!
//! ## Features
//!
//! - **Label Formatting**: Converts labels to appropriate format for loss computation
//! - **Multi-label Support**: Handles both single and multi-label classification
//! - **Class Balancing**: Supports weighted loss through metadata
//! - **Sequence Processing**: Standard text preprocessing and padding
//! - **Flexible Architecture**: Works with BERT, RoBERTa, and similar models
//!
//! ## Usage Examples
//!
//! ### Sentiment Analysis
//!
//! ```rust,ignore
//! use trustformers::auto::data_collators::classification::{
//!     ClassificationDataCollator, ClassificationCollatorConfig
//! };
//! use trustformers::auto::types::PaddingStrategy;
//!
//! let config = ClassificationCollatorConfig {
//!     max_length: Some(512),
//!     padding: PaddingStrategy::Longest,
//!     truncation: true,
//!     pad_token_id: 0,
//!     num_labels: 3,  // Negative, Neutral, Positive
//! };
//!
//! let collator = ClassificationDataCollator::new(config);
//! ```
//!
//! ### Multi-label Classification
//!
//! ```rust,ignore
//! use trustformers::auto::data_collators::classification::{
//!     ClassificationDataCollator, ClassificationCollatorConfig
//! };
//! use trustformers::auto::types::{DataExample, PaddingStrategy};
//! use std::collections::HashMap;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Configuration for multi-label topic classification
//! let config = ClassificationCollatorConfig {
//!     max_length: Some(256),
//!     padding: PaddingStrategy::Longest,
//!     truncation: true,
//!     pad_token_id: 0,
//!     num_labels: 10,  // Multiple topics can be assigned
//! };
//!
//! let collator = ClassificationDataCollator::new(config);
//!
//! // Example with multiple labels
//! let examples = vec![
//!     DataExample {
//!         input_ids: vec![101, 2023, 2003, 102],  // [CLS] this is [SEP]
//!         attention_mask: Some(vec![1, 1, 1, 1]),
//!         token_type_ids: None,
//!         labels: Some(vec![1, 4]),  // Multiple class labels
//!         metadata: HashMap::new(),
//!     },
//! ];
//!
//! let batch = collator.collate(&examples)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Supported Tasks
//!
//! - **Sentiment Analysis**: Positive/Negative/Neutral classification
//! - **Topic Classification**: Document categorization
//! - **Intent Recognition**: Understanding user intents
//! - **Spam Detection**: Email/message filtering
//! - **Language Identification**: Detecting input language
//! - **Multi-label Classification**: Multiple categories per text
//!
//! ## Label Formats
//!
//! The collator supports various label formats:
//! - **Single Label**: One class per example (sentiment analysis)
//! - **Multi-label**: Multiple classes per example (topic tagging)
//! - **Hierarchical**: Nested classification categories
//! - **Weighted**: Different importance weights per class

use super::{DataCollator, DataCollatorConfig};
use crate::auto::data_collators::language_modeling::{
    LanguageModelingCollatorConfig, LanguageModelingDataCollator,
};
use crate::auto::types::{CollatedBatch, DataExample, PaddingStrategy};
use crate::error::{Result, TrustformersError};
use serde::{Deserialize, Serialize};

// =============================================================================
// Classification Data Collator
// =============================================================================

/// Data collator for text classification tasks (BERT-like models)
///
/// This collator is specifically designed for classification models that need
/// to assign labels to input text sequences. It handles the formatting of
/// labels and ensures proper input representation for classification training.
///
/// ## Features
///
/// - **Label Processing**: Converts various label formats to model-compatible form
/// - **Single/Multi-label**: Supports both single and multi-label classification
/// - **Class Handling**: Manages different numbers of output classes
/// - **Loss Masking**: Properly handles missing labels with -100 (ignored in loss)
/// - **Sequence Processing**: Standard text tokenization and padding
///
/// ## Label Format
///
/// The collator expects labels in the `DataExample.labels` field:
/// - **Single-label**: `vec![class_id]` (e.g., `vec![2]` for class 2)
/// - **Multi-label**: `vec![class1, class2, ...]` (e.g., `vec![0, 3, 7]`)
/// - **No label**: `None` (will be replaced with -100 for loss computation)
///
/// ## Training vs Inference
///
/// During training, labels are required and formatted appropriately for loss
/// computation. During inference, labels can be omitted and the collator will
/// handle missing labels gracefully.
///
/// ## Usage Examples
///
/// ```rust,ignore
/// use trustformers::auto::data_collators::classification::{
///     ClassificationDataCollator, ClassificationCollatorConfig
/// };
/// use trustformers::auto::types::{DataExample, PaddingStrategy};
///
/// // Configuration for binary sentiment classification
/// let config = ClassificationCollatorConfig {
///     max_length: Some(128),
///     padding: PaddingStrategy::Longest,
///     truncation: true,
///     pad_token_id: 0,
///     num_labels: 2,  // Binary: negative (0), positive (1)
/// };
///
/// let collator = ClassificationDataCollator::new(config);
///
/// // Example for sentiment analysis
/// let examples = vec![
///     DataExample {
///         input_ids: vec![101, 1045, 2293, 2023, 102],  // [CLS] I love this [SEP]
///         attention_mask: Some(vec![1, 1, 1, 1, 1]),
///         token_type_ids: None,
///         labels: Some(vec![1]),  // Positive sentiment
///         metadata: HashMap::new(),
///     },
/// ];
///
/// let batch = collator.collate(&examples)?;
/// ```
#[derive(Debug, Clone)]
pub struct ClassificationDataCollator {
    config: ClassificationCollatorConfig,
}

impl ClassificationDataCollator {
    /// Create a new classification data collator
    ///
    /// # Arguments
    ///
    /// * `config` - Configuration specifying collation behavior for classification
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let config = ClassificationCollatorConfig {
    ///     max_length: Some(512),
    ///     padding: PaddingStrategy::Longest,
    ///     truncation: true,
    ///     pad_token_id: 0,
    ///     num_labels: 5,
    /// };
    /// let collator = ClassificationDataCollator::new(config);
    /// ```
    pub fn new(config: ClassificationCollatorConfig) -> Self {
        Self { config }
    }

    /// Process labels for classification training
    ///
    /// This method converts the raw labels from examples into the format
    /// expected by classification models. It handles both single-label and
    /// multi-label scenarios.
    ///
    /// # Arguments
    ///
    /// * `examples` - Input examples with labels
    ///
    /// # Returns
    ///
    /// Processed labels formatted for classification loss computation
    fn process_classification_labels(&self, examples: &[DataExample]) -> Vec<Vec<i64>> {
        let mut processed_labels = Vec::with_capacity(examples.len());

        for example in examples {
            if let Some(ref example_labels) = example.labels {
                if example_labels.is_empty() {
                    // No labels provided
                    processed_labels.push(vec![-100]);
                } else if example_labels.len() == 1 {
                    // Single-label classification
                    processed_labels.push(vec![example_labels[0]]);
                } else {
                    // Multi-label classification - take the first label as primary
                    // In a full implementation, this would create a proper multi-label vector
                    processed_labels.push(vec![example_labels[0]]);
                }
            } else {
                // No labels field - use -100 (ignored in loss)
                processed_labels.push(vec![-100]);
            }
        }

        processed_labels
    }

    /// Create one-hot encoded labels for multi-label classification
    ///
    /// This method creates one-hot encoded label vectors for multi-label
    /// classification tasks where each example can belong to multiple classes.
    ///
    /// # Arguments
    ///
    /// * `examples` - Input examples with multi-label annotations
    ///
    /// # Returns
    ///
    /// One-hot encoded label vectors for multi-label classification
    fn create_multilabel_encoding(&self, examples: &[DataExample]) -> Vec<Vec<f32>> {
        let mut multilabel_vectors = Vec::with_capacity(examples.len());

        for example in examples {
            let mut label_vector = vec![0.0f32; self.config.num_labels];

            if let Some(ref example_labels) = example.labels {
                for &label in example_labels {
                    if label >= 0 && (label as usize) < self.config.num_labels {
                        label_vector[label as usize] = 1.0;
                    }
                }
            }

            multilabel_vectors.push(label_vector);
        }

        multilabel_vectors
    }

    /// Validate label consistency across the batch
    ///
    /// This method checks that all labels in the batch are consistent with
    /// the configured number of classes.
    ///
    /// # Arguments
    ///
    /// * `examples` - Input examples to validate
    ///
    /// # Returns
    ///
    /// Result indicating whether labels are valid
    fn validate_labels(&self, examples: &[DataExample]) -> Result<()> {
        for (i, example) in examples.iter().enumerate() {
            if let Some(ref labels) = example.labels {
                for &label in labels {
                    if label >= 0 && (label as usize) >= self.config.num_labels {
                        return Err(TrustformersError::invalid_input_simple(format!(
                            "Label {} in example {} exceeds num_labels {}",
                            label, i, self.config.num_labels
                        )));
                    }
                }
            }
        }
        Ok(())
    }
}

impl DataCollator for ClassificationDataCollator {
    fn collate(&self, examples: &[DataExample]) -> Result<CollatedBatch> {
        if examples.is_empty() {
            return Err(TrustformersError::invalid_input_simple(
                "Cannot collate empty batch for classification".to_string(),
            ));
        }

        // Validate labels before processing
        self.validate_labels(examples)?;

        // Use the base language modeling collator for sequence processing
        // (without MLM masking)
        let sequence_collator = LanguageModelingDataCollator::new(LanguageModelingCollatorConfig {
            max_length: self.config.max_length,
            padding: self.config.padding,
            truncation: self.config.truncation,
            pad_token_id: self.config.pad_token_id,
            mask_token_id: 0, // No masking for classification
            mlm_probability: 0.0,
        });

        // Collate input sequences
        let mut batch = sequence_collator.collate(examples)?;

        // Process classification labels
        let processed_labels = self.process_classification_labels(examples);
        batch.labels = Some(processed_labels);

        // Add classification-specific metadata
        batch.metadata.insert(
            "num_labels".to_string(),
            serde_json::Value::Number(self.config.num_labels.into()),
        );

        // Check if this is a multi-label task
        let is_multilabel = examples
            .iter()
            .any(|ex| ex.labels.as_ref().is_some_and(|labels| labels.len() > 1));

        if is_multilabel {
            let multilabel_encoding = self.create_multilabel_encoding(examples);
            batch.metadata.insert(
                "multilabel_targets".to_string(),
                serde_json::to_value(multilabel_encoding)
                    .map_err(|e| TrustformersError::runtime_error(e.to_string()))?,
            );
            batch.metadata.insert(
                "task_type".to_string(),
                serde_json::Value::String("multilabel".to_string()),
            );
        } else {
            batch.metadata.insert(
                "task_type".to_string(),
                serde_json::Value::String("single_label".to_string()),
            );
        }

        Ok(batch)
    }

    fn config(&self) -> &dyn DataCollatorConfig {
        &self.config
    }

    fn preprocess_examples(&self, examples: &[DataExample]) -> Result<Vec<DataExample>> {
        // For classification, we might want to add preprocessing like
        // text normalization, but for now just return as-is
        Ok(examples.to_vec())
    }
}

// =============================================================================
// Classification Configuration
// =============================================================================

/// Configuration for classification data collator
///
/// This configuration struct controls all aspects of data collation for
/// text classification tasks. It extends the basic collation parameters
/// with classification-specific settings like the number of output classes.
///
/// ## Configuration Parameters
///
/// - `max_length`: Maximum sequence length for padding/truncation
/// - `padding`: Strategy for padding sequences in batch
/// - `truncation`: Whether to truncate sequences exceeding max_length
/// - `pad_token_id`: Token ID used for padding positions
/// - `num_labels`: Number of classification classes
///
/// ## Classification Types
///
/// The collator supports various classification scenarios:
/// - **Binary Classification**: `num_labels = 2` (e.g., spam detection)
/// - **Multi-class**: `num_labels > 2` (e.g., topic classification)
/// - **Multi-label**: Multiple labels per example (via label formatting)
///
/// ## Default Values
///
/// The `from_config` method provides sensible defaults:
/// - `num_labels`: 2 (binary classification)
/// - `padding`: Longest sequence in batch
/// - `truncation`: Enabled
/// - `pad_token_id`: From model config or 0
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassificationCollatorConfig {
    /// Maximum sequence length for padding/truncation
    pub max_length: Option<usize>,
    /// Padding strategy for batch collation
    pub padding: PaddingStrategy,
    /// Whether to truncate sequences exceeding max_length
    pub truncation: bool,
    /// Token ID used for padding
    pub pad_token_id: u32,
    /// Number of classification labels/classes
    pub num_labels: usize,
}

impl ClassificationCollatorConfig {
    /// Create configuration from model config JSON
    ///
    /// This method extracts relevant configuration parameters from a model's
    /// config.json file and creates an appropriate collator configuration
    /// for classification tasks.
    ///
    /// # Arguments
    ///
    /// * `config` - Model configuration as JSON value (typically from config.json)
    ///
    /// # Returns
    ///
    /// A configured `ClassificationCollatorConfig` with parameters extracted
    /// from the model configuration and classification-specific defaults.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use trustformers::auto::data_collators::classification::ClassificationCollatorConfig;
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let model_config = serde_json::json!({
    ///     "max_position_embeddings": 512,
    ///     "pad_token_id": 0,
    ///     "num_labels": 3,
    ///     "vocab_size": 30522
    /// });
    ///
    /// let config = ClassificationCollatorConfig::from_config(&model_config)?;
    /// assert_eq!(config.max_length, Some(512));
    /// assert_eq!(config.pad_token_id, 0);
    /// assert_eq!(config.num_labels, 3);
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_config(config: &serde_json::Value) -> Result<Self> {
        Ok(Self {
            max_length: config
                .get("max_position_embeddings")
                .or_else(|| config.get("max_length"))
                .and_then(|v| v.as_u64())
                .map(|v| v as usize),
            padding: PaddingStrategy::Longest,
            truncation: true,
            pad_token_id: config.get("pad_token_id").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
            num_labels: config.get("num_labels").and_then(|v| v.as_u64()).unwrap_or(2) as usize,
        })
    }

    /// Create a configuration for binary classification
    ///
    /// Creates a configuration specifically for binary classification tasks
    /// like sentiment analysis (positive/negative) or spam detection.
    ///
    /// # Arguments
    ///
    /// * `config` - Model configuration as JSON value
    ///
    /// # Returns
    ///
    /// A configuration optimized for binary classification
    pub fn for_binary_classification(config: &serde_json::Value) -> Result<Self> {
        let mut binary_config = Self::from_config(config)?;
        binary_config.num_labels = 2;
        Ok(binary_config)
    }

    /// Create a configuration for multi-label classification
    ///
    /// Creates a configuration for multi-label classification where each
    /// example can belong to multiple classes simultaneously.
    ///
    /// # Arguments
    ///
    /// * `config` - Model configuration as JSON value
    /// * `num_labels` - Number of possible labels
    ///
    /// # Returns
    ///
    /// A configuration optimized for multi-label classification
    pub fn for_multilabel_classification(
        config: &serde_json::Value,
        num_labels: usize,
    ) -> Result<Self> {
        let mut multilabel_config = Self::from_config(config)?;
        multilabel_config.num_labels = num_labels;
        Ok(multilabel_config)
    }

    /// Create a configuration for sentiment analysis
    ///
    /// Creates a configuration specifically for sentiment analysis tasks
    /// with common settings for positive/negative/neutral classification.
    ///
    /// # Arguments
    ///
    /// * `config` - Model configuration as JSON value
    /// * `include_neutral` - Whether to include neutral class (3-class) or not (2-class)
    ///
    /// # Returns
    ///
    /// A configuration optimized for sentiment analysis
    pub fn for_sentiment_analysis(
        config: &serde_json::Value,
        include_neutral: bool,
    ) -> Result<Self> {
        let mut sentiment_config = Self::from_config(config)?;
        sentiment_config.num_labels = if include_neutral { 3 } else { 2 };
        Ok(sentiment_config)
    }
}

impl DataCollatorConfig for ClassificationCollatorConfig {
    fn max_length(&self) -> Option<usize> {
        self.max_length
    }

    fn padding(&self) -> PaddingStrategy {
        self.padding
    }

    fn truncation(&self) -> bool {
        self.truncation
    }
}

// =============================================================================
// Module Re-exports
// =============================================================================

// Note: No need to re-export since these are already public structs in this module

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_classification_collator_creation() {
        let config = ClassificationCollatorConfig {
            max_length: Some(128),
            padding: PaddingStrategy::Longest,
            truncation: true,
            pad_token_id: 0,
            num_labels: 3,
        };

        let collator = ClassificationDataCollator::new(config);
        assert_eq!(collator.config().max_length(), Some(128));
        assert_eq!(collator.config.num_labels, 3);
    }

    #[test]
    fn test_classification_config_from_json() {
        let config_json = serde_json::json!({
            "max_position_embeddings": 512,
            "pad_token_id": 1,
            "num_labels": 5,
            "vocab_size": 30522
        });

        let config = ClassificationCollatorConfig::from_config(&config_json)
            .expect("operation failed in test");
        assert_eq!(config.max_length, Some(512));
        assert_eq!(config.pad_token_id, 1);
        assert_eq!(config.num_labels, 5);
    }

    #[test]
    fn test_collate_classification_examples() {
        let config = ClassificationCollatorConfig {
            max_length: Some(10),
            padding: PaddingStrategy::Longest,
            truncation: true,
            pad_token_id: 0,
            num_labels: 2,
        };

        let collator = ClassificationDataCollator::new(config);

        let examples = vec![
            DataExample {
                input_ids: vec![101, 2023, 2003, 102], // [CLS] this is [SEP]
                attention_mask: Some(vec![1, 1, 1, 1]),
                token_type_ids: None,
                labels: Some(vec![1]), // Positive class
                metadata: HashMap::new(),
            },
            DataExample {
                input_ids: vec![101, 2025, 102], // [CLS] bad [SEP]
                attention_mask: Some(vec![1, 1, 1]),
                token_type_ids: None,
                labels: Some(vec![0]), // Negative class
                metadata: HashMap::new(),
            },
        ];

        let batch = collator.collate(&examples).expect("operation failed in test");
        assert_eq!(batch.batch_size, 2);
        assert_eq!(batch.input_ids.len(), 2);
        assert!(batch.labels.is_some());

        let labels = batch.labels.as_ref().expect("operation failed in test");
        assert_eq!(labels.len(), 2);
        assert_eq!(labels[0], vec![1]);
        assert_eq!(labels[1], vec![0]);
    }

    #[test]
    fn test_multilabel_encoding() {
        let config = ClassificationCollatorConfig {
            max_length: Some(10),
            padding: PaddingStrategy::Longest,
            truncation: true,
            pad_token_id: 0,
            num_labels: 5,
        };

        let collator = ClassificationDataCollator::new(config);

        let examples = vec![DataExample {
            input_ids: vec![101, 1037, 3231, 102],
            attention_mask: Some(vec![1, 1, 1, 1]),
            token_type_ids: None,
            labels: Some(vec![0, 2, 4]), // Multi-label
            metadata: HashMap::new(),
        }];

        let multilabel_encoding = collator.create_multilabel_encoding(&examples);
        assert_eq!(multilabel_encoding.len(), 1);
        assert_eq!(multilabel_encoding[0].len(), 5);
        assert_eq!(multilabel_encoding[0][0], 1.0);
        assert_eq!(multilabel_encoding[0][1], 0.0);
        assert_eq!(multilabel_encoding[0][2], 1.0);
        assert_eq!(multilabel_encoding[0][3], 0.0);
        assert_eq!(multilabel_encoding[0][4], 1.0);
    }

    #[test]
    fn test_sentiment_analysis_config() {
        let model_config = serde_json::json!({
            "max_position_embeddings": 128,
            "pad_token_id": 0
        });

        let config = ClassificationCollatorConfig::for_sentiment_analysis(&model_config, true)
            .expect("operation failed in test");
        assert_eq!(config.num_labels, 3); // Negative, Neutral, Positive

        let config = ClassificationCollatorConfig::for_sentiment_analysis(&model_config, false)
            .expect("operation failed in test");
        assert_eq!(config.num_labels, 2); // Negative, Positive
    }

    #[test]
    fn test_label_validation() {
        let config = ClassificationCollatorConfig {
            max_length: Some(10),
            padding: PaddingStrategy::Longest,
            truncation: true,
            pad_token_id: 0,
            num_labels: 2, // Only 2 classes allowed
        };

        let collator = ClassificationDataCollator::new(config);

        // Valid labels
        let valid_examples = vec![DataExample {
            input_ids: vec![101, 102],
            attention_mask: Some(vec![1, 1]),
            token_type_ids: None,
            labels: Some(vec![1]),
            metadata: HashMap::new(),
        }];

        assert!(collator.validate_labels(&valid_examples).is_ok());

        // Invalid labels (class 2 doesn't exist with num_labels=2)
        let invalid_examples = vec![DataExample {
            input_ids: vec![101, 102],
            attention_mask: Some(vec![1, 1]),
            token_type_ids: None,
            labels: Some(vec![2]), // Invalid: only 0,1 allowed
            metadata: HashMap::new(),
        }];

        assert!(collator.validate_labels(&invalid_examples).is_err());
    }
}