brainwires-training 0.9.0

Model training and fine-tuning for the Brainwires Agent Framework — cloud fine-tuning and local LoRA/QLoRA/DoRA training
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
//! Dataset loading for local training.
//!
//! Parses JSONL training files into tokenized batches for the Burn training loop.
//! Supports instruction-tuning formats: `{"prompt": ..., "completion": ...}` and
//! `{"messages": [...]}` (chat format).
//!
//! Also supports preference pair datasets for DPO/ORPO alignment:
//! `{"prompt": "...", "chosen": "...", "rejected": "..."}`.

use std::io::BufRead;
use std::path::Path;

use tracing::info;

use crate::error::TrainingError;

/// A single training example (prompt + completion text).
#[derive(Debug, Clone)]
pub struct TrainingExample {
    /// Input text (prompt/instruction).
    pub prompt: String,
    /// Target text (completion/response).
    pub completion: String,
}

/// Parsed dataset ready for batching.
#[derive(Debug)]
pub struct TrainingDataset {
    /// All training examples.
    pub examples: Vec<TrainingExample>,
}

impl TrainingDataset {
    /// Load a JSONL dataset from disk.
    ///
    /// Supports two formats:
    /// 1. `{"prompt": "...", "completion": "..."}`
    /// 2. `{"messages": [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]}`
    pub fn load_jsonl(path: &Path) -> Result<Self, TrainingError> {
        let file = std::fs::File::open(path).map_err(|e| {
            TrainingError::Config(format!("Failed to open dataset: {}: {}", path.display(), e))
        })?;
        let reader = std::io::BufReader::new(file);
        let mut examples = Vec::new();

        for (line_num, line) in reader.lines().enumerate() {
            let line = line.map_err(|e| {
                TrainingError::Config(format!("Failed to read line {}: {}", line_num + 1, e))
            })?;
            let line = line.trim().to_string();
            if line.is_empty() {
                continue;
            }

            let value: serde_json::Value = serde_json::from_str(&line).map_err(|e| {
                TrainingError::Config(format!("Invalid JSON on line {}: {}", line_num + 1, e))
            })?;

            let example = if value.get("messages").is_some() {
                parse_chat_format(&value, line_num + 1)?
            } else if value.get("prompt").is_some() && value.get("completion").is_some() {
                parse_prompt_completion(&value, line_num + 1)?
            } else if value.get("instruction").is_some() {
                parse_alpaca_format(&value, line_num + 1)?
            } else {
                return Err(TrainingError::Config(format!(
                    "Line {}: expected 'prompt'+'completion', 'messages', or 'instruction'+'output' field",
                    line_num + 1,
                )));
            };

            examples.push(example);
        }

        if examples.is_empty() {
            return Err(TrainingError::Config(
                "Dataset is empty (no valid examples found)".to_string(),
            ));
        }

        info!(
            "Loaded {} training examples from {:?}",
            examples.len(),
            path
        );
        Ok(Self { examples })
    }

    /// Number of examples in the dataset.
    pub fn len(&self) -> usize {
        self.examples.len()
    }

    /// Whether the dataset is empty.
    pub fn is_empty(&self) -> bool {
        self.examples.is_empty()
    }

    /// Calculate steps per epoch given a batch size.
    pub fn steps_per_epoch(&self, batch_size: usize) -> u64 {
        (self.examples.len() / batch_size.max(1)).max(1) as u64
    }

    /// Get a batch of examples by index range.
    pub fn get_batch(&self, start: usize, batch_size: usize) -> &[TrainingExample] {
        let end = (start + batch_size).min(self.examples.len());
        &self.examples[start..end]
    }
}

/// Parse `{"prompt": "...", "completion": "..."}` format.
fn parse_prompt_completion(
    value: &serde_json::Value,
    line_num: usize,
) -> Result<TrainingExample, TrainingError> {
    let prompt = value
        .get("prompt")
        .and_then(|v| v.as_str())
        .ok_or_else(|| {
            TrainingError::Config(format!("Line {}: 'prompt' must be a string", line_num))
        })?
        .to_string();

    let completion = value
        .get("completion")
        .and_then(|v| v.as_str())
        .ok_or_else(|| {
            TrainingError::Config(format!("Line {}: 'completion' must be a string", line_num))
        })?
        .to_string();

    Ok(TrainingExample { prompt, completion })
}

/// Parse `{"messages": [{"role": "...", "content": "..."}]}` chat format.
fn parse_chat_format(
    value: &serde_json::Value,
    line_num: usize,
) -> Result<TrainingExample, TrainingError> {
    let messages = value
        .get("messages")
        .and_then(|v| v.as_array())
        .ok_or_else(|| {
            TrainingError::Config(format!("Line {}: 'messages' must be an array", line_num))
        })?;

    let mut prompt_parts = Vec::new();
    let mut completion = String::new();

    for msg in messages {
        let role = msg.get("role").and_then(|v| v.as_str()).unwrap_or("");
        let content = msg.get("content").and_then(|v| v.as_str()).unwrap_or("");

        match role {
            "system" | "user" => prompt_parts.push(content.to_string()),
            "assistant" => completion = content.to_string(),
            _ => {}
        }
    }

    if prompt_parts.is_empty() {
        return Err(TrainingError::Config(format!(
            "Line {}: no user/system messages found",
            line_num
        )));
    }
    if completion.is_empty() {
        return Err(TrainingError::Config(format!(
            "Line {}: no assistant message found",
            line_num
        )));
    }

    Ok(TrainingExample {
        prompt: prompt_parts.join("\n"),
        completion,
    })
}

/// Trait for tokenizers used in training.
///
/// Both `SimpleTokenizer` (byte-level fallback) and `ModelTokenizer` (BPE via
/// HuggingFace `tokenizers` crate) implement this trait.
pub trait Tokenizer {
    /// Encode text into token IDs.
    fn encode(&self, text: &str) -> Vec<u32>;

    /// Encode a training example into (input_ids, target_ids).
    ///
    /// Concatenates prompt + completion, with prompt tokens masked in targets
    /// (set to `u32::MAX`). Truncates to max sequence length.
    fn encode_example(&self, example: &TrainingExample) -> (Vec<u32>, Vec<u32>);

    /// Vocabulary size of this tokenizer.
    fn vocab_size(&self) -> usize;
}

/// Simple character-level tokenizer for training.
///
/// In production, this would be a BPE/SentencePiece tokenizer loaded from the model.
/// This basic implementation enables the training loop to work end-to-end.
pub struct SimpleTokenizer {
    max_seq_len: usize,
}

impl SimpleTokenizer {
    /// Create a tokenizer with the given maximum sequence length.
    pub fn new(max_seq_len: usize) -> Self {
        Self { max_seq_len }
    }
}

impl Tokenizer for SimpleTokenizer {
    fn encode(&self, text: &str) -> Vec<u32> {
        text.bytes()
            .take(self.max_seq_len)
            .map(|b| b as u32)
            .collect()
    }

    fn encode_example(&self, example: &TrainingExample) -> (Vec<u32>, Vec<u32>) {
        let prompt_tokens = self.encode(&example.prompt);
        let completion_tokens = self.encode(&example.completion);
        let prompt_len = prompt_tokens.len();

        let mut input_ids = prompt_tokens;
        input_ids.extend_from_slice(&completion_tokens);
        input_ids.truncate(self.max_seq_len);

        // Targets: shifted input_ids, with prompt portion masked
        let mut target_ids = vec![u32::MAX; input_ids.len()];
        target_ids[prompt_len..input_ids.len()].copy_from_slice(&input_ids[prompt_len..]);

        (input_ids, target_ids)
    }

    fn vocab_size(&self) -> usize {
        257
    }
}

/// BPE tokenizer wrapping HuggingFace `tokenizers` crate.
///
/// Provides correct vocab-size alignment with real models (e.g., LLaMA, Mistral).
/// Load from a `tokenizer.json` file or a pretrained HuggingFace model ID.
pub struct ModelTokenizer {
    tokenizer: tokenizers::Tokenizer,
    max_seq_len: usize,
}

impl ModelTokenizer {
    /// Load a tokenizer from a `tokenizer.json` file on disk.
    pub fn from_file(path: &Path) -> Result<Self, TrainingError> {
        let tokenizer = tokenizers::Tokenizer::from_file(path).map_err(|e| {
            TrainingError::Config(format!(
                "Failed to load tokenizer from {}: {}",
                path.display(),
                e
            ))
        })?;
        Ok(Self {
            tokenizer,
            max_seq_len: 2048,
        })
    }

    /// Load a tokenizer from raw JSON bytes.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, TrainingError> {
        let tokenizer = tokenizers::Tokenizer::from_bytes(bytes).map_err(|e| {
            TrainingError::Config(format!("Failed to load tokenizer from bytes: {}", e))
        })?;
        Ok(Self {
            tokenizer,
            max_seq_len: 2048,
        })
    }

    /// Set the maximum sequence length for encoding.
    pub fn with_max_seq_len(mut self, max_seq_len: usize) -> Self {
        self.max_seq_len = max_seq_len;
        self
    }
}

impl Tokenizer for ModelTokenizer {
    fn encode(&self, text: &str) -> Vec<u32> {
        match self.tokenizer.encode(text, false) {
            Ok(encoding) => encoding
                .get_ids()
                .iter()
                .take(self.max_seq_len)
                .copied()
                .collect(),
            Err(_) => Vec::new(),
        }
    }

    fn encode_example(&self, example: &TrainingExample) -> (Vec<u32>, Vec<u32>) {
        let prompt_tokens = self.encode(&example.prompt);
        let completion_tokens = self.encode(&example.completion);
        let prompt_len = prompt_tokens.len();

        let mut input_ids = prompt_tokens;
        input_ids.extend_from_slice(&completion_tokens);
        input_ids.truncate(self.max_seq_len);

        let mut target_ids = vec![u32::MAX; input_ids.len()];
        target_ids[prompt_len..input_ids.len()].copy_from_slice(&input_ids[prompt_len..]);

        (input_ids, target_ids)
    }

    fn vocab_size(&self) -> usize {
        self.tokenizer.get_vocab_size(true)
    }
}

/// Parse `{"instruction": "...", "input": "...", "output": "..."}` Alpaca format.
fn parse_alpaca_format(
    value: &serde_json::Value,
    line_num: usize,
) -> Result<TrainingExample, TrainingError> {
    let instruction = value
        .get("instruction")
        .and_then(|v| v.as_str())
        .ok_or_else(|| {
            TrainingError::Config(format!("Line {}: 'instruction' must be a string", line_num))
        })?;

    let input = value.get("input").and_then(|v| v.as_str()).unwrap_or("");

    let output = value
        .get("output")
        .and_then(|v| v.as_str())
        .ok_or_else(|| {
            TrainingError::Config(format!("Line {}: 'output' must be a string", line_num))
        })?;

    let prompt = if input.is_empty() {
        instruction.to_string()
    } else {
        format!("{}\n{}", instruction, input)
    };

    Ok(TrainingExample {
        prompt,
        completion: output.to_string(),
    })
}

/// A single preference pair example for DPO/ORPO alignment training.
#[derive(Debug, Clone)]
pub struct PreferenceExample {
    /// Input prompt text.
    pub prompt: String,
    /// Preferred (chosen) completion.
    pub chosen: String,
    /// Dispreferred (rejected) completion.
    pub rejected: String,
}

/// Preference pair dataset for alignment training (DPO/ORPO).
#[derive(Debug)]
pub struct PreferenceDataset {
    /// All preference examples.
    pub examples: Vec<PreferenceExample>,
}

impl PreferenceDataset {
    /// Load preference pairs from JSONL.
    ///
    /// Each line: `{"prompt": "...", "chosen": "...", "rejected": "..."}`
    pub fn load_jsonl(path: &Path) -> Result<Self, TrainingError> {
        let file = std::fs::File::open(path).map_err(|e| {
            TrainingError::Config(format!(
                "Failed to open preference dataset: {}: {}",
                path.display(),
                e
            ))
        })?;
        let reader = std::io::BufReader::new(file);
        let mut examples = Vec::new();

        for (line_num, line) in reader.lines().enumerate() {
            let line = line.map_err(|e| {
                TrainingError::Config(format!("Failed to read line {}: {}", line_num + 1, e))
            })?;
            let line = line.trim().to_string();
            if line.is_empty() {
                continue;
            }

            let value: serde_json::Value = serde_json::from_str(&line).map_err(|e| {
                TrainingError::Config(format!("Invalid JSON on line {}: {}", line_num + 1, e))
            })?;

            let prompt = value
                .get("prompt")
                .and_then(|v| v.as_str())
                .ok_or_else(|| {
                    TrainingError::Config(format!(
                        "Line {}: 'prompt' must be a string",
                        line_num + 1
                    ))
                })?
                .to_string();

            let chosen = value
                .get("chosen")
                .and_then(|v| v.as_str())
                .ok_or_else(|| {
                    TrainingError::Config(format!(
                        "Line {}: 'chosen' must be a string",
                        line_num + 1
                    ))
                })?
                .to_string();

            let rejected = value
                .get("rejected")
                .and_then(|v| v.as_str())
                .ok_or_else(|| {
                    TrainingError::Config(format!(
                        "Line {}: 'rejected' must be a string",
                        line_num + 1
                    ))
                })?
                .to_string();

            examples.push(PreferenceExample {
                prompt,
                chosen,
                rejected,
            });
        }

        if examples.is_empty() {
            return Err(TrainingError::Config(
                "Preference dataset is empty (no valid examples found)".to_string(),
            ));
        }

        info!(
            "Loaded {} preference examples from {:?}",
            examples.len(),
            path
        );
        Ok(Self { examples })
    }

    /// Number of examples in the dataset.
    pub fn len(&self) -> usize {
        self.examples.len()
    }

    /// Whether the dataset is empty.
    pub fn is_empty(&self) -> bool {
        self.examples.is_empty()
    }

    /// Calculate steps per epoch given a batch size.
    pub fn steps_per_epoch(&self, batch_size: usize) -> u64 {
        (self.examples.len() / batch_size.max(1)).max(1) as u64
    }

    /// Get a batch of examples by index range.
    pub fn get_batch(&self, start: usize, batch_size: usize) -> &[PreferenceExample] {
        let end = (start + batch_size).min(self.examples.len());
        &self.examples[start..end]
    }
}

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

    #[test]
    fn test_load_prompt_completion() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("train.jsonl");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(f, r#"{{"prompt": "Hello", "completion": "World"}}"#).unwrap();
        writeln!(f, r#"{{"prompt": "Foo", "completion": "Bar"}}"#).unwrap();

        let dataset = TrainingDataset::load_jsonl(&path).unwrap();
        assert_eq!(dataset.len(), 2);
        assert_eq!(dataset.examples[0].prompt, "Hello");
        assert_eq!(dataset.examples[0].completion, "World");
    }

    #[test]
    fn test_load_chat_format() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("train.jsonl");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(
            f,
            r#"{{"messages": [{{"role": "user", "content": "Hi"}}, {{"role": "assistant", "content": "Hello!"}}]}}"#
        )
        .unwrap();

        let dataset = TrainingDataset::load_jsonl(&path).unwrap();
        assert_eq!(dataset.len(), 1);
        assert_eq!(dataset.examples[0].prompt, "Hi");
        assert_eq!(dataset.examples[0].completion, "Hello!");
    }

    #[test]
    fn test_load_alpaca_format() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("train.jsonl");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(
            f,
            r#"{{"instruction": "Translate to French", "input": "Hello", "output": "Bonjour"}}"#
        )
        .unwrap();
        writeln!(f, r#"{{"instruction": "What is 2+2?", "output": "4"}}"#).unwrap();

        let dataset = TrainingDataset::load_jsonl(&path).unwrap();
        assert_eq!(dataset.len(), 2);
        assert!(dataset.examples[0].prompt.contains("Translate to French"));
        assert!(dataset.examples[0].prompt.contains("Hello"));
        assert_eq!(dataset.examples[0].completion, "Bonjour");
        assert_eq!(dataset.examples[1].prompt, "What is 2+2?");
    }

    #[test]
    fn test_empty_dataset_error() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("empty.jsonl");
        std::fs::File::create(&path).unwrap();

        let result = TrainingDataset::load_jsonl(&path);
        assert!(result.is_err());
    }

    #[test]
    fn test_steps_per_epoch() {
        let dataset = TrainingDataset {
            examples: vec![
                TrainingExample {
                    prompt: "a".into(),
                    completion: "b".into(),
                };
                100
            ],
        };
        assert_eq!(dataset.steps_per_epoch(4), 25);
        assert_eq!(dataset.steps_per_epoch(10), 10);
    }

    #[test]
    fn test_simple_tokenizer() {
        let tok = SimpleTokenizer::new(512);
        let tokens = tok.encode("Hello");
        assert_eq!(tokens.len(), 5);
        assert_eq!(tokens[0], b'H' as u32);
    }

    #[test]
    fn test_encode_example() {
        let tok = SimpleTokenizer::new(512);
        let example = TrainingExample {
            prompt: "Hi".to_string(),
            completion: "Ok".to_string(),
        };
        let (input, target) = tok.encode_example(&example);
        assert_eq!(input.len(), 4); // "Hi" + "Ok"
        // First 2 tokens (prompt) should be masked
        assert_eq!(target[0], u32::MAX);
        assert_eq!(target[1], u32::MAX);
        // Completion tokens should have actual values
        assert_eq!(target[2], b'O' as u32);
        assert_eq!(target[3], b'k' as u32);
    }

    #[test]
    fn test_tokenizer_trait_simple() {
        let tok: Box<dyn Tokenizer> = Box::new(SimpleTokenizer::new(512));
        assert_eq!(tok.vocab_size(), 257);
        let tokens = tok.encode("Hello");
        assert_eq!(tokens.len(), 5);
    }

    #[test]
    fn test_model_tokenizer_from_file() {
        // Create a minimal tokenizer.json for testing
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("tokenizer.json");
        // Minimal BPE tokenizer JSON (3-token vocab: a, b, c)
        let tokenizer_json = r#"{
            "version": "1.0",
            "model": {
                "type": "BPE",
                "vocab": {"a": 0, "b": 1, "c": 2},
                "merges": []
            }
        }"#;
        std::fs::write(&path, tokenizer_json).unwrap();

        let tok = ModelTokenizer::from_file(&path).unwrap();
        assert!(tok.vocab_size() >= 3);
        let tokens = tok.encode("abc");
        assert!(!tokens.is_empty());
    }

    #[test]
    fn test_model_tokenizer_encode_example() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("tokenizer.json");
        let tokenizer_json = r#"{
            "version": "1.0",
            "model": {
                "type": "BPE",
                "vocab": {"H": 0, "e": 1, "l": 2, "o": 3, "W": 4, "r": 5, "d": 6},
                "merges": []
            }
        }"#;
        std::fs::write(&path, tokenizer_json).unwrap();

        let tok = ModelTokenizer::from_file(&path).unwrap();
        let example = TrainingExample {
            prompt: "Hello".to_string(),
            completion: "World".to_string(),
        };
        let (input, target) = tok.encode_example(&example);
        assert!(!input.is_empty());
        // Prompt portion should be masked
        let prompt_len = tok.encode("Hello").len();
        for i in 0..prompt_len.min(target.len()) {
            assert_eq!(target[i], u32::MAX, "Prompt token {} should be masked", i);
        }
    }

    #[test]
    fn test_preference_dataset_load() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("prefs.jsonl");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(
            f,
            r#"{{"prompt": "What is 2+2?", "chosen": "4", "rejected": "5"}}"#
        )
        .unwrap();
        writeln!(
            f,
            r#"{{"prompt": "Capital of France?", "chosen": "Paris", "rejected": "London"}}"#
        )
        .unwrap();

        let dataset = PreferenceDataset::load_jsonl(&path).unwrap();
        assert_eq!(dataset.len(), 2);
        assert_eq!(dataset.examples[0].prompt, "What is 2+2?");
        assert_eq!(dataset.examples[0].chosen, "4");
        assert_eq!(dataset.examples[0].rejected, "5");
    }

    #[test]
    fn test_preference_dataset_empty() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("empty.jsonl");
        std::fs::File::create(&path).unwrap();

        let result = PreferenceDataset::load_jsonl(&path);
        assert!(result.is_err());
    }

    #[test]
    fn test_preference_dataset_batching() {
        let dataset = PreferenceDataset {
            examples: vec![
                PreferenceExample {
                    prompt: "a".into(),
                    chosen: "b".into(),
                    rejected: "c".into(),
                };
                10
            ],
        };
        assert_eq!(dataset.steps_per_epoch(3), 3);
        let batch = dataset.get_batch(0, 3);
        assert_eq!(batch.len(), 3);
    }
}