Datasets¶
Dataset utilities for loading, tokenizing, and packing sequences for causal language model training.
Related documentation:
- Fast HF Loader — indexed Arrow loading with seconds-to-open on large datasets
- Fast HF Loader Checkpoints — stateful resume from any dataset position
- Sequence Packing — packing multiple documents per batch with document-boundary tracking
- Sequence Packing Quick Reference
- Document Boundaries — enforcing no cross-document attention with Flex Attention
- Dataset Projects — organising datasets as standalone Forgather projects
- Dataset CLI —
forgather datasetcommands for inspecting and sampling datasets
Fast HuggingFace Loader¶
forgather.ml.datasets.fast_hf_loader.FastDatasetLoaderSimple
¶
Fast HuggingFace dataset loader backed by an Arrow file index.
On the first call for a given dataset/split combination the loader
downloads (or locates) the dataset via the HuggingFace datasets
library, records the paths and per-file example counts of the
underlying Arrow cache files in a compact JSON index, and returns
a ComposableIterableDataset wrapping an ArrowBackend. All
subsequent calls for the same configuration load in milliseconds
by reading the index directly.
Both HuggingFace Hub datasets and locally saved datasets (produced
by Dataset.save_to_disk()) are supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_dir
|
str
|
Directory in which the JSON index files are stored. Defaults to
|
None
|
Examples:
>>> loader = FastDatasetLoaderSimple()
>>> ds = loader.load_iterable("allenai/c4", name="en", split="train")
>>> ds = ds.shuffle(seed=42).shard(num_shards=4, index=0)
>>> for example in ds:
... pass
Source code in src/forgather/ml/datasets/fast_hf_loader.py
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 | |
load_iterable(path, name=None, split=None, data_files=None, revision=None, force_reindex=False, num_proc=None, length_estimate='dynamic', reset_length_on_iter=False, **load_dataset_kwargs)
¶
Load a dataset as a ComposableIterableDataset over an
ArrowBackend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
HuggingFace Hub identifier or a local saved-dataset path. |
required |
name
|
str
|
Dataset configuration name. |
None
|
split
|
str
|
Split, with optional slice notation (e.g. |
None
|
data_files
|
optional
|
Forwarded to |
None
|
revision
|
optional
|
Forwarded to |
None
|
num_proc
|
optional
|
Forwarded to |
None
|
force_reindex
|
bool
|
Rebuild the Arrow file index even when a valid cached index already exists. |
False
|
length_estimate
|
(dynamic, static, exact)
|
Length-estimation mode for the wrapper. Default |
"dynamic"
|
reset_length_on_iter
|
bool
|
Reset wrapper length-estimation counters at the start of each
new iteration. Default |
False
|
**load_dataset_kwargs
|
Forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
ComposableIterableDataset
|
Wrapper around an |
Source code in src/forgather/ml/datasets/fast_hf_loader.py
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 | |
forgather.ml.datasets.fast_hf_loader.fast_load_iterable_dataset(path, name=None, split=None, data_files=None, revision=None, force_reindex=False, num_proc=None, index_dir=None, length_estimate='dynamic', reset_length_on_iter=False, **load_dataset_kwargs)
¶
Load a HuggingFace dataset as a fast iterable with sharding and checkpoint support.
Routing
- If the
FORGATHER_DATASET_SERVERenvironment variable is set to a URL (e.g.http://host:8765), the load is routed transparently through the dataset server and aRemoteBackend-wrapped dataset is returned. The server must have been started with--allow-load. Server-only options (force_reindex,num_proc,index_dir,**load_dataset_kwargs) are not forwarded over the wire and take effect only on the local path. - Otherwise, loads locally via
FastDatasetLoaderSimple. The first call for a given dataset is slow (it builds an Arrow file index); all subsequent calls are instant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
HuggingFace Hub identifier (e.g. |
required |
name
|
str
|
Dataset configuration name (e.g. |
None
|
split
|
str
|
Split to load. Supports HuggingFace slice notation such as
|
None
|
data_files
|
str or list of str
|
Specific data files to load (forwarded to |
None
|
revision
|
str
|
Dataset revision or commit hash (forwarded to |
None
|
force_reindex
|
bool
|
Rebuild the Arrow file index from scratch (local path only). |
False
|
num_proc
|
int
|
Number of processes for the initial dataset download/indexing step (local path only). |
None
|
index_dir
|
str
|
Directory where JSON index files are stored (local path only). |
None
|
length_estimate
|
(dynamic, static, exact)
|
Length-estimation mode for the wrapper. |
"dynamic"
|
reset_length_on_iter
|
bool
|
Whether to reset length-estimation counters at the start of each new iteration pass. |
False
|
**load_dataset_kwargs
|
Extra keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
ComposableIterableDataset
|
Iterable dataset (wrapper over
|
Examples:
>>> ds = fast_load_iterable_dataset("allenai/c4", name="en", split="train")
>>> ds = ds.shuffle(seed=42)
>>> ds = ds.shard(num_shards=world_size, index=rank)
>>> ds = ds.map(tokenize)
>>> for example in ds:
... pass
Source code in src/forgather/ml/datasets/fast_hf_loader.py
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 | |
Backend abstraction¶
The loader returns a ComposableIterableDataset wrapped around an
ArrowBackend. The same wrapper can sit on top of an
InMemoryBackend or a RemoteBackend (network proxy to a
Dataset Server) without
client code changes.
forgather.ml.datasets.composable_iterable_dataset.ComposableIterableDataset
¶
Bases: IterableDataset
Backend-agnostic iterable dataset wrapper.
Wraps any IterableDatasetBackend. Composable transformations
(map, slice, shard, shuffle, …) return new wrapper
instances; set_epoch mutates in place (callers re-use the same
wrapper instance across epochs). Backend-mutating ops (shuffle,
seek) return new backend instances and the wrapper holds a
reference to the latest one.
The shard mode parameter that the legacy Arrow class supported
is intentionally absent: at this layer sharding is purely logical
(compute a contiguous example range; restrict iteration to it).
Backends that want to do physical optimizations (e.g. file-level
affinity) can do so privately on their own; the wrapper does not
surface that distinction.
Multi-worker DataLoader support is built in: when iterated under
torch.utils.data.DataLoader(num_workers > 1) each worker takes a
contiguous sub-window of the visible range. Per-worker checkpoint
state is captured by state_dict and restored by load_state_dict.
Length estimation has three modes (length_estimate_mode):
"static"—__len__always returns the view length (after slice/shard), ignoring map-induced cardinality changes."dynamic"(default) — progressive ratio-based estimate during the first complete pass, then locked to the exact count via_cached_exact_lengthonce iteration runs to completion."exact"— alias for"dynamic".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
IterableDatasetBackend
|
Underlying storage backend. |
required |
length_estimate
|
('dynamic', 'static', 'exact')
|
Initial length-estimation mode. Default |
"dynamic"
|
reset_length_on_iter
|
bool
|
If |
False
|
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
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 | |
shuffle(seed=None, buffer_size=1000)
¶
Re-permute the underlying example order via the backend and configure an example-level reservoir shuffle buffer.
Length-estimation cache is invalidated; existing input/output counts are preserved as a ratio carry-over.
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
set_epoch(epoch)
¶
Set the current epoch and re-shuffle the backend if any seed is in play. Mutates in place.
set_epoch(0) always restores the wrapper's natural
backend state (the post-construction or post-shuffle()
baseline) — even if a previous set_epoch(N>0) left the
backend in an N-shuffled state. Without this, going back to
epoch 0 would silently reuse the stale epoch-N order.
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
slice(start=None, end=None)
¶
Return a view restricted to [start, end).
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
select(indices)
¶
Contiguous-range select; non-contiguous indices not supported.
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
shard(num_shards, index)
¶
Split into num_shards disjoint slices and return the one
at index. Logical sharding only — there is no mode
parameter at this layer; the backend may do whatever physical
optimization it wants internally.
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
map(function=None, with_indices=False, input_columns=None, batched=False, batch_size=1000, drop_last_batch=False, remove_columns=None, fn_kwargs=None)
¶
Append a map step to the chain. Multiple map calls compose.
A non-batched function returning None filters the example
out (matches the legacy Arrow class behavior).
Mixed batched / non-batched chains are not supported (raises).
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
filter(function, with_indices=False, input_columns=None, fn_kwargs=None)
¶
Keep examples where function(example) returns truthy.
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
to_hf_iterable()
¶
Wrap this dataset in a HuggingFace IterableDataset for APIs
that require one. The returned object exposes __len__ via
IterableDatasetWithLength so it can drive torch.DataLoader;
the wrapper checkpoint protocol is not preserved on the
returned value.
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
state_dict()
¶
Capture wrapper state plus the backend's flat position.
The backend's position() is in underlying-example space, not
in user-facing post-slice/shard/map space — that's deliberate
so resume can call backend.seek(saved_position) and continue
consuming examples regardless of how a map function may have
changed cardinality.
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
load_state_dict(state)
¶
Restore wrapper state and seek the backend to the saved
position. Map functions themselves are not serialised — the
caller must reconstruct the same map chain before calling
load_state_dict (a fingerprint is checked).
Source code in src/forgather/ml/datasets/composable_iterable_dataset.py
forgather.ml.datasets.iterable_backend.IterableDatasetBackend
¶
Bases: ABC
Abstract storage backend for an iterable dataset.
The contract: __iter__ yields dict examples in some order,
__len__ returns the total example count, position() reports
the flat example index where the next iteration would start, and
shuffle/seek return a new backend instance with the requested
state change.
Implementations must:
- Be deterministic given the same shuffle seed and seek position.
- Update
position()as iteration progresses, so a wrapper can capture it forstate_dictat any point. - After
shuffle(seed), position resets to 0 (the new instance is fresh). Afterseek(n), position isn.
Implementations may optionally expose:
column_names: list[str]— schema column names.features— schema feature dict (HuggingFace-style).n_shards: int— number of natural physical shards (e.g. files).
The wrapper forwards these via attribute access and tolerates
AttributeError for backends that don't provide them.
Source code in src/forgather/ml/datasets/iterable_backend.py
__iter__()
abstractmethod
¶
__len__()
abstractmethod
¶
shuffle(seed=None)
abstractmethod
¶
Return a new backend with the underlying example order re-permuted.
No buffer parameter — the example-level reservoir buffer lives
in the composing wrapper, not in the backend. The seed
determines the new order deterministically; if None an
implementation-chosen seed is generated and surfaced via the
new instance's state so it can be reproduced from a checkpoint.
The returned instance has position() reset to 0.
Source code in src/forgather/ml/datasets/iterable_backend.py
seek(position)
abstractmethod
¶
Return a new backend whose next __iter__ begins at the given
flat example index.
Not expected to be O(1) — implementations may need to walk
index metadata to translate the flat position into their
internal representation. The returned instance has position()
equal to the requested value.
Source code in src/forgather/ml/datasets/iterable_backend.py
position()
abstractmethod
¶
Current flat example index where the next __iter__ would
start.
Must update during iteration so a wrapper can capture it for
state_dict() after any number of yielded examples.
Source code in src/forgather/ml/datasets/iterable_backend.py
forgather.ml.datasets.arrow_backend.ArrowBackend
¶
Bases: IterableDatasetBackend
Storage backend over a list of memory-mapped Arrow files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arrow_files
|
list of str
|
Ordered list of Arrow file paths that make up the dataset. Each file is treated as one natural shard. |
required |
file_lengths
|
list of int
|
Per-file example counts, parallel to |
None
|
Notes
__iter__ mutates the cursor; multiple concurrent iterators on
the same instance would interfere. In multi-worker DataLoader
setups each worker receives its own copy (via fork or pickle), so
concurrent cursors aren't an issue in practice.
Source code in src/forgather/ml/datasets/arrow_backend.py
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 | |
__iter__()
¶
Walk Arrow files from the current cursor to end, yielding
examples and updating _position as each one is emitted.
_position is incremented BEFORE yield so callers that read
it (e.g. the wrapper's _iter_window in check-then-consume
mode) see "index of the next example" semantics consistently
with InMemoryBackend / RemoteBackend.
Source code in src/forgather/ml/datasets/arrow_backend.py
shuffle(seed=None)
¶
Return a new backend with files re-permuted under seed.
Cursor is reset to 0. No example-level buffer — that lives in
the wrapper.
Source code in src/forgather/ml/datasets/arrow_backend.py
seek(position)
¶
Return a new backend with the cursor at position. Past-the-end
positions are clamped to the end (next iteration yields nothing).
Source code in src/forgather/ml/datasets/arrow_backend.py
state_dict()
¶
Capture cursor + order seed + dataset-identity fingerprint.
The wrapper picks this up via the optional-backend-state_dict path so a checkpoint round-trip can detect "different files behind the same handle" early.
Source code in src/forgather/ml/datasets/arrow_backend.py
forgather.ml.datasets.remote_backend.RemoteBackend
¶
Bases: IterableDatasetBackend
Network-proxy backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Base URL of the dataset server, e.g. |
required |
handle
|
str
|
Server-side identifier for the registered backend to consume. |
required |
seed
|
int
|
Shuffle seed; |
None
|
position
|
int
|
Initial flat example index. Default |
0
|
timeout
|
float
|
Per-request HTTP timeout (seconds). Default |
60.0
|
token
|
str
|
Explicit bearer token. If omitted, the constructor consults
|
None
|
Source code in src/forgather/ml/datasets/remote_backend.py
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 | |
column_names
property
¶
Column names of the underlying dataset.
Populated either from the /v1/load response (most common —
the loader passes them through) or by a lazy GET to
/v1/datasets/{handle} on first access. Returns None if
the server can't determine them.
__iter__()
¶
Open a streaming /iter request from the current position and
yield decoded examples. Updates self._position as each
example arrives so callers can capture progress mid-stream.
Network and 5xx errors at any point (initial open or mid-stream)
are translated to :class:DatasetServerUnreachable. 4xx errors
(token, bad request) propagate unchanged.
Source code in src/forgather/ml/datasets/remote_backend.py
shuffle(seed=None)
¶
Return a new client with the new seed; position resets to 0.
No RPC is issued — the seed travels with the next /iter
request. The cached length is preserved (shuffling doesn't
change the underlying example count).
Source code in src/forgather/ml/datasets/remote_backend.py
seek(position)
¶
Return a new client positioned at the given flat example index.
No RPC is issued — the position travels with the next
/iter request.
Source code in src/forgather/ml/datasets/remote_backend.py
Interleaved Datasets¶
forgather.ml.datasets.interleaved.InterleavedDataset
¶
Bases: IterableDataset
An iterable dataset that interleaves examples from multiple child datasets.
Works with any iterable dataset that supports the stateful checkpoint
protocol (state_dict / load_state_dict), including
ComposableIterableDataset. Designed for multi-dataset pre-training where
examples from several corpora need to be mixed in a single training loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasets
|
list
|
Child datasets to interleave. Must be non-empty. Each element can be
any iterable; checkpointing is available for elements that implement
|
required |
probabilities
|
list of float or callable
|
Controls which child dataset is sampled at each step:
|
None
|
seed
|
int
|
Random seed for reproducible probabilistic sampling. Ignored when
|
None
|
stopping_strategy
|
(first_exhausted, all_exhausted)
|
When to stop iteration:
|
"first_exhausted"
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> ds1 = fast_load_iterable_dataset("corpus_a", split="train")
>>> ds2 = fast_load_iterable_dataset("corpus_b", split="train")
>>> combined = InterleavedDataset([ds1, ds2], probabilities=[0.7, 0.3], seed=42)
>>> for example in combined:
... pass
Source code in src/forgather/ml/datasets/interleaved.py
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 | |
column_names
property
¶
Get column names from first dataset.
features
property
¶
Get features from first dataset.
n_shards
property
¶
Total number of shards across all datasets.
__iter__()
¶
Yield interleaved examples from all child datasets.
Selects which child to draw from at each step using the configured
sampling strategy (round-robin or probabilistic). Stops according to
stopping_strategy. If load_state_dict was called before
iteration, child iterators are fast-forwarded to their checkpointed
positions automatically.
Yields:
| Type | Description |
|---|---|
dict
|
One example per step from whichever child dataset was selected. |
Source code in src/forgather/ml/datasets/interleaved.py
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 | |
__len__()
¶
Return an estimate of the total number of examples that will be yielded.
The estimate depends on the stopping_strategy:
"first_exhausted"with round-robin —min(child_lengths) * num_datasets."first_exhausted"with probabilities —sum(child_lengths)(approximation; exact calculation is complex)."all_exhausted"—sum(child_lengths)regardless of sampling mode.
Returns:
| Type | Description |
|---|---|
int
|
Estimated total example count. |
Source code in src/forgather/ml/datasets/interleaved.py
state_dict()
¶
Serialize the interleaving position and all child dataset states.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the following keys:
|
Source code in src/forgather/ml/datasets/interleaved.py
load_state_dict(state_dict)
¶
Restore the interleaving position and all child dataset states.
After calling this method, the next iteration resumes from the saved
position. Child datasets that implement load_state_dict are
restored individually; others are left at their natural start position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
dict
|
Dictionary previously returned by |
required |
Source code in src/forgather/ml/datasets/interleaved.py
Utilities¶
forgather.ml.datasets.iterable_with_length.IterableDatasetWithLength
¶
Bases: IterableDataset
A thin wrapper that adds a known length to an iterable dataset.
PyTorch's IterableDataset does not require __len__, but trainers
and data-loader utilities often query it to calculate epoch step counts.
When a map-style Dataset is converted to an iterable form with
to_iterable_dataset(), the length information is lost. This wrapper
re-attaches it.
All attribute and method accesses that are not handled by this class are
forwarded transparently to the wrapped dataset via __getattr__,
including state_dict / load_state_dict for checkpointing, and
HuggingFace Dataset attributes such as column_names and features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable_dataset
|
IterableDataset
|
The dataset to wrap. Any iterable dataset is accepted. |
required |
length
|
int
|
The length to report from |
required |
Notes
The map and shuffle methods are overridden to return a new
IterableDatasetWithLength with the same reported length, so that
the length is preserved through chained transformations.
filter is not overridden: the filtered dataset is returned as-is
because the new length cannot be determined without iterating.
Examples:
>>> from torch.utils.data import IterableDataset
>>> ds = some_map_style_dataset.to_iterable_dataset()
>>> ds_with_len = IterableDatasetWithLength(ds, length=len(some_map_style_dataset))
>>> len(ds_with_len)
1000
Source code in src/forgather/ml/datasets/iterable_with_length.py
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 | |
map(*args, **kwargs)
¶
Apply a map transformation while preserving the reported length.
Delegates to the wrapped dataset's map method and re-wraps the
result in a new IterableDatasetWithLength with the same length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments forwarded to the wrapped dataset's |
()
|
|
**kwargs
|
Keyword arguments forwarded to the wrapped dataset's |
{}
|
Returns:
| Type | Description |
|---|---|
IterableDatasetWithLength
|
Mapped dataset with the same reported length as this instance. |
Source code in src/forgather/ml/datasets/iterable_with_length.py
shuffle(*args, **kwargs)
¶
Shuffle the dataset while preserving the reported length.
Delegates to the wrapped dataset's shuffle method and re-wraps the
result in a new IterableDatasetWithLength with the same length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments forwarded to the wrapped dataset's |
()
|
|
**kwargs
|
Keyword arguments forwarded to the wrapped dataset's |
{}
|
Returns:
| Type | Description |
|---|---|
IterableDatasetWithLength
|
Shuffled dataset with the same reported length as this instance. |
Source code in src/forgather/ml/datasets/iterable_with_length.py
filter(*args, **kwargs)
¶
Filter the dataset.
Delegates to the wrapped dataset's filter method. The length
information is not preserved because the post-filter count cannot
be determined without iterating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments forwarded to the wrapped dataset's |
()
|
|
**kwargs
|
Keyword arguments forwarded to the wrapped dataset's |
{}
|
Returns:
| Type | Description |
|---|---|
IterableDataset
|
Filtered dataset without a |