-
Notifications
You must be signed in to change notification settings - Fork 146
Main parquet text loader #1220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Main parquet text loader #1220
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3e16f2e
add parquet based text dataloader with packing, plus minor refactors
c1150cd
missing file
a3236ba
using stric_state=False to avoid big buffer serialization
e9ffc87
mypy
6babf74
annotations
a376fc1
flake 8
6f477ec
pinned_memory
77ea4d6
comments
3eefea8
rm recipes
71d1557
Merge branch 'main' into main_parquet_text_loader
artemru 4f60566
rm npc=1
5930e52
stricter split selection
8eb2424
adding split pattern
8296bc6
copy
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,10 +14,7 @@ | |
| from torch import Tensor | ||
| from typing_extensions import override | ||
|
|
||
| from fairseq2.data import ( | ||
| DataPipeline, | ||
| read_sequence, | ||
| ) | ||
| from fairseq2.data import DataPipeline, DataPipelineBuilder, read_sequence | ||
| from fairseq2.data.text import read_text | ||
| from fairseq2.data.text.tokenizers import TextTokenEncoder | ||
| from fairseq2.datasets import ( | ||
|
|
@@ -27,15 +24,13 @@ | |
| LengthBatching, | ||
| SequenceBatch, | ||
| ) | ||
| from fairseq2.datasets.text import TextDataset, TextReadOptions | ||
| from fairseq2.datasets.text import GenericTextDataset, TextDataset, TextReadOptions | ||
| from fairseq2.device import Device | ||
| from fairseq2.error import NotSupportedError | ||
| from fairseq2.gang import Gang | ||
| from fairseq2.logging import log | ||
| from fairseq2.nn import BatchLayout | ||
|
|
||
| # TODO: FIX, INFER | ||
| npc = 10 | ||
|
|
||
|
|
||
| JSONL_DATASET_FAMILY: Final = "jsonl" | ||
|
|
||
|
|
||
|
|
@@ -75,20 +70,32 @@ def create_reader( | |
| min_seq_len: int, | ||
| max_seq_len: int, | ||
| options: TextReadOptions | None = None, | ||
| split: str | None = None, | ||
| ) -> DataReader[SequenceBatch]: | ||
| if options is None: | ||
| options = TextReadOptions() | ||
|
|
||
| file_rank = gang.rank | ||
|
|
||
| file_world_size = gang.size | ||
|
|
||
| if len(self._files) < file_world_size: | ||
| text_column_name = options.extras.get("text_column_name", "text") | ||
| assert isinstance(text_column_name, str) | ||
|
|
||
| if min_seq_len > 0: | ||
| log.warning( | ||
| f"The `min_seq_len={min_seq_len}` is ignored for JSONL datasets because of packing." | ||
| ) | ||
|
|
||
| split_files = GenericTextDataset.filter_split( | ||
| self._files, split, extention="jsonl" | ||
| ) | ||
|
|
||
| if len(split_files) < file_world_size: | ||
| raise NotSupportedError( | ||
| "The number of dataset files must be greater than or equal to the number of world size." | ||
| ) | ||
|
|
||
| builder = read_sequence(self._files) | ||
| builder = read_sequence(split_files) | ||
|
|
||
| if file_world_size > 1: | ||
| builder.shard(file_rank, file_world_size, allow_uneven=True) | ||
|
|
@@ -98,9 +105,39 @@ def read_file(file: Path) -> DataPipeline: | |
|
|
||
| builder.yield_from(read_file) | ||
|
|
||
| pipeline = JsonlDataset.build_pipeline_backend( | ||
| builder, | ||
| options, | ||
| text_encoder, | ||
| pad_idx=pad_idx, | ||
| max_seq_len=max_seq_len, | ||
| text_column_name=text_column_name, | ||
| device=gang.device, | ||
| ) | ||
| return DataPipelineReader[SequenceBatch]( | ||
| self._name, "default", pipeline, gang, options | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def build_pipeline_backend( | ||
| builder: DataPipelineBuilder, | ||
| options: TextReadOptions, | ||
| text_encoder: TextTokenEncoder, | ||
| max_seq_len: int, | ||
| pad_idx: int | None, | ||
| text_column_name: str, | ||
| device: Device, | ||
| ) -> DataPipeline: | ||
| if pad_idx is None: | ||
| pad_idx = 0 | ||
|
|
||
| seed = options.seed | ||
| if options.example_shuffle_window != 1: | ||
| builder.shuffle(options.example_shuffle_window, seed) | ||
|
|
||
| # Tokenize. | ||
| def encode(example: dict[str, Any]) -> Tensor: | ||
| return text_encoder(example["text"]) | ||
| return text_encoder(example[text_column_name]) | ||
|
|
||
| builder.map(encode, num_parallel_calls=1) | ||
|
|
||
|
|
@@ -109,29 +146,29 @@ def encode(example: dict[str, Any]) -> Tensor: | |
| if isinstance(batching, LengthBatching): | ||
| max_num_elements = batching.max_num_elements | ||
|
|
||
| pinned_memory = device.type == "cuda" | ||
| # Pack. | ||
| builder.pack( | ||
| max_num_elements + 1, max_seq_len, truncate=True, pinned_memory=True | ||
| max_num_elements + 1, | ||
| max_seq_len, | ||
| pad_value=pad_idx, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cbalioglu : pad_value is not documented, I hope I used it correctly |
||
| truncate=True, | ||
| pinned_memory=pinned_memory, | ||
| ) | ||
| BatchLayout.compiled_max_seq_len = max_seq_len | ||
| else: | ||
| raise NotSupportedError(f"`{batching}` is not supported.") | ||
|
|
||
| BatchLayout.compiled_max_seq_len = max_seq_len | ||
|
|
||
| # Return only the first `max_num_batches`. | ||
| if options.max_num_batches is not None: | ||
| builder.take(options.max_num_batches) | ||
|
|
||
| # Prefetch `num_prefetch` batches in background. | ||
| builder.prefetch(options.num_prefetch) | ||
|
|
||
| # Convert to `SequenceBatch`. | ||
| def to_batch(example: dict[str, Any]) -> SequenceBatch: | ||
| seqs, seq_lens = example["seqs"], example["seq_lens"] | ||
|
|
||
| return SequenceBatch(seqs, seq_lens, packed=True) | ||
|
|
||
| pipeline = builder.map(to_batch).and_return() | ||
| pipeline = builder.map(to_batch).prefetch(options.num_prefetch).and_return() | ||
|
|
||
| return DataPipelineReader[SequenceBatch]( | ||
| self._name, "default", pipeline, gang, options | ||
| ) | ||
| return pipeline | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.